コード例 #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
ファイル: conditions_test.py プロジェクト: ehossam/grr
 def Stat(self):
   return utils.Stat(self.temp_filepath, follow_symlink=False)