示例#1
0
文件: scanner.go 项目: glyn/bloblets
func doScan(appDir, path string) (*directory, error) {
	fullPath := filepath.Join(appDir, path)
	fi, err := os.Lstat(fullPath)
	if err != nil {
		return nil, fmt.Errorf("Scan of %s for path %s failed: %s", appDir, path, err)
	}

	if !fi.IsDir() {
		return nil, fmt.Errorf("Not a directory: %s", path)
	}

	dir := directory{
		path:     path,
		hash:     filehash.Zero(),
		size:     0,
		files:    make(map[string]*models.AppFileFields, 50),
		children: make(map[string]*directory, 10),
	}

	f, err := os.Open(fullPath)
	if err != nil {
		return nil, fmt.Errorf("Scan of %s failed on Open: %s", fullPath, err)
	}
	fis, err := f.Readdir(-1)
	if err != nil {
		return nil, fmt.Errorf("Scan of %s failed on Readdir: %s", fullPath, err)
	}

	for _, fi := range fis {
		fp := filepath.Join(fullPath, fi.Name())
		fpRelative := filepath.Join(path, fi.Name())
		if !fi.IsDir() {
			h := filehash.New(fp)
			dir.files[fp] = &models.AppFileFields{
				Sha1: h.String(),
				Size: fi.Size(),
				Path: fpRelative,
				Mode: fmt.Sprintf("%#o", fi.Mode()),
			}
			dir.size += fi.Size()
			dir.hash.Combine(h)
		} else {
			s, err := doScan(appDir, fpRelative)
			if err != nil {
				return nil, fmt.Errorf("Scan of %s failed to scan subdirectory %s: %s", fullPath, fp, err)
			}
			dir.children[fp] = s
		}
	}

	return &dir, nil
}
示例#2
0
		Expect(err).NotTo(HaveOccurred())
		Expect(dir.path).To(Equal("."))
		Expect(dir.hash).To(Equal(filehash.Zero()))
		Expect(dir.size).To(Equal(int64(0)))
		Expect(dir.files).To(BeEmpty())
		Expect(dir.children).To(BeEmpty())
		Expect(dir.bloblet).To(BeNil())
	})

	It("should correctly scan a directory with no children", func() {
		dir, err := Scan("./test/nochildren")
		Expect(err).NotTo(HaveOccurred())
		Expect(dir.path).To(Equal("."))
		Expect(dir.hash.String()).To(Equal("6fbf71631414af6ab45c4508bc4bc030ae12f891"))
		Expect(dir.size).To(Equal(int64(3218)))
		Expect(hashes(dir.files)).To(Equal(map[string]string{"test/nochildren/file1": filehash.New("./test/nochildren/file1").String(),
			"test/nochildren/file2": filehash.New("./test/nochildren/file2").String()}))
		Expect(dir.children).To(BeEmpty())
		Expect(dir.bloblet).To(BeNil())
	})

	It("should correctly scan a directory with children", func() {
		dir, err := Scan("./test/withchildren")
		Expect(err).NotTo(HaveOccurred())
		Expect(dir.path).To(Equal("."))
		Expect(dir.hash.String()).To(Equal("6fbf71631414af6ab45c4508bc4bc030ae12f891"))
		Expect(dir.size).To(Equal(int64(3218)))
		Expect(hashes(dir.files)).To(Equal(map[string]string{"test/withchildren/file1": filehash.New("./test/withchildren/file1").String(),
			"test/withchildren/file2": filehash.New("./test/withchildren/file2").String()}))
		Expect(len(dir.children)).To(Equal(1))
		_, ok := dir.children["test/withchildren/child1"]