/
zipcmmt.go
72 lines (68 loc) · 1.65 KB
/
zipcmmt.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
// Package zipcmmt processes the text adverts that are sometimes embedded
// into Zip file archives.
package zipcmmt
import (
"database/sql"
"fmt"
"io"
"os"
"time"
"github.com/Defacto2/df2/pkg/conf"
"github.com/Defacto2/df2/pkg/database"
"github.com/Defacto2/df2/pkg/directories"
"github.com/Defacto2/df2/pkg/str"
"github.com/Defacto2/df2/pkg/zipcmmt/internal/cmmt"
)
const (
errPrefix = "zipcmmt"
fixStmt = `SELECT id, uuid, filename, filesize, file_magic_type FROM` +
` files WHERE filename LIKE "%.zip"`
)
func Fix(db *sql.DB, w io.Writer, cfg conf.Config, unicode, overwrite, stdout bool) error {
if db == nil {
return database.ErrDB
}
if w == nil {
w = io.Discard
}
start := time.Now()
dir, err := directories.Init(cfg, false)
if err != nil {
return err
}
rows, err := db.Query(fixStmt)
if err != nil {
return fmt.Errorf("%s, db query: %w", errPrefix, err)
} else if rows.Err() != nil {
return fmt.Errorf("%s, db rows: %w", errPrefix, rows.Err())
}
defer rows.Close()
// create a writer specifically for the zip comment reader
swr := io.Discard
if stdout {
swr = os.Stdout
}
i := 0
for rows.Next() {
z := cmmt.Zipfile{
CP437: unicode,
Overwrite: overwrite,
}
if err := rows.Scan(&z.ID, &z.UUID, &z.Name, &z.Size, &z.Magic); err != nil {
return fmt.Errorf("%s rows scan: %w", errPrefix, err)
}
i++
if ok, err := z.Exist(dir.UUID); err != nil {
return err
} else if !ok {
continue
}
if _, err := z.Save(swr, dir.UUID); err != nil {
fmt.Fprintln(w, err)
}
}
fmt.Fprintln(w)
fmt.Fprintf(w, "%d zip archives scanned for comments", i)
str.TimeTaken(w, time.Since(start).Seconds())
return nil
}