Exemplo n.º 1
0
func (c cpCopier) FilteredCopyToTemp(dir string, filters []string) (tempDir string, err error) {
	tempDir, err = c.fs.TempDir("bosh-platform-disk-TarballCompressor-CompressFilesInDir")
	if err != nil {
		err = bosherr.WrapError(err, "Creating temporary directory")
		return
	}

	dirGlob := glob.NewDir(dir)
	filesToCopy, err := dirGlob.Glob(filters...)
	if err != nil {
		err = bosherr.WrapError(err, "Finding files matching filters")
		c.fs.RemoveAll(tempDir)
		tempDir = ""
		return
	}

	for _, relativePath := range filesToCopy {
		src := filepath.Join(dir, relativePath)

		dst := filepath.Join(tempDir, relativePath)

		err = c.fs.MkdirAll(filepath.Dir(dst), os.ModePerm)
		if err != nil {
			err = bosherr.WrapError(err, "Making destination directory for %s", relativePath)
			c.fs.RemoveAll(tempDir)
			tempDir = ""
			return
		}

		// Golang does not have a way of copying files and preserving file info...
		_, _, err = c.cmdRunner.RunCommand("cp", "-Rp", src, dst)
		if err != nil {
			err = bosherr.WrapError(err, "Shelling out to cp")
			c.fs.RemoveAll(tempDir)
			tempDir = ""
			return
		}
	}

	err = c.fs.Chmod(tempDir, os.FileMode(0755))
	if err != nil {
		err = bosherr.WrapError(err, "Fixing permissions on temp dir")
		return
	}

	return
}
Exemplo n.º 2
0
func (c cpCopier) FilteredCopyToTemp(dir string, filters []string) (string, error) {
	filters = c.convertDirectoriesToGlobs(dir, filters)

	dirGlob := glob.NewDir(dir)
	filesToCopy, err := dirGlob.Glob(filters...)
	if err != nil {
		return "", bosherr.WrapError(err, "Finding files matching filters")
	}

	return c.tryInTempDir(func(tempDir string) error {
		for _, relativePath := range filesToCopy {
			src := filepath.Join(dir, relativePath)
			dst := filepath.Join(tempDir, relativePath)

			fileInfo, err := os.Stat(src)
			if err != nil {
				return bosherr.WrapErrorf(err, "Getting file info for '%s'", src)
			}

			if !fileInfo.IsDir() {
				err = c.cp(src, dst, tempDir)
			}

			if err != nil {
				return err
			}
		}

		err = c.fs.Chmod(tempDir, os.FileMode(0755))
		if err != nil {
			bosherr.WrapError(err, "Fixing permissions on temp dir")
		}

		return nil
	})
}