class Synqa::DirContentHost

Base class for an object representing a remote system where the contents of a directory on the system are enumerated by one command to list all sub-directories and another command to list all files in the directory and their hash values.

Attributes

hashCommand[R]

The HashCommand object used to calculate and parse hash values of files

pathPrefix[R]

Prefix required for find command (usually nothing, since it should be on the system path)

Public Class Methods

new(hashCommand, pathPrefix = "") click to toggle source
# File lib/synqa.rb, line 122
def initialize(hashCommand, pathPrefix = "")
  @hashCommand = hashCommand
  @pathPrefix = pathPrefix
end

Public Instance Methods

findDirectoriesCommand(baseDir) click to toggle source

Generate the find command which will list all the sub-directories of the base directory

# File lib/synqa.rb, line 128
def findDirectoriesCommand(baseDir)
  return ["#{@pathPrefix}find", baseDir, "-type", "d", "-print"]
end
findFilesCommand(baseDir) click to toggle source

Generate the find command which will list all the files within the base directory

# File lib/synqa.rb, line 155
def findFilesCommand(baseDir)
  return ["#{@pathPrefix}find", baseDir, "-type", "f", "-print"]
end
getContentTree(baseDir) click to toggle source

Construct the ContentTree for the given base directory

# File lib/synqa.rb, line 174
def getContentTree(baseDir)
  contentTree = ContentTree.new()
  contentTree.time = Time.now.utc
  for dir in listDirectories(baseDir)
    contentTree.addDir(dir)
  end
  for fileHash in listFileHashes(baseDir)
    contentTree.addFile(fileHash.relativePath, fileHash.hash)
  end
  return contentTree
end
listDirectories(baseDir) click to toggle source

Return the list of sub-directories relative to the base directory

# File lib/synqa.rb, line 133
def listDirectories(baseDir)
  baseDir = normalisedDir(baseDir)
  command = findDirectoriesCommand(baseDir)
  output = getCommandOutput(command)
  directories = []
  baseDirLen = baseDir.length
  puts "Listing directories ..."
  while (line = output.gets)
    line = line.chomp
    puts " #{line}"
    if line.start_with?(baseDir)
      directories << line[baseDirLen..-1]
    else
      raise "Directory #{line} is not a sub-directory of base directory #{baseDir}"
    end
  end
  output.close()
  checkProcessStatus(command)
  return directories
end
listFileHashes(baseDir) click to toggle source

List file hashes by executing the command to hash each file on the output of the find command which lists all files, and parse the output.

# File lib/synqa.rb, line 161
def listFileHashes(baseDir)
  baseDir = normalisedDir(baseDir)
  fileHashes = []
  listFileHashLines(baseDir) do |fileHashLine|
    fileHash = self.hashCommand.parseFileHashLine(baseDir, fileHashLine)
    if fileHash != nil
      fileHashes << fileHash
    end
  end
  return fileHashes
end