Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
laktak committed Aug 20, 2024
1 parent 4bbd7b4 commit 181b3d8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (context *Context) Start(pathList []string) {
for i := 0; i < context.NumWorkers; i++ {
go func(id int) {
defer wg.Done()
context.RunWorker(id)
context.runWorker(id)
}(i)
}
go func() {
Expand Down
2 changes: 1 addition & 1 deletion hashfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Hashfile(path string, hashAlgo string, perfMonBytes func(int64)) (string, e
return hex.EncodeToString(h.Sum(nil)), nil
}

func HashMd5(data []byte) string {
func hashMd5(data []byte) string {
h := md5.New()
h.Write(data)
return hex.EncodeToString(h.Sum(nil))
Expand Down
44 changes: 22 additions & 22 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@ var (
algoMd5 = "md5"
)

type IdxInfo struct {
type idxInfo struct {
ModTime int64 `json:"mod"`
Algo *string `json:"a,omitempty"`
Hash *string `json:"h,omitempty"`
LegacyHash *string `json:"md5,omitempty"`
}

type IndexFile struct {
type indexFile struct {
V int `json:"v"`
// IdxRaw -> map[string]IdxInfo
// IdxRaw -> map[string]idxInfo
IdxRaw json.RawMessage `json:"idx"`
IdxHash string `json:"idx_hash"`
}

type IdxInfo1 struct {
type idxInfo1 struct {
ModTime int64 `json:"mod"`
Hash string `json:"md5"`
}

type IndexFile1 struct {
Data map[string]IdxInfo1 `json:"data"`
type indexFile1 struct {
Data map[string]idxInfo1 `json:"data"`
}

type Index struct {
context *Context
path string
files []string
cur map[string]IdxInfo
new map[string]IdxInfo
cur map[string]idxInfo
new map[string]idxInfo
modified bool
readonly bool
}
Expand All @@ -50,8 +50,8 @@ func NewIndex(context *Context, path string, files []string, readonly bool) *Ind
context: context,
path: path,
files: files,
cur: make(map[string]IdxInfo),
new: make(map[string]IdxInfo),
cur: make(map[string]idxInfo),
new: make(map[string]idxInfo),
readonly: readonly,
}
}
Expand Down Expand Up @@ -80,13 +80,13 @@ func (i *Index) calcHashes(ignore *Ignore) {
}

var err error
var info *IdxInfo
var info *idxInfo
algo := i.context.HashAlgo
if val, ok := i.cur[name]; ok {
// existing
if val.LegacyHash != nil {
// convert from py1 to new format
val = IdxInfo{
val = idxInfo{
ModTime: val.ModTime,
Algo: &algoMd5,
Hash: val.LegacyHash,
Expand All @@ -99,7 +99,7 @@ func (i *Index) calcHashes(ignore *Ignore) {
info, err = i.calcFile(name, algo)
} else {
if i.readonly {
info = &IdxInfo{Algo: &algo}
info = &idxInfo{Algo: &algo}
} else {
info, err = i.calcFile(name, algo)
}
Expand Down Expand Up @@ -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)
info, _ := os.Stat(path)
mtime := int64(info.ModTime().UnixNano() / 1e6)
Expand All @@ -164,7 +164,7 @@ func (i *Index) calcFile(name string, a string) (*IdxInfo, error) {
return nil, err
}
i.context.perfMonFiles(1)
return &IdxInfo{
return &idxInfo{
ModTime: mtime,
Algo: &a,
Hash: &h,
Expand All @@ -181,10 +181,10 @@ func (i *Index) save() (bool, error) {
if err != nil {
return false, err
}
data := IndexFile{
data := indexFile{
V: VERSION,
IdxRaw: text,
IdxHash: HashMd5(text),
IdxHash: hashMd5(text),
}

file, err := json.Marshal(data)
Expand Down Expand Up @@ -214,7 +214,7 @@ func (i *Index) load() error {
if err != nil {
return err
}
var data IndexFile
var data indexFile
err = json.Unmarshal(file, &data)
if err != nil {
return err
Expand All @@ -225,22 +225,22 @@ func (i *Index) load() error {
return err
}
text := data.IdxRaw
if data.IdxHash != HashMd5(text) {
if data.IdxHash != hashMd5(text) {
// old versions may have saved the JSON encoded with extra spaces
text, _ = json.Marshal(data.IdxRaw)
} else {
}
if data.IdxHash != HashMd5(text) {
if data.IdxHash != hashMd5(text) {
i.setMod(true)
i.logFile(STATUS_ERR_IDX, i.getIndexFilepath())
}
} else {
var data1 IndexFile1
var data1 indexFile1
json.Unmarshal(file, &data1)
if data1.Data != nil {
// convert from js to new format
for name, item := range data1.Data {
i.cur[name] = IdxInfo{
i.cur[name] = idxInfo{
ModTime: item.ModTime,
Algo: &algoMd5,
Hash: &item.Hash,
Expand Down
2 changes: 1 addition & 1 deletion worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type WorkItem struct {
ignore *Ignore
}

func (context *Context) RunWorker(id int) {
func (context *Context) runWorker(id int) {
for {
item := <-context.WorkQueue
if item == nil {
Expand Down

0 comments on commit 181b3d8

Please sign in to comment.