From c626033037ff4c5b82aa28f0147cd7cd24cbdbf2 Mon Sep 17 00:00:00 2001 From: Daira Hopwood Date: Wed, 30 Sep 2015 16:08:01 +0100 Subject: [PATCH] Make the PathInfo returned by get_pathinfo for a nonexistent path use "now" for ctime and mtime. Signed-off-by: Daira Hopwood --- src/allmydata/test/test_util.py | 16 ++++++++++++++-- src/allmydata/util/fileutil.py | 8 +++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/allmydata/test/test_util.py b/src/allmydata/test/test_util.py index d205309c..4b7354e9 100644 --- a/src/allmydata/test/test_util.py +++ b/src/allmydata/test/test_util.py @@ -648,7 +648,7 @@ class FileUtil(ReallyEqualMixin, unittest.TestCase): self.failUnlessFalse(dirinfo.islink) # create a file under the directory - f = os.path.join(basedir, "a/1.txt") + f = os.path.join(basedir, "a", "1.txt") self.touch(basedir, "a/1.txt", data="a"*10) fileinfo = fileutil.get_pathinfo(f) self.failUnlessTrue(fileinfo.isfile) @@ -658,7 +658,7 @@ class FileUtil(ReallyEqualMixin, unittest.TestCase): self.failUnlessEqual(fileinfo.size, 10) # create a symlink under the directory a pointing to 1.txt - slname = os.path.join(basedir, "a/linkto1.txt") + slname = os.path.join(basedir, "a", "linkto1.txt") os.symlink(f, slname) symlinkinfo = fileutil.get_pathinfo(slname) self.failUnlessTrue(symlinkinfo.islink) @@ -666,6 +666,18 @@ class FileUtil(ReallyEqualMixin, unittest.TestCase): self.failUnlessFalse(symlinkinfo.isfile) self.failUnlessFalse(symlinkinfo.isdir) + # path at which nothing exists + dnename = os.path.join(basedir, "a", "doesnotexist") + now = time.time() + dneinfo = fileutil.get_pathinfo(dnename, now=now) + self.failUnlessFalse(dneinfo.exists) + self.failUnlessFalse(dneinfo.isfile) + self.failUnlessFalse(dneinfo.isdir) + self.failUnlessFalse(dneinfo.islink) + self.failUnlessEqual(dneinfo.size, None) + self.failUnlessEqual(dneinfo.mtime, now) + self.failUnlessEqual(dneinfo.ctime, now) + class PollMixinTests(unittest.TestCase): def setUp(self): diff --git a/src/allmydata/util/fileutil.py b/src/allmydata/util/fileutil.py index d1e3d616..8194fcd4 100644 --- a/src/allmydata/util/fileutil.py +++ b/src/allmydata/util/fileutil.py @@ -631,7 +631,7 @@ else: PathInfo = namedtuple('PathInfo', 'isdir isfile islink exists size mtime ctime') -def get_pathinfo(path_u): +def get_pathinfo(path_u, now=None): try: statinfo = os.lstat(path_u) mode = statinfo.st_mode @@ -645,12 +645,14 @@ def get_pathinfo(path_u): ) except OSError as e: if e.errno == ENOENT: + if now is None: + now = time.time() return PathInfo(isdir =False, isfile=False, islink=False, exists=False, size =None, - mtime =None, - ctime =None, + mtime =now, + ctime =now, ) raise -- 2.45.2