-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdirs.go
58 lines (52 loc) · 1.11 KB
/
dirs.go
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
package freezer
import (
"fmt"
"os"
"path/filepath"
"strconv"
"github.com/uw-labs/straw"
)
func seqToPath(basepath string, seq int) string {
seqStr := fmt.Sprintf("%014d", seq)
path := basepath
for len(seqStr) > 0 {
path = filepath.Join(path, seqStr[0:2])
seqStr = seqStr[2:]
}
return path
}
// This is the depth of parent directories within which the data is stored.
const dirDepth = 6
func nextSequence(ss straw.StreamStore, basedir string) (int, error) {
_, err := ss.Stat(seqToPath(basedir, 0))
if err != nil {
if os.IsNotExist(err) {
return 0, nil
}
return -1, err
}
dir := basedir
total := 0
for i := 0; i <= dirDepth; i++ {
fis, err := ss.Readdir(dir)
if err != nil {
return -1, err
}
l := len(fis)
if l == 0 {
return -1, fmt.Errorf("'%s' does not contain enough folders", basedir)
}
fi := fis[l-1]
if i < dirDepth && !fi.IsDir() {
return -1, fmt.Errorf("'%s' is not a directory", fi.Name())
}
dir = filepath.Join(dir, fi.Name())
num, err := strconv.Atoi(fi.Name())
if err != nil {
return -1, err
}
total *= 100
total += num
}
return total + 1, nil
}