-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscan.go
236 lines (192 loc) · 5.75 KB
/
scan.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// This file is part of *kellner*
//
// Copyright (C) 2015, Travelping GmbH <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
"sync/atomic"
"time"
)
func scanRoot(root, cache string, nworkers int, doMD5, doSHA1 bool, gzipper gzWrite) {
filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
if fi == nil || !fi.IsDir() {
// not existing root-directory. we don't crash or exit here, the
// operator might create it later on and trigger a rescan.
if fi == nil && root == path {
log.Printf("warning: not existent root directory %q\n", root)
}
return nil
}
// skip cache-directory
if strings.HasPrefix(path, cache) {
return filepath.SkipDir
}
var (
now = time.Now()
relPath, _ = filepath.Rel(root, path)
cachePath, _ = filepath.Abs(filepath.Join(cache, relPath))
scanner = packageScanner{
cache: cachePath,
doMD5: doMD5,
doSHA1: doSHA1,
}
)
if err = scanner.scan(path, nworkers); err != nil {
log.Printf("error: %v", err)
return nil
}
log.Printf("done building index for %q", path)
log.Printf("time to parse %d packages in %q: %s\n",
scanner.packages.Len(), path, time.Since(now))
//
// write the package index
//
var indexName = filepath.Join(cachePath, "Packages")
var indexNameGz = indexName + ".gz"
var indexNameStamps = indexName + ".stamps"
var packagesFile, packagesFileGz, packagesFileStamps *os.File
if packagesFile, err = ioutil.TempFile(cachePath, "Packages"); err != nil {
log.Printf("error: can't create the Packages file in %q: %v\n", cachePath, err)
return nil
}
defer packagesFile.Close()
if packagesFileGz, err = os.Create(packagesFile.Name() + ".gz"); err != nil {
log.Printf("error: can't create the Packages.gz file in %q: %v\n", cachePath, err)
return nil
}
defer packagesFileGz.Close()
if packagesFileStamps, err = os.Create(packagesFile.Name() + ".stamps"); err != nil {
log.Printf("error: can't create the Packages.stamps file in %q: %v\n", cachePath, err)
return nil
}
defer packagesFileStamps.Close()
var index = scanner.packages.String()
io.Copy(packagesFile, strings.NewReader(index))
packagesFile.Sync()
gzipper(packagesFileGz, strings.NewReader(index))
packagesFileGz.Sync()
scanner.packages.StampsTo(packagesFileStamps)
packagesFileStamps.Sync()
os.Remove(indexName)
os.Remove(indexNameGz)
os.Remove(indexNameStamps)
os.Rename(packagesFile.Name(), indexName)
os.Rename(packagesFileGz.Name(), indexNameGz)
os.Rename(packagesFileStamps.Name(), indexNameStamps)
return nil
})
}
type packageScanner struct {
packages *packageIndex
nScanned int64
nCached int64
cache string
doSHA1 bool
doMD5 bool
}
func (s *packageScanner) clear() {
s.packages = &packageIndex{Entries: make(map[string]*ipkArchive)}
}
// scanPackages scans all files in dirPath for packages. if it finds one, it
// checks the cache folder, if a cached version of the meta-information exists
// or if the meta-information is out-of-date. only when needed the meta
// information will be extracted from the packages and stored in the
// cache folder
func (s *packageScanner) scan(dirPath string, nworkers int) error {
var dir, err = os.Open(dirPath)
if err != nil {
return fmt.Errorf("opening %q: %v\n", dirPath, err)
}
defer dir.Close()
if err = s.provideCachePath(); err != nil {
return err
}
var entries []os.FileInfo
if entries, err = dir.Readdir(-1); err != nil {
return fmt.Errorf("reading entries %q: %v\n", dirPath, err)
}
if s.packages == nil {
s.clear()
}
var worker = newWorkerPool(nworkers)
for _, entry := range entries {
if s.skipNonPackage(entry) {
continue
}
if s.fromCache(entry) {
continue
}
worker.Hire()
go s.scanAndCache(path.Join(dirPath, entry.Name()), worker)
}
worker.Wait()
log.Printf("scanned %d packages (fresh %d|%d from cache) in %q.",
s.packages.Len(), s.nScanned, s.nCached, dirPath)
return nil
}
func (s *packageScanner) scanAndCache(filePath string, worker *workerPool) {
var n = time.Now()
defer func() {
worker.Release()
log.Println("processed", filePath, time.Now().Sub(n))
}()
var archive, err = newIpkFromFile(filePath, s.doMD5, s.doSHA1)
if err != nil {
log.Printf("error: %v\n", err)
return
}
s.packages.Add(filepath.Base(filePath), archive)
atomic.AddInt64(&s.nScanned, 1)
if s.cache == "" {
return
}
var cacheName = genCachedControlName(filepath.Base(filePath), s.cache)
if err = ioutil.WriteFile(cacheName, []byte(archive.Control), 0644); err != nil {
log.Println(cacheName, err)
}
}
func (s *packageScanner) fromCache(entry os.FileInfo) bool {
var controlName = genCachedControlName(entry.Name(), s.cache)
var cacheEntry, err = os.Stat(controlName)
if err != nil {
return false
}
if entry.ModTime().Before(cacheEntry.ModTime()) {
var archive, err = newIpkFromCache(entry.Name(), s.cache, s.doMD5, s.doSHA1)
if err != nil {
log.Printf("error: %v\n", err)
return false
}
s.packages.Add(entry.Name(), archive)
s.nCached += 1
return true
}
return false
}
func (s *packageScanner) skipNonPackage(fi os.FileInfo) bool {
return path.Ext(fi.Name()) != ".ipk" || fi.IsDir()
}
func (s *packageScanner) provideCachePath() error {
if s.cache == "" {
return nil
}
if err := os.MkdirAll(s.cache, 0755); err != nil {
return fmt.Errorf("creating cache-folder %q: %v\n", s.cache, err)
}
return nil
}
func genCachedControlName(name, cache string) string {
var cacheName = filepath.Join(cache, name)
return cacheName + ".control"
}