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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/* 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 dna
import (
"bytes"
"encoding/gob"
"fmt"
"io"
"os"
"path/filepath"
"github.com/n-peugnet/dna-backup/export"
"github.com/n-peugnet/dna-backup/logger"
)
type Direction int
const (
Forward Direction = iota
Backward
)
type DnaDrive struct {
poolCount int
trackSize int
tracksPerPool int
pools []Pool
}
type Pool struct {
Data io.ReadWriteCloser
TrackCount int
}
type Header struct {
Chunks uint64
Recipe uint64
Files uint64
}
func New(
destination string,
poolCount int,
trackSize int,
tracksPerPool int,
) *DnaDrive {
pools := make([]Pool, poolCount)
os.MkdirAll(destination, 0755)
for i := range pools {
path := filepath.Join(destination, fmt.Sprintf("%02d", i))
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
logger.Panic(err)
}
stat, err := file.Stat()
if err != nil {
logger.Panic(err)
}
pools[i] = Pool{file, int(stat.Size()) / trackSize}
}
return &DnaDrive{
poolCount: poolCount,
trackSize: trackSize,
tracksPerPool: tracksPerPool,
pools: pools,
}
}
func (d *DnaDrive) ExportVersion(end chan<- bool) export.Input {
rChunks, wChunks := io.Pipe()
rRecipe, wRecipe := io.Pipe()
rFiles, wFiles := io.Pipe()
version := export.Version{
Input: export.Input{
Chunks: wChunks,
Recipe: wRecipe,
Files: wFiles,
},
Output: export.Output{
Chunks: rChunks,
Recipe: rRecipe,
Files: rFiles,
},
}
go d.writeVersion(version.Output, end)
return version.Input
}
func (d *DnaDrive) writeVersion(output export.Output, end chan<- bool) {
var err error
var recipe, files, version bytes.Buffer
n := write(output.Chunks, d.pools[1:], d.trackSize, d.tracksPerPool, Forward)
_, err = io.Copy(&recipe, output.Recipe)
if err != nil {
logger.Error("dna export recipe ", err)
}
_, err = io.Copy(&files, output.Files)
if err != nil {
logger.Error("dna export files ", err)
}
header := Header{
uint64(n),
uint64(recipe.Len()),
uint64(files.Len()),
}
e := gob.NewEncoder(&version)
err = e.Encode(header)
if err != nil {
logger.Error("dna export version header: ", err)
}
logger.Debugf("version len %d", version.Len())
rest := int64(d.trackSize - version.Len())
logger.Debugf("version rest %d", rest)
n, err = io.CopyN(&version, &recipe, rest)
logger.Debugf("recipe copied in version %d", n)
rest -= n
logger.Debugf("version rest %d", rest)
if err == io.EOF && rest > 0 { // recipe is written to version but there is space left
n, err = io.CopyN(&version, &files, rest)
logger.Debugf("files copied in version %d", n)
rest -= n
logger.Debugf("version rest %d", rest)
if err == io.EOF && rest > 0 { // files is writtent to version but there is space left
// version.Write(make([]byte, rest))
} else if err != nil { // another error than EOF happened
logger.Error("dna export files: ", err)
} else { // files has not been fully written so we write what is left to pools
write(&files, d.pools[1:], d.trackSize, d.tracksPerPool, Backward)
}
} else if err != nil { // another error than EOF happened
logger.Error("dna export recipe: ", err)
} else { // recipe has not been fully written so we concat with files and write what is left to pools
io.Copy(&recipe, &files)
write(&recipe, d.pools[1:], d.trackSize, d.tracksPerPool, Backward)
}
write(&version, d.pools[:1], d.trackSize, d.tracksPerPool, Forward)
end <- true
}
func write(r io.Reader, pools []Pool, trackSize int, tracksPerPool int, direction Direction) int64 {
var err error
var i, n int
var count int64
if direction == Backward {
i = len(pools) - 1
}
for err != io.ErrUnexpectedEOF && err != io.EOF {
if pools[i].TrackCount == tracksPerPool {
if direction == Backward {
i--
} else {
i++
}
if i < 0 || i >= len(pools) {
logger.Panic("dna export: no space left")
}
continue
}
buf := make([]byte, trackSize)
n, err = io.ReadFull(r, buf)
if err == io.EOF {
break
}
logger.Debug("written track:", n, err)
count += int64(n)
n, errw := pools[i].Data.Write(buf)
if errw != nil {
logger.Error("dna export: pool %d: %d/%d bytes written: %s", i, n, len(buf), errw)
}
pools[i].TrackCount++
}
return count
}
|