class Object

Constants

ANALYSING
EXITING
HELP
INDEXING

The name of the different tasks to do

INDEX_FILE
PROG_NAME

The name of the program

RANDOM_KEY

Used to have an unique file to write result after indexing

Public Instance Methods

analysing() click to toggle source

Given a list of keys-values where a key is the absolute path of a file (the name) and the value the md5 of the file A file is modified if his name appears in the list but are not associated with the same md5 (case 1) A file is created if his name don’t appears in the list (case 2) A file is deleted a key of the list don’t corresspond with the name of a file (case 3) The following actions are taking in account like this: Copy create a new file Move delte a file and create a new one Rename is the equivalent of move

# File bin/file_checking, line 110
def analysing
  unless File.exists?(INDEX_FILE)
    puts 'You need to index the files first'
    exit
  end
  index = YAML::load_file INDEX_FILE
  created_file = []
  modified_file = []
  each_md5 do |md5, file|
    current_entry = index.delete(file_path file) # case 3
    if current_entry.nil? # case 2
      created_file << file.path
    elsif current_entry[:md5] != md5 # case 1
      modified_file << current_entry[:name]
    end
  end
  output 'Created', created_file
  output 'Modified', modified_file
  output 'Deleted', index.map { |key, file| file[:name] }
  if created_file.empty? and modified_file.empty? and index.empty?
    puts 'no change since indexing'
  else
    puts @output.join("\n" + ('+' * 25) + "\n")
  end
end
display_help() click to toggle source

Display the help message

# File bin/file_checking, line 71
def display_help
  puts <<-END
#{PROG_NAME} is a tool designed for indexing files and after that to tell which file has been modified, deleted or created.

Usage in static mode: #{PROG_NAME} <task> <exception>
  <task> can have three different values:
    -indexing (index all files but not folders in directory and sub-directories this tool is run)
    -analysing (analyse all files but not folders in directory and sub-directories this tool is run and which files have been modified, deleted or created)
    -help display this help message
  [OPTIONAL] <exception> is the optional path of a file which indicate which file should not be indexed
    Each line of the file indicate the path relative to the current directory of a file which should not be indexed.
    For example, in your current directory you have the file not-to-index.txt. So you need to write in the file containing exceptions
    not-to.index.txt
    For prevented the file not-to-index.txt to be indexed

Usage in interactive mode: #{PROG_NAME}
  You will be prompted to choose four tasks. The thirds one are these described above. The fourth is to quit the programm
  The path of the file containing the exceptions list will be asked in a second time (Leave it blank if you have none).

Note:
  A file is modified if his content has changed but not his absolute path
  A file is created if his absolute path is not indexed
  A file is deleted if his absolute path is indexed but don't correspond with any file in current directory or sub-directories

  Indexing is based on the MD5 algorithm

Limitations:
  This program is mono-thread. For indexing a big amount of files, it can take hours
  END
end
each_md5() { |hexdigest(read), file| ... } click to toggle source

Iterate over all files and sub-directories. The arguments code block are the md5 of the file and a corressponding instance of the class File

# File bin/file_checking, line 33
def each_md5
  Dir.glob(File.join('**', '*'), File::FNM_DOTMATCH) do |file|
    next if @exceptions.include?(file) or file == INDEX_FILE or File.directory?(file) or file == @file_exceptions
    yield(Digest::MD5.hexdigest(IO.read(file)), File.new(file))
  end
end
file_path(file) click to toggle source

return the absolute path of the file passed in parameters hashed with the md5 algorithm

# File bin/file_checking, line 41
def file_path(file)
  md5 = Digest::MD5.hexdigest(File.expand_path(file))
  file.close
  md5
end
indexing() click to toggle source

Index all files and sub-directories The result of the indeaxtion is a hash where the keys are a the md5 of the absolute path or a file and the values the md5 of the file and his name The hash is saved to a file in YAML format

# File bin/file_checking, line 59
def indexing
  md5_hash = {}
  i = 0
  each_md5 do |md5, file|
    md5_hash[file_path file] = {md5: md5, name: file.path}
    i += 1
  end
  IO.write(INDEX_FILE, md5_hash.to_yaml)
  puts "#{i} file(s) indexed "
end
output(string, array) click to toggle source

Prepare datas for building a nice report after the analysing task

# File bin/file_checking, line 48
def output(string, array)
  @output ||= []
  return if array.empty?
  msg = 'file'
  msg += 's' if array.size > 1
  @output << "#{string} #{msg}:\n#{array.join("\n")}"
end
to_array(file) click to toggle source

Return all lines of a file organized in an array.

# File bin/file_checking, line 27
def to_array(file)
  IO.read(file).split("\n")
end