Beispiel #1
0
func AppFilesInDir(dir string) (appFiles []models.AppFileFields, err error) {
	dir, err = filepath.Abs(dir)
	if err != nil {
		return
	}

	err = WalkAppFiles(dir, func(fileName string, fullPath string) (err error) {
		fileInfo, err := os.Lstat(fullPath)
		if err != nil {
			return
		}

		appFile := models.AppFileFields{
			Path: filepath.ToSlash(fileName),
			Size: fileInfo.Size(),
		}

		if fileInfo.IsDir() {
			appFile.Sha1 = "0"
			appFile.Size = 0
		} else {
			hash := sha1.New()
			err = fileutils.CopyPathToWriter(fullPath, hash)
			if err != nil {
				return
			}
			appFile.Sha1 = fmt.Sprintf("%x", hash.Sum(nil))
		}

		appFiles = append(appFiles, appFile)
		return
	})
	return
}
Beispiel #2
0
func copyPathToPath(fromPath, toPath string) (err error) {
	srcFileInfo, err := os.Stat(fromPath)
	if err != nil {
		return
	}

	if srcFileInfo.IsDir() {
		err = os.MkdirAll(toPath, srcFileInfo.Mode())
		if err != nil {
			return
		}
	} else {
		var dst *os.File
		dst, err = fileutils.Create(toPath)
		if err != nil {
			return
		}
		defer dst.Close()

		dst.Chmod(srcFileInfo.Mode())

		err = fileutils.CopyPathToWriter(fromPath, dst)
	}
	return err
}
Beispiel #3
0
func sha(path string) string {
	hash := sha1.New()
	err := fileutils.CopyPathToWriter(path, hash)
	if err != nil {
		panic("Failed to compute sha1")
	}
	return fmt.Sprintf("%x", hash.Sum(nil))
}
Beispiel #4
0
func (b *bloblet) Write(w io.Writer) {
	fileutils.TempDir("bloblet-zipdir", func(zipDir string, err error) {
		cliutil.Check(err)
		zipPath := filepath.Join(zipDir, BlobletFileName)
		cliutil.Check(b.Compress(zipPath))
		cliutil.Check(fileutils.CopyPathToWriter(zipPath, w))
	})
}
Beispiel #5
0
func (zipper ApplicationZipper) Zip(dirOrZipFile string, targetFile *os.File) (err error) {
	if zipper.IsZipFile(dirOrZipFile) {
		err = fileutils.CopyPathToWriter(dirOrZipFile, targetFile)
	} else {
		err = writeZipFile(dirOrZipFile, targetFile)
	}
	targetFile.Seek(0, os.SEEK_SET)
	return
}
Beispiel #6
0
// New returns a Hash of the contents of the specified file.
func New(filePath string) Hash {
	fileInfo, err := os.Lstat(filePath)
	if err != nil {
		panic(err)
	}

	if fileInfo.IsDir() {
		panic("cannot compute hash of directory")
	} else {
		hash := sha1.New()
		err = fileutils.CopyPathToWriter(filePath, hash)
		if err != nil {
			panic(err)
		}
		return Hash(hash.Sum(nil))
	}
}
func writeZipFile(dir string, targetFile *os.File) error {
	isEmpty, err := fileutils.IsDirEmpty(dir)
	if err != nil {
		return err
	}

	if isEmpty {
		return errors.NewEmptyDirError(dir)
	}

	writer := zip.NewWriter(targetFile)
	defer writer.Close()

	appfiles := ApplicationFiles{}
	return appfiles.WalkAppFiles(dir, func(fileName string, fullPath string) error {
		fileInfo, err := os.Stat(fullPath)
		if err != nil {
			return err
		}

		header, err := zip.FileInfoHeader(fileInfo)
		if err != nil {
			return err
		}

		header.Name = filepath.ToSlash(fileName)

		if fileInfo.IsDir() {
			header.Name += "/"
		}

		zipFilePart, err := writer.CreateHeader(header)
		if err != nil {
			return err
		}

		if fileInfo.IsDir() {
			return nil
		}

		return fileutils.CopyPathToWriter(fullPath, zipFilePart)
	})
}
Beispiel #8
0
func (b *bloblet) Compress(path string) error {
	f, err := os.OpenFile(path, os.O_CREATE+os.O_RDWR, 0644)
	if err != nil {
		return err
	}
	defer f.Close()
	zw := zip.NewWriter(f)
	defer zw.Close()

	for filePath, _ := range b.files {
		w, err := zw.Create(filePath)
		if err != nil {
			return err
		}
		err = fileutils.CopyPathToWriter(filePath, w)
		if err != nil {
			return err
		}
	}
	return nil
}
Beispiel #9
0
				Expect(fileInfo.Mode()).To(Equal(expectedFileInfo.Mode()))
			})
		})
	})

	Describe("CopyPathToWriter", func() {
		var destPath string

		BeforeEach(func() {
			destFile, err := ioutil.TempFile("", "copy_test")
			Expect(err).NotTo(HaveOccurred())
			defer destFile.Close()

			destPath = destFile.Name()

			err = fileutils.CopyPathToWriter(fixturePath, destFile)
			Expect(err).NotTo(HaveOccurred())
		})

		It("copies the file contents", func() {
			fileBytes, err := ioutil.ReadFile(destPath)
			Expect(err).NotTo(HaveOccurred())

			Expect(fileBytes).To(Equal(fixtureBytes))
		})
	})

	Describe("CopyReaderToPath", func() {
		var destPath = fileutils.TempPath("copy_test")

		BeforeEach(func() {