Class: Bwrap::Resolvers::Mime Private

Inherits:
Object
  • Object
show all
Includes:
Execution, Execution::Path, Output
Defined in:
lib/bwrap/resolvers/mime.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Finds out mime type of given executable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Output

debug?, debug_output, error_output, handle_output_options, info_output, quiet?, trace?, trace_output, verb_output, verbose?, warn_output

Methods included from Execution

do_execute, last_status, popen2e

Constructor Details

#initialize(executable_name, executable_path) ⇒ Mime

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Mime.



24
25
26
27
# File 'lib/bwrap/resolvers/mime.rb', line 24

def initialize executable_name, executable_path
  @executable_name = executable_name
  @executable_path = executable_path
end

Instance Attribute Details

#executable_nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Name given to #initialize.



16
17
18
# File 'lib/bwrap/resolvers/mime.rb', line 16

def executable_name
  @executable_name
end

#executable_pathObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Either path given to #initialize or one parsed from shebang.



19
20
21
# File 'lib/bwrap/resolvers/mime.rb', line 19

def executable_path
  @executable_path
end

#mime_typeString|nil (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Resolved mime type of the executable.

Returns:

  • (String|nil)

    Resolved mime type of the executable



22
23
24
# File 'lib/bwrap/resolvers/mime.rb', line 22

def mime_type
  @mime_type
end

Instance Method Details

#interpreter?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks if the executable is run using another executable, using a shebang.

For example, if the executable is a bash script, returns ‘true`.

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
# File 'lib/bwrap/resolvers/mime.rb', line 40

def interpreter?
  return false unless @mime_type[0..6] == "text/x-"

  @shebang_line = File.open @executable_path, &:readline
  if @shebang_line[0..1] != "#!"
    return false
  end

  true
end

#resolve_mime_typeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks if target executable is a script, in which case executable is parsed from a shebang line, if found.



31
32
33
34
# File 'lib/bwrap/resolvers/mime.rb', line 31

def resolve_mime_type
  @mime_type = execvalue %W{ file --brief --mime-type #{@executable_path} }
  trace "Mime type of #{@executable_path} is #{@mime_type}"
end

#resolve_real_executableObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parses shebang line to find out path to actual executable used to run the script.

TODO: Handle this is better way.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bwrap/resolvers/mime.rb', line 55

def resolve_real_executable
  # Following sets @shebang_line.
  interpreter? if @shebang_line.nil?

  shebang = @shebang_line
  #trace "Figuring out correct executable from shebang #{shebang}"

  command_line = shebang.delete_prefix("#!").strip
  real_executable, args = command_line.split " ", 2

  if [ "/usr/bin/env", "/bin/env" ].include? real_executable
    # First argument is name of the executable, resolved from PATH.
    executable_name = args.split(" ", 2).first
    real_executable = which executable_name
  end

  debug "Parsed #{real_executable} from the script’s shebang. Using as executable."

  real_executable
end