diff options
Diffstat (limited to 'chunk.go')
-rw-r--r-- | chunk.go | 25 |
1 files changed, 25 insertions, 0 deletions
@@ -2,6 +2,7 @@ package main import ( "bytes" + "errors" "fmt" "io" "log" @@ -24,6 +25,11 @@ type BufferedChunk interface { Bytes() []byte } +type StorerChunk interface { + Chunk + Store(path string) error +} + type ChunkId struct { Ver int Idx uint64 @@ -68,6 +74,10 @@ func (c *LoadedChunk) Bytes() []byte { return c.value } +func (c *LoadedChunk) Store(path string) error { + return storeChunk(c.Reader(), c.id.Path(path)) +} + func NewStoredFile(repo *Repo, id *ChunkId) *StoredChunk { return &StoredChunk{repo: repo, id: id} } @@ -140,3 +150,18 @@ func (c *DeltaChunk) Reader() io.ReadSeeker { func (c *DeltaChunk) Len() int { return c.size } + +func storeChunk(r io.Reader, path string) error { + file, err := os.Create(path) + if err != nil { + return errors.New(fmt.Sprintf("Error creating chunk for '%s'; %s\n", path, err)) + } + n, err := io.Copy(file, r) + if err != nil { + return errors.New(fmt.Sprintf("Error writing chunk content for '%s', written %d bytes: %s\n", path, n, err)) + } + if err := file.Close(); err != nil { + return errors.New(fmt.Sprintf("Error closing chunk for '%s': %s\n", path, err)) + } + return nil +} |