This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.go
144 lines (121 loc) · 2.96 KB
/
api.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
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
package atomfs
import (
"crypto/sha256"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/anuvu/atomfs/db"
"github.com/anuvu/atomfs/types"
"github.com/schollz/sqlite3dump"
)
type Instance struct {
config types.Config
db *db.AtomfsDB
}
func New(config types.DBBasedConfig) (*Instance, error) {
if err := os.MkdirAll(config.Path, 0755); err != nil {
if !os.IsExist(err) {
return nil, err
}
}
db, err := db.New(config)
if err != nil {
return nil, err
}
return &Instance{config: config, db: db}, nil
}
func (atomfs *Instance) Close() error {
return atomfs.db.Close()
}
// FSCK does a filesystem check on this atomfs instance, returning any errors.
func (atomfs *Instance) FSCK() ([]string, error) {
atoms, err := atomfs.db.GetAtoms()
if err != nil {
return nil, err
}
errs := []string{}
// TODO, we could do progress here.
for _, atom := range atoms {
f, err := os.Open(atomfs.config.AtomsPath(atom.Hash))
if err != nil {
// TODO: should check and see if this atom is used in
// any molecules, and if so delete those molecules,
// and if not at least delete it from the db.
errs = append(errs, err.Error())
continue
}
h := sha256.New()
_, err = io.Copy(h, f)
f.Close()
if err != nil {
errs = append(errs, err.Error())
continue
}
// Uh oh. Again, we should try to prune this, perhaps based on
// some "fix" parameter.
if fmt.Sprintf("%x", h.Sum(nil)) != atom.Hash {
errs = append(errs, fmt.Sprintf("%s does not match its hash", atom.Hash))
}
}
return errs, nil
}
// GC does a garbage collection of atomfs, deleting any unused atoms, and any
// files in the atom directory that aren't in the database.
func (atomfs *Instance) GC(dryRun bool) error {
// First, let's prune unused atoms from the DB.
unusedAtoms, err := atomfs.db.GetUnusedAtoms()
if err != nil {
return err
}
if !dryRun {
for _, atom := range unusedAtoms {
if err := atomfs.db.DeleteThing(atom.ID, "atom"); err != nil {
return err
}
}
}
// Now, delete everything that's on disk that isn't in our DB.
onDiskAtoms, err := ioutil.ReadDir(atomfs.config.AtomsPath())
if err != nil {
// It's possible that there may not have been any atoms
// imported yet. Don't fail in this case.
if os.IsNotExist(err) {
return nil
}
return err
}
inDBAtoms, err := atomfs.db.GetAtoms()
if err != nil {
return err
}
for _, onDiskAtom := range onDiskAtoms {
found := false
for _, inDBAtom := range inDBAtoms {
if onDiskAtom.Name() == inDBAtom.Hash {
found = true
break
}
}
if !found && !dryRun {
err := os.Remove(atomfs.config.AtomsPath(onDiskAtom.Name()))
if err != nil {
return err
}
}
}
return nil
}
// DumpDB() dumps the underlying sqlite3 db for inspection.
func (atomfs *Instance) DumpDB() io.ReadCloser {
reader, writer := io.Pipe()
go func() {
err := sqlite3dump.DumpMigration(atomfs.db.DB, writer)
if err != nil {
writer.CloseWithError(err)
} else {
writer.Close()
}
}()
return reader
}