lib/metadata/util/md5deep.rb in manageiq-smartstate-0.3.10 vs lib/metadata/util/md5deep.rb in manageiq-smartstate-0.4.0

- old
+ new

@@ -1,9 +1,8 @@ require 'time' require 'metadata/util/win32/peheader' require 'metadata/util/win32/versioninfo' -require 'metadata/util/find_class_methods' require 'util/miq-xml' require 'ostruct' require 'util/miq-encode' class MD5deep @@ -53,39 +52,33 @@ end def scan_glob(filename) filename.tr!("\\", "/") startDir = File.dirname(filename) + globPattern = File.basename(filename) @xml.root.add_attribute("base_path", startDir) - path_prefix = startDir[0, 2] - @drive_letter = path_prefix.match?(/^\w\:/) ? path_prefix : "" + @fs.chdir(startDir) # First check if we are passed a fully qualifed file name if @fs.fileExists?(filename) - base_file = File.basename(filename) - isDir?(filename) ? process_dir_as_file(startDir, base_file, @xml.root) : processFile(startDir, base_file, @xml.root) + isDir?(filename) ? process_dir_as_file(startDir, globPattern, @xml.root) : processFile(startDir, globPattern, @xml.root) else # If the file is not found then process the data as a glob pattern. - process_each_glob_file(filename) + @fs.dirGlob(globPattern) do |f| + # $log.info "Glob file found: [#{f}]" + # Passing "startDir" as the first parameter is a work-around for issues + # when scanning Win VMs from Linux where the path returned from dirGlob + # do not include the drive letter. + # Below is the original line + # processFile(File.dirname(f), File.basename(f), @xml.root) + processFile(startDir, File.basename(f), @xml.root) + end end @xml end - def process_each_glob_file(file_name) - FindClassMethods.glob(file_name, @fs) do |f| - # Passing "startDir" as the first parameter is a work-around for issues - # when scanning Win VMs from Linux where the path returned from dirGlob - # do not include the drive letter. - processFile(File.dirname(f), File.basename(f), @xml.root) - end - rescue => err - $log.error "process_each_glob_file: Exception #{err} rescued" - $log.debug err.backtrace.join("\n") - end - def read_fs(path, xmlNode) - @drive_letter = @drive_letter.nil? ? "" : @drive_letter if @fs @fs.dirForeach(path) { |x| processFile(path, x, xmlNode) } @fs.dirForeach(path) { |x| processDir(path, x, xmlNode) } else Dir.foreach(path) { |x| processFile(path, x, xmlNode) } @@ -96,11 +89,11 @@ xmlNode.add_attributes(calculate_sums(xmlNode)) end def processDir(path, x, xmlNode) if x != "." && x != ".." - currFile = File.join(@drive_letter, path, x) + currFile = File.join(path, x) begin if File.directory?(currFile) @fullDirCount += 1 # $log.debug "DIR : #{currFile}" @@ -115,11 +108,11 @@ end end def process_dir_as_file(path, x, xml_node) if x != "." && x != ".." - curr_dir = File.join(@drive_letter, path, x) + curr_dir = File.join(path, x) if isDir?(curr_dir) xml_file_node = xml_node.add_element("file", "name" => x, "fqname" => curr_dir) stat_hash = {} stat_hash.merge!(get_dir_stats(curr_dir)) xml_file_node.add_attributes(stat_hash) @@ -127,49 +120,45 @@ end end def processFile(path, x, xmlNode) if (@opts.exclude.include?(x) == false) && x[0..0] != "$" - currFile = File.join(@drive_letter, path, x) + currFile = File.join(path, x) begin # unless File.directory?(currFile) then - return if isDir?(currFile) + unless isDir?(currFile) + # File we have an exclusion list and the current file is in it, skip to the next file + @fullFileCount += 1 + fh = fileOpen(currFile) - # File we have an exclusion list and the current file is in it, skip to the next file - @fullFileCount += 1 - fh = fileOpen(currFile) + xmlFileNode = xmlNode.add_element("file", "name" => x, "fqname" => currFile) + statHash = {} + statHash.merge!(getFileStats(fh)) + statHash.merge!(calculate_digest(fh)) + xmlFileNode.add_attributes(statHash) - xmlFileNode = xmlNode.add_element("file", "name" => x, "fqname" => currFile) - statHash = {} - statHash.merge!(getFileStats(fh)) - statHash.merge!(calculate_digest(fh)) - xmlFileNode.add_attributes(statHash) + ext = File.extname(currFile).downcase + if @opts.winVerList.include?(ext) + if @opts.versioninfo || @opts.imports + peHdr = PEheader.new(fh) rescue nil + unless peHdr.nil? + xmlFileNode.add_element("versioninfo", peHdr.versioninfo) if @opts.versioninfo && !peHdr.versioninfo.blank? + xmlFileNode.add_element("libraries", "imports" => peHdr.getImportList) if @opts.imports && !peHdr.imports.blank? + end + end + end - ext = File.extname(currFile).downcase - if @opts.winVerList.include?(ext) - pe_hdr = PEheader.new(fh) rescue nil - process_pe_header(pe_hdr, xmlFileNode) unless pe_hdr.nil? + getFileContents(fh, xmlFileNode) if @opts.contents == true + fh.close end - - getFileContents(fh, xmlFileNode) if @opts.contents == true - fh.close rescue Errno::EACCES, RuntimeError, SystemCallError fh.close if fh.kind_of?(File) && !fh.closed? end end - $log.debug "processFile: finished @xml is #{@xml}" end - def process_pe_header(pe_hdr, xml_file_node) - xml_file_node.add_element("versioninfo", pe_hdr.versioninfo) if @opts.versioninfo && pe_hdr.versioninfo.present? - xml_file_node.add_element("libraries", "imports" => pe_hdr.getImportList) if @opts.imports && pe_hdr.imports.present? - rescue TypeError => err - $log.info "process_pe_header: TypeError handling PEheader; skipping PEheader info" - $log.debug err.backtrace.join("\n") - end - def isDir?(currFile) if @fs @fs.fileDirectory?(currFile) else File.directory?(currFile) @@ -224,18 +213,13 @@ # end # Create hash for requested digests digest = create_digest_hash - begin - fileName.seek(0, IO::SEEK_SET) - # Loop over each digest and add the file contents - while (buf = fileName.read(10_240_000)) - digest.each_pair { |_k, v| v << buf } - end - rescue => err - $log.error "Error #{err} reading file to calculate digest" - $log.debug err.backtrace.join("\n") + fileName.seek(0, IO::SEEK_SET) + # Loop over each digest and add the file contents + while buf = fileName.read(10240000) + digest.each_pair { |_k, v| v << buf } end end digest.each_pair { |k, v| digest[k] = v.to_s } digest