From 88ecfcb7b517fd3cbed1c683b0d8835a4d55b2fc Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Mon, 6 Sep 2021 18:21:29 +0200 Subject: initial chunk storage --- chunk.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'chunk.go') diff --git a/chunk.go b/chunk.go index d9833ea..670ed24 100644 --- a/chunk.go +++ b/chunk.go @@ -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 +} -- cgit v1.2.3