예제 #1
0
 def testRegular(self):
     with temp.AutoTempFilePath() as temp_filepath:
         stat = utils.Stat(temp_filepath, follow_symlink=False)
         self.assertFalse(stat.IsDirectory())
         self.assertTrue(stat.IsRegular())
         self.assertFalse(stat.IsSocket())
         self.assertFalse(stat.IsSymlink())
예제 #2
0
    def testGetSize(self):
        with temp.AutoTempFilePath() as temp_filepath:
            with open(temp_filepath, "wb") as fd:
                fd.write("foobarbaz")

            stat = utils.Stat(temp_filepath, follow_symlink=False)
            self.assertEqual(stat.GetSize(), 9)
예제 #3
0
 def testDirectory(self):
     with test_lib.AutoTempDirPath() as temp_dirpath:
         stat = utils.Stat(temp_dirpath, follow_symlink=False)
         self.assertTrue(stat.IsDirectory())
         self.assertFalse(stat.IsRegular())
         self.assertFalse(stat.IsSocket())
         self.assertFalse(stat.IsSymlink())
예제 #4
0
    def testGetFlagsSymlink(self):
        with temp.AutoTempDirPath(remove_non_empty=True) as temp_dirpath,\
             temp.AutoTempFilePath() as temp_filepath:
            temp_linkpath = os.path.join(temp_dirpath, "foo")
            os.symlink(temp_filepath, temp_linkpath)

            stat = utils.Stat(temp_linkpath, follow_symlink=False)
            self.assertTrue(stat.IsSymlink())
            self.assertEqual(stat.GetLinuxFlags(), 0)
            self.assertEqual(stat.GetOsxFlags(), 0)
예제 #5
0
    def testGetOsxFlags(self):
        with temp.AutoTempFilePath() as temp_filepath:
            client_test_lib.Chflags(temp_filepath, flags=["nodump", "hidden"])

            stat = utils.Stat(temp_filepath, follow_symlink=False)
            self.assertTrue(stat.IsRegular())
            self.assertTrue(stat.GetOsxFlags() & self.UF_NODUMP)
            self.assertTrue(stat.GetOsxFlags() & self.UF_HIDDEN)
            self.assertFalse(stat.GetOsxFlags() & self.UF_IMMUTABLE)
            self.assertEqual(stat.GetLinuxFlags(), 0)
예제 #6
0
    def testGetLinuxFlags(self):
        with temp.AutoTempFilePath() as temp_filepath:
            client_test_lib.Chattr(temp_filepath, attrs=["+c", "+d"])

            stat = utils.Stat(temp_filepath, follow_symlink=False)
            self.assertTrue(stat.IsRegular())
            self.assertTrue(stat.GetLinuxFlags() & self.FS_COMPR_FL)
            self.assertTrue(stat.GetLinuxFlags() & self.FS_NODUMP_FL)
            self.assertFalse(stat.GetLinuxFlags() & self.FS_IMMUTABLE_FL)
            self.assertEqual(stat.GetOsxFlags(), 0)
예제 #7
0
  def testGetTime(self):
    adate = datetime.datetime(2017, 10, 2, 8, 45)
    mdate = datetime.datetime(2001, 5, 3, 10, 30)

    with test_lib.AutoTempFilePath() as temp_filepath:
      self._Touch(temp_filepath, "-a", adate)
      self._Touch(temp_filepath, "-m", mdate)

      stat = utils.Stat(temp_filepath, follow_symlink=False)
      self.assertEqual(stat.GetAccessTime(), self._EpochMillis(adate))
      self.assertEqual(stat.GetModificationTime(), self._EpochMillis(mdate))
예제 #8
0
    def testSymlink(self):
        with temp.AutoTempDirPath(remove_non_empty=True) as temp_dirpath,\
             temp.AutoTempFilePath() as temp_filepath:
            with open(temp_filepath, "wb") as fd:
                fd.write("foobar")

            temp_linkpath = os.path.join(temp_dirpath, "foo")
            os.symlink(temp_filepath, temp_linkpath)

            stat = utils.Stat(temp_linkpath, follow_symlink=False)
            self.assertFalse(stat.IsDirectory())
            self.assertFalse(stat.IsRegular())
            self.assertFalse(stat.IsSocket())
            self.assertTrue(stat.IsSymlink())

            stat = utils.Stat(temp_linkpath, follow_symlink=True)
            self.assertFalse(stat.IsDirectory())
            self.assertTrue(stat.IsRegular())
            self.assertFalse(stat.IsSocket())
            self.assertFalse(stat.IsSymlink())
            self.assertEqual(stat.GetSize(), 6)
예제 #9
0
    def testGetFlagsSocket(self):
        with temp.AutoTempDirPath(remove_non_empty=True) as temp_dirpath:
            temp_socketpath = os.path.join(temp_dirpath, "foo")

            sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
            try:
                sock.bind(temp_socketpath)

                stat = utils.Stat(temp_socketpath, follow_symlink=False)
                self.assertTrue(stat.IsSocket())
                self.assertEqual(stat.GetLinuxFlags(), 0)
                self.assertEqual(stat.GetOsxFlags(), 0)
            finally:
                sock.close()
예제 #10
0
    def testSocket(self):
        with temp.AutoTempDirPath(remove_non_empty=True) as temp_dirpath:
            temp_socketpath = os.path.join(temp_dirpath, "foo")

            sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
            try:
                sock.bind(temp_socketpath)

                stat = utils.Stat(temp_socketpath, follow_symlink=False)
                self.assertFalse(stat.IsDirectory())
                self.assertFalse(stat.IsRegular())
                self.assertTrue(stat.IsSocket())
                self.assertFalse(stat.IsSymlink())
            finally:
                sock.close()
예제 #11
0
def StatEntryFromPath(path, pathspec, ext_attrs=True):
  """Builds a stat entry object from a given path.

  Args:
    path: A path (string value) to stat.
    pathspec: A `PathSpec` corresponding to the `path`.
    ext_attrs: Whether to include extended file attributes in the result.

  Returns:
    `StatEntry` object.
  """
  try:
    stat = utils.Stat(path)
  except (IOError, OSError) as error:
    logging.error("Failed to obtain stat for '%s': %s", pathspec, error)
    return rdf_client_fs.StatEntry(pathspec=pathspec)

  return StatEntryFromStat(stat, pathspec, ext_attrs=ext_attrs)
예제 #12
0
 def testGetPath(self):
     with temp.AutoTempFilePath() as temp_filepath:
         stat = utils.Stat(temp_filepath, follow_symlink=False)
         self.assertEqual(stat.GetPath(), temp_filepath)
예제 #13
0
 def _GetDevice(self, path):
     try:
         return utils.Stat(path).GetDevice()
     except (IOError, OSError) as error:
         logging.error("Failed to obtain device for '%s' (%s)", path, error)
         return None
예제 #14
0
 def Stat(self):
   return utils.Stat(self.temp_filepath, follow_symlink=False)