aboutsummaryrefslogtreecommitdiff
path: root/repo/chunk.go
blob: d3d44934fc9c4bac8ddfc0eac9874657e22e29bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* Copyright (C) 2021 Nicolas Peugnet <n.peugnet@free.fr>

   This file is part of dna-backup.

   dna-backup is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   dna-backup is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with dna-backup.  If not, see <https://www.gnu.org/licenses/>. */

package repo

import (
	"bytes"
	"fmt"
	"io"
	"path/filepath"
)

type Chunk interface {
	Reader() io.ReadSeeker
	Len() int
}

type IdentifiedChunk interface {
	Chunk
	GetId() *ChunkId
}

type BufferedChunk interface {
	Chunk
	Bytes() []byte
}

type RepoChunk interface {
	Chunk
	SetRepo(r *Repo)
}

type ChunkId struct {
	Ver int
	Idx uint64
}

func (i *ChunkId) Path(repo string) string {
	return filepath.Join(repo, fmt.Sprintf(versionFmt, i.Ver), chunksName, fmt.Sprintf(chunkIdFmt, i.Idx))
}

func NewStoredChunk(repo *Repo, id *ChunkId) *StoredChunk {
	return &StoredChunk{repo: repo, Id: id}
}

type StoredChunk struct {
	repo *Repo
	Id   *ChunkId
}

func (c *StoredChunk) GetId() *ChunkId {
	return c.Id
}

func (c *StoredChunk) SetRepo(r *Repo) {
	c.repo = r
}

func (c *StoredChunk) Reader() io.ReadSeeker {
	// log.Printf("Chunk %d: Reading from file\n", c.id)
	return c.repo.LoadChunkContent(c.Id)
}

func (c *StoredChunk) Len() int {
	return c.repo.chunkSize
}

func NewTempChunk(value []byte) *TempChunk {
	return &TempChunk{Value: value}
}

type TempChunk struct {
	Value []byte
}

func (c *TempChunk) Reader() io.ReadSeeker {
	return bytes.NewReader(c.Value)
}

func (c *TempChunk) Len() int {
	return len(c.Value)
}

func (c *TempChunk) Bytes() []byte {
	return c.Value
}

func (c *TempChunk) AppendFrom(r io.Reader) {
	buff, err := io.ReadAll(r)
	if err != nil {
		println("Chunk: error appending to temp chunk:", err)
	}
	c.Value = append(c.Value, buff...)
}

type DeltaChunk struct {
	repo   *Repo
	Source *ChunkId
	Patch  []byte
	Size   int
}

func (c *DeltaChunk) SetRepo(r *Repo) {
	c.repo = r
}

func (c *DeltaChunk) Reader() io.ReadSeeker {
	var buff bytes.Buffer
	c.repo.Patcher().Patch(c.repo.LoadChunkContent(c.Source), &buff, bytes.NewReader(c.Patch))
	return bytes.NewReader(buff.Bytes())
}

// TODO: Maybe return the size of the patch instead ?
func (c *DeltaChunk) Len() int {
	return c.Size
}