f381c9b1afd0 — Ted Unangst a month ago
confine the madness to backup code, and hardlink attachments
2 files changed, 17 insertions(+), 6 deletions(-)

M backupdb.go
M filestoragemanagerfactory.go
M backupdb.go +17 -1
@@ 182,6 182,7 @@ func svalbard(dirname string) {
 	backup.Close()
 
 	var blob *sql.DB
+	var filesavepath string
 	if storeTheFilesInTheFileSystem {
 		filesavepath = fmt.Sprintf("%s/attachments-%d", dirname, now)
 		os.Mkdir(filesavepath, 0700)

          
@@ 203,12 204,27 @@ func svalbard(dirname string) {
 		checkErr(err)
 	}
 	for xid := range filexids {
+		if storeTheFilesInTheFileSystem {
+			oldname := filepath(xid)
+			newname := filesavepath + oldname[14:]
+			os.Mkdir(newname[:strings.LastIndexByte(newname, '/')], 0700)
+			err = os.Link(oldname, newname)
+			if err == nil {
+				continue
+			}
+		}
 		data, closer, err := loaddata(xid)
 		if err != nil {
 			elog.Printf("lost a file: %s", xid)
 			continue
 		}
-		err = savefiledata(xid, data)
+		if storeTheFilesInTheFileSystem {
+			oldname := filepath(xid)
+			newname := filesavepath + oldname[14:]
+			err = os.WriteFile(newname, data, 0700)
+		} else {
+			_, err = stmtSaveBlobData.Exec(xid, data)
+		}
 		if err != nil {
 			elog.Printf("failed to save file %s: %s", xid, err)
 		}

          
M filestoragemanagerfactory.go +0 -5
@@ 50,14 50,9 @@ func savefile(name string, desc string, 
 	return fileid, err
 }
 
-var filesavepath = "" // used to redirect backups
-
 func savefiledata(xid string, data []byte) error {
 	if storeTheFilesInTheFileSystem {
 		fname := filepath(xid)
-		if filesavepath != "" {
-			fname = filesavepath + fname[14:]
-		}
 		os.Mkdir(fname[:strings.LastIndexByte(fname, '/')], 0700)
 		err := os.WriteFile(fname, data, 0700)
 		return err