Skip to content

Commit 3e316c2

Browse files
committed
Allow reading FileInfo from a dummy file instead of the file itself
1 parent e3fef5b commit 3e316c2

File tree

4 files changed

+69
-16
lines changed

4 files changed

+69
-16
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ DisallowRedirects | Disable any mirror trying to do an HTTP redirect
101101
WeightDistributionRange | Multiplier of the distance to the first mirror to find other possible mirrors in order to distribute the load
102102
DisableOnMissingFile | Disable a mirror if an advertised file on rsync/ftp appears to be missing on HTTP
103103
MaxLinkHeaders | Amount of backup mirror locations returned in HTTP headers
104+
DummyFiles | Allows reading file information from a dummy json file. This Allows saving storage on the host.
104105
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.
105106

106107
## 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
SHA1: On
2627
SHA256: Off

scan/scan.go

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
package scan
55

66
import (
7+
"encoding/json"
78
"errors"
89
"fmt"
10+
"io/ioutil"
911
"os"
1012
"path/filepath"
1113
"strconv"
@@ -62,6 +64,14 @@ type scan struct {
6264
count uint
6365
}
6466

67+
type DummyFile struct {
68+
Size int64 `json:"Size"`
69+
ModTime string `json:"ModTime"`
70+
Sha1 string `json:"Sha1"`
71+
Sha256 string `json:"Sha256"`
72+
Md5 string `json:"Md5"`
73+
}
74+
6575
// IsScanning returns true is a scan is already in progress for the given mirror
6676
func IsScanning(conn redis.Conn, identifier string) (bool, error) {
6777
return redis.Bool(conn.Do("EXISTS", fmt.Sprintf("SCANNING_%s", identifier)))
@@ -237,10 +247,43 @@ func (s *sourcescanner) walkSource(conn redis.Conn, path string, f os.FileInfo,
237247
return nil, nil
238248
}
239249

250+
var dfData DummyFile
251+
dummyFile := GetConfig().DummyFiles
252+
240253
d := new(filedata)
241254
d.path = path[len(GetConfig().Repository):]
242-
d.size = f.Size()
243-
d.modTime = f.ModTime()
255+
256+
if dummyFile {
257+
raw, err := ioutil.ReadFile(path)
258+
if err != nil {
259+
fmt.Println(err.Error())
260+
fmt.Println("Failed to read file, DELETING!")
261+
os.Remove(path)
262+
fmt.Println("==> done deleting file")
263+
return nil, err
264+
}
265+
266+
var c []DummyFile
267+
err = json.Unmarshal(raw, &c)
268+
if err != nil {
269+
log.Errorf("error decoding json: %v", err)
270+
if e, ok := err.(*json.SyntaxError); ok {
271+
log.Errorf("syntax error at byte offset %d", e.Offset)
272+
}
273+
log.Errorf("json response: %q", c)
274+
return nil, err
275+
}
276+
277+
dfData = c[0]
278+
d.size = dfData.Size
279+
d.modTime, err = time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", dfData.ModTime)
280+
if err != nil {
281+
fmt.Println(err)
282+
}
283+
} else {
284+
d.size = f.Size()
285+
d.modTime = f.ModTime()
286+
}
244287

245288
// Get the previous file properties
246289
properties, err := redis.Strings(conn.Do("HMGET", fmt.Sprintf("FILE_%s", d.path), "size", "modTime", "sha1", "sha256", "md5"))
@@ -263,21 +306,27 @@ func (s *sourcescanner) walkSource(conn redis.Conn, path string, f os.FileInfo,
263306
(GetConfig().Hashes.MD5 && len(md5) == 0)
264307

265308
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())
309+
if dummyFile {
310+
d.sha1 = dfData.Sha1
311+
d.sha256 = dfData.Sha256
312+
d.md5 = dfData.Md5
269313
} 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)
314+
h, err := filesystem.HashFile(GetConfig().Repository + d.path)
315+
if err != nil {
316+
log.Warningf("%s: hashing failed: %s", d.path, err.Error())
317+
} else {
318+
d.sha1 = h.Sha1
319+
d.sha256 = h.Sha256
320+
d.md5 = h.Md5
321+
if len(d.sha1) > 0 {
322+
log.Infof("%s: SHA1 %s", d.path, d.sha1)
323+
}
324+
if len(d.sha256) > 0 {
325+
log.Infof("%s: SHA256 %s", d.path, d.sha256)
326+
}
327+
if len(d.md5) > 0 {
328+
log.Infof("%s: MD5 %s", d.path, d.md5)
329+
}
281330
}
282331
}
283332
} else {

0 commit comments

Comments
 (0)