Пример #1
0
    def _CopySourceFiles(self, mount_point):
        """Copies all source files/directories to a mounted raw disk.

    There are several cases which must be handled separately:
      1. src=dir1 and dest is empty. In this case we simply copy the content of
        dir1 to mount_point.
      2. src=dir1 and dest=dir2. In this case dir1 is copied to mount_point
        under a new name dir2, so its content would be copied under
        mount_point/dir2.
      3. src=file1/dir1 and dest=file2/dir2 and is_recursive=False. file1/dir1
        is copied to mount_point/file2 or mount_point/dir2.

    Args:
      mount_point: A path to a mounted raw disk.
    """
        for (src, dest, is_recursive) in self._srcs:
            # Generate a list of files/directories excluded from copying to raw disk.
            # rsync expects them to be relative to src directory so we need to
            # regenerate this list for every src separately.
            with tempfile.NamedTemporaryFile(
                    dir=self._scratch_dir) as rsync_file:
                for spec in self._excludes:
                    rsync_file.write(spec.GetRsyncSpec(src))

                # make sure that rsync utility sees all the content of rsync_file which
                # otherwise can be buffered.
                rsync_file.flush()
                if is_recursive:
                    # if a directory ends with / rsync copies the content of a
                    # directory, otherwise it also copies the directory itself.
                    src = src.rstrip('/')
                    if not dest:
                        src += '/'
                    utils.Rsync(src,
                                mount_point,
                                rsync_file.name,
                                self._ignore_hard_links,
                                recursive=True,
                                xattrs=True)
                    if dest:
                        os.rename(
                            os.path.join(mount_point, os.path.basename(src)),
                            os.path.join(mount_point, dest))
                else:
                    utils.Rsync(src,
                                os.path.join(mount_point, dest),
                                rsync_file.name,
                                self._ignore_hard_links,
                                recursive=False,
                                xattrs=True)
Пример #2
0
    def _ProcessOverwriteList(self, mount_point):
        """Overwrites a set of files/directories requested by platform.

    Args:
      mount_point: A path to a mounted raw disk.
    """
        for file_name in self._overwrite_list:
            file_path = os.path.join(mount_point, file_name)
            if os.path.exists(file_path):
                if os.path.isdir(file_path):
                    # TODO(user): platform.Overwrite is expected to overwrite the
                    # directory in place from what I can tell. In case of a file it will
                    # create a new file which must be copied to mounted raw disk. So there
                    # some inconsistency which would need to be addresses if and when we
                    # encounter a platform which would want to overwrite a directory.
                    self._platform.Overwrite(file_path, file_name,
                                             self._scratch_dir)
                    logging.info('rawdisk: modifying directory %s', file_path)
                else:
                    new_file = self._platform.Overwrite(
                        file_path, file_name, self._scratch_dir)
                    logging.info('rawdisk: modifying %s from %s', file_path,
                                 new_file)
                    utils.Rsync(new_file,
                                file_path,
                                None,
                                self._ignore_hard_links,
                                recursive=False,
                                xattrs=True)
Пример #3
0
  def _CopyPlatformSpecialFiles(self, mount_point):
    """Copies platform special files to a mounted raw disk.

    Args:
      mount_point: A path to a mounted raw disk.
    """
    if self._platform:
      special_files = self._platform.GetPlatformSpecialFiles(self._scratch_dir)
      for (src, dest) in special_files:
        # Ensure we don't use extended attributes here, so that copying /selinux
        # on Linux doesn't try and fail to preserve the SELinux context. That
        # doesn't work and causes rsync to return a nonzero status code.
        utils.Rsync(src, os.path.join(mount_point, dest), None,
                    self._ignore_hard_links, recursive=False, xattrs=False)