aboutsummaryrefslogtreecommitdiff
path: root/chunk.go
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2021-08-24 18:31:13 +0200
committern-peugnet <n.peugnet@free.fr>2021-08-24 18:40:05 +0200
commitdf6d5f7e24a290718adf8f068649c3bc61f5eb4d (patch)
treeddc31a133a8e82b0529264962fd75d6515b1ab4e /chunk.go
parentb070eae35c1e7a4996b90208153d01f2be08d588 (diff)
downloaddna-backup-df6d5f7e24a290718adf8f068649c3bc61f5eb4d.tar.gz
dna-backup-df6d5f7e24a290718adf8f068649c3bc61f5eb4d.zip
refactor: extract chunk.og & add Reader getter
Diffstat (limited to 'chunk.go')
-rw-r--r--chunk.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/chunk.go b/chunk.go
new file mode 100644
index 0000000..9f09e55
--- /dev/null
+++ b/chunk.go
@@ -0,0 +1,47 @@
+package main
+
+import (
+ "bytes"
+ "fmt"
+ "io"
+ "log"
+ "os"
+ "path"
+)
+
+type ChunkId struct {
+ Ver int
+ Idx uint64
+}
+
+func (i *ChunkId) Reader(repo string) io.Reader {
+ p := path.Join(repo, fmt.Sprintf(versionFmt, i.Ver), chunksName, fmt.Sprintf(chunkIdFmt, i.Idx))
+ f, err := os.Open(p)
+ if err != nil {
+ log.Printf("Cannot open chunk %s\n", p)
+ }
+ return f
+}
+
+type Chunk struct {
+ Id *ChunkId
+ Value []byte
+}
+
+func (c *Chunk) Reader(repo string) (io.Reader, error) {
+ if c.Value != nil {
+ return bytes.NewReader(c.Value), nil
+ }
+ if c.Id != nil {
+ return c.Id.Reader(repo), nil
+ }
+ return nil, &ChunkError{"Uninitialized chunk"}
+}
+
+type ChunkError struct {
+ err string
+}
+
+func (e *ChunkError) Error() string {
+ return fmt.Sprintf("Chunk error: %s", e.err)
+}