Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Oct 27, 2022
1 parent 25bb897 commit 2dc80c5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
7 changes: 5 additions & 2 deletions client/file/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ func Sync(versionDB *tmdb.Store, remoteGrpcUrl, remoteUrl, remoteWsUrl, rootDir
pairs, err := client.DecodeData(data.Data)
fmt.Printf("mm-pairs: %+v, %+v\n", len(pairs), err)
if err == nil {
versionDB.PutAtVersion(int64(data.BlockNum), pairs)
if err = versionDB.PutAtVersion(int64(data.BlockNum), pairs); err != nil {
fmt.Println("mm-put-at-version-panic")
panic(err)
}
}
case err := <-chErr:
// fail read
Expand All @@ -119,7 +122,7 @@ func Sync(versionDB *tmdb.Store, remoteGrpcUrl, remoteUrl, remoteWsUrl, rootDir
case data := <-chData:
file := GetLocalDataFileName(directory, data.BlockNum)
fmt.Printf("mm-data.BlockNum: %+v\n", data.BlockNum)
if err := os.WriteFile(file, data.Data, 0644); err != nil {
if err := os.WriteFile(file, data.Data, 0600); err != nil {
fmt.Println("mm-WriteFile-panic")
panic(err)
}
Expand Down
15 changes: 9 additions & 6 deletions client/file/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"net/http"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
Expand All @@ -22,7 +23,7 @@ type fileDownloader interface {
type localFileDownloader struct{}

func (d *localFileDownloader) GetData(path string) ([]byte, error) {
data, err := os.ReadFile(path)
data, err := os.ReadFile(filepath.Clean(path))
if err != nil {
if os.IsNotExist(err) {
err = errNotExist
Expand Down Expand Up @@ -114,18 +115,20 @@ func (w *BlockFileWatcher) SetMaxBlockNum(num int) {

func (w *BlockFileWatcher) fetch(blockNum int) error {
path := w.getFilePath(blockNum)
f, err := os.Open(path)
f, err := os.Open(filepath.Clean(path))
if err == nil {
defer f.Close() //nolint
defer func() {
_ = f.Close()
}()
// valid 1st 8 bytes for downloaded file
var bytes [8]byte
if _, err = io.ReadFull(f, bytes[:]); err == nil {
size := binary.BigEndian.Uint64(bytes[:])
if info, err := f.Stat(); err == nil && size+8 == uint64(info.Size()) {
if info, err := f.Stat(); err == nil && size+uint64(8) == uint64(info.Size()) {
return nil
}
}
f.Close() //nolint
_ = f.Close()
}

// download if file not exist
Expand Down Expand Up @@ -203,7 +206,7 @@ func (w *BlockFileWatcher) Start(
}
}
}
for k := range finishedBlockNums {
for k, _ := range finishedBlockNums {
if k <= blockNum {
delete(finishedBlockNums, k)
}
Expand Down
2 changes: 1 addition & 1 deletion client/file/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func setupDirectory(t *testing.T, directory string) func(t *testing.T) {
func setupBlockFiles(directory string, start, end int) {
for i := start; i <= end; i++ {
file := GetLocalDataFileName(directory, i)
os.WriteFile(file, []byte(fmt.Sprint("block", i)), 0644)
os.WriteFile(file, []byte(fmt.Sprint("block", i)), 0600)
}
}

Expand Down

0 comments on commit 2dc80c5

Please sign in to comment.