Example #1
0
    def chflags(self, *flags):
        s = 0
        
        if "no dump" in flags:
            s |= stat.UF_NODUMP
        if "immutable" in flags:
            s |= stat.UF_IMMUTABLE
        if "append" in flags:
            s |= stat.UF_APPEND
        if "opaque" in flags:
            s |= stat.UF_OPAQUE
        if "no unlink" in flags:
            s |= stat.UF_NOUNLINK
        if "archived" in flags:
            s |= stat.SF_ARCHIVED
        if "system immutable" in flags:
            s |= stat.SF_IMMUTABLE
        if "system append" in flags:
            s |= stat.SF_APPEND
        if "system no unlink" in flags:
            s |= stat.SF_NOUNLINK
        if "snapshot" in flags:
            s |= stat.SF_SNAPSHOT

        os.chflags(self.path, s)
Example #2
0
def copystat(src, dst):
    """Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""

    st = os.stat(src)

    mode = stat.S_IMODE(st.st_mode)

    if hasattr(os, 'utime'):

        os.utime(dst, (st.st_atime, st.st_mtime))

    if hasattr(os, 'chmod'):

        os.chmod(dst, mode)

    if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):

        try:

            os.chflags(dst, st.st_flags)

        except OSError as why:

            if (not hasattr(errno, 'EOPNOTSUPP')
                    or why.errno != errno.EOPNOTSUPP):

                raise
Example #3
0
 def set_access_control_string(self, security, information=SecurityInformation.OWNER | SecurityInformation.GROUP | SecurityInformation.DACL):
     '''
     fi.set_access_control_string(security, [infomarion]) -> None
     Applies access control described by a SECURITY string to the file described by the current FileInfo object.
     If non-WinNT system, sets os.chmode(), os.chown(), os.chflags() to security, else use SetFileSecurity(self.original_path, information, security) api function, with security as a SECURITY string
     'information' defaults to SecurityInformation.OWNER | SecurityInformation.GROUP | SecurityInformation.DACL
     '''
     if os.path.exists(self.original_path):
         if isinstance(information, SecurityInformation):
             information = information.value
         if os.name != "nt":
             if information & 0x1:
                 os.chmod(self.original_path, security.st_mode)
             if information & 0x2:
                 os.chown(self.original_path,
                          security.st_uid, security.st_gid)
             if information & 0x4:
                 os.chflags(self.original_path, security.st_flags)
         else:
             try:
                 import win32security
             except:
                 raise NotSupportedException("you need pywin modules")
             try:
                 security = win32security.ConvertStringSecurityDescriptorToSecurityDescriptor(
                     security, win32security.SDDL_REVISION_1)
                 win32security.SetFileSecurity(
                     self.original_path, information, security)
             except win32security.error as err:
                 raise UnauthorizedAccessException(err)
     else:
         raise FileNotFoundException("'%s' not found" % self.original_path)
Example #4
0
def unlock_path(root, name, mode):
    rmflags = stat.UF_IMMUTABLE | stat.UF_NOUNLINK
    p = os.path.join(root, name)
    pst = os.stat(p)
    logging.info(f'unlocking path “{p}”')
    os.chflags(p, pst.st_flags & ~rmflags)
    os.chmod(p, mode)
Example #5
0
def build_directory(p):
    fa1 = p.join('filea1.yaml').ensure()
    os.chflags(fa1.strpath, 0b1000000000000000)
    fa2 = p.join('filea2.json').ensure()
    fa2.chmod(0o600)
    p.join('filea3.txt').ensure()

    d1 = p.mkdir('test')
    d1.join('fileb1.txt').ensure()
    fb2 = d1.join('fileb2.json').ensure()
    fb2.chmod(0o600)
    os.chflags(fb2.strpath, 0b1000000000000000)
    d1.join('fileb3.txt').ensure()

    s1 = d1.join('symlinkb1.txt')
    s1.mksymlinkto('fileb1.txt')
    s2 = d1.join('symlinkb2.json')
    s2.mksymlinkto('fileb2.json')
    s3 = d1.join('symlinkb3.txt')
    s3.mksymlinkto('fileb3.txt')

    d2 = d1.mkdir('wow')
    d2.join('filec1.zip').ensure()
    d2.join('filec2.txt').ensure()
    d2.join('filec3.yaml').ensure()
Example #6
0
def test_file_action_set_file_attributes_change_flags_same(tmpdir):
    p = tmpdir.join('test.txt').ensure()
    os.chflags(p.strpath, 0b1000000000000000)

    file_action = FileAction(flags=['hidden'])
    assert file_action.set_file_attributes(p.strpath) is False
    assert p.stat().flags == 0b1000000000000000
Example #7
0
def lock_path(root, name, mode):
    """Lock down a path"""
    flags = stat.UF_IMMUTABLE | stat.UF_NOUNLINK
    p = os.path.join(root, name)
    logging.info('locking path “{}”'.format(p))
    os.chmod(p, mode)
    os.chflags(p, flags)
Example #8
0
    def set_folder_icon_darwin(self, ref: str, icon: str) -> None:
        """
        macOS: configure a folder with a given custom icon
            1. Read the com.apple.ResourceFork extended attribute from the icon file
            2. Set the com.apple.FinderInfo extended attribute with folder icon flag
            3. Create a Icon file (name: Icon\r) inside the target folder
            4. Set extended attributes com.apple.FinderInfo & com.apple.ResourceFork for icon file (name: Icon\r)
            5. Hide the icon file (name: Icon\r)
        """

        target_folder = self.abspath(ref)

        # Generate the value for 'com.apple.FinderInfo'
        has_icon_xdata = bytes(bytearray(self._get_icon_xdata()))

        # Configure 'com.apple.FinderInfo' for the folder
        xattr.setxattr(target_folder, xattr.XATTR_FINDERINFO_NAME,
                       has_icon_xdata)

        # Create the 'Icon\r' file
        meta_file = os.path.join(target_folder, "Icon\r")
        if os.path.isfile(meta_file):
            os.remove(meta_file)
        open(meta_file, "w").close()

        # Configure 'com.apple.FinderInfo' for the Icon file
        xattr.setxattr(meta_file, xattr.XATTR_FINDERINFO_NAME, has_icon_xdata)

        # Configure 'com.apple.ResourceFork' for the Icon file
        info = self._read_data(icon)
        xattr.setxattr(meta_file, xattr.XATTR_RESOURCEFORK_NAME, info)
        os.chflags(meta_file, stat.UF_HIDDEN)
Example #9
0
    def set_folder_icon_darwin(self, ref, icon):
        ''' Mac: Configure a folder with a given custom icon
            1. Read the com.apple.ResourceFork extended attribute from the icon file
            2. Set the com.apple.FinderInfo extended attribute with folder icon flag
            3. Create a Icon file (name: Icon\r) inside the target folder
            4. Set extended attributes com.apple.FinderInfo & com.apple.ResourceFork for icon file (name: Icon\r)
            5. Hide the icon file (name: Icon\r)
        '''
        try:
            import xattr
            import stat
            target_folder = self._abspath(ref)
            # Generate the value for 'com.apple.FinderInfo'
            has_icon_xdata = bytes(bytearray(self._get_icon_xdata()))
            # Configure 'com.apple.FinderInfo' for the folder
            xattr.setxattr(target_folder, xattr.XATTR_FINDERINFO_NAME, has_icon_xdata)
            # Create the 'Icon\r' file
            meta_file = os.path.join(target_folder, "Icon\r")
            if os.path.exists(meta_file):
                os.remove(meta_file)
            open(meta_file, "w").close()
            # Configure 'com.apple.FinderInfo' for the Icon file
            xattr.setxattr(meta_file, xattr.XATTR_FINDERINFO_NAME, has_icon_xdata)
            # Configure 'com.apple.ResourceFork' for the Icon file
            info = self._read_data(icon)
            xattr.setxattr(meta_file, xattr.XATTR_RESOURCEFORK_NAME, info)
            os.chflags(meta_file, stat.UF_HIDDEN)

        except Exception as e:
            log.error("Exception when setting folder icon : %s", e)
Example #10
0
    def set_folder_icon(self, ref: Path, icon: Path) -> None:
        """Create a special file to customize the folder icon.
            1. Read the com.apple.ResourceFork extended attribute from the icon file
            2. Set the com.apple.FinderInfo extended attribute with folder icon flag
            3. Create a Icon file (name: Icon\r) inside the target folder
            4. Set extended attributes com.apple.FinderInfo & com.apple.ResourceFork for icon file (name: Icon\r)
            5. Hide the icon file (name: Icon\r)
        """
        log.debug(f"Setting the folder icon of {ref!r} using {icon!r}")

        target_folder = self.abspath(ref)

        # Generate the value for 'com.apple.FinderInfo'
        has_icon_xdata = bytes(bytearray(self._get_icon_xdata()))

        # Configure 'com.apple.FinderInfo' for the folder
        xattr.setxattr(str(target_folder), xattr.XATTR_FINDERINFO_NAME,
                       has_icon_xdata)

        # Create the 'Icon\r' file
        meta_file = target_folder / "Icon\r"
        if meta_file.is_file():
            meta_file.unlink()
        meta_file.touch()

        # Configure 'com.apple.FinderInfo' for the Icon file
        xattr.setxattr(str(meta_file), xattr.XATTR_FINDERINFO_NAME,
                       has_icon_xdata)

        # Configure 'com.apple.ResourceFork' for the Icon file
        info = icon.read_bytes()
        xattr.setxattr(str(meta_file), xattr.XATTR_RESOURCEFORK_NAME, info)
        os.chflags(meta_file, stat.UF_HIDDEN)  # type: ignore
Example #11
0
    def set_folder_icon_darwin(self, ref, icon):
        ''' Mac: Configure a folder with a given custom icon
            1. Read the com.apple.ResourceFork extended attribute from the icon file
            2. Set the com.apple.FinderInfo extended attribute with folder icon flag
            3. Create a Icon file (name: Icon\r) inside the target folder
            4. Set extended attributes com.apple.FinderInfo & com.apple.ResourceFork for icon file (name: Icon\r)
            5. Hide the icon file (name: Icon\r)
        '''
        try:
            import xattr
            import stat
            target_folder = self._abspath(ref)
            # Generate the value for 'com.apple.FinderInfo'
            has_icon_xdata = bytes(bytearray(self._get_icon_xdata()))
            # Configure 'com.apple.FinderInfo' for the folder
            xattr.setxattr(target_folder, xattr.XATTR_FINDERINFO_NAME,
                           has_icon_xdata)
            # Create the 'Icon\r' file
            meta_file = os.path.join(target_folder, "Icon\r")
            if os.path.exists(meta_file):
                os.remove(meta_file)
            open(meta_file, "w").close()
            # Configure 'com.apple.FinderInfo' for the Icon file
            xattr.setxattr(meta_file, xattr.XATTR_FINDERINFO_NAME,
                           has_icon_xdata)
            # Configure 'com.apple.ResourceFork' for the Icon file
            info = self._read_data(icon)
            xattr.setxattr(meta_file, xattr.XATTR_RESOURCEFORK_NAME, info)
            os.chflags(meta_file, stat.UF_HIDDEN)

        except Exception as e:
            log.error("Exception when setting folder icon : %s", e)
Example #12
0
    def __init__(self,
                 path,
                 mode='w',
                 bufsize=-1,
                 encoding='utf-8',
                 errors='strict'):
        self._file = None
        self._path = os.path.realpath(path)
        dir, name = os.path.split(self._path)
        fd, self._temp = tempfile.mkstemp(prefix=name + '-', dir=dir)
        kwargs = {} if 'b' in mode else \
                 {'encoding': encoding, 'errors': errors}
        self._file = os.fdopen(fd, mode, bufsize, **kwargs)

        # Try to preserve permissions and group ownership, but failure
        # should not be fatal
        try:
            st = os.stat(self._path)
            if hasattr(os, 'chmod'):
                os.chmod(self._temp, st.st_mode)
            if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
                os.chflags(self._temp, st.st_flags)
            if hasattr(os, 'chown'):
                os.chown(self._temp, -1, st.st_gid)
        except OSError:
            pass
Example #13
0
 def resetperms(path):
     try:
         _os.chflags(path, 0)
         _os.chmod(path, stat.S_IWRITE)
     except AttributeError:
         pass
     _os.chmod(path, 0o700)
Example #14
0
def set_write_bit(fn):
    # type: (str) -> None
    """
    Set read-write permissions for the current user on the target path.  Fail silently
    if the path doesn't exist.

    :param str fn: The target filename or path
    :return: None
    """

    if not os.path.exists(fn):
        return
    file_stat = os.stat(fn).st_mode
    os.chmod(fn, file_stat | stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
    if not os.path.isdir(fn):
        for path in [fn, os.path.dirname(fn)]:
            try:
                os.chflags(path, 0)
            except AttributeError:
                pass
        return None
    for root, dirs, files in os.walk(fn, topdown=False):
        for dir_ in [os.path.join(root, d) for d in dirs]:
            set_write_bit(dir_)
        for file_ in [os.path.join(root, f) for f in files]:
            set_write_bit(file_)
Example #15
0
def copystat(stat, dst):
    if hasattr(os, "utime"):
        os.utime(dst, (stat.st_atime, stat.st_mtime))
    if hasattr(os, "chmod"):
        os.chmod(dst, getMode(stat))
    if hasattr(os, "chflags") and hasattr(stat, "st_flags"):
        os.chflags(dst, stat.st_flags)
Example #16
0
def unlock_path(root, name, mode):
    rmflags = stat.UF_IMMUTABLE | stat.UF_NOUNLINK
    p = os.path.join(root, name)
    pst = os.stat(p)
    logging.info(f'unlocking path “{p}”')
    os.chflags(p, pst.st_flags & ~rmflags)
    os.chmod(p, mode)
Example #17
0
def move_file(f, fullpath, dest):
  dest_fullpath = os.path.join(dest, f)
  if not filename_exists(dest_fullpath):
    print("moving: {} -> {}".format(fullpath, dest_fullpath))
    os.chflags(fullpath, 0)
    shutil.move(fullpath, dest_fullpath)
    return

  # try filenames
  splitted = split_media_filename(f)
  if splitted and len(splitted) == 3:
    (letters, numbers, ext) = splitted
  else:
    print("cannot process file, we couldn't parse the filename. {}".format(f))
    return
  #print("tuple: {} / {} / {}".format(letters, numbers, ext))
  print("NOT moving: {} -> {}".format(fullpath, dest_fullpath))
  for x in xrange(1, 999):
    try_f = "{0}_{1}_{2:0>3}.{3}".format(letters, numbers, x, ext)
    dest_fullpath = os.path.join(dest, try_f)
    if not filename_exists(dest_fullpath):
      print("no file here, we can move it: {}".format(dest_fullpath))
      os.chflags(fullpath, 0)
      shutil.move(fullpath, dest_fullpath)
      break
Example #18
0
    def chflags(self, *flags):
        s = 0

        if "no dump" in flags:
            s |= stat.UF_NODUMP
        if "immutable" in flags:
            s |= stat.UF_IMMUTABLE
        if "append" in flags:
            s |= stat.UF_APPEND
        if "opaque" in flags:
            s |= stat.UF_OPAQUE
        if "no unlink" in flags:
            s |= stat.UF_NOUNLINK
        if "archived" in flags:
            s |= stat.SF_ARCHIVED
        if "system immutable" in flags:
            s |= stat.SF_IMMUTABLE
        if "system append" in flags:
            s |= stat.SF_APPEND
        if "system no unlink" in flags:
            s |= stat.SF_NOUNLINK
        if "snapshot" in flags:
            s |= stat.SF_SNAPSHOT

        os.chflags(self.path, s)
def main():
    sh('rm -rf ${DEBUG_WORLD}')
    sh('mkdir -p ${DEBUG_WORLD}')

    info('Saving debug information in ${{DEBUG_WORLD}}')

    for root, dirs, files in os.walk(e('${WORLD_DESTDIR}/')):
        for name in files:
            filename = os.path.join(root, name)
            relpath = os.path.relpath(filename, e('${WORLD_DESTDIR}'))
            destpath = os.path.join(e('${DEBUG_WORLD}'), relpath)

            ext = os.path.splitext(name)[1]

            if ext == '.ko':
                continue

            if relpath.startswith(('boot', 'usr/local/lib/grub')):
                continue

            if ext == '.c':
                make_dir(destpath)
                shutil.move(filename, destpath)
                for f in os.listdir(root):
                    if f.endswith('.html'):
                        html_path = os.path.join(root, f)
                        if 'Generated by Cython' in open(html_path).read():
                            relpath = os.path.relpath(html_path,
                                                      e('${WORLD_DESTDIR}'))
                            destpath = os.path.join(e('${DEBUG_WORLD}'),
                                                    relpath)
                            shutil.move(html_path, destpath)
                continue

            if ext == '.html':
                continue

            if not is_elf(filename):
                continue

            make_dir(destpath)

            # We need to remove any flags on protected files and restore
            # them after stripping
            flags = os.stat(filename).st_flags
            os.chflags(filename, 0)

            if not relpath.startswith('rescue'):
                sh('objcopy --only-keep-debug ${filename} ${destpath}.debug')
                sh('objcopy --strip-unneeded ${filename}', nofail=True)
                sh('objcopy --add-gnu-debuglink="${destpath}.debug" ${filename}',
                   log='/dev/null',
                   nofail=True)
            else:
                sh('strip ${filename}')

            os.chflags(filename, flags)
Example #20
0
 def write(self, path, chmod_only=False):
     """\
     Apply all stat info (mode bits, atime, mtime, flags) to path.
     """
     if not chmod_only:
         os.utime(path, (self.atime, self.mtime), follow_symlinks=False)
         os.chown(path, self.uid, self.gid, follow_symlinks=False)
         os.chflags(path, self.flags, follow_symlinks=False)
     os.chmod(path, self.mode, follow_symlinks=False)
Example #21
0
 def write(self, path, chmod_only=False):
     """\
     Apply all stat info (mode bits, atime, mtime, flags) to path.
     """
     if not chmod_only:
         os.utime(path, (self.atime, self.mtime), follow_symlinks=False)
         os.chown(path, self.uid, self.gid, follow_symlinks=False)
         os.chflags(path, self.flags, follow_symlinks=False)
     os.chmod(path, self.mode, follow_symlinks=False)
    def run(self, edit):
        myFile = self.view.file_name()
        fileAtt = os.stat(myFile)[0]
        myPlatform = os.name

        print "Making " + myFile + " readonly"
        if (myPlatform == 'nt'):
            os.chmod(myFile, not stat.S_IWRITE)
        else:
            os.chflags(myFile, not stat.UF_IMMUTABLE)
Example #23
0
 def chflags(self, flags, files=None):
     if files is None:
         files = [self]
     elif isinstance(files, self.basecls):
         files = self.glob(files)
     else:
         files = [f for fs in files for f in self.glob(fs)]
     for file in files:
         file = base_class(file)
         _os.chflags(file, flags)
Example #24
0
def copystat(stats, dst):
    if isinstance(stats, str):
        stats_ = getStat(stats)
        stats = stats_
    if hasattr(os, "utime") and False:
        os.utime(dst, (stats.st_atime, stats.st_mtime))
    if hasattr(os, "chmod") and hasattr(stats, "st_mode"):
        chmod(dst, getMode(stats))
    if hasattr(os, "chflags") and hasattr(stats, "st_flags"):
        os.chflags(dst, stats.st_flags)
Example #25
0
def copystat(stats, dst):
    if isinstance(stats, str):
        stats_ = getStat(stats)
        stats = stats_
    if hasattr(os, "utime") and False:
        os.utime(dst, (stats.st_atime, stats.st_mtime))
    if hasattr(os, "chmod") and hasattr(stats, "st_mode"):
        chmod(dst, getMode(stats))
    if hasattr(os, "chflags") and hasattr(stats, "st_flags"):
        os.chflags(dst, stats.st_flags)
Example #26
0
def copystat(src, dst):
    """Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""
    st = os.stat(src)
    mode = stat.S_IMODE(st.st_mode)
    if hasattr(os, 'utime'):
        os.utime(dst, (st.st_atime, st.st_mtime))
    if hasattr(os, 'chmod'):
        os.chmod(dst, mode)
    if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
        os.chflags(dst, st.st_flags)
Example #27
0
def copystat(src, dst):
    """Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""
    st = os.stat(src)
    mode = stat.S_IMODE(st.st_mode)
    if hasattr(os, 'utime'):
        os.utime(dst, (st.st_atime, st.st_mtime))
    if hasattr(os, 'chmod'):
        os.chmod(dst, mode)
    if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
        os.chflags(dst, st.st_flags)
Example #28
0
def _copy_metadata(src, dst):
    """Copy the set of metadata we want for atomic_writing.
    
    Permission bits and flags. We'd like to copy file ownership as well, but we
    can't do that.
    """
    shutil.copymode(src, dst)
    st = os.stat(src)
    if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
        os.chflags(dst, st.st_flags)
Example #29
0
def _copy_metadata(src, dst):
    """Copy the set of metadata we want for atomic_writing.
    
    Permission bits and flags. We'd like to copy file ownership as well, but we
    can't do that.
    """
    shutil.copymode(src, dst)
    st = os.stat(src)
    if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
        os.chflags(dst, st.st_flags)
Example #30
0
    def run(self, edit):
        myFile = self.view.file_name()
        fileAtt = os.stat(myFile)[0]
        myPlatform = os.name

        print "Making " + myFile + " readonly"
        if (myPlatform == 'nt'):
            os.chmod(myFile, not stat.S_IWRITE)
        else:
            os.chflags(myFile, not stat.UF_IMMUTABLE)
Example #31
0
def changeReadonly(filename, state):
    if (os.name == 'nt'):
        flag = ~stat.S_IWRITE
        if not state:
            flag = ~flag;
        os.chmod(filename, flag)
    else:  
        flag = ~stat.UF_IMMUTABLE
        if not state:
            flag = flag;
        os.chflags(filename, flag)
Example #32
0
def lock_path(root, name, mode):
    """Lock down a path"""
    addflags = stat.UF_IMMUTABLE | stat.UF_NOUNLINK
    p = os.path.join(root, name)
    pst = os.stat(p)
    if pst.st_flags & stat.UF_IMMUTABLE:
        # Temporarily remove user immutable flag, so we can chmod.
        os.chflags(p, pst.st_flags ^ stat.UF_IMMUTABLE)
    logging.info(f'locking path “{p}”')
    os.chmod(p, mode)
    os.chflags(p, pst.st_flags | addflags)
def changeReadonly(filename, state):
    if (os.name == 'nt'):
        flag = ~stat.S_IWRITE
        if not state:
            flag = ~flag;
        os.chmod(filename, flag)
    else:  
        flag = ~stat.UF_IMMUTABLE
        if not state:
            flag = flag;
        os.chflags(filename, flag)
Example #34
0
def lock_path(root, name, mode):
    """Lock down a path"""
    addflags = stat.UF_IMMUTABLE | stat.UF_NOUNLINK
    p = os.path.join(root, name)
    pst = os.stat(p)
    if pst.st_flags & stat.UF_IMMUTABLE:
        # Temporarily remove user immutable flag, so we can chmod.
        os.chflags(p, pst.st_flags ^ stat.UF_IMMUTABLE)
    logging.info(f'locking path “{p}”')
    os.chmod(p, mode)
    os.chflags(p, pst.st_flags | addflags)
Example #35
0
 def _rmtree(self, path):
     if os.path.islink(path):
         os.unlink(path)
         return
     elif os.path.isdir(path):
         for item in os.listdir(path):
             self._rmtree(f"{path}/{item}")
         os.chflags(path, 2048)
         os.rmdir(path)
     else:
         os.chflags(path, 2048)
         os.remove(path)
Example #36
0
def test_file_flags(tmpdir):
    p = tmpdir.join('test.txt').ensure()
    os.chflags(p.strpath, 0b1000000000000000)

    file_info = FileInfo(path=p.strpath)
    assert file_info.process() == ActionResponse(changed=False, data={
        'exists': True,
        'file_type': 'file',
        'source': None,
        'mount': False,
        'flags': ['hidden']
    })
def main():
    sh('rm -rf ${DEBUG_WORLD}')
    sh('mkdir -p ${DEBUG_WORLD}')

    info('Saving debug information in ${{DEBUG_WORLD}}')

    for root, dirs, files in os.walk(e('${WORLD_DESTDIR}/')):
        for name in files:
            filename = os.path.join(root, name)
            relpath = os.path.relpath(filename, e('${WORLD_DESTDIR}'))
            destpath = os.path.join(e('${DEBUG_WORLD}'), relpath)

            ext = os.path.splitext(name)[1]

            if ext == '.ko':
                continue

            if relpath.startswith(('boot', 'usr/local/lib/grub')):
                continue

            if ext == '.c':
                make_dir(destpath)
                shutil.move(filename, destpath)
                for f in os.listdir(root):
                    if f.endswith('.html'):
                        html_path = os.path.join(root, f)
                        if 'Generated by Cython' in open(html_path).read():
                            relpath = os.path.relpath(html_path, e('${WORLD_DESTDIR}'))
                            destpath = os.path.join(e('${DEBUG_WORLD}'), relpath)
                            shutil.move(html_path, destpath)
                continue

            if ext == '.html':
                continue

            if not is_elf(filename):
                continue

            make_dir(destpath)

            # We need to remove any flags on protected files and restore
            # them after stripping
            flags = os.stat(filename).st_flags
            os.chflags(filename, 0)

            if not relpath.startswith('rescue'):
                sh('objcopy --only-keep-debug ${filename} ${destpath}.debug')
                sh('objcopy --strip-unneeded ${filename}')
                sh('objcopy --add-gnu-debuglink="${destpath}.debug" ${filename}', log='/dev/null', nofail=True)
            else:
                sh('strip ${filename}')

            os.chflags(filename, flags)
Example #38
0
def set_path_attr(path, attr, follow_symlinks):
    atime_ns, mtime_ns, mode, flags, xattr = attr
    os.utime(path, ns=(atime_ns, mtime_ns), follow_symlinks=follow_symlinks)
    try:
        os.chmod(path, mode, follow_symlinks=follow_symlinks)
    except NotImplementedError:
        pass
    except SystemError:
        pass
    if flags is not None:
        os.chflags(path, flags, follow_symlinks=follow_symlinks)
    for name, value in xattr:
        os.setxattr(path, name, value, follow_symlinks=follow_symlinks)
Example #39
0
def copystat(src, dst):
    st = os.stat(src)
    mode = stat.S_IMODE(st.st_mode)
    if hasattr(os, 'utime'):
        os.utime(dst, (st.st_atime, st.st_mtime))
    if hasattr(os, 'chmod'):
        os.chmod(dst, mode)
    if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
        try:
            os.chflags(dst, st.st_flags)
        except OSError as why:
            if not hasattr(errno, 'EOPNOTSUPP') or why.errno != errno.EOPNOTSUPP:
                raise
Example #40
0
def copyStat(src, dst, symlinks=True):
    """Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""
    if symlinks and os.path.islink(src):
        st = os.lstat(src)
    else:
        st = os.stat(src)
    mode = stat.S_IMODE(st.st_mode)
    if hasattr(os, 'utime'):
        os.utime(dst, (st.st_atime, st.st_mtime))
    if hasattr(os, 'chmod'):
        os.chmod(dst, mode)
    if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
        os.chflags(dst, st.st_flags)  #IGNORE:E1101
Example #41
0
def copy_metadata(path_src, path_dst, symlink=False):
    # print('copystat')
    # shutil.copystat(path_src, path_dst)
    try:
        tmp_flags = 0x0
        if symlink:
            file_stat = os.lstat(path_src)
            dbg_print('lstat: {}'.format(file_stat))
            dbg_print('lchown: {} : {} : {}'.format(path_dst, file_stat.st_uid, file_stat.st_gid))
            os.lchown(path_dst, file_stat.st_uid, file_stat.st_gid)
            dbg_print('lchmod: {}'.format(file_stat.st_mode))
            os.lchmod(path_dst, file_stat.st_mode)
        else:
            file_stat = os.stat(path_src)
            dbg_print('stat: {}'.format(file_stat))
            dbg_print('chown: {} : {} : {}'.format(path_dst, file_stat.st_uid, file_stat.st_gid))
            os.chown(path_dst, file_stat.st_uid, file_stat.st_gid)
            dbg_print('copymode')
            shutil.copymode(path_src, path_dst)

        # Unfortunately, os.utime() of Python 2 does not have the "follow_symlinks" option, so I have no idea to modify atime and mtime of a symlink itself.
        # https://stackoverflow.com/questions/48068739/how-can-i-change-atime-and-mtime-of-a-symbolic-link-from-python
        dbg_print('utime')
        os.utime(path_dst, (file_stat.st_atime, file_stat.st_mtime))

        if file_stat.st_flags & stat.SF_NOUNLINK:
            tmp_flags |= stat.SF_NOUNLINK
        if file_stat.st_flags & 0x80000:
            # 0x80000 means SF_RESTRICTED, but Python cannot recognize it.
            # https://github.com/pypa/virtualenv/issues/1173
            # https://bugs.python.org/issue32347
            tmp_flags |= 0x80000
        dbg_print('file_stat st_flags ^ tmp_flags: {} | {}'.format(hex(file_stat.st_flags), hex(tmp_flags)))
        if symlink:
            os.lchflags(path_dst, file_stat.st_flags ^ tmp_flags)
        else:
            os.chflags(path_dst, file_stat.st_flags ^ tmp_flags)

        extattr_src = xattr.xattr(path_src)
        extattr_src_items = dict(extattr_src.items())
        extattr_dst = xattr.xattr(path_dst)
        dbg_print('xattr src: {}'.format(extattr_src.items()))
        if 'com.apple.rootless' in extattr_src.keys():
            del extattr_src_items['com.apple.rootless']
        # dbg_print('xattr dst: {}'.format(extattr_dst.items()))
        dbg_print('xattr src: {}'.format(extattr_src_items))
        extattr_dst.update(extattr_src_items)
        return True
    except (IOError, OSError, shutil.Error) as err:
        # sys.exit('Error has been occurred in copy_metadata(): {}'.format(err))
        return False
Example #42
0
def set_write_bit(fn):
    # type: (str) -> None
    """
    Set read-write permissions for the current user on the target path.  Fail silently
    if the path doesn't exist.

    :param str fn: The target filename or path
    :return: None
    """

    fn = fs_encode(fn)
    if not os.path.exists(fn):
        return
    file_stat = os.stat(fn).st_mode
    os.chmod(fn, file_stat | stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
    if os.name == "nt":
        from ._winconsole import get_current_user

        user_sid = get_current_user()
        icacls_exe = _find_icacls_exe() or "icacls"
        from .misc import run

        if user_sid:
            c = run(
                [
                    icacls_exe,
                    "''{0}''".format(fn),
                    "/grant",
                    "{0}:WD".format(user_sid),
                    "/T",
                    "/C",
                    "/Q",
                ],
                nospin=True,
                return_object=True,
            )
            if not c.err and c.returncode == 0:
                return

    if not os.path.isdir(fn):
        for path in [fn, os.path.dirname(fn)]:
            try:
                os.chflags(path, 0)
            except AttributeError:
                pass
        return None
    for root, dirs, files in os.walk(fn, topdown=False):
        for dir_ in [os.path.join(root, d) for d in dirs]:
            set_write_bit(dir_)
        for file_ in [os.path.join(root, f) for f in files]:
            set_write_bit(file_)
Example #43
0
def copystat(src, dst):
    st = os.stat(src)
    mode = stat.S_IMODE(st.st_mode)
    if hasattr(os, 'utime'):
        os.utime(dst, (st.st_atime, st.st_mtime))
    if hasattr(os, 'chmod'):
        os.chmod(dst, mode)
    if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
        try:
            os.chflags(dst, st.st_flags)
        except OSError as why:
            if not hasattr(errno,
                           'EOPNOTSUPP') or why.errno != errno.EOPNOTSUPP:
                raise
Example #44
0
    def __init__(self, auditFile):
        self.file = auditFile
        if (not os.path.exists(auditFile)):
            # Create file if not exists.
            self.handle = open(auditFile, 'a+')
            try:
                os.chflags(auditFile, stat.UF_APPEND | stat.SF_APPEND)
            except:
                pass

            self.handle.close()
        # TODO: some exclusive lock for the file to stop other processes from reading it.
        self.handle = open(auditFile, 'r+b')
        self.mutex = Lock()
Example #45
0
def copystat(src, dst):
    """Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""
    st = os.stat(src)
    mode = stat.S_IMODE(st.st_mode)
    if hasattr(os, "utime"):
        os.utime(dst, (st.st_atime, st.st_mtime))
    if hasattr(os, "chmod"):
        os.chmod(dst, mode)
    if hasattr(os, "chflags") and hasattr(st, "st_flags"):
        try:
            os.chflags(dst, st.st_flags)
        except OSError as why:
            if not hasattr(errno, "EOPNOTSUPP") or why.errno != errno.EOPNOTSUPP:
                raise
Example #46
0
def copystat(src, dst):
    """Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""
    st = os.stat(src)
    mode = stat.S_IMODE(st.st_mode)
    if hasattr(os, 'utime'):
        os.utime(dst, (st.st_atime, st.st_mtime))
    if hasattr(os, 'chmod'):
        os.chmod(dst, mode)
    if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
        try:
            os.chflags(dst, st.st_flags)
        except OSError, why:
            if (not hasattr(errno, 'EOPNOTSUPP') or
                why.errno != errno.EOPNOTSUPP):
                raise
Example #47
0
def _copyStats(src, dst):
    st = os.stat(src)
    mode = stat.S_IMODE(st.st_mode)
    if hasattr(os, 'utime'):
        os.utime(dst, (st.st_atime, st.st_mtime))
    if hasattr(os, 'chmod'):
        os.chmod(dst, mode)
    if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
        try:
            os.chflags(dst, st.st_flags)
        except OSError, why:
            for err in 'EOPNOTSUPP', 'ENOTSUP':
                if hasattr(errno, err) and why.errno == getattr(errno, err):
                    break
            else:
                raise
Example #48
0
 def copystat(cls, src, dest, copy_own=True, copy_xattr=True):
     """
     Copy all stat info (mode bits, atime, mtime, flags) from `src` to
     `dest`.  If `copy_own=True`, the uid and gid are also copied.
     If `copy_xattr=True`, the extended attributes are also copied
     (only available on Linux).
     """
     st = os.stat(src)
     mode = stat.S_IMODE(st.st_mode)
     os.chmod(dest, mode=mode)
     os.utime(dest, ns=(st.st_atime_ns, st.st_mtime_ns))
     if hasattr(st, "st_flags"):
         os.chflags(dest, flags=st.st_flags)
     if copy_own:
         os.chown(dest, uid=st.st_uid, gid=st.st_gid)
     if copy_xattr:
         cls.copyxattr(src, dest)
Example #49
0
def copystat(src, dst):
    """Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""
    st = os.stat(src)
    mode = stat.S_IMODE(st.st_mode)
    if hasattr(os, "utime"):
        os.utime(dst, (st.st_atime, st.st_mtime))
    if hasattr(os, "chmod"):
        os.chmod(dst, mode)
    if hasattr(os, "chflags") and hasattr(st, "st_flags"):
        try:
            os.chflags(dst, st.st_flags)
        except OSError, why:
            for err in "EOPNOTSUPP", "ENOTSUP":
                if hasattr(errno, err) and why.errno == getattr(errno, err):
                    break
            else:
                raise
Example #50
0
def copystat(src, dst):
    """Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""
    st = os.stat(src)
    mode = stat.S_IMODE(st.st_mode)
    if hasattr(os, 'utime'):
        os.utime(dst, (st.st_atime, st.st_mtime))
    if hasattr(os, 'chmod'):
        os.chmod(dst, mode)
    if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
        try:
            os.chflags(dst, st.st_flags)
        except OSError as why:
            for err in ('EOPNOTSUPP', 'ENOTSUP'):
                if hasattr(errno, err) and why.errno == getattr(errno, err):
                    break
            else:
                raise
	def start(self):
		self.start_time = time.time()
		
		if not os.path.exists(self.tmp_mask_dir): os.makedirs(self.tmp_mask_dir)
		
		self.get_file_names()
		self.get_start_point()

		#copy the video file locally if requiered. See getFileNames
		if self.final_has_finished == 0 and self.tmp_video_file != self.video_file:
			print_flush(self.base_name + ' Copying video file to local temporary directory.')
			shutil.copy(self.video_file, self.tmp_video_file)
			if self.is_single_worm:
				try:
					dd = self.video_file.rpartition('.')[0]
					shutil.copy(dd + '.log.csv', self.tmp_mask_dir)
					shutil.copy(dd + '.info.xml', self.tmp_mask_dir)
				except FileNotFoundError:
					try:
						dd = self.video_file.rpartition('.')[0]
						dd, base_name = os.path.split(dd)
						dd = os.path.join(dd, '.data', base_name)
						shutil.copy(dd + '.log.csv', self.tmp_mask_dir)
						shutil.copy(dd + '.info.xml', self.tmp_mask_dir)
					except FileNotFoundError:
						pass

			
		#this might be more logically group in main_code, but this operation can and should be do remotely if required
		if self.final_has_finished  == 1:
			#The file finished to be processed but the additional data was not stored. We can do this remotely. 
			os.chflags(self.masked_image_file, not stat.UF_IMMUTABLE)
			compressSingleWorker(self.video_file, self.mask_dir, self.json_file, self.is_single_worm, self.cmd_original)
			os.chflags(self.masked_image_file, stat.UF_IMMUTABLE)

		if self.final_has_finished  == 2:
			print_flush('File alread exists: %s. If you want to calculate the mask again delete the existing file.' % self.masked_image_file)
	
		#parameters to calculate the mask, this will return a list with necessary commands in case I want to use it as a subprocess
		if self.is_calculate_mask:
			self.main_input_params = [self.tmp_video_file, self.tmp_mask_dir, self.json_file, self.is_single_worm, self.cmd_original]
		else:
			self.main_input_params = []
		
		return self.create_script()
Example #52
0
 def copystat(src, dst):
     """Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""
     st = os.stat(src)  # pylint: disable=invalid-name
     mode = stat.S_IMODE(st.st_mode)
     if hasattr(os, 'utime'):
         try:
             os.utime(dst, (st.st_atime, st.st_mtime))
         except OSError:
             pass
     if hasattr(os, 'chmod'):
         try:
             os.chmod(dst, mode)
         except OSError:
             pass
     if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
         try:
             os.chflags(dst, st.st_flags)  # pylint: disable=no-member
         except OSError:
             pass
Example #53
0
def process_mediadir(basedir):
  for f in os.listdir(basedir):
    fullpath = os.path.join(basedir, f)
    s = os.stat(fullpath)
    if stat.S_ISREG(s.st_mode):
      if fullpath.lower().endswith('jpg'):
        move_image(f, fullpath)
      elif fullpath.lower().endswith('mov') or fullpath.lower().endswith('mp4'):
        if fullpath.lower().endswith('_thm.mp4'):
          # delete thumbnail vid files
          print('deleting: {}'.format(fullpath))
          os.chflags(fullpath, 0)
          os.remove(fullpath)
        else:
          move_movie(f, fullpath)
      else:
        print("did not recognize file extension: {}".format(fullfile))
    else:
      print("not a file: {}".format(fullpath))
Example #54
0
def clean_ufs_image():
    sh('${BUILD_ROOT}/build/customize/remove-bits.py ${INSTUFS_DESTDIR}')

    # Strip binaries
    for root, dirs, files in os.walk(e('${INSTUFS_DESTDIR}/')):
        for name in files:
            filename = os.path.join(root, name)
            if os.path.splitext(name)[1] == '.ko':
                continue

            if not is_elf(filename):
                continue

            # We need to remove any flags on protected files and restore
            # them after stripping
            flags = os.stat(filename).st_flags
            os.chflags(filename, 0)
            sh('strip ${filename}')
            os.chflags(filename, flags)
def CopyStat(src, dst):
    """
    ***Modified from shutil to ignore the windows access denied error***
    Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""
    st = os.stat(src)
    mode = stat.S_IMODE(st.st_mode)
    if hasattr(os, 'utime'):
        ###IGNORES THE WINDOWS ACCESS DENIED ERROR. EVERYTHING ELSE REMAINS SAME###
        try:
            os.utime(dst, (st.st_atime, st.st_mtime))
        except WindowsError:
            pass
    if hasattr(os, 'chmod'):
        os.chmod(dst, mode)
    if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
        try:
            os.chflags(dst, st.st_flags)
        except OSError, why:
            if (not hasattr(errno, 'EOPNOTSUPP') or
                why.errno != errno.EOPNOTSUPP):
                raise
	def clean(self):
		if self.final_has_finished == 0:
			print_flush(self.base_name + ' Moving files to final destination and removing temporary files.')
			if os.path.abspath(self.tmp_video_file) != os.path.abspath(self.video_file):
				#print_flush(self.base_name + ' Removing video file from local temporary directory.')
				assert os.path.abspath(self.tmp_video_file) != os.path.abspath(self.video_file)
				os.remove(self.tmp_video_file)
				assert os.path.exists(self.video_file)

				if self.is_single_worm:
					dd = self.tmp_video_file.rpartition('.')[0]
					os.remove(dd + '.log.csv')
					os.remove(dd + '.info.xml')


			#assert the local file did finished
			with h5py.File(self.tmp_mask_file, "r") as mask_fid:
				assert mask_fid['/mask'].attrs['has_finished'] > 0

			if os.path.abspath(self.tmp_mask_file) != os.path.abspath(self.masked_image_file):
				#it is very important to use os.path.abspath() otherwise there could be some 
				#confunsion in the same file name
				#print_flush(self.base_name + " Copying temporal masked file into the final directory.")
				shutil.copy(self.tmp_mask_file, self.masked_image_file)
			
				#print_flush(self.base_name + " Removing temporary files.")
				assert os.path.exists(self.masked_image_file)
				os.remove(self.tmp_mask_file)

			#Change the permissions so everybody can read/write. 
			#Otherwise only the owner would be able to change the ummutable flag.
			os.chmod(self.masked_image_file, stat.S_IRUSR|stat.S_IRGRP|stat.S_IROTH|stat.S_IWUSR|stat.S_IWGRP|stat.S_IWOTH) 
			
			#Protect file from deletion.
			os.chflags(self.masked_image_file, stat.UF_IMMUTABLE)
			#print_flush(self.base_name + " Finished to create masked file")


		time_str = str(datetime.timedelta(seconds=round(time.time()-self.start_time)))
		print_flush('%s Finished. Total time = %s' % (self.base_name, time_str))
Example #57
0
def main():
    sh('rm -rf ${DEBUG_WORLD}')
    sh('mkdir -p ${DEBUG_WORLD}')

    info('Saving debug information in ${{DEBUG_WORLD}}')

    for root, dirs, files in os.walk(e('${WORLD_DESTDIR}/')):
        for name in files:
            filename = os.path.join(root, name)
            relpath = os.path.relpath(filename, e('${WORLD_DESTDIR}'))
            destpath = os.path.join(e('${DEBUG_WORLD}'), relpath)

            if os.path.splitext(name)[1] == '.ko':
                continue

            if relpath.startswith(('boot', 'usr/local/lib/grub')):
                continue

            if not is_elf(filename):
                continue

            try:
                os.makedirs(os.path.dirname(destpath))
            except OSError as err:
                if err.errno != errno.EEXIST:
                    raise

            # We need to remove any flags on protected files and restore
            # them after stripping
            flags = os.stat(filename).st_flags
            os.chflags(filename, 0)

            if not relpath.startswith('rescue'):
                sh('objcopy --only-keep-debug ${filename} ${destpath}.debug')
                sh('objcopy --strip-unneeded ${filename}')
                sh('objcopy --add-gnu-debuglink="${destpath}.debug" ${filename}', log='/dev/null', nofail=True)
            else:
                sh('strip ${filename}')

            os.chflags(filename, flags)
Example #58
0
def test_is_hidden(temp_dir):
    from pydgeot.filesystem import is_hidden

    with open(os.path.join(temp_dir, 'visible'), 'w') as fh:
        assert not is_hidden(fh.name)

    if sys.platform != 'win32':
        with open(os.path.join(temp_dir, '.dotfile'), 'w') as fh:
            assert is_hidden(fh.name)

    if sys.platform == 'win32':
        import ctypes

        with open(os.path.join(temp_dir, 'hidden'), 'w') as fh:
            ctypes.windll.kernel32.SetFileAttributesW(fh.name, 2)
            assert is_hidden(fh.name)
    elif sys.platform == 'darwin':
        import stat

        with open(os.path.join(temp_dir, 'hidden'), 'w') as fh:
            os.chflags(fh.name, os.stat(fh.name).st_flags ^ stat.UF_HIDDEN)
            assert is_hidden(fh.name)
Example #59
0
    def copystat(src, dst):
        """Update attribute of dst file with src attributes.

        :param src: the source FileInfo object
        :type src: FileInfo
        :param dst: the target FileInfo object
        :type dst: FileInfo
        """
        if islink(src):
            mode = stat.S_IMODE(src.stat.st_mode)
            if hasattr(os, 'lchmod'):
                os.lchmod(dst.path, mode)

            if hasattr(os, 'lchflags') and hasattr(src.stat, 'st_flags'):
                try:
                    os.lchflags(dst.path, src.stat.st_flags)
                except OSError as why:
                    import errno
                    if (not hasattr(errno, 'EOPNOTSUPP') or
                            why.errno != errno.EOPNOTSUPP):
                        raise
        else:
            mode = stat.S_IMODE(src.stat.st_mode)
            if hasattr(os, 'utime'):
                if preserve_timestamps:
                    os.utime(dst.path, (src.stat.st_atime, src.stat.st_mtime))
                else:
                    os.utime(dst.path, None)
            if hasattr(os, 'chmod'):
                os.chmod(dst.path, mode)
            if hasattr(os, 'chflags') and hasattr(src.stat, 'st_flags'):
                try:
                    os.chflags(dst.path, src.stat.st_flags)
                except OSError as why:
                    import errno
                    if (not hasattr(errno, 'EOPNOTSUPP') or
                            why.errno != errno.EOPNOTSUPP):
                        raise
Example #60
0
def copystat(src, dst):
    """Copy file metadata

    Copy the permission bits, last access time, last modification time, and
    flags from `src` to `dst`. On Linux, copystat() also copies the "extended
    attributes" where possible. The file contents, owner, and group are
    unaffected. `src` and `dst` are path names given as strings.
    """
    st = os.stat(src)
    mode = stat.S_IMODE(st.st_mode)
    if hasattr(os, 'utime'):
        os.utime(dst, (st.st_atime, st.st_mtime))
    if hasattr(os, 'chmod'):
        os.chmod(dst, mode)
    if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
        try:
            os.chflags(dst, st.st_flags)
        except OSError, why:
            for err in 'EOPNOTSUPP', 'ENOTSUP':
                if hasattr(errno, err) and why.errno == getattr(errno, err):
                    break
            else:
                raise