Example #1
0
    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())
Example #2
0
    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)
Example #3
0
    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())
Example #4
0
  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)
Example #5
0
    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
Example #6
0
 def testRdev(self):
     with temp.AutoTempFilePath() as tempfile:
         result = statx.Get(tempfile.encode("utf-8"))
         self.assertEqual(result.rdev, os.lstat(tempfile).st_rdev)
Example #7
0
 def testIno(self):
     with temp.AutoTempFilePath() as tempfile:
         result = statx.Get(tempfile.encode("utf-8"))
         self.assertNotEqual(result.ino, 0)
Example #8
0
 def testModeDir(self):
     with temp.AutoTempDirPath() as tempdir:
         result = statx.Get(tempdir.encode("utf-8"))
         self.assertTrue(stat.S_ISDIR(result.mode))
Example #9
0
 def testModeReg(self):
     with temp.AutoTempFilePath() as tempfile:
         result = statx.Get(tempfile.encode("utf-8"))
         self.assertTrue(stat.S_ISREG(result.mode))
Example #10
0
 def testGid(self):
     with temp.AutoTempFilePath() as tempfile:
         result = statx.Get(tempfile.encode("utf-8"))
         self.assertEqual(result.gid, os.getgid())
Example #11
0
 def testRaisesExceptionOnError(self):
     with temp.AutoTempDirPath() as tempdir:
         with self.assertRaises(OSError):
             statx.Get(
                 os.path.join(tempdir, "non-existing-file").encode("utf-8"))