Python stat.S_IFREG Examples

The following are 30 code examples of stat.S_IFREG(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module stat , or try the search function .
Example #1
Source File: __init__.py    From adbutils with MIT License 6 votes vote down vote up
def push(self, src, dst: str, mode: int = 0o755, filesize: int = None):
        # IFREG: File Regular
        # IFDIR: File Directory
        path = dst + "," + str(stat.S_IFREG | mode)
        total_size = 0
        with self._prepare_sync(path, "SEND") as c:
            r = src if hasattr(src, "read") else open(src, "rb")
            try:
                while True:
                    chunk = r.read(4096)
                    if not chunk:
                        mtime = int(datetime.datetime.now().timestamp())
                        c.conn.send(b"DONE" + struct.pack("<I", mtime))
                        break
                    c.conn.send(b"DATA" + struct.pack("<I", len(chunk)))
                    c.conn.send(chunk)
                    total_size += len(chunk)
                assert c.read(4) == _OKAY
            finally:
                if hasattr(r, "close"):
                    r.close()
        # wait until really pushed
        # if filesize:
        #     print("Read: %d Copied: %d" % (filesize, total_size), self.stat(dst)) 
Example #2
Source File: t3_fsck.py    From s3ql with GNU General Public License v3.0 6 votes vote down vote up
def test_wrong_block_refcount(self):

        obj_id = self.db.rowid('INSERT INTO objects (refcount, size) VALUES(1, 23)')
        self.backend['s3ql_data_%d' % obj_id] = b'foo'
        block_id = self.db.rowid('INSERT INTO blocks (refcount, obj_id, size, hash) '
                                 'VALUES(?,?,?,?)', (1, obj_id, 0, sha256(b'')))

        inode = self.db.rowid("INSERT INTO inodes (mode,uid,gid,mtime_ns,atime_ns,ctime_ns,refcount,size) "
                              "VALUES (?,?,?,?,?,?,?,?)",
                              (stat.S_IFREG | stat.S_IRUSR | stat.S_IWUSR, os.getuid(), os.getgid(),
                               time_ns(), time_ns(), time_ns(), 1, self.max_obj_size))
        self._link(b'test-entry', inode)

        self.db.execute('INSERT INTO inode_blocks (inode, blockno, block_id) VALUES(?,?,?)',
                        (inode, 0, block_id))
        self.db.execute('INSERT INTO inode_blocks (inode, blockno, block_id) VALUES(?,?,?)',
                        (inode, 1, block_id))

        self.assert_fsck(self.fsck.check_blocks_refcount) 
Example #3
Source File: t3_fsck.py    From s3ql with GNU General Public License v3.0 6 votes vote down vote up
def test_obj_refcounts(self):

        obj_id = self.db.rowid('INSERT INTO objects (refcount, size) VALUES(1, 42)')
        block_id_1 = self.db.rowid('INSERT INTO blocks (refcount, obj_id, size, hash) '
                                   'VALUES(?,?,?,?)', (1, obj_id, 0, sha256(b'foo')))
        block_id_2 = self.db.rowid('INSERT INTO blocks (refcount, obj_id, size, hash) '
                                   'VALUES(?,?,?,?)', (1, obj_id, 0, sha256(b'bar')))
        self.backend['s3ql_data_%d' % obj_id] = b'foo and bar'

        inode = self.db.rowid("INSERT INTO inodes (mode,uid,gid,mtime_ns,atime_ns,ctime_ns,refcount,size) "
                              "VALUES (?,?,?,?,?,?,?,?)",
                              (stat.S_IFREG | stat.S_IRUSR | stat.S_IWUSR,
                               os.getuid(), os.getgid(), time_ns(), time_ns(), time_ns(),
                               1, 2048))
        self._link(b'test-entry', inode)
        self.db.execute('INSERT INTO inode_blocks (inode, blockno, block_id) VALUES(?,?,?)',
                        (inode, 1, block_id_1))
        self.db.execute('INSERT INTO inode_blocks (inode, blockno, block_id) VALUES(?,?,?)',
                        (inode, 2, block_id_2))

        self.assert_fsck(self.fsck.check_objects_refcount) 
Example #4
Source File: address_space_fuse.py    From rekall with GNU General Public License v2.0 6 votes vote down vote up
def getattr(self, path):
        # The path represents a pid.
        components = os.path.split(path)
        if len(components) > 2:
            return

        if len(components) == 2 and components[1] in self.tasks:
            s = make_stat(components[1])
            s.st_mode = stat.S_IFREG
            s.st_size = self.address_space_size

            return s

        elif components[0] == "/":
            s = make_stat(2)
            s.st_mode = stat.S_IFDIR

            return s 
Example #5
Source File: fuse.py    From filesystem_spec with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def getattr(self, path, fh=None):
        path = "".join([self.root, path.lstrip("/")]).rstrip("/")
        try:
            info = self.fs.info(path)
        except FileNotFoundError:
            raise FuseOSError(ENOENT)
        data = {"st_uid": 1000, "st_gid": 1000}
        perm = 0o777

        if info["type"] != "file":
            data["st_mode"] = stat.S_IFDIR | perm
            data["st_size"] = 0
            data["st_blksize"] = 0
        else:
            data["st_mode"] = stat.S_IFREG | perm
            data["st_size"] = info["size"]
            data["st_blksize"] = 5 * 2 ** 20
            data["st_nlink"] = 1
        data["st_atime"] = time.time()
        data["st_ctime"] = time.time()
        data["st_mtime"] = time.time()
        return data 
Example #6
Source File: panel.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def _check_cert(filename):
    """
    Does this certificate file look okay?

    Returns error message, or None if okay
    """
    try:
        st = os.stat(filename)
    except OSError:
        return filename + " doesn't exist"
    else:
        good_perm = stat.S_IFREG | stat.S_IRUSR # | stat.S_IWUSR
        if (st[stat.ST_UID], st[stat.ST_GID]) != (0,0):
            return 'not owned by root.root'
        perm = st[stat.ST_MODE]
        if good_perm != perm:
            return "expected permissions %o but found %o." % (good_perm, perm) 
Example #7
Source File: test_smbclient_os.py    From smbprotocol with MIT License 6 votes vote down vote up
def test_lstat_on_file(smb_share):
    filename = ntpath.join(smb_share, 'file.txt')
    with smbclient.open_file(filename, mode='w') as fd:
        fd.write(u"Content")

    actual = smbclient.lstat(filename)
    assert isinstance(actual, smbclient.SMBStatResult)
    assert actual.st_atime == actual.st_atime_ns / 1000000000
    assert actual.st_mtime == actual.st_mtime_ns / 1000000000
    assert actual.st_ctime == actual.st_ctime_ns / 1000000000
    assert actual.st_chgtime == actual.st_chgtime_ns / 1000000000
    assert actual.st_dev is not None
    assert actual.st_file_attributes == FileAttributes.FILE_ATTRIBUTE_ARCHIVE
    assert actual.st_gid == 0
    assert actual.st_uid == 0
    assert actual.st_ino is not None
    assert actual.st_mode == stat.S_IFREG | 0o666
    assert actual.st_nlink == 1
    assert actual.st_size == 7
    assert actual.st_uid == 0
    assert actual.st_reparse_tag == 0 
Example #8
Source File: t3_fsck.py    From s3ql with GNU General Public License v3.0 6 votes vote down vote up
def test_unix_nomode_dir(self):

        perms = stat.S_IRUSR | stat.S_IWUSR | stat.S_IROTH | stat.S_IRGRP
        stamp = time_ns()
        inode = self.db.rowid("INSERT INTO inodes (mode,uid,gid,mtime_ns,atime_ns,ctime_ns,refcount) "
                              "VALUES (?,?,?,?,?,?,?)",
                              (perms, os.getuid(), os.getgid(), stamp, stamp, stamp, 1))
        inode2 = self.db.rowid("INSERT INTO inodes (mode,uid,gid,mtime_ns,atime_ns,ctime_ns,refcount) "
                              "VALUES (?,?,?,?,?,?,?)",
                              (perms | stat.S_IFREG, os.getuid(), os.getgid(), stamp,
                               stamp, stamp, 1))

        self._link(b'test-entry', inode)
        self._link(b'subentry', inode2, inode)

        self.assert_fsck(self.fsck.check_unix)

        newmode = self.db.get_val('SELECT mode FROM inodes WHERE id=?', (inode,))
        self.assertEqual(stat.S_IMODE(newmode), perms)
        self.assertEqual(stat.S_IFMT(newmode), stat.S_IFDIR) 
Example #9
Source File: kmip_secret_store.py    From barbican with Apache License 2.0 6 votes vote down vote up
def _validate_keyfile_permissions(self, path):
        """Check that file has permissions appropriate for a sensitive key

        Key files are extremely sensitive, they should be owned by the user
        who they relate to. They should be readable only (to avoid accidental
        changes). They should not be readable or writable by any other user.

        :raises: KMIPSecretStoreError
        """
        expected = (stat.S_IRUSR | stat.S_IFREG)  # 0o100400
        st = os.stat(path)
        if st.st_mode != expected:
            raise KMIPSecretStoreError(
                u._('Bad key file permissions found, expected 400 '
                    'for path: {file_path}').format(file_path=path)
            ) 
Example #10
Source File: fileutil.py    From osxphotos with MIT License 6 votes vote down vote up
def cmp_sig(cls, f1, s2):
        """Compare file f1 to signature s2.
        Arguments:
        f1 --  File name
        s2 -- stats as returned by sig

        Return value:
        True if the files are the same, False otherwise.
        """

        if not s2:
            return False

        s1 = cls._sig(os.stat(f1))

        if s1[0] != stat.S_IFREG or s2[0] != stat.S_IFREG:
            return False
        return s1 == s2 
Example #11
Source File: mkfs.py    From s3ql with GNU General Public License v3.0 6 votes vote down vote up
def init_tables(conn):
    # Insert root directory
    now_ns = time_ns()
    conn.execute("INSERT INTO inodes (id,mode,uid,gid,mtime_ns,atime_ns,ctime_ns,refcount) "
                 "VALUES (?,?,?,?,?,?,?,?)",
                   (ROOT_INODE, stat.S_IFDIR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
                   | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH,
                    os.getuid(), os.getgid(), now_ns, now_ns, now_ns, 1))

    # Insert control inode, the actual values don't matter that much
    conn.execute("INSERT INTO inodes (id,mode,uid,gid,mtime_ns,atime_ns,ctime_ns,refcount) "
                 "VALUES (?,?,?,?,?,?,?,?)",
                 (CTRL_INODE, stat.S_IFREG | stat.S_IRUSR | stat.S_IWUSR,
                  0, 0, now_ns, now_ns, now_ns, 42))

    # Insert lost+found directory
    inode = conn.rowid("INSERT INTO inodes (mode,uid,gid,mtime_ns,atime_ns,ctime_ns,refcount) "
                       "VALUES (?,?,?,?,?,?,?)",
                       (stat.S_IFDIR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR,
                        os.getuid(), os.getgid(), now_ns, now_ns, now_ns, 1))
    name_id = conn.rowid('INSERT INTO names (name, refcount) VALUES(?,?)',
                         (b'lost+found', 1))
    conn.execute("INSERT INTO contents (name_id, inode, parent_inode) VALUES(?,?,?)",
                 (name_id, inode, ROOT_INODE)) 
Example #12
Source File: repository.py    From gitfs with Apache License 2.0 6 votes vote down vote up
def get_git_object_default_stats(self, ref, path):
        types = {
            GIT_FILEMODE_LINK: {"st_mode": S_IFLNK | 0o444},
            GIT_FILEMODE_TREE: {"st_mode": S_IFDIR | 0o555, "st_nlink": 2},
            GIT_FILEMODE_BLOB: {"st_mode": S_IFREG | 0o444},
            GIT_FILEMODE_BLOB_EXECUTABLE: {"st_mode": S_IFREG | 0o555},
        }

        if path == "/":
            return types[GIT_FILEMODE_TREE]

        obj_type = self.get_git_object_type(ref, path)
        if obj_type is None:
            return obj_type

        stats = types[obj_type]
        if obj_type in [GIT_FILEMODE_BLOB, GIT_FILEMODE_BLOB_EXECUTABLE]:
            stats["st_size"] = self.get_blob_size(ref, path)

        return stats 
Example #13
Source File: test_repository.py    From gitfs with Apache License 2.0 6 votes vote down vote up
def test_git_obj_default_stats_with_valid_obj(self):
        mocked_repo = MagicMock()
        mocked_git_obj = MagicMock()
        mocked_size = MagicMock()

        mocked_git_obj.return_value = GIT_FILEMODE_BLOB
        mocked_size.return_value = 10

        repo = Repository(mocked_repo)
        repo.get_git_object_type = mocked_git_obj
        repo.get_blob_size = mocked_size

        assert repo.get_git_object_default_stats("ref", "/ups") == {
            "st_mode": S_IFREG | 0o444,
            "st_size": 10,
        }
        mocked_size.assert_called_once_with("ref", "/ups")
        mocked_git_obj.assert_called_once_with("ref", "/ups") 
Example #14
Source File: DropBox.py    From stash with MIT License 6 votes vote down vote up
def stat(self, name):
        ap = self.abspath(name)
        if ap in ("/", "/.", "./", "//", ""):
            bytes = 0
            isdir = True
        else:
            try:
                meta = self.client.files_get_metadata(ap)
            except dropbox.exceptions.ApiError as e:
                raise OperationFailure(e.message)
            if isinstance(meta, (dropbox.files.FolderMetadata, dropbox.sharing.SharedFolderMetadata)):
                bytes = 0
                isdir = True
            else:
                bytes = meta.size
                isdir = False

        type_ = (stat.S_IFDIR if isdir else stat.S_IFREG)
        m = calc_mode(type=type_)
        s = make_stat(size=bytes, mode=m)
        return s 
Example #15
Source File: zip.py    From stash with MIT License 6 votes vote down vote up
def stat(self, name):
        ap = self.abspath(name)
        self.log("stat: {ap}\n".format(ap=ap))
        isdir = self.isdir(name)
        isfile = self.isfile(name)
        if not (isdir or isfile):
            self.log("stat-target not found.\n")
            raise errors.OperationFailure("Not found!")
        if isdir:
            size = 1
            mtime = None
        else:
            zipinfo = self.zf.getinfo(ap)
            size = zipinfo.file_size
            timestamp = zipinfo.date_time
            dt = datetime.datetime(*timestamp)
            mtime = (dt - datetime.datetime(1970, 1, 1)).total_seconds()
        type_ = (stat.S_IFREG if isfile else stat.S_IFDIR)
        mode = base.calc_mode(type=type_)
        self.log("stat return\n")
        return base.make_stat(size=size, mtime=mtime, ctime=mtime, mode=mode) 
Example #16
Source File: sn_fuse.py    From standardnotes-fs with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, sn_api, sync_sec, ext, path='.'):
        self.item_manager = ItemManager(sn_api, ext)

        self.uid = os.getuid()
        self.gid = os.getgid()

        now = datetime.now().timestamp()
        self.dir_stat = dict(st_mode=(S_IFDIR | DIR_PERMISSIONS), st_ctime=now,
                             st_mtime=now, st_atime=now, st_nlink=2,
                             st_uid=self.uid, st_gid=self.gid)

        self.note_stat = dict(st_mode=(S_IFREG | FILE_PERMISSIONS), st_ctime=now,
                              st_mtime=now, st_atime=now, st_nlink=1,
                              st_uid=self.uid, st_gid=self.gid)

        self.sync_sec = sync_sec
        self.ext = ext
        self.run_sync = Event()
        self.stop_sync = Event()
        self.sync_thread = Thread(target=self._sync_thread) 
Example #17
Source File: pathio.py    From aioftp with Apache License 2.0 6 votes vote down vote up
def stat(self, path):
        node = self.get_node(path)
        if node is None:
            raise FileNotFoundError
        else:
            if node.type == "file":
                size = len(node.content.getbuffer())
                mode = stat.S_IFREG | 0o666
            else:
                size = 0
                mode = stat.S_IFDIR | 0o777
            return MemoryPathIO.Stats(
                size,
                node.ctime,
                node.mtime,
                1,
                mode,
            ) 
Example #18
Source File: mount.py    From python-ntfs with Apache License 2.0 6 votes vote down vote up
def getattr(self, path, fh=None):
        (uid, gid, pid) = fuse_get_context()
        entry = self._get_path_entry(path)

        if entry.is_directory():
            mode = (stat.S_IFDIR | PERMISSION_ALL_READ)
            nlink = 2
        else:
            mode = (stat.S_IFREG | PERMISSION_ALL_READ)
            nlink = 1

        return {
            "st_atime": unixtimestamp(entry.get_si_accessed_timestamp()),
            "st_ctime": unixtimestamp(entry.get_si_changed_timestamp()),
            "st_crtime": unixtimestamp(entry.get_si_created_timestamp()),
            "st_mtime": unixtimestamp(entry.get_si_modified_timestamp()),
            "st_size": entry.get_size(),
            "st_uid": uid,
            "st_gid": gid,
            "st_mode": mode,
            "st_nlink": nlink,
        } 
Example #19
Source File: interface.py    From pytest-sftpserver with MIT License 6 votes vote down vote up
def stat(self):
        if self.content_provider.get(self.path) is None:
            return SFTP_NO_SUCH_FILE

        mtime = calendar.timegm(datetime.now().timetuple())

        sftp_attrs = SFTPAttributes()
        sftp_attrs.st_size = self.content_provider.get_size(self.path)
        sftp_attrs.st_uid = 0
        sftp_attrs.st_gid = 0
        sftp_attrs.st_mode = (
            stat.S_IRWXO
            | stat.S_IRWXG
            | stat.S_IRWXU
            | (stat.S_IFDIR if self.content_provider.is_dir(self.path) else stat.S_IFREG)
        )
        sftp_attrs.st_atime = mtime
        sftp_attrs.st_mtime = mtime
        sftp_attrs.filename = posixpath.basename(self.path)
        return sftp_attrs 
Example #20
Source File: kmip_secret_store.py    From sgx-kms with Apache License 2.0 6 votes vote down vote up
def _validate_keyfile_permissions(self, path):
        """Check that file has permissions appropriate for a sensitive key

        Key files are extremely sensitive, they should be owned by the user
        who they relate to. They should be readable only (to avoid accidental
        changes). They should not be readable or writeable by any other user.

        :raises: KMIPSecretStoreError
        """
        expected = (stat.S_IRUSR | stat.S_IFREG)  # 0o100400
        st = os.stat(path)
        if st.st_mode != expected:
            raise KMIPSecretStoreError(
                u._('Bad key file permissions found, expected 400 '
                    'for path: {file_path}').format(file_path=path)
            ) 
Example #21
Source File: test_kmip.py    From sgx-kms with Apache License 2.0 5 votes vote down vote up
def test_check_keyfile_permissions_bad(self):
        config = {'return_value.st_mode':
                  (stat.S_IWOTH | stat.S_IFREG)}

        with mock.patch('os.stat', **config):
            self.assertRaises(
                kss.KMIPSecretStoreError,
                self.secret_store._validate_keyfile_permissions,
                '/some/path/') 
Example #22
Source File: test_stat.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_mode(self):
        with open(TESTFN, 'w'):
            pass
        if os.name == 'posix':
            os.chmod(TESTFN, 0o700)
            st_mode = self.get_mode()
            self.assertS_IS("REG", st_mode)
            self.assertEqual(stat.S_IMODE(st_mode),
                             stat.S_IRWXU)

            os.chmod(TESTFN, 0o070)
            st_mode = self.get_mode()
            self.assertS_IS("REG", st_mode)
            self.assertEqual(stat.S_IMODE(st_mode),
                             stat.S_IRWXG)

            os.chmod(TESTFN, 0o007)
            st_mode = self.get_mode()
            self.assertS_IS("REG", st_mode)
            self.assertEqual(stat.S_IMODE(st_mode),
                             stat.S_IRWXO)

            os.chmod(TESTFN, 0o444)
            st_mode = self.get_mode()
            self.assertS_IS("REG", st_mode)
            self.assertEqual(stat.S_IMODE(st_mode), 0o444)
        else:
            os.chmod(TESTFN, 0o700)
            st_mode = self.get_mode()
            self.assertS_IS("REG", st_mode)
            self.assertEqual(stat.S_IFMT(st_mode),
                             stat.S_IFREG) 
Example #23
Source File: baidufuse2.py    From baidu-fuse with GNU General Public License v2.0 5 votes vote down vote up
def _add_file_to_buffer(self, path,file_info):
        foo = File()
        foo['st_ctime'] = file_info['local_ctime']
        foo['st_mtime'] = file_info['local_mtime']
        foo['st_mode'] = (stat.S_IFDIR | 0777) if file_info['isdir'] \
            else (stat.S_IFREG | 0777)
        foo['st_nlink'] = 2 if file_info['isdir'] else 1
        foo['st_size'] = file_info['size']
        self.buffer[path] = foo 
Example #24
Source File: dptmount.py    From dpt-rp1-py with MIT License 5 votes vote down vote up
def create(self, path, mode, fi=None):
        #TODO: check if files is necessary
        logger.debug("create path {}".format(path))
        self.files[path] = dict(
            st_mode=(S_IFREG | mode),
            st_nlink=1,
            st_size=0,
            st_ctime=time.time(),
            st_mtime=time.time(),
            st_atime=time.time(),
        )

        self.fd += 1
        self.handle[self.fd] = FileHandle(self, path, new=True)
        return self.fd 
Example #25
Source File: test_kmip.py    From sgx-kms with Apache License 2.0 5 votes vote down vote up
def test_validate_keyfile_permissions_good(self):
        config = {'return_value.st_mode':
                  (stat.S_IRUSR | stat.S_IFREG)}

        with mock.patch('os.stat', **config):
            self.assertIsNone(
                self.secret_store._validate_keyfile_permissions('/some/path/')) 
Example #26
Source File: filecmp.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def cmp(f1, f2, shallow=1):
    """Compare two files.

    Arguments:

    f1 -- First file name

    f2 -- Second file name

    shallow -- Just check stat signature (do not read the files).
               defaults to 1.

    Return value:

    True if the files are the same, False otherwise.

    This function uses a cache for past comparisons and the results,
    with a cache invalidation mechanism relying on stale signatures.

    """

    s1 = _sig(os.stat(f1))
    s2 = _sig(os.stat(f2))
    if s1[0] != stat.S_IFREG or s2[0] != stat.S_IFREG:
        return False
    if shallow and s1 == s2:
        return True
    if s1[1] != s2[1]:
        return False

    result = _cache.get((f1, f2))
    if result and (s1, s2) == result[:2]:
        return result[2]
    outcome = _do_cmp(f1, f2)
    _cache[f1, f2] = s1, s2, outcome
    return outcome 
Example #27
Source File: t3_fsck.py    From s3ql with GNU General Public License v3.0 5 votes vote down vote up
def test_unix_child(self):

        inode = self.db.rowid("INSERT INTO inodes (mode,uid,gid,mtime_ns,atime_ns,ctime_ns,refcount) "
                   "VALUES (?,?,?,?,?,?,?)",
                   (stat.S_IFREG | stat.S_IRUSR | stat.S_IWUSR,
                    os.getuid(), os.getgid(), time_ns(), time_ns(), time_ns(), 1))
        self._link(b'test-entry', inode)

        self.fsck.found_errors = False
        self.fsck.check_unix()
        self.assertFalse(self.fsck.found_errors)
        self.db.execute('INSERT INTO contents (name_id, inode, parent_inode) VALUES(?,?,?)',
                        (self._add_name(b'foo'), ROOT_INODE, inode))
        self.fsck.check_unix()
        self.assertTrue(self.fsck.found_errors) 
Example #28
Source File: sn_fuse.py    From standardnotes-fs with GNU General Public License v3.0 5 votes vote down vote up
def access(self, path, mode):
        if mode == os.X_OK:
            if self.getattr(path)['st_mode'] & S_IFREG:
                raise FuseOSError(errno.EPERM)
            else:
                return 0

        return 0 
Example #29
Source File: win32stat.py    From script.service.kodi.callbacks with GNU General Public License v3.0 5 votes vote down vote up
def _to_mode(attr):
    m = 0
    if (attr & FILE_ATTRIBUTE_DIRECTORY):
        m |= stdstat.S_IFDIR | 0o111
    else:
        m |= stdstat.S_IFREG
    if (attr & FILE_ATTRIBUTE_READONLY):
        m |= 0o444
    else:
        m |= 0o666
    return m 
Example #30
Source File: filecmp.py    From datafari with Apache License 2.0 5 votes vote down vote up
def cmp(f1, f2, shallow=1):
    """Compare two files.

    Arguments:

    f1 -- First file name

    f2 -- Second file name

    shallow -- Just check stat signature (do not read the files).
               defaults to 1.

    Return value:

    True if the files are the same, False otherwise.

    This function uses a cache for past comparisons and the results,
    with a cache invalidation mechanism relying on stale signatures.

    """

    s1 = _sig(os.stat(f1))
    s2 = _sig(os.stat(f2))
    if s1[0] != stat.S_IFREG or s2[0] != stat.S_IFREG:
        return False
    if shallow and s1 == s2:
        return True
    if s1[1] != s2[1]:
        return False

    outcome = _cache.get((f1, f2, s1, s2))
    if outcome is None:
        outcome = _do_cmp(f1, f2)
        if len(_cache) > 100:      # limit the maximum size of the cache
            _cache.clear()
        _cache[f1, f2, s1, s2] = outcome
    return outcome