From: Tobias Ulmer Date: Tue, 11 Oct 2016 16:08:33 +0000 (+0200) Subject: e2lib: split up stat() into lstat() and remove inconsistencies X-Git-Tag: e2factory-2.3.15rc1~70 X-Git-Url: https://git.e2factory.org/?a=commitdiff_plain;h=5006281ad1fa74f9619605c95cca7691caf6ee9a;p=e2factory.git e2lib: split up stat() into lstat() and remove inconsistencies This allows symlinks (not encouraged!) to work more consistently. It also fixes a bug where we cached the symlink information instead of the real file. Prior to this change stat(foo, true) worked like posix stat(), while stat(foo) or stat(foo, false) worked like lstat. This change undoes the confusion, both functions work like their posix namesakes. Signed-off-by: Tobias Ulmer --- diff --git a/generic/e2lib.lua b/generic/e2lib.lua index 2df095d..2987808 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -162,13 +162,12 @@ end --- Get file status information. -- @param path Path to the file, including special files. --- @param followlinks Whether symlinks should be followed, or information --- about the link should be returned (boolean). -- @return Dirent table containing attributes (see @{dirent}), or false on error -- @return Error object on failure. -function e2lib.stat(path, followlinks) - local dirent, errstring = le2lib.stat(path, followlinks) +function e2lib.stat(path) + local dirent, errstring + dirent, errstring = le2lib.stat(path, true) if not dirent then return false, err.new("stat failed: %s: %s", path, errstring) end @@ -176,6 +175,21 @@ function e2lib.stat(path, followlinks) return dirent end +--- Get file status information, don't follow symbolic links. +-- @param path Path to the file, including special files. +-- @return Dirent table containing attributes (see @{dirent}), or false on error +-- @return Error object on failure. +function e2lib.lstat(path) + local dirent, errstring + + dirent, errstring = le2lib.stat(path, false) + if not dirent then + return false, err.new("lstat failed: %s: %s", path, errstring) + end + + return dirent +end + --- Checks if file exists. -- If executable is true, it also checks whether the file is executable. -- @param path Path to check. @@ -613,7 +627,7 @@ function e2lib.rotate_log(file) local files = {} local dst - if not e2lib.stat(file) then + if not e2lib.lstat(file) then -- file may be an invalid symlink return true end @@ -1850,7 +1864,7 @@ function e2lib.unlink_recursive(pathname) local de, rc, re local filepath - de, re = e2lib.stat(pathname, false) -- do not follow links + de, re = e2lib.lstat(pathname) -- do not follow links if not de then return false, re end @@ -1863,7 +1877,7 @@ function e2lib.unlink_recursive(pathname) filepath = e2lib.join(pathname, file) - de, re = e2lib.stat(filepath, false) -- do not follow links + de, re = e2lib.lstat(filepath) -- do not follow links if not de then return false, re end @@ -2232,7 +2246,7 @@ end -- @param dir string: path -- @return bool function e2lib.isdir(dir) - local t = e2lib.stat(dir, true) + local t = e2lib.stat(dir) if t and t.type == "directory" then return true end @@ -2244,7 +2258,7 @@ end -- @param dir string: path -- @return bool function e2lib.isfile(path) - local t = e2lib.stat(path, true) + local t = e2lib.stat(path) if t and t.type == "regular" then return true end diff --git a/local/e2build.lua b/local/e2build.lua index f9d62a3..b739c6e 100644 --- a/local/e2build.lua +++ b/local/e2build.lua @@ -379,7 +379,7 @@ function e2build.build_process_class:helper_chroot_remove(res) return false, e:cat(re) end local f = e2lib.join(info.root, "playground") - local s = e2lib.stat(f) + local s = e2lib.lstat(f) if s and s.type == "symbolic-link" then rc, re = e2lib.unlink(f) if not rc then @@ -1056,7 +1056,7 @@ function e2build.build_process_class:_linklast(res, return_flags) e2lib.logf(3, "%s: copy to out/%s/last, server %q has no cache/not local", res:get_name(), res:get_name(), server) - if e2lib.stat(lnk, false) then + if e2lib.lstat(lnk) then e2lib.unlink_recursive(lnk) -- ignore errors end @@ -1085,7 +1085,7 @@ function e2build.build_process_class:_linklast(res, return_flags) return false, e:cat(re) end - if e2lib.stat(lnk, false) then + if e2lib.lstat(lnk) then e2lib.unlink_recursive(lnk) -- ignore errors, symlink will catch it end diff --git a/local/result.lua b/local/result.lua index 27df5cf..e4a7c66 100644 --- a/local/result.lua +++ b/local/result.lua @@ -589,7 +589,7 @@ local function gather_result_paths(info, basedir, results) resdir = e2tool.resultdir(entry, info.root) resconfig = e2tool.resultconfig(entry, info.root) - s = e2lib.stat(resdir, false) + s = e2lib.stat(resdir) if s.type == "directory" then if e2lib.exists(resconfig) then table.insert(results, entry) diff --git a/local/source.lua b/local/source.lua index fbe70a6..d6572ad 100644 --- a/local/source.lua +++ b/local/source.lua @@ -147,7 +147,7 @@ local function gather_source_paths(info, basedir, sources) sdir = e2tool.sourcedir(entry, info.root) sconfig = e2tool.sourceconfig(entry, info.root) - s = e2lib.stat(sdir, false) + s = e2lib.stat(sdir) if s.type == "directory" then if e2lib.exists(sconfig) then table.insert(sources, entry) diff --git a/plugins/files.lua b/plugins/files.lua index cf4aca4..39f3a3d 100644 --- a/plugins/files.lua +++ b/plugins/files.lua @@ -566,7 +566,7 @@ function files.prepare_source(info, sourcename, sourceset, buildpath) end local expected_location = e2lib.join(buildpath, file.unpack) - if not e2lib.stat(expected_location, true) then + if not e2lib.stat(expected_location) then return false, err.new("expected unpack location '%s' does not exist", expected_location) end