Module: Fsinv

Included in:
BaseDescription, DirectoryDescription, FileDescription, Inventory
Defined in:
lib/fsinv.rb,
lib/fsinv/inventory.rb,
lib/fsinv/lookuptable.rb,
lib/fsinv/basedescription.rb,
lib/fsinv/filedescription.rb,
lib/fsinv/directorydescription.rb

Defined Under Namespace

Classes: BaseDescription, DirectoryDescription, FileDescription, Inventory, LookupTable

Constant Summary collapse

VERSION =
'0.1.4'
BYTES_IN_KiB =

Kibibyte, Mebibyte, Gibibyte, etc… all the IEC sizes

2**10
BYTES_IN_MiB =
2**20
BYTES_IN_GiB =
2**30
BYTES_IN_TiB =
2**40
BYTES_IN_KB =

these define a KB as 1000 bits, according to the SI prefix

10**3
BYTES_IN_MB =
10**6
BYTES_IN_GB =
10**9
BYTES_IN_TB =
10**12
IGNORE_FILES =
['.AppleDouble','.Parent','.DS_Store','Thumbs.db','__MACOSX','.wine']
PSEUDO_FILES =

calculate the sizes of these folders, yet do not write their content into the inventory index. these appear as files on osx (.app, .bundle)

[
  '.app',
  '.bundle',
  '.mbox', # osx mailbox exports
  '.plugin',
  '.sparsebundle',
  '.abbu', # osx contact archive exports
  '.mode'  # SubEthaEdit and Coda modes
]

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.fmagicObject

Returns the value of attribute fmagic.



63
64
65
# File 'lib/fsinv.rb', line 63

def fmagic
  @fmagic
end

.fshugo_tabObject

Returns the value of attribute fshugo_tab.



63
64
65
# File 'lib/fsinv.rb', line 63

def fshugo_tab
  @fshugo_tab
end

.magic_tabObject

Returns the value of attribute magic_tab.



63
64
65
# File 'lib/fsinv.rb', line 63

def magic_tab
  @magic_tab
end

.mime_tabObject

Returns the value of attribute mime_tab.



63
64
65
# File 'lib/fsinv.rb', line 63

def mime_tab
  @mime_tab
end

.optionsObject

Returns the value of attribute options.



63
64
65
# File 'lib/fsinv.rb', line 63

def options
  @options
end

.osx_tabObject

Returns the value of attribute osx_tab.



63
64
65
# File 'lib/fsinv.rb', line 63

def osx_tab
  @osx_tab
end

Class Method Details

.filestructure_to_db(structitem) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/fsinv.rb', line 153

def filestructure_to_db(structitem)
  
  h = {
    :path => structitem.path,
    :bytes => structitem.bytes,
    :ctime => structitem.ctime,
    :mtime => structitem.mtime
  }

  case structitem
  when DirectoryDescription
    h[:entity_type] = "directory"
    h[:file_count] = structitem.file_count
    h[:item_count] = structitem.item_count
  when FileDescription
    h[:entity_type] = "file"
    
    mime_descr = Fsinv.mime_tab.get_value(structitem.mimetype)
    mime_id = MimeType.where(:mimetype => mime_descr).ids.first
    h[:mimetype] = mime_id
  
    magic_descr = Fsinv.magic_tab.get_value(structitem.magicdescr)
    magic_id = MagicDescription.where(:magicdescr => magic_descr).ids.first
    h[:magicdescr] = magic_id
  end

  osx_tags = [] # will be array of db ids
  unless structitem.osx_tags.nil?
    structitem.osx_tags.each do |json_id|
      tag = Fsinv.osx_tab.get_value(json_id)
      osx_tags << OsxTag.where(:tag => tag).ids.first
    end
  end
  h[:osx_tags] = osx_tags

  fshugo_tags = [] # will be array of db ids
  unless structitem.fshugo_tags.nil?
    structitem.fshugo_tags.each do |json_id|
      tag = Fsinv.fshugo_tab.get_value(json_id)
      fshugo_tags << FshugoTag.where(:tag => tag).ids.first
    end
  end
  h[:fshugo_tags] = fshugo_tags

  FileStructure.create(h)

  structitem.file_list.each { |child| filestructure_to_db(child) } if h[:entity_type] == "directory" 

end

.filestructure_to_xml(xml, defobj) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/fsinv.rb', line 217

def filestructure_to_xml(xml, defobj)
  case defobj
  when DirectoryDescription
    xml.directory{
      xml.path(defobj.path)
      xml.bytes(defobj.bytes)
      xml.file_count(defobj.file_count)
      xml.item_count(defobj.item_count)
      xml.file_list {
        defobj.file_list.each do |child|
          filestructure_to_xml(xml, child)
        end
      }
    }
  when FileDescription
    xml.file{
      xml.path(defobj.path)
      xml.bytes(defobj.bytes)
      xml.mimetype(defobj.mimetype)
      xml.magicdescr(defobj.magicdescr)
    }
  end 
end

.inventory_to_json(inventory) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/fsinv.rb', line 204

def inventory_to_json(inventory)
  json_data = nil
  begin 
    require 'json'
    json_data = JSON.parse(inventory.to_json(max_nesting: 100))
    json_data = JSON.pretty_generate(json_data, :max_nesting => 100) 
  rescue LoadError
    puts "gem 'json' needed for JSON creation. Install using 'gem install json'"
  end
  return json_data
end

.inventory_to_xml(inventory) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/fsinv.rb', line 242

def inventory_to_xml(inventory)
  xml_data = nil
  begin
    require 'nokogiri'
    builder = Nokogiri::XML::Builder.new do |xml| 
      xml.inventory{
        #output the file structure
        xml.file_structure{
          inventory.file_structure.each do |fstruct|
            filestructure_to_xml(xml, fstruct)
          end
        } 
        #output the magic tab
        xml.magic_tab{
          inventory.magic_tab.val_map.each{ |id, val|
            xml.item{
              xml.id(id)
              xml.value(val)
        } } }
        #ouput the mime tab
        xml.mime_tab{
          inventory.mime_tab.val_map.each{ |id, val|
            xml.item{
              xml.id(id)
              xml.value(val)
        } } }
        
        xml.osx_tab{
          inventory.osx_tab.val_map.each{ |id, val|
            xml.item{
              xml.id(id)
              xml.value(val)
        } } }
        
        xml.fshugo_tab{
          inventory.fshugo_tab.val_map.each{ |id, val|
            xml.item{
              xml.id(id)
              xml.value(val)
        } } }
      }
    end
    xml_data = builder.to_xml
  rescue LoadError
    puts "gem 'nokogiri' needed for XML creation. Install using 'gem install nokogiri'"
  end
  return xml_data
end

.inventory_to_yaml(inventory) ⇒ Object



292
293
294
295
296
297
298
299
300
301
# File 'lib/fsinv.rb', line 292

def inventory_to_yaml(inventory)
  yml_data = nil
  begin
    require 'yaml'  
    yml_data = YAML::dump(inventory)
  rescue LoadError
    puts "gem 'yaml' needed for YAML creation. Install using 'gem install yaml'"
  end
  return yml_data
end

.parse(folder_path, reduced_scan = false) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/fsinv.rb', line 108

def parse(folder_path, reduced_scan = false)

  if IGNORE_FILES.include?(File.basename(folder_path))
    # do nothing
  elsif File.basename(folder_path)[0..1] == "._"
    # these are some osx files no one cares about -> ignore
  elsif PSEUDO_FILES.include?(File.extname(folder_path)) # stuff like .app, .bundle, .mbox etc.
    puts "processing reduced_scan #{folder_path}" unless reduced_scan || Fsinv.options[:silent]
    reduced_scan = true
  elsif File.basename(folder_path)[0] == "."
    puts "processing dotfile #{folder_path}" unless reduced_scan || Fsinv.options[:silent]
    reduced_scan = true
  else
    puts "processing #{folder_path}/*" unless reduced_scan || Fsinv.options[:silent]
  end

  curr_dir = Fsinv::DirectoryDescription.new(folder_path, reduced_scan)

  #begin
    Pathname.new(folder_path).children.each { |f| 
      file = f.to_s.encode("UTF-8")
      if IGNORE_FILES.include?(File.basename(file))
        # do nothing
      elsif File.directory?(file) 
        sub_folder = parse(file, reduced_scan)
        curr_dir.bytes += sub_folder.bytes 
        curr_dir.file_list << sub_folder unless reduced_scan
        curr_dir.item_count += 1 # count this directory as an item
        curr_dir.item_count += sub_folder.item_count unless reduced_scan
      else
        puts "processing #{file}" if Fsinv.options[:verbose] && !reduced_scan && Fsinv.options[:silent].nil?
        sub_file = Fsinv::FileDescription.new(file, reduced_scan)
        curr_dir.bytes += sub_file.bytes 
        curr_dir.file_list << sub_file unless reduced_scan
        curr_dir.item_count += 1 unless reduced_scan
      end
    }
    #rescue
    #puts "permission denied: #{folder_path}" unless Fsinv.options[:silent]
    #end

  return curr_dir
end

.pretty_IEC_bytes(bytes) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/fsinv.rb', line 100

def pretty_IEC_bytes(bytes)
  return "%.1f TiB" % (bytes.to_f / BYTES_IN_TiB) if bytes > BYTES_IN_TiB
  return "%.1f GiB" % (bytes.to_f / BYTES_IN_GiB) if bytes > BYTES_IN_GiB
  return "%.1f MiB" % (bytes.to_f / BYTES_IN_MiB) if bytes > BYTES_IN_MiB
  return "%.1f KiB" % (bytes.to_f / BYTES_IN_KiB) if bytes > BYTES_IN_KiB
  return "#{bytes} B"
end

.pretty_SI_bytes(bytes) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/fsinv.rb', line 92

def pretty_SI_bytes(bytes)
  return "%.1f TB" % (bytes.to_f / BYTES_IN_TB) if bytes > BYTES_IN_TB
  return "%.1f GB" % (bytes.to_f / BYTES_IN_GB) if bytes > BYTES_IN_GB
  return "%.1f MB" % (bytes.to_f / BYTES_IN_MB) if bytes > BYTES_IN_MB
  return "%.1f KB" % (bytes.to_f / BYTES_IN_KB) if bytes > BYTES_IN_KB
  return "#{bytes} B"
end

.sanitize_string(string) ⇒ Object

tries to handle various encoding problems encounterd with path strings



85
86
87
88
89
90
# File 'lib/fsinv.rb', line 85

def sanitize_string(string)
  return string.encode("UTF-16BE", :invalid=>:replace, :undef => :replace, :replace=>"?")
               .encode("UTF-8")
               .gsub(/[\u0080-\u009F]/) {|x| x.getbyte(1).chr.force_encoding('windows-1252').encode('utf-8') }
               .gsub(/\"/, "\\\"") # escape double quotes in string
end