refactor
This commit is contained in:
parent
4bbd7b421e
commit
181b3d8c9a
@ -77,7 +77,7 @@ func (context *Context) Start(pathList []string) {
|
|||||||
for i := 0; i < context.NumWorkers; i++ {
|
for i := 0; i < context.NumWorkers; i++ {
|
||||||
go func(id int) {
|
go func(id int) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
context.RunWorker(id)
|
context.runWorker(id)
|
||||||
}(i)
|
}(i)
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -50,7 +50,7 @@ func Hashfile(path string, hashAlgo string, perfMonBytes func(int64)) (string, e
|
|||||||
return hex.EncodeToString(h.Sum(nil)), nil
|
return hex.EncodeToString(h.Sum(nil)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func HashMd5(data []byte) string {
|
func hashMd5(data []byte) string {
|
||||||
h := md5.New()
|
h := md5.New()
|
||||||
h.Write(data)
|
h.Write(data)
|
||||||
return hex.EncodeToString(h.Sum(nil))
|
return hex.EncodeToString(h.Sum(nil))
|
||||||
|
44
index.go
44
index.go
@ -12,35 +12,35 @@ var (
|
|||||||
algoMd5 = "md5"
|
algoMd5 = "md5"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IdxInfo struct {
|
type idxInfo struct {
|
||||||
ModTime int64 `json:"mod"`
|
ModTime int64 `json:"mod"`
|
||||||
Algo *string `json:"a,omitempty"`
|
Algo *string `json:"a,omitempty"`
|
||||||
Hash *string `json:"h,omitempty"`
|
Hash *string `json:"h,omitempty"`
|
||||||
LegacyHash *string `json:"md5,omitempty"`
|
LegacyHash *string `json:"md5,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type IndexFile struct {
|
type indexFile struct {
|
||||||
V int `json:"v"`
|
V int `json:"v"`
|
||||||
// IdxRaw -> map[string]IdxInfo
|
// IdxRaw -> map[string]idxInfo
|
||||||
IdxRaw json.RawMessage `json:"idx"`
|
IdxRaw json.RawMessage `json:"idx"`
|
||||||
IdxHash string `json:"idx_hash"`
|
IdxHash string `json:"idx_hash"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type IdxInfo1 struct {
|
type idxInfo1 struct {
|
||||||
ModTime int64 `json:"mod"`
|
ModTime int64 `json:"mod"`
|
||||||
Hash string `json:"md5"`
|
Hash string `json:"md5"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type IndexFile1 struct {
|
type indexFile1 struct {
|
||||||
Data map[string]IdxInfo1 `json:"data"`
|
Data map[string]idxInfo1 `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Index struct {
|
type Index struct {
|
||||||
context *Context
|
context *Context
|
||||||
path string
|
path string
|
||||||
files []string
|
files []string
|
||||||
cur map[string]IdxInfo
|
cur map[string]idxInfo
|
||||||
new map[string]IdxInfo
|
new map[string]idxInfo
|
||||||
modified bool
|
modified bool
|
||||||
readonly bool
|
readonly bool
|
||||||
}
|
}
|
||||||
@ -50,8 +50,8 @@ func NewIndex(context *Context, path string, files []string, readonly bool) *Ind
|
|||||||
context: context,
|
context: context,
|
||||||
path: path,
|
path: path,
|
||||||
files: files,
|
files: files,
|
||||||
cur: make(map[string]IdxInfo),
|
cur: make(map[string]idxInfo),
|
||||||
new: make(map[string]IdxInfo),
|
new: make(map[string]idxInfo),
|
||||||
readonly: readonly,
|
readonly: readonly,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -80,13 +80,13 @@ func (i *Index) calcHashes(ignore *Ignore) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
var info *IdxInfo
|
var info *idxInfo
|
||||||
algo := i.context.HashAlgo
|
algo := i.context.HashAlgo
|
||||||
if val, ok := i.cur[name]; ok {
|
if val, ok := i.cur[name]; ok {
|
||||||
// existing
|
// existing
|
||||||
if val.LegacyHash != nil {
|
if val.LegacyHash != nil {
|
||||||
// convert from py1 to new format
|
// convert from py1 to new format
|
||||||
val = IdxInfo{
|
val = idxInfo{
|
||||||
ModTime: val.ModTime,
|
ModTime: val.ModTime,
|
||||||
Algo: &algoMd5,
|
Algo: &algoMd5,
|
||||||
Hash: val.LegacyHash,
|
Hash: val.LegacyHash,
|
||||||
@ -99,7 +99,7 @@ func (i *Index) calcHashes(ignore *Ignore) {
|
|||||||
info, err = i.calcFile(name, algo)
|
info, err = i.calcFile(name, algo)
|
||||||
} else {
|
} else {
|
||||||
if i.readonly {
|
if i.readonly {
|
||||||
info = &IdxInfo{Algo: &algo}
|
info = &idxInfo{Algo: &algo}
|
||||||
} else {
|
} else {
|
||||||
info, err = i.calcFile(name, algo)
|
info, err = i.calcFile(name, algo)
|
||||||
}
|
}
|
||||||
@ -155,7 +155,7 @@ func (i *Index) checkFix(forceUpdateDmg bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Index) calcFile(name string, a string) (*IdxInfo, error) {
|
func (i *Index) calcFile(name string, a string) (*idxInfo, error) {
|
||||||
path := filepath.Join(i.path, name)
|
path := filepath.Join(i.path, name)
|
||||||
info, _ := os.Stat(path)
|
info, _ := os.Stat(path)
|
||||||
mtime := int64(info.ModTime().UnixNano() / 1e6)
|
mtime := int64(info.ModTime().UnixNano() / 1e6)
|
||||||
@ -164,7 +164,7 @@ func (i *Index) calcFile(name string, a string) (*IdxInfo, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
i.context.perfMonFiles(1)
|
i.context.perfMonFiles(1)
|
||||||
return &IdxInfo{
|
return &idxInfo{
|
||||||
ModTime: mtime,
|
ModTime: mtime,
|
||||||
Algo: &a,
|
Algo: &a,
|
||||||
Hash: &h,
|
Hash: &h,
|
||||||
@ -181,10 +181,10 @@ func (i *Index) save() (bool, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
data := IndexFile{
|
data := indexFile{
|
||||||
V: VERSION,
|
V: VERSION,
|
||||||
IdxRaw: text,
|
IdxRaw: text,
|
||||||
IdxHash: HashMd5(text),
|
IdxHash: hashMd5(text),
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := json.Marshal(data)
|
file, err := json.Marshal(data)
|
||||||
@ -214,7 +214,7 @@ func (i *Index) load() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var data IndexFile
|
var data indexFile
|
||||||
err = json.Unmarshal(file, &data)
|
err = json.Unmarshal(file, &data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -225,22 +225,22 @@ func (i *Index) load() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
text := data.IdxRaw
|
text := data.IdxRaw
|
||||||
if data.IdxHash != HashMd5(text) {
|
if data.IdxHash != hashMd5(text) {
|
||||||
// old versions may have saved the JSON encoded with extra spaces
|
// old versions may have saved the JSON encoded with extra spaces
|
||||||
text, _ = json.Marshal(data.IdxRaw)
|
text, _ = json.Marshal(data.IdxRaw)
|
||||||
} else {
|
} else {
|
||||||
}
|
}
|
||||||
if data.IdxHash != HashMd5(text) {
|
if data.IdxHash != hashMd5(text) {
|
||||||
i.setMod(true)
|
i.setMod(true)
|
||||||
i.logFile(STATUS_ERR_IDX, i.getIndexFilepath())
|
i.logFile(STATUS_ERR_IDX, i.getIndexFilepath())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var data1 IndexFile1
|
var data1 indexFile1
|
||||||
json.Unmarshal(file, &data1)
|
json.Unmarshal(file, &data1)
|
||||||
if data1.Data != nil {
|
if data1.Data != nil {
|
||||||
// convert from js to new format
|
// convert from js to new format
|
||||||
for name, item := range data1.Data {
|
for name, item := range data1.Data {
|
||||||
i.cur[name] = IdxInfo{
|
i.cur[name] = idxInfo{
|
||||||
ModTime: item.ModTime,
|
ModTime: item.ModTime,
|
||||||
Algo: &algoMd5,
|
Algo: &algoMd5,
|
||||||
Hash: &item.Hash,
|
Hash: &item.Hash,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user