def testBaseTime(self): with temp.AutoTempFilePath() as tempfile: result = statx.Get(tempfile.encode("utf-8")) self.assertGreater(result.atime_ns, 0) self.assertGreater(result.mtime_ns, 0) self.assertGreater(result.ctime_ns, 0) # TODO(hanuszczak): Remove this once support for Python 3.6 is dropped. if hasattr(time, "time_ns"): self.assertLess(result.atime_ns, time.time_ns()) self.assertLess(result.mtime_ns, time.time_ns()) self.assertLess(result.ctime_ns, time.time_ns())
def testNlink(self): with temp.AutoTempDirPath(remove_non_empty=True) as tempdir: target = os.path.join(tempdir, "foo") link_1 = os.path.join(tempdir, "bar") link_2 = os.path.join(tempdir, "baz") with open(target, mode="wb"): pass os.link(target, link_1) os.link(target, link_2) result = statx.Get(target.encode("utf-8")) self.assertEqual(result.nlink, 3)
def testBirthTime(self): # On Linux, file birth time is collected only if `statx` is available. if platform.system() == "Linux": try: ctypes.CDLL("libc.so.6").statx except AttributeError: raise absltest.SkipTest("`statx` not available") with temp.AutoTempFilePath() as tempfile: result = statx.Get(tempfile.encode("utf-8")) self.assertGreater(result.btime_ns, 0) # TODO(hanuszczak): Remove this once support for Python 3.6 is dropped. if hasattr(time, "time_ns"): self.assertLess(result.btime_ns, time.time_ns())
def testFromStatx(self): with temp.AutoTempFilePath() as filepath: filepath = filepath.encode("utf-8") with open(filepath, mode="wb") as filedesc: filedesc.write(b"1234567") statx_result = statx.Get(filepath) entry = rdf_timeline.TimelineEntry.FromStatx(filepath, statx_result) self.assertEqual(entry.size, 7) self.assertTrue(stat_mode.S_ISREG(entry.mode)) self.assertEqual(entry.dev, statx_result.dev) self.assertEqual(entry.ino, statx_result.ino) self.assertEqual(entry.uid, statx_result.uid) self.assertEqual(entry.gid, statx_result.gid) self.assertEqual(entry.attributes, statx_result.attributes) self.assertEqual(entry.atime_ns, statx_result.atime_ns) self.assertEqual(entry.btime_ns, statx_result.btime_ns) self.assertEqual(entry.ctime_ns, statx_result.ctime_ns) self.assertEqual(entry.mtime_ns, statx_result.mtime_ns)
def Recurse(path: bytes) -> Iterator[rdf_timeline.TimelineEntry]: """Performs the recursive walk over the file hierarchy.""" try: stat = statx.Get(path) except OSError: return yield rdf_timeline.TimelineEntry.FromStatx(path, stat) # We want to recurse only to folders on the same device. if not stat_mode.S_ISDIR(stat.mode) or stat.dev != dev: return try: childnames = os.listdir(path) except OSError: childnames = [] # TODO(hanuszczak): Implement more efficient auto-batcher instead of having # multi-level iterators. for childname in childnames: for entry in Recurse(os.path.join(path, childname)): yield entry
def testRdev(self): with temp.AutoTempFilePath() as tempfile: result = statx.Get(tempfile.encode("utf-8")) self.assertEqual(result.rdev, os.lstat(tempfile).st_rdev)
def testIno(self): with temp.AutoTempFilePath() as tempfile: result = statx.Get(tempfile.encode("utf-8")) self.assertNotEqual(result.ino, 0)
def testModeDir(self): with temp.AutoTempDirPath() as tempdir: result = statx.Get(tempdir.encode("utf-8")) self.assertTrue(stat.S_ISDIR(result.mode))
def testModeReg(self): with temp.AutoTempFilePath() as tempfile: result = statx.Get(tempfile.encode("utf-8")) self.assertTrue(stat.S_ISREG(result.mode))
def testGid(self): with temp.AutoTempFilePath() as tempfile: result = statx.Get(tempfile.encode("utf-8")) self.assertEqual(result.gid, os.getgid())
def testRaisesExceptionOnError(self): with temp.AutoTempDirPath() as tempdir: with self.assertRaises(OSError): statx.Get( os.path.join(tempdir, "non-existing-file").encode("utf-8"))