class Fsinv::BaseDescription

Attributes

bytes[RW]
ctime[RW]
fshugo_tags[RW]
mtime[RW]
osx_tags[RW]
path[RW]

Public Class Methods

new(path, reduced_scan = false) click to toggle source
# File lib/fsinv/basedescription.rb, line 12
def initialize(path, reduced_scan = false)
  @path = path.encode("UTF-8")
  
  @bytes = 0
  
  unless reduced_scan # don't do this if we only want to know file sizes (for pseudofiles, .git folders, etc)
    @ctime = File.ctime(path) rescue (puts "error getting creation time for file #{path}" if Fsinv.options[:verbose])
    @mtime = File.ctime(path) rescue (puts "error getting modification time for file #{path}" if Fsinv.options[:verbose])
    @osx_tags = osx_tag_ids(path) 
    @fshugo_tags = fshugo_tag_ids(path)
  else
    @osx_tags = []
    @fshugo_tags = []
  end
end

Public Instance Methods

as_json(options = { }) click to toggle source
# File lib/fsinv/basedescription.rb, line 41
def as_json(options = { })
  return to_hash
end
fshugo_tag_ids(file_path) click to toggle source
# File lib/fsinv/basedescription.rb, line 78
def fshugo_tag_ids(file_path)
  
  # if we had problem loading (or installing) ffi-xattr
  # don't do the tags thing at all (fixes FreeBSD bug)
  return [] unless Fsinv.options[:xattr]
  
  xattr = Xattr.new(file_path)
  unless xattr["fshugo"].nil?
    tags = xattr["fshugo"].split(";") 
    tag_ids = []
    tags.each do |tag|
      Fsinv.fshugo_tab.add(tag) unless Fsinv.fshugo_tab.contains?(tag)
      tag_ids << Fsinv.fshugo_tab.get_id(tag)
    end
    return tag_ids
    #return tags
  else
    return []
  end 
end
osx_tag_ids(file_path) click to toggle source
# File lib/fsinv/basedescription.rb, line 49
def osx_tag_ids(file_path)
  
  # well, we can only that if we are on osx, for the
  # mechanism used is only avalable on that plattform
  return [] unless /darwin/.match(RUBY_PLATFORM) # == osx
  
  # if we had problem loading (or installing) ffi-xattr
  # don't do the tags thing at all (fixes FreeBSD bug)
  return [] unless Fsinv.options[:xattr]
  
  # array with the kMDItemUserTags strings
  # of the extended file attributes of 'path'
  tags = %x{mdls -name 'kMDItemUserTags' -raw "#{file_path}"|tr -d "()\n"}.split(',').map { |tag| 
    tag.strip.gsub(/"(.*?)"/,"\\1")
  }
  # if there are now tags, mdls returns "null" -> we don't want this
  if tags.length == 1 && tags[0] == "null"
    return []
  else
    tag_ids = []
    tags.each do |tag|
      Fsinv.osx_tab.add(tag) unless Fsinv.osx_tab.contains?(tag)
      tag_ids << Fsinv.osx_tab.get_id(tag)
    end
    return tag_ids
  end
end
to_hash() click to toggle source
# File lib/fsinv/basedescription.rb, line 28
def to_hash
  p = sanitize_string(@path) rescue "path encoding broken" # there can be ArgumentError and UndefinedConversionError
  h = {
    "path" => p,
    "bytes" => @bytes
  }
  h['ctime'] = @ctime unless @ctime.nil?
  h['mtime'] = @mtime unless @mtime.nil?
  h["osx_tags"] = @osx_tags unless @osx_tags.empty?
  h["fshugo_tags"] = @fshugo_tags unless @fshugo_tags.empty?
  return h
end
to_json(*a) click to toggle source
# File lib/fsinv/basedescription.rb, line 45
def to_json(*a)
  return as_json.to_json(*a )
end