-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
75 lines (64 loc) · 1.46 KB
/
config.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func sstatDirExists(path string) bool {
if fi, err := os.Stat(path); err == nil {
return fi.IsDir()
}
return false
}
// findDotMtree returns the nearest .mtree from the given path, and the offset
// Eg. a/b/c/d/e might return a/b/.mtree, c/d/e
func findDotMtree(path string) (string, string) {
root, _ := normPath(path)
offset := ""
for {
dmtpath := root + "/.mtree"
if sstatDirExists(dmtpath) {
return dmtpath, offset
}
if root == "/" {
break
}
if offset == "" {
offset = filepath.Base(root)
} else {
offset = filepath.Base(root) + "/" + offset
}
root = filepath.Dir(root)
}
return "", ""
}
func latestMtree(dir string) (string, error) {
files, err := ioutil.ReadDir(dir) // Eh. wastes some resources...
if err != nil {
return "", err
}
latest := ""
for _, file := range files { // This is sorted, so last is newest...
fname := file.Name()
// Format should be same as tmBaseName() ... need more checks.
if len(fname) < len("2006-01-02--1504Z.mtree") {
continue
}
switch {
case strings.HasSuffix(fname, ".mtree"):
latest = fname
case strings.HasSuffix(fname, ".mtree.gz"):
latest = fname
case strings.HasSuffix(fname, ".mtree.bz2"):
latest = fname
case strings.HasSuffix(fname, ".mtree.xz"):
latest = fname
}
}
if latest == "" {
return "", fmt.Errorf("Can't find a local snapshot from: %s", dir)
}
return latest, nil
}