Class: Inspec::Resources::FileResource
  
  
  
Overview
  
    
TODO: rename file_resource.rb
   
 
  
  Instance Attribute Summary collapse
  
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  
  #includes_whitespaces?, #parse_mount_options
  
  
  
  
  
  
  
  
  
  #select_file_perms_style
  Constructor Details
  
    
  
  
    
Returns a new instance of FileResource.
   
 
  
  
    | 
39
40
41
42
43 | # File 'lib/inspec/resources/file.rb', line 39
def initialize(path)
    @perms_provider = select_file_perms_style(inspec.os)
  @file = inspec.backend.file(path)
end | 
 
  
 
  
    Instance Attribute Details
    
      
      
      
  
  
    #file  ⇒ Object  
  
  
  
  
    
Returns the value of attribute file.
   
 
  
  
    | 
38
39
40 | # File 'lib/inspec/resources/file.rb', line 38
def file
  @file
end | 
 
    
      
      
      
  
  
    #mount_options  ⇒ Object  
  
  
  
  
    
Returns the value of attribute mount_options.
   
 
  
  
    | 
38
39
40 | # File 'lib/inspec/resources/file.rb', line 38
def mount_options
  @mount_options
end | 
 
    
   
  
    Instance Method Details
    
      
  
  
    #allowed?(permission, opts = {})  ⇒ Boolean 
  
  
  
  
    | 
89
90
91
92
93
94 | # File 'lib/inspec/resources/file.rb', line 89
def allowed?(permission, opts = {})
  return false unless exist?
  return skip_resource "`allowed?` is not supported on your OS yet." if @perms_provider.nil?
  file_permission_granted?(permission, opts[:by], opts[:by_user])
end | 
 
    
      
  
  
    #contain(*_)  ⇒ Object 
  
  
  
  
    | 
64
65
66 | # File 'lib/inspec/resources/file.rb', line 64
def contain(*_)
  raise "Contain is not supported. Please use standard RSpec matchers."
end | 
 
    
      
  
  
    #content  ⇒ Object 
  
  
  
  
    | 
57
58
59
60
61
62 | # File 'lib/inspec/resources/file.rb', line 57
def content
  res = file.content
  return nil if res.nil?
  res.force_encoding("utf-8")
end | 
 
    
      
  
  
    #executable?(by_usergroup, by_specific_user)  ⇒ Boolean 
  
  
  
  
    | 
82
83
84
85
86
87 | # File 'lib/inspec/resources/file.rb', line 82
def executable?(by_usergroup, by_specific_user)
  return false unless exist?
  return skip_resource "`executable?` is not supported on your OS yet." if @perms_provider.nil?
  file_permission_granted?("execute", by_usergroup, by_specific_user)
end | 
 
    
      
  
  
    #more_permissive_than?(max_mode = nil)  ⇒ Boolean 
  
  
  
  
    | 
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 | # File 'lib/inspec/resources/file.rb', line 138
def more_permissive_than?(max_mode = nil)
  raise Inspec::Exceptions::ResourceFailed, "The file" + file.path + "doesn't seem to exist" unless exist?
  raise ArgumentError, "You must proivde a value for the `maximum allowable permission` for the file." if max_mode.nil?
  raise ArgumentError, "You must proivde the `maximum permission target` as a `String`, you provided: " + max_mode.class.to_s unless max_mode.is_a?(String)
  raise ArgumentError, "The value of the `maximum permission target` should be a valid file mode in 4-ditgit octal format: for example, `0644` or `0777`" unless /(0)?([0-7])([0-7])([0-7])/.match?(max_mode)
                                
  max_mode = max_mode.to_i(8)
  inv_mode = 0777 ^ max_mode
  inv_mode & file.mode != 0
end | 
 
    
      
  
  
    #mounted?(expected_options = nil, identical = false)  ⇒ Boolean 
  
  
  
  
    | 
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 | # File 'lib/inspec/resources/file.rb', line 96
def mounted?(expected_options = nil, identical = false)
  mounted = file.mounted
    return file.mounted? if expected_options.nil?
    Inspec.deprecate(:file_resource_be_mounted_matchers, "The file resource `be_mounted.with` and `be_mounted.only_with` matchers are deprecated. Please use the `mount` resource instead")
    return nil unless inspec.os.linux?
    @mount_options ||= parse_mount_options(mounted.stdout, true)
  if identical
        @mount_options == expected_options
  else
        @mount_options.contains(expected_options)
  end
end | 
 
    
      
  
  
    #readable?(by_usergroup, by_specific_user)  ⇒ Boolean 
  
  
  
  
    | 
68
69
70
71
72
73 | # File 'lib/inspec/resources/file.rb', line 68
def readable?(by_usergroup, by_specific_user)
  return false unless exist?
  return skip_resource "`readable?` is not supported on your OS yet." if @perms_provider.nil?
  file_permission_granted?("read", by_usergroup, by_specific_user)
end | 
 
    
      
  
  
    #sgid  ⇒ Object 
  
  
    Also known as:
    setgid?
    
  
  
  
    | 
126
127
128 | # File 'lib/inspec/resources/file.rb', line 126
def sgid
  (mode & 02000) > 0
end | 
 
    
      
  
  
    #sticky  ⇒ Object 
  
  
    Also known as:
    sticky?
    
  
  
  
    | 
132
133
134 | # File 'lib/inspec/resources/file.rb', line 132
def sticky
  (mode & 01000) > 0
end | 
 
    
      
  
  
    #suid  ⇒ Object 
  
  
    Also known as:
    setuid?
    
  
  
  
    | 
120
121
122 | # File 'lib/inspec/resources/file.rb', line 120
def suid
  (mode & 04000) > 0
end | 
 
    
      
  
  
    #to_s  ⇒ Object 
  
  
  
  
    | 
167
168
169
170
171
172
173 | # File 'lib/inspec/resources/file.rb', line 167
def to_s
  if file
    "File #{source_path}"
  else
    "Bad File on %s" % [inspec.backend.class]
  end
end | 
 
    
      
  
  
    #writable?(by_usergroup, by_specific_user)  ⇒ Boolean 
  
  
  
  
    | 
75
76
77
78
79
80 | # File 'lib/inspec/resources/file.rb', line 75
def writable?(by_usergroup, by_specific_user)
  return false unless exist?
  return skip_resource "`writable?` is not supported on your OS yet." if @perms_provider.nil?
  file_permission_granted?("write", by_usergroup, by_specific_user)
end |