Skip to content

Commit ff7d767

Browse files
committed
sqlite err panics
1 parent b662378 commit ff7d767

File tree

25 files changed

+165
-526
lines changed

25 files changed

+165
-526
lines changed

backup/backup.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,7 @@ func DryBackup(rawPaths []string) {
151151

152152
// scanning
153153
tx, err := db.DB.Begin()
154-
if err != nil {
155-
panic(err)
156-
}
154+
db.Must(err)
157155
defer tx.Rollback()
158156
statuses := make([]FileStatus, 0)
159157
var allPathsToBackup []string

backup/database.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ func dbKeyImpl(interactive bool) []byte {
8383
}
8484
_, err = db.DB.Exec("INSERT INTO db_key (key, id) VALUES (?, 0)", key)
8585
}
86-
if err != nil {
87-
panic(err)
88-
}
86+
db.Must(err)
8987
if len(key) != 16 {
9088
panic("bad key")
9189
}

backup/hasher.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ func (s *BackupSession) hashOneFile(plan HashPlan) {
4848
log.Println("Updating fs_modifed in db so next time I don't reread this for no reason lol")
4949
// this is VERY uncommon, so it is NOT worth maintaining a db WRITE transaction for it sadly
5050
_, err := db.DB.Exec("UPDATE files SET fs_modified = ?, permissions = ? WHERE path = ? AND end IS NULL", info.ModTime().Unix(), info.Mode()&os.ModePerm, path)
51-
if err != nil {
52-
panic(err)
53-
}
51+
db.Must(err)
5452
return
5553
}
5654

@@ -64,18 +62,14 @@ func (s *BackupSession) hashOneFile(plan HashPlan) {
6462
s.hashLateMapLock.Lock() // this lock ensures atomicity between the hashLateMap, and the database (blob_entries and files)
6563
defer s.hashLateMapLock.Unlock()
6664
tx, err := db.DB.Begin()
67-
if err != nil {
68-
panic(err)
69-
}
65+
db.Must(err)
7066
defer tx.Rollback() // fileHasKnownData can panic
7167
var dbHash []byte
7268
err = tx.QueryRow("SELECT hash FROM blob_entries WHERE hash = ?", hash).Scan(&dbHash)
7369
if err == nil {
7470
// yeah so we already have this hash backed up, so the train stops here. we just need to add this to files table, and we're done!
7571
s.fileHasKnownData(tx, path, info, hash)
76-
if err := tx.Commit(); err != nil {
77-
panic(err)
78-
}
72+
db.Must(tx.Commit())
7973
return nil // done, no need to upload
8074
}
8175
if err != db.ErrNoRows {

backup/scanner.go

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -112,42 +112,27 @@ func (s *BackupSession) pruneDeletedFiles(backupPath string, filesMap map[string
112112
// we cannot upgrade the long lived RO transaction to a RW transaction, it would conflict with the intermediary RW transactions, it seems
113113
// reusing the tx from scanner results in a sqlite busy panic, very consistently
114114
tx, err := db.DB.Begin()
115-
if err != nil {
116-
panic(err)
117-
}
115+
db.Must(err)
118116
if !strings.HasSuffix(backupPath, "/") {
119117
panic(backupPath) // sanity check, should have already been completed
120118
}
121119
log.Println("Finally, handling deleted files!")
122120
// anything that was in this directory but is no longer can be deleted
123121
rows, err := tx.Query("SELECT path FROM files WHERE end IS NULL AND path "+db.StartsWithPattern(1), backupPath)
124-
if err != nil {
125-
panic(err)
126-
}
122+
db.Must(err)
127123
defer rows.Close()
128124
for rows.Next() {
129125
var databasePath string
130-
err := rows.Scan(&databasePath)
131-
if err != nil {
132-
panic(err)
133-
}
126+
db.Must(rows.Scan(&databasePath))
134127
if _, ok := filesMap[databasePath]; !ok {
135128
log.Println(databasePath, "used to exist but does not any longer. Marking as ended.")
136129
_, err = tx.Exec("UPDATE files SET end = ? WHERE path = ? AND end IS NULL", s.now, databasePath)
137-
if err != nil {
138-
panic(err)
139-
}
130+
db.Must(err)
140131
}
141132
}
142-
err = rows.Err()
143-
if err != nil {
144-
panic(err)
145-
}
133+
db.Must(rows.Err())
146134
log.Println("Pruner committing")
147-
err = tx.Commit()
148-
if err != nil {
149-
panic(err)
150-
}
135+
db.Must(tx.Commit())
151136
log.Println("Pruner committed")
152137
}
153138

@@ -165,9 +150,7 @@ func (ctx *ScannerTransactionContext) Tx() *sql.Tx {
165150
}
166151
if ctx.tx == nil {
167152
tx, err := db.DB.Begin()
168-
if err != nil {
169-
panic(err)
170-
}
153+
db.Must(err)
171154
ctx.tx = tx
172155
return tx
173156
}
@@ -183,10 +166,7 @@ func (ctx *ScannerTransactionContext) Tx() *sql.Tx {
183166

184167
func (ctx *ScannerTransactionContext) Close() {
185168
if ctx.tx != nil {
186-
err := ctx.tx.Commit()
187-
if err != nil {
188-
panic(err)
189-
}
169+
db.Must(ctx.tx.Commit())
190170
ctx.tx = nil
191171
}
192172
}

backup/uploader.go

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,11 @@ func (s *BackupSession) executeBlobUploadPlan(plan BlobPlan, serv UploadService)
124124
s.hashLateMapLock.Lock() // YES, the database query MUST be within this lock (to make sure that the Commit happens before this defer!)
125125
defer s.hashLateMapLock.Unlock()
126126
tx, err := db.DB.Begin()
127-
if err != nil {
128-
panic(err)
129-
}
127+
db.Must(err)
130128
defer tx.Rollback()
131129
// **obviously** all this needs to be in a tx
132130
_, err = tx.Exec("INSERT INTO blobs (blob_id, padding_key, size, final_hash) VALUES (?, ?, ?, ?)", blobID, paddingKey, totalSize, hashPostEnc)
133-
if err != nil {
134-
panic(err)
135-
}
131+
db.Must(err)
136132
now := time.Now().Unix()
137133
for _, completed := range completeds {
138134
if !bytes.Equal(completed.BlobID, blobID) {
@@ -142,16 +138,12 @@ func (s *BackupSession) executeBlobUploadPlan(plan BlobPlan, serv UploadService)
142138
panic("sanity check")
143139
}
144140
_, err = tx.Exec("INSERT INTO blob_storage (blob_id, storage_id, path, checksum, timestamp) VALUES (?, ?, ?, ?, ?)", blobID, completed.StorageID, completed.Path, completed.Checksum, now)
145-
if err != nil {
146-
panic(err)
147-
}
141+
db.Must(err)
148142
}
149143
for _, entry := range entries {
150144
// do this first (before fileHasKnownData) because of that pesky foreign key
151145
_, err = tx.Exec("INSERT OR IGNORE INTO sizes (hash, size) VALUES (?, ?)", entry.hash, entry.preCompressionSize)
152-
if err != nil {
153-
panic(err)
154-
}
146+
db.Must(err)
155147
if bytes.Equal(entry.originalPlan.hash, entry.hash) {
156148
// fetch ALL the files that hashed to this hash
157149
files := s.hashLateMap[utils.SliceToArr(entry.hash)]
@@ -176,34 +168,25 @@ func (s *BackupSession) executeBlobUploadPlan(plan BlobPlan, serv UploadService)
176168
}
177169
// and either way, make a note of what hash is stored in this blob at this location
178170
_, err = tx.Exec("INSERT INTO blob_entries (hash, blob_id, encryption_key, final_size, offset, compression_alg) VALUES (?, ?, ?, ?, ?, ?)", entry.hash, blobID, entry.key, entry.postCompressionSize, entry.offset, entry.compression)
179-
if err != nil {
180-
panic(err)
181-
}
171+
db.Must(err)
182172
}
183173
log.Println("Uploader done with blob", plan)
184174
log.Println("Uploader committing to database")
185175
txCommitted = true // err on the side of caution - if tx.Commit returns an err, very likely it did not actually commit, but, it's possible! so don't delete the blob if there's ANY chance that the db is expecting this blob to exist.
186-
err = tx.Commit()
187-
if err != nil {
188-
panic(err)
189-
}
176+
db.Must(tx.Commit())
190177
log.Println("Committed uploaded blob")
191178
}
192179

193180
func (s *BackupSession) fileHasKnownData(tx *sql.Tx, path string, info os.FileInfo, hash []byte) {
194181
// important to use the same "now" for both of these queries, so that the file's history is presented without "gaps" (that could be present if we called time.Now() twice in a row)
195182
_, err := tx.Exec("UPDATE files SET end = ? WHERE path = ? AND end IS NULL", s.now, path)
196-
if err != nil {
197-
panic(err)
198-
}
183+
db.Must(err)
199184
modTime := info.ModTime().Unix()
200185
if modTime < 0 {
201186
panic(fmt.Sprintf("Invalid modification time for %s: %d", path, modTime))
202187
}
203188
_, err = tx.Exec("INSERT INTO files (path, hash, start, fs_modified, permissions) VALUES (?, ?, ?, ?, ?)", path, hash, s.now, modTime, info.Mode()&os.ModePerm)
204-
if err != nil {
205-
panic(err)
206-
}
189+
db.Must(err)
207190
}
208191

209192
// this function should be called if the uploader thread was intended to upload a backup of a certain hash, but failed to do so, for any reason

db/db.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ var ErrNoRows = sql.ErrNoRows
1919

2020
var DB *sql.DB
2121

22+
// only to be used for sqlite errors
23+
// sql errors in gb are 1. unrecoverable 2. not expected to ever be user-facing
24+
// a panic is the right choice
25+
func Must(err error) {
26+
if err != nil {
27+
panic(err)
28+
}
29+
}
30+
2231
// see https://sqlite.org/forum/info/eabfcd13dcd71807
2332
func StartsWithPattern(arg int32) string {
2433
return fmt.Sprintf(" BETWEEN (?%d) AND (?%d || x'ff') ", arg, arg)
@@ -47,13 +56,9 @@ func setupDatabase(fullPath string, setupSchema bool) {
4756
//log.Println("Opening database file", fullPath)
4857
var err error
4958
DB, err = sql.Open("sqlite3", fullPath)
50-
if err != nil {
51-
panic(err)
52-
}
59+
Must(err)
5360
_, err = DB.Exec("PRAGMA journal_size_limit = 100000000") // 100 megabytes
54-
if err != nil {
55-
panic(err)
56-
}
61+
Must(err)
5762
//log.Println("Database connection created")
5863
//DB.SetMaxOpenConns(1) // 100x better to block for a few hundred ms than to panic with SQLITE_BUSY!!!!
5964
// commenting out until i actually hit a sqlite_busy

db/schema.go

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,13 @@ const (
1212
func initialSetup() {
1313
switch determineDatabaseLayer() {
1414
case DATABASE_LAYER_EMPTY:
15-
err := schemaVersionOne()
16-
if err != nil {
17-
panic(err)
18-
}
15+
Must(schemaVersionOne())
1916
fallthrough
2017
case DATABASE_LAYER_1:
21-
err := schemaVersionTwo()
22-
if err != nil {
23-
panic(err)
24-
}
18+
Must(schemaVersionTwo())
2519
fallthrough
2620
case DATABASE_LAYER_2:
27-
err := schemaVersionThree()
28-
if err != nil {
29-
panic(err)
30-
}
21+
Must(schemaVersionThree())
3122
fallthrough
3223
case DATABASE_LAYER_3:
3324
// up to date
@@ -54,9 +45,7 @@ To see the final schema that gb actually uses today, refer to schema.sql
5445

5546
func schemaVersionOne() error {
5647
tx, err := DB.Begin()
57-
if err != nil {
58-
panic(err)
59-
}
48+
Must(err)
6049
defer tx.Rollback()
6150
_, err = tx.Exec(`
6251
CREATE TABLE sizes (
@@ -180,22 +169,15 @@ func schemaVersionOne() error {
180169
if err != nil {
181170
return err
182171
}
183-
err = tx.Commit()
184-
if err != nil {
185-
panic(err)
186-
}
172+
Must(tx.Commit())
187173
return nil
188174
}
189175

190176
func schemaVersionTwo() error {
191177
_, err := DB.Exec("PRAGMA foreign_keys = OFF")
192-
if err != nil {
193-
panic(err)
194-
}
178+
Must(err)
195179
tx, err := DB.Begin()
196-
if err != nil {
197-
panic(err)
198-
}
180+
Must(err)
199181
// defer rollback BUT commit after successful execution
200182
defer tx.Rollback() // !!!intended to NOT actually rollback!!!
201183
_, err = tx.Exec(`
@@ -248,22 +230,15 @@ func schemaVersionTwo() error {
248230
if err != nil {
249231
return err
250232
}
251-
err = tx.Commit()
252-
if err != nil {
253-
panic(err)
254-
}
233+
Must(tx.Commit())
255234
_, err = DB.Exec("PRAGMA foreign_keys = ON")
256-
if err != nil {
257-
panic(err)
258-
}
235+
Must(err)
259236
return nil
260237
}
261238

262239
func schemaVersionThree() error {
263240
tx, err := DB.Begin()
264-
if err != nil {
265-
panic(err)
266-
}
241+
Must(err)
267242
defer tx.Rollback()
268243
_, err = tx.Exec(`
269244
DROP INDEX blob_entries_by_blob_id;
@@ -314,32 +289,21 @@ func schemaVersionThree() error {
314289
if err != nil {
315290
return err
316291
}
317-
err = tx.Commit()
318-
if err != nil {
319-
panic(err)
320-
}
292+
Must(tx.Commit())
321293
return nil
322294
}
323295

324296
func query(query string) string {
325297
rows, err := DB.Query(query)
326-
if err != nil {
327-
panic(err)
328-
}
298+
Must(err)
329299
defer rows.Close()
330300
ret := ""
331301
for rows.Next() {
332302
var tableName string
333-
err = rows.Scan(&tableName)
334-
if err != nil {
335-
panic(err)
336-
}
303+
Must(rows.Scan(&tableName))
337304
ret = ret + tableName + ","
338305
}
339-
err = rows.Err()
340-
if err != nil {
341-
panic(err)
342-
}
306+
Must(rows.Err())
343307
return ret
344308
}
345309

download/cat.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ func LookupBlobEntry(hash []byte, tx *sql.Tx, stor storage_base.Storage) BlobEnt
8787
INNER JOIN sizes ON sizes.hash = blob_entries.hash
8888
WHERE blob_entries.hash = ? AND blob_storage.storage_id = ?`,
8989
hash, stor.GetID()).Scan(&blobID, &offset, &length, &compressionAlg, &key, &path, &expectedSize)
90-
if err != nil {
91-
panic(err)
92-
}
90+
db.Must(err)
9391

9492
return BlobEntryInfo{
9593
BlobID: blobID,
@@ -116,9 +114,7 @@ func Cat(hash []byte, tx *sql.Tx, stor storage_base.Storage) io.Reader {
116114

117115
func CatEz(hash []byte, stor storage_base.Storage) io.Reader {
118116
tx, err := db.DB.Begin()
119-
if err != nil {
120-
panic(err)
121-
}
117+
db.Must(err)
122118
defer tx.Rollback()
123119

124120
return Cat(hash, tx, stor)

0 commit comments

Comments
 (0)