aboutsummaryrefslogtreecommitdiff
path: root/chunk.go
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2021-09-06 18:21:29 +0200
committern-peugnet <n.peugnet@free.fr>2021-09-06 18:21:29 +0200
commit88ecfcb7b517fd3cbed1c683b0d8835a4d55b2fc (patch)
treea1896dffa20d384dd44852dc82c50075f2db4aeb /chunk.go
parent656a2c10b177e7fafc0b4bbddf3c634c642a2221 (diff)
downloaddna-backup-88ecfcb7b517fd3cbed1c683b0d8835a4d55b2fc.tar.gz
dna-backup-88ecfcb7b517fd3cbed1c683b0d8835a4d55b2fc.zip
initial chunk storage
Diffstat (limited to 'chunk.go')
-rw-r--r--chunk.go25
1 files changed, 25 insertions, 0 deletions
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
+}