-
Notifications
You must be signed in to change notification settings - Fork 2
/
storage.go
278 lines (256 loc) · 7.07 KB
/
storage.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package main
import (
"fmt"
"github.com/sqweek/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"database/sql"
_ "github.com/mattn/go-sqlite3"
"github.com/sqweek/sqribe/log"
)
type FileContext struct {
Audio string
State string
Timestamp time.Time // last modified time when state was read. 0 => fresh state (file didn't exist)
Version int // version of loaded state file (0 if it didn't exist)
ViaState bool // did we open a state file?
}
// file can be either a .sqs file or audio file
func Open(file string) (files FileContext, s State, err error) {
if file, err = filepath.Abs(file); err != nil {
return
}
files.ViaState = IsStateFilename(file)
if files.ViaState {
// if a state file is named, it must exist
if _, err = os.Stat(file); err != nil {
return
}
files.State = file
if s, files.Timestamp, err = LoadState(file); err != nil {
return
}
a, dberr := filesDB.AudioFile(file)
if dberr != nil {
log.DB.Printf("error retrieving linked audio file: %s: %v\n", file, dberr)
}
files.Audio = a
if files.Audio == "" {
files.Audio = s.Headers().String("Filename")
filesDB.CheckStaleStates(files.Audio)
}
files.Version = s.Headers().Version
} else {
files.Audio = file
_, err = os.Stat(Cfg.FS.SaveDir)
if err != nil {
err = os.MkdirAll(Cfg.FS.SaveDir, 0777)
}
if err != nil {
return
}
states, dberr := filesDB.StateFiles(file)
if dberr != nil {
log.DB.Printf("error retrieving linked state file: %s: %v\n", file, dberr)
}
if len(states) == 0 {
files.State = filepath.Join(Cfg.FS.SaveDir, stateKey(file))
} else {
files.State = states[0]
if len(states) > 1 {
// XXX should allow user to choose which state to load
log.DB.Printf("multiple states available for audio %s; using first\n", file)
for _, f := range states {
log.DB.Printf(" %s\n", f)
}
}
}
if s, files.Timestamp, err = LoadState(files.State); err != nil {
return
}
if !files.Timestamp.IsZero() {
files.Version = s.Headers().Version
}
}
return
}
func IsStateFilename(file string) bool {
return strings.HasSuffix(strings.ToLower(file), ".sqs")
}
func flatpath(r rune) rune {
if r < 26 || strings.ContainsRune(" /:\\", r) {
return '_'
}
return r
}
func stateKey(audiofile string) string {
return strings.TrimLeft(strings.Map(flatpath, audiofile) + ".sqs", "_")
}
func LoadState(statefile string) (s State, modTime time.Time, err error) {
var f *os.File
if f, err = os.Open(statefile); err != nil {
if os.IsNotExist(err) {
return EmptyState(), modTime, nil
}
return nil, modTime, err
}
defer f.Close()
var stat os.FileInfo
if stat, err = f.Stat(); err != nil {
return nil, modTime, err
}
if s, err = ReadState(f); err != nil {
return nil, modTime, err
}
return s, stat.ModTime(), nil
}
func SaveState(files *FileContext, s State) (err error) {
var tmpfile *os.File
d, f := filepath.Split(files.State)
err = fs.CreateVia(d, func() (e error) {
tmpfile, e = ioutil.TempFile(d, f)
return e
})
if err == nil {
err = s.Write(tmpfile)
tmpfile.Close()
if err == nil {
if files.Version != 0 && files.Version != currentVersion {
bak := fmt.Sprintf("%s.v%d.bak", files.State, files.Version)
if fserr := os.Rename(files.State, bak); fserr != nil {
log.FS.Printf("error backing up %s: %v\n", bak, err)
} else {
files.Version = currentVersion
}
}
if err = fs.ReplaceFile(tmpfile.Name(), files.State); err == nil {
if dberr := filesDB.Associate(files.State, files.Audio); dberr != nil {
log.DB.Printf("error associating %s -> %s: %v\n", files.State, files.Audio, dberr)
}
}
} else {
os.Remove(tmpfile.Name())
}
}
return
}
type AssociationConflict struct {
Statefile string
Attempt string // the audio file we're attempting to link to
Existing string // the audio file already linked in the DB
}
func (e *AssociationConflict) Error() string {
return fmt.Sprintf("Can't associate %s with %s - it's already associated with %s", e.Statefile, e.Attempt, e.Existing)
}
type FilesDB interface {
StateFiles(audiofile string) ([]string, error)
AudioFile(statefile string) (string, error)
Associate(statefile, audiofile string) error
}
type filesSqlite struct {
db *sql.DB
initialised bool
}
var filesDB filesSqlite
func (f *filesSqlite) withDB(fn func(db *sql.DB)error) (err error) {
if f.db == nil {
dbdir := App.Data
if err = os.MkdirAll(dbdir, 0777); err != nil {
return err
}
if f.db, err = sql.Open("sqlite3", filepath.Join(dbdir, "files.qps?_busy_timeout=3500")); err != nil {
return err
}
}
if !f.initialised {
if err = f.createSchema(f.db); err != nil {
return err
}
f.initialised = true
}
return fn(f.db)
}
func (f *filesSqlite) createSchema(db *sql.DB) (err error) {
var tx *sql.Tx
if tx, err = db.Begin(); err == nil {
var vers int
defer tx.Rollback()
row := tx.QueryRow("PRAGMA schema_version;")
if err = row.Scan(&vers); err == nil && vers == 0 {
_, err = tx.Exec("CREATE TABLE paths (state TEXT NOT NULL PRIMARY KEY, audio TEXT NOT NULL, CHECK(length(state) > 0 AND length(audio) > 0));")
if err == nil {
err = tx.Commit()
}
}
}
return
}
func qStateFiles(db *sql.DB, audiofile string) (statefiles []string, err error) {
var rows *sql.Rows
if rows, err = db.Query("SELECT state FROM paths WHERE audio = ?", audiofile); err == nil {
defer rows.Close()
for rows.Next() {
var s string
rows.Scan(&s)
statefiles = append(statefiles, s)
}
}
return
}
func (f *filesSqlite) StateFiles(audiofile string) (statefiles []string, err error) {
err = f.withDB(func(db *sql.DB) (err error) {
statefiles, err = qStateFiles(db, audiofile)
return
})
return
}
// Removes any DB entries referring to non-existant state files.
// The presumption is that said files have been moved elsewhere.
func (f *filesSqlite) CheckStaleStates(audiofile string) (err error) {
err = f.withDB(func(db *sql.DB) (err error) {
var statefiles []string
if statefiles, err = qStateFiles(db, audiofile); err != nil {
return
}
for _, state := range statefiles {
if _, staterr := os.Stat(state); os.IsNotExist(staterr) {
_, subErr := db.Exec("DELETE FROM paths WHERE state = ?", state)
if err == nil && subErr != nil {
err = subErr // report first error
}
}
}
return
})
return
}
func (f *filesSqlite) AudioFile(statefile string) (audiofile string, err error) {
err = f.withDB(func(db *sql.DB) (err error) {
row := db.QueryRow("SELECT audio FROM paths WHERE state = ?", statefile)
return row.Scan(&audiofile)
})
return
}
func (f *filesSqlite) Associate(statefile, audiofile string) error {
return f.withDB(func (db *sql.DB) (err error) {
var tx *sql.Tx
if tx, err = db.Begin(); err != nil {
return
}
defer tx.Rollback()
row := tx.QueryRow("SELECT audio FROM paths WHERE state = ?", statefile)
var audio string
if err = row.Scan(&audio); err == sql.ErrNoRows {
_, err = tx.Exec("INSERT INTO paths VALUES (?, ?)", statefile, audiofile)
if err == nil {
err = tx.Commit()
}
} else if err == nil && audio != audiofile {
err = &AssociationConflict{statefile, audiofile, audio}
}
return
})
}