Skip to content

Commit

Permalink
Copy chat DB to log folder
Browse files Browse the repository at this point in the history
  • Loading branch information
tagatac committed Jul 26, 2022
1 parent 78488e9 commit c50b14c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
5 changes: 2 additions & 3 deletions bagoup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ func (cfg configuration) bagoup() error {
return err
}

logDir := filepath.Join(cfg.Options.ExportPath, ".bagoup")
if err := cfg.OS.MkdirAll(logDir, os.ModePerm); err != nil {
if err := cfg.OS.MkdirAll(cfg.logDir, os.ModePerm); err != nil {
return errors.Wrap(err, "make log directory")
}
logFile, err := cfg.OS.Create(filepath.Join(logDir, "out.log"))
logFile, err := cfg.OS.Create(filepath.Join(cfg.logDir, "out.log"))
if err != nil {
return errors.Wrap(err, "create log file")
}
Expand Down
1 change: 1 addition & 0 deletions bagoup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func TestBagoup(t *testing.T) {
Options: tt.opts,
OS: osMock,
ChatDB: dbMock,
logDir: "messages-export/.bagoup",
}
err := cfg.bagoup()
if tt.wantErr != "" {
Expand Down
21 changes: 19 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package main
import (
"database/sql"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"time"

"github.com/Masterminds/semver"
Expand Down Expand Up @@ -57,6 +59,7 @@ type (
Options options
opsys.OS
chatdb.ChatDB
logDir string
MacOSVersion *semver.Version
HandleMap map[int]string
AttachmentPaths map[int][]chatdb.Attachment
Expand Down Expand Up @@ -95,18 +98,32 @@ func main() {
s, err := opsys.NewOS(afero.NewOsFs(), os.Stat, exec.Command)
logFatalOnErr(errors.Wrap(err, "instantiate OS"))
db, err := sql.Open("sqlite3", opts.DBPath)
logFatalOnErr(errors.Wrapf(err, "open DB file %q", opts.DBPath))
logFatalOnErr(errors.Wrapf(err, "open DB file %q", dbPath))
defer db.Close()
cdb := chatdb.NewChatDB(db, opts.SelfHandle)

logDir := filepath.Join(opts.ExportPath, ".bagoup")
cfg := configuration{
Options: opts,
OS: s,
ChatDB: cdb,
Options: opts,
logDir: logDir,
Counts: counts{attachments: map[string]int{}, attachmentsEmbedded: map[string]int{}},
StartTime: startTime,
}
logFatalOnErr(cfg.bagoup())
logFatalOnErr(errors.Wrapf(db.Close(), "close DB file %q", dbPath))
dbf, err := os.Open(dbPath)
logFatalOnErr(errors.Wrapf(err, "open DB file %q for copying", dbPath))
defer dbf.Close()
dbfNewPath := filepath.Join(logDir, filepath.Base(dbPath))
dbfNew, err := os.Create(dbfNewPath)
logFatalOnErr(errors.Wrapf(err, "create file %q to copy chat DB into", dbfNewPath))
defer dbfNew.Close()
_, err = io.Copy(dbfNew, dbf)
logFatalOnErr(errors.Wrapf(err, "copy DB file from %q to %q", dbPath, dbfNewPath))
logFatalOnErr(errors.Wrapf(dbf.Close(), "close DB file %q after copying", dbPath))
logFatalOnErr(errors.Wrapf(dbfNew.Close(), "close DB copy %q", dbfNewPath))
}

func logFatalOnErr(err error) {
Expand Down

0 comments on commit c50b14c

Please sign in to comment.