-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
206 lines (166 loc) · 3.38 KB
/
main.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
package main
import (
"database/sql"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
// sqlite driver
_ "github.com/mattn/go-sqlite3"
// magic functionality
"github.com/rakyll/magicmime"
)
const (
// each progressStep notification is thrown
progressStep = 100
capacity = 1024
workers = 8
)
type request struct {
path string
f os.FileInfo
}
type entry struct {
path string
basename string
directory bool
tipo string
modified time.Time
size int64
}
var (
conn *sql.DB
filesProcessed = 0
lastChunk = time.Now()
requests = make(chan *request, capacity)
wg = sync.WaitGroup{}
write = sync.Mutex{}
)
func visit(path string, f os.FileInfo, err error) error {
// skip non-reachable dir
if err != nil {
log.Println("Warning, skipping because of error:", err)
return nil
}
wg.Add(1)
requests <- &request{
path: path,
f: f,
}
return nil
}
func worker() {
var entries []entry
for {
if len(entries) > capacity { // flush if too many requests
flush(entries)
entries = nil
}
select {
case <-time.Tick(time.Second): // flush on idle
flush(entries)
entries = nil
case msg := <-requests:
var fileOutput = ""
// only for files get magic
if !msg.f.IsDir() && !strings.Contains(msg.f.Name(), ".") {
magic, err := magicmime.TypeByFile(msg.path)
if err == nil {
fileOutput = magic
}
}
entries = append(entries, entry{
msg.path,
msg.f.Name(),
msg.f.IsDir(),
fileOutput,
msg.f.ModTime(),
msg.f.Size(),
})
}
}
}
func flush(entries []entry) {
write.Lock()
// begin TX -- more effective way to write tons of data
tx, err := conn.Begin()
if err != nil {
log.Fatal(err)
}
// create STMT for fast insert
stmt, err := tx.Prepare(`INSERT INTO files(
path, basename, directory, type, modified, size
) VALUES (
?, ?, ?, ?, ?, ?
)`)
if err != nil {
log.Println(err)
return
}
defer stmt.Close()
// write queries
for _, e := range entries {
stmt.Exec(e.path, e.basename, e.directory, e.tipo, e.modified, e.size)
}
// finish TX
tx.Commit()
for range entries {
wg.Done()
filesProcessed++
}
log.Printf("Processing speed: %v fps", float64(filesProcessed)/time.Since(lastChunk).Seconds())
lastChunk = time.Now()
write.Unlock()
}
func main() {
// check args
if len(os.Args) < 3 {
log.Fatal(fmt.Errorf("Usage: %v directory sqlitedb", os.Args[0]))
}
// init magic lib
if err := magicmime.Open(magicmime.MAGIC_MIME_TYPE | magicmime.MAGIC_SYMLINK | magicmime.MAGIC_ERROR); err != nil {
log.Fatal(err)
}
defer magicmime.Close()
// open database
db, err := sql.Open("sqlite3", os.Args[2])
if err != nil {
log.Fatal(err)
}
conn = db
defer db.Close()
// set mode to WAL
_, err = db.Exec(`PRAGMA journal_mode = WAL`)
if err != nil {
log.Println(err) // no more fatal as we need defer
return
}
// make sure table exists
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS
files (
path text not null primary key,
basename text,
directory bool,
type text,
modified datetime,
size integer
) `)
if err != nil {
log.Println(err)
return
}
// start workers
for i := 1; i <= workers; i++ {
go worker()
}
err = filepath.Walk(os.Args[1], visit)
if err != nil {
log.Println(err)
return
}
log.Println("Finished; waiting for last files to upd")
wg.Wait()
}