Skip to content

Commit cdfbd78

Browse files
committed
Allow reading FileInfo from a dummy file instead of the file itself
1 parent 1f0ecd9 commit cdfbd78

4 files changed

Lines changed: 91 additions & 16 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ DisallowRedirects | Disable any mirror trying to do an HTTP redirect
102102
WeightDistributionRange | Multiplier of the distance to the first mirror to find other possible mirrors in order to distribute the load
103103
DisableOnMissingFile | Disable a mirror if an advertised file on rsync/ftp appears to be missing on HTTP
104104
MaxLinkHeaders | Amount of backup mirror locations returned in HTTP headers
105+
DummyFiles | Allows reading file information from a dummy json file. This Allows saving storage on the host.
105106
Fallbacks | A list of possible mirrors to use as fallback if a request fails or if the database is unreachable. **These mirrors are not tracked by mirrorbits.** It is assumed they have all the files available in the local repository.
106107

107108
## Running

config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var (
3535
CheckInterval: 1,
3636
RepositoryScanInterval: 5,
3737
MaxLinkHeaders: 10,
38+
DummyFiles: false,
3839
Hashes: hashing{
3940
SHA1: true,
4041
SHA256: false,
@@ -70,6 +71,7 @@ type Configuration struct {
7071
CheckInterval int `yaml:"CheckInterval"`
7172
RepositoryScanInterval int `yaml:"RepositoryScanInterval"`
7273
MaxLinkHeaders int `yaml:"MaxLinkHeaders"`
74+
DummyFiles bool `yaml:"DummyFiles"`
7375
Hashes hashing `yaml:"Hashes"`
7476
DisallowRedirects bool `yaml:"DisallowRedirects"`
7577
WeightDistributionRange float32 `yaml:"WeightDistributionRange"`

mirrorbits.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ ConcurrentSync: 5
2121
ScanInterval: 30
2222
CheckInterval: 1
2323
RepositoryScanInterval: 5
24+
DummyFiles: false
2425
Hashes:
2526
SHA256: On
2627
SHA1: Off

scan/scan.go

Lines changed: 87 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package scan
55

66
import (
7+
"encoding/json"
78
"errors"
89
"fmt"
910
"os"
@@ -62,6 +63,14 @@ type scan struct {
6263
count uint
6364
}
6465

66+
type DummyFile struct {
67+
Size int64 `json:"Size"`
68+
ModTime string `json:"ModTime"`
69+
Sha1 string `json:"Sha1"`
70+
Sha256 string `json:"Sha256"`
71+
Md5 string `json:"Md5"`
72+
}
73+
6574
// IsScanning returns true is a scan is already in progress for the given mirror
6675
func IsScanning(conn redis.Conn, identifier string) (bool, error) {
6776
return redis.Bool(conn.Do("EXISTS", fmt.Sprintf("SCANNING_%s", identifier)))
@@ -229,6 +238,7 @@ func (s *scan) setLastSync(conn redis.Conn, identifier string, successful bool)
229238
}
230239

231240
type sourcescanner struct {
241+
dummyFile bool
232242
}
233243

234244
// Walk inside the source/reference repository
@@ -237,11 +247,65 @@ func (s *sourcescanner) walkSource(conn redis.Conn, path string, f os.FileInfo,
237247
return nil, nil
238248
}
239249

250+
var dfData DummyFile
251+
dummyFile := s.dummyFile
252+
240253
d := new(filedata)
241254
d.path = path[len(GetConfig().Repository):]
242-
d.size = f.Size()
243-
d.modTime = f.ModTime()
244255

256+
if dummyFile {
257+
// var err String
258+
// fi, _ := os.Stat(d.path)
259+
// if fi.Size() > 1048576 {
260+
// err = "Filesize too big"
261+
// goto skip1
262+
// }
263+
// raw, err := ioutil.ReadFile(path)
264+
// skip1:
265+
// err = json.Unmarshal(raw, &c)
266+
file, err := os.Open(path)
267+
if err != nil {
268+
log.Errorf(err.Error())
269+
}
270+
dec := json.NewDecoder(file)
271+
// read open bracket, when there is none, the file is not a json.
272+
// Fallback to normal mode.
273+
_ , err = dec.Token()
274+
if err != nil {
275+
goto skipdec
276+
}
277+
err = dec.Decode(&dfData)
278+
skipdec:
279+
if err != nil {
280+
log.Errorf("Failed to read file: %s", err.Error())
281+
dummyFile = false
282+
d.size = f.Size()
283+
d.modTime = f.ModTime()
284+
goto skip
285+
}
286+
287+
// var c []DummyFile
288+
// err = json.Unmarshal(raw, &c)
289+
// if err != nil {
290+
// log.Errorf("error decoding json: %v", err)
291+
// if e, ok := err.(*json.SyntaxError); ok {
292+
// log.Errorf("syntax error at byte offset %d", e.Offset)
293+
// }
294+
// log.Errorf("json response: %q", c)
295+
// return nil, err
296+
// }
297+
298+
d.size = dfData.Size
299+
d.modTime, err = time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", dfData.ModTime)
300+
if err != nil {
301+
log.Errorf(err.Error())
302+
}
303+
} else {
304+
d.size = f.Size()
305+
d.modTime = f.ModTime()
306+
}
307+
308+
skip:
245309
// Get the previous file properties
246310
properties, err := redis.Strings(conn.Do("HMGET", fmt.Sprintf("FILE_%s", d.path), "size", "modTime", "sha1", "sha256", "md5"))
247311
if err != nil && err != redis.ErrNil {
@@ -263,21 +327,27 @@ func (s *sourcescanner) walkSource(conn redis.Conn, path string, f os.FileInfo,
263327
(GetConfig().Hashes.MD5 && len(md5) == 0)
264328

265329
if rehash || size != d.size || !modTime.Equal(d.modTime) {
266-
h, err := filesystem.HashFile(GetConfig().Repository + d.path)
267-
if err != nil {
268-
log.Warningf("%s: hashing failed: %s", d.path, err.Error())
330+
if dummyFile {
331+
d.sha1 = dfData.Sha1
332+
d.sha256 = dfData.Sha256
333+
d.md5 = dfData.Md5
269334
} else {
270-
d.sha1 = h.Sha1
271-
d.sha256 = h.Sha256
272-
d.md5 = h.Md5
273-
if len(d.sha1) > 0 {
274-
log.Infof("%s: SHA1 %s", d.path, d.sha1)
275-
}
276-
if len(d.sha256) > 0 {
277-
log.Infof("%s: SHA256 %s", d.path, d.sha256)
278-
}
279-
if len(d.md5) > 0 {
280-
log.Infof("%s: MD5 %s", d.path, d.md5)
335+
h, err := filesystem.HashFile(GetConfig().Repository + d.path)
336+
if err != nil {
337+
log.Warningf("%s: hashing failed: %s", d.path, err.Error())
338+
} else {
339+
d.sha1 = h.Sha1
340+
d.sha256 = h.Sha256
341+
d.md5 = h.Md5
342+
if len(d.sha1) > 0 {
343+
log.Infof("%s: SHA1 %s", d.path, d.sha1)
344+
}
345+
if len(d.sha256) > 0 {
346+
log.Infof("%s: SHA256 %s", d.path, d.sha256)
347+
}
348+
if len(d.md5) > 0 {
349+
log.Infof("%s: MD5 %s", d.path, d.md5)
350+
}
281351
}
282352
}
283353
} else {
@@ -292,6 +362,7 @@ func (s *sourcescanner) walkSource(conn redis.Conn, path string, f os.FileInfo,
292362
// ScanSource starts a scan of the local repository
293363
func ScanSource(r *database.Redis, forceRehash bool, stop chan bool) (err error) {
294364
s := &sourcescanner{}
365+
s.dummyFile = GetConfig().DummyFiles
295366

296367
conn := r.Get()
297368
defer conn.Close()

0 commit comments

Comments
 (0)