Example #1
0
    def __restore_attributes(self, tar_info, path):
        """Restores all attributes of a restored file."""

        if os.geteuid() == 0:
            try:
                try:
                    uid = utils.getpwnam(tar_info.uname)[2]
                except KeyError:
                    uid = tar_info.uid

                try:
                    gid = utils.getgrnam(tar_info.gname)[2]
                except KeyError:
                    gid = tar_info.gid

                os.lchown(path, uid, gid)
            except Exception as e:
                if not psys.is_errno(e, errno.ENOENT):
                    LOG.error("Failed to set owner of '%s': %s.", path, psys.e(e))
                    self.__ok = False

        if not tar_info.issym():
            try:
                os.chmod(path, tar_info.mode)
            except Exception as e:
                if not psys.is_errno(e, errno.ENOENT):
                    LOG.error("Failed to change permissions of '%s': %s.", path, psys.e(e))
                    self.__ok = False

            try:
                os.utime(path, ( tar_info.mtime, tar_info.mtime ))
            except Exception as e:
                if not psys.is_errno(e, errno.ENOENT):
                    LOG.error("Failed to change access and modification time of '%s': %s.", path, psys.e(e))
                    self.__ok = False
Example #2
0
    def __backup_path(self, path, filters, toplevel):
        """Backups the specified path."""

        ok = True
        LOG.info("Backing up '%s'...", path)

        try:
            stat_info = os.lstat(path)

            if stat.S_ISREG(stat_info.st_mode):
                self.__backup_file(path)
            else:
                if stat.S_ISLNK(stat_info.st_mode):
                    try:
                        link_target = os.readlink(path)
                    except EnvironmentError as e:
                        if e.errno == errno.EINVAL:
                            raise FileTypeChangedError()
                        else:
                            raise
                else:
                    link_target = None

                self.__backup.add_file(
                    path, stat_info, link_target = link_target)

            if stat.S_ISDIR(stat_info.st_mode):
                prefix = toplevel + os.path.sep

                for filename in os.listdir(path):
                    file_path = os.path.join(path, filename)

                    for allow, regex in filters:
                        if not file_path.startswith(prefix):
                            raise LogicalError()

                        if regex.search(file_path[len(prefix):]):
                            if allow:
                                self.__backup_path(file_path, filters, toplevel)
                            else:
                                LOG.info("Filtering out '%s'...", file_path)

                            break
                    else:
                        self.__backup_path(file_path, filters, toplevel)
        except FileTypeChangedError as e:
            LOG.error("Failed to backup '%s': it has suddenly changed its type during the backup.", path)
            ok = False
        except Exception as e:
            if psys.is_errno(e, (errno.ENOENT, errno.ENOTDIR)) and path != toplevel:
                LOG.warning("Failed to backup '%s': it has suddenly vanished.", path)
            else:
                LOG.error("Failed to backup '%s': %s.", path, psys.e(e))
                ok = False

        return ok