From: Tobias Ulmer Date: Mon, 28 Nov 2016 11:50:16 +0000 (+0100) Subject: e2tool: move fileid() into file_class and adjust e2 X-Git-Tag: e2factory-2.3.16rc1~46 X-Git-Url: https://git.e2factory.org/?a=commitdiff_plain;h=848dab5a8ddb8eb873584469cfaba78dedd77907;p=e2factory.git e2tool: move fileid() into file_class and adjust e2 Signed-off-by: Tobias Ulmer --- diff --git a/local/chroot.lua b/local/chroot.lua index 9138185..f5e7dac 100644 --- a/local/chroot.lua +++ b/local/chroot.lua @@ -150,7 +150,7 @@ function chroot.chroot:chrootgroupid(info) for file in self:file_iter() do hash.hash_append(hc, file:server()) hash.hash_append(hc, file:location()) - fileid, re = e2tool.fileid(info, file) + fileid, re = file:fileid() if not fileid then return false, e:cat(re) end diff --git a/local/e2tool.lua b/local/e2tool.lua index 7b35fda..704e976 100644 --- a/local/e2tool.lua +++ b/local/e2tool.lua @@ -114,6 +114,61 @@ function e2tool.file_class:servloc() return self._server .. ":" .. self._location end +--- Compute checksum of file by retreiving it via the cache transport, +-- no matter the configuration what and hashing local. +-- @return fileid on success, false if an error occured. +-- @return error object on failure. +function e2tool.file_class:_compute_fileid(flags) + local rc, re, info, path, fileid + + info = e2tool.info() + + path, re = cache.fetch_file_path(info.cache, self:server(), self:location()) + if not path then + return false, re + end + + fileid, re = hash.hash_file_once(path) + if not fileid then + return false, re + end + + return fileid +end + +--- Calculate the FileID for a file. +-- The name and location attributes are not included. +-- @return FileID string: hash value, or false on error. +-- @return an error object on failure +function e2tool.file_class:fileid() + local rc, re, e, fileid + + e = err.new("error calculating file id for file: %s", self:servloc()) + + if self:sha1() then + fileid = self:sha1() + else + fileid, re = self:_compute_fileid(self) + if not fileid then + return false, e:cat(re) + end + end + + if e2option.opts["check-remote"] then + local filever + + filever = self:instance_copy() + filever:sha1(fileid) + + rc, re = e2tool.verify_hash(e2tool.info(), filever) + if not rc then + return false, e:cat(re) + end + end + + return fileid +end + --- Set or return the server attribute. -- Server name is any name known to cache. -- @param server Optional server name to set @@ -953,45 +1008,6 @@ function e2tool.verify_hash(info, file) return false, e end ---- calculate a representation for file content. The name and location --- attributes are not included. --- @param info info table --- @param file table: file table from configuration --- @return fileid string: hash value, or false on error. --- @return an error object on failure -function e2tool.fileid(info, file) - assertIsTable(info) - assertIsTable(file) - assert(file:isInstanceOf(e2tool.file_class)) - - local rc, re, e, fileid - - e = err.new("error calculating file id for file: %s", file:servloc()) - - if file:sha1() then - fileid = file:sha1() - else - fileid, re = compute_fileid(file) - if not fileid then - return false, e:cat(re) - end - end - - if e2option.opts["check-remote"] then - local filever - - filever = file:instance_copy() - filever:sha1(fileid) - - rc, re = e2tool.verify_hash(info, filever) - if not rc then - return false, e:cat(re) - end - end - - return fileid -end - --- select (mark) results based upon a list of results usually given on the -- command line. Parameters are assigned to all selected results. -- @param info the info structure diff --git a/local/licence.lua b/local/licence.lua index 5d43077..dd9b612 100644 --- a/local/licence.lua +++ b/local/licence.lua @@ -128,7 +128,7 @@ function licence.licence:licenceid(info) hash.hash_append(hc, file:server()) hash.hash_append(hc, file:location()) - fileid, re = e2tool.fileid(info, file) + fileid, re = file:fileid() if not fileid then return false, e:cat(re) end diff --git a/local/project.lua b/local/project.lua index 63fe633..c34db31 100644 --- a/local/project.lua +++ b/local/project.lua @@ -290,7 +290,7 @@ function project.projid(info) if not e2lib.is_backup_file(f) then location = e2lib.join("proj/init", f) file = e2tool.file_class:new(cache.server_names().dot, location) - fileid, re = e2tool.fileid(info, file) + fileid, re = file:fileid() if not fileid then return false, re end diff --git a/local/result.lua b/local/result.lua index d7ec61f..5ba529d 100644 --- a/local/result.lua +++ b/local/result.lua @@ -508,7 +508,7 @@ function result.result_class:buildid() e2tool.resultbuildscript(self:get_name_as_path()) ) - id, re = e2tool.fileid(info, file) + id, re = file:fileid() if not id then return false, re end diff --git a/plugins/files.lua b/plugins/files.lua index 14db4ff..8d40f51 100644 --- a/plugins/files.lua +++ b/plugins/files.lua @@ -261,7 +261,7 @@ function files.files_source:sourceid(sourceset --[[always ignored for files]]) end for file in self:file_iter() do - local fileid, re = e2tool.fileid(info, file) + local fileid, re = file:fileid() if not fileid then return false, re end