Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Commit

Permalink
Use crc instead of filesize to validate existing files
Browse files Browse the repository at this point in the history
  • Loading branch information
colinmarc committed Mar 12, 2015
1 parent 23918ce commit 813bcf8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
31 changes: 19 additions & 12 deletions index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,20 @@ func (index *Index) buildFileList() error {
}

func (index *Index) loadIndexFromManifest(m manifest) error {
for i, manifestFile := range m.Files {
for i, entry := range m.Files {
indexFile := index.files[i]
baseName := filepath.Base(indexFile.file.Name())
if baseName != filepath.Base(manifestFile.Name) {
return fmt.Errorf("unmatched file: %s", manifestFile.Name)
if baseName != filepath.Base(entry.Name) {
return fmt.Errorf("unmatched file: %s", entry.Name)
}

info, err := indexFile.file.Stat()
crc, err := fileCrc(indexFile.file.Name())
if err != nil {
return err
}

if info.Size() != manifestFile.Size {
return fmt.Errorf("local file %s has a different size (%d) than the manifest says it should (%d)",
baseName, info.Size(), manifestFile.Size)
if crc != entry.CRC {
return fmt.Errorf("local file %s has an invalid CRC, according to the manifest", baseName)
}
}

Expand Down Expand Up @@ -157,7 +156,11 @@ func (index *Index) buildNewIndex() error {
log.Println("Finished indexing", path)
}

manifest := index.buildManifest()
manifest, err := index.buildManifest()
if err != nil {
return fmt.Errorf("error building manifest: %s", err)
}

manifestPath := filepath.Join(index.Path, ".manifest")
log.Println("Writing manifest file to", manifestPath)
err = writeManifest(manifestPath, manifest)
Expand All @@ -168,21 +171,25 @@ func (index *Index) buildNewIndex() error {
return nil
}

func (index *Index) buildManifest() manifest {
func (index *Index) buildManifest() (manifest, error) {
m := manifest{
Files: make([]manifestEntry, len(index.files)),
Count: index.count,
}

for i, f := range index.files {
info, _ := f.file.Stat()
crc, err := fileCrc(f.file.Name())
if err != nil {
return m, err
}

m.Files[i] = manifestEntry{
Name: filepath.Base(f.file.Name()),
Size: info.Size(),
CRC: crc,
}
}

return m
return m, nil
}

func (index *Index) addFile(subPath string) error {
Expand Down
19 changes: 18 additions & 1 deletion index/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package index

import (
"encoding/json"
"hash/crc32"
"io"
"io/ioutil"
"os"
)
Expand All @@ -13,7 +15,7 @@ type manifest struct {

type manifestEntry struct {
Name string `json:"name"`
Size int64 `json:"size"`
CRC uint32 `json:"crc"`
}

func readManifest(path string) (manifest, error) {
Expand Down Expand Up @@ -49,3 +51,18 @@ func writeManifest(path string, m manifest) error {
_, err = writer.Write(bytes)
return err
}

func fileCrc(path string) (uint32, error) {
file, err := os.Open(path)
if err != nil {
return 0, err
}

hash := crc32.NewIEEE()
_, err = io.Copy(hash, file)
if err != nil {
return 0, err
}

return hash.Sum32(), nil
}

0 comments on commit 813bcf8

Please sign in to comment.