This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from stripe/manifests
Add the ability to save and read manifests
- Loading branch information
Showing
7 changed files
with
216 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
sequins | ||
testdata | ||
.index | ||
.manifest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package index | ||
|
||
import ( | ||
"encoding/json" | ||
"hash/crc32" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
type manifest struct { | ||
Files []manifestEntry `json:"files"` | ||
Count int `json:"count"` | ||
} | ||
|
||
type manifestEntry struct { | ||
Name string `json:"name"` | ||
CRC uint32 `json:"crc"` | ||
} | ||
|
||
func readManifest(path string) (manifest, error) { | ||
m := manifest{} | ||
|
||
reader, err := os.Open(path) | ||
if err != nil { | ||
return m, err | ||
} | ||
|
||
defer reader.Close() | ||
bytes, err := ioutil.ReadAll(reader) | ||
if err != nil { | ||
return m, err | ||
} | ||
|
||
err = json.Unmarshal(bytes, &m) | ||
return m, err | ||
} | ||
|
||
func writeManifest(path string, m manifest) error { | ||
bytes, err := json.Marshal(m) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
writer, err := os.Create(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer writer.Close() | ||
_, 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters