diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..4ae0279 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,49 @@ +# golangci-lint configuration + +run: + skip-dirs: + - build + - examples + + timeout: 10m + +issues: + +linters: + enable: + - asciicheck # check for non-ascii characters + - errorlint # enure error wrapping is safely done + - gocritic # check for certain simplifications + - gofmt # ensure code is formatted + - gosec # check for security concerns + - nilerr # ensure errors aren't mishandled + - staticcheck # check for suspicious constructs + - unused # check for unused constructs + +linters-settings: + errcheck: + # report when type assertions aren't checked for errors as in + # a := b.(MyStruct) + # + check-type-assertions: true + + gocritic: + disabled-tags: + - experimental + - opinionated + + disabled-checks: + - ifElseChain + - assignOp + - unlambda + - exitAfterDefer + + gosec: + excludes: + - G404 # checks that random numbers are securely generated + + govet: + enable-all: true + disable: + - shadow + - fieldalignment diff --git a/api/http_api.go b/api/http_api.go index 4067a54..5220f93 100644 --- a/api/http_api.go +++ b/api/http_api.go @@ -96,6 +96,9 @@ func (api *HttpApi) putFileHandler(ctx *fiber.Ctx) error { return api.writeErrResponse(ctx, http.StatusBadRequest, errors.New("got content length < 2")) } f, err := fileHeader.Open() + if err != nil { + return err + } defer f.Close() cmd := &engine.PutFileCmd{} @@ -121,20 +124,21 @@ func (api *HttpApi) downloadFileHandler(ctx *fiber.Ctx) error { return api.writeErrResponse(ctx, http.StatusBadRequest, errors.New("invalid request")) } _, buff, err := api.engine.DownloadFile(fId) - + if err != nil { + return err + } zr := lz4.NewReader(buff) if err := zr.Apply(lz4.ConcurrencyOption(4)); err != nil { return err } - ctx.SendStream(zr) - return err + return ctx.SendStream(zr) } // TODO For browsers use attachment+file name -func (api *HttpApi) downloadFileAttachHandler(ctx *fiber.Ctx) error { - return nil -} +// func (api *HttpApi) downloadFileAttachHandler(ctx *fiber.Ctx) error { +// return nil +// } func (api *HttpApi) getFileMetadata(ctx *fiber.Ctx) error { fIdParam := ctx.Params("fId") diff --git a/build.sh b/build.sh index bfa6fd2..34ee6d5 100644 --- a/build.sh +++ b/build.sh @@ -1,3 +1,5 @@ +golangci-lint run + go build -ldflags "-s -w" -o build/mosaic #env GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" -o build/mosaic.exe #env GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w" -o build/mosaic-darwin diff --git a/engine/cmd_put_file.go b/engine/cmd_put_file.go index 5afac3c..fc0deed 100644 --- a/engine/cmd_put_file.go +++ b/engine/cmd_put_file.go @@ -70,6 +70,9 @@ func (cmd *PutFileCmd) Prepare(engine *Engine) error { // check if file already uploaded checkMeta, err := cmd.engine.db.GetFileMetadataById(fileHash) + if err != nil { + return err + } if checkMeta != nil { cmd.FileMetadata = checkMeta slog.Info("File already exists", "fId", fileHash.String(), "oSize", checkMeta.OriginalFileSize) diff --git a/engine/cmd_upload_err.go b/engine/cmd_upload_err.go index 6b05025..7e4a50f 100644 --- a/engine/cmd_upload_err.go +++ b/engine/cmd_upload_err.go @@ -17,7 +17,9 @@ func (cmd *FileUploadErrCmd) Execute() error { err := cmd.Err slog.Info("Try resolve upload err", "fId", fileMeta.Id.String(), "err", err.Error()) // decrease used space by file - cmd.engine.db.UpdateShardsSize(fileMeta.UsedSpace(), false) + if err := cmd.engine.db.UpdateShardsSize(fileMeta.UsedSpace(), false); err != nil { + return err + } // async free resources on shards go func() { for _, chunk := range fileMeta.Chunks { diff --git a/engine/engine_test.go b/engine/engine_test.go index 08a630e..987ab58 100644 --- a/engine/engine_test.go +++ b/engine/engine_test.go @@ -42,16 +42,6 @@ func buildShardState() map[types.ShardId]uint64 { return shards } -func buildShardState2() map[types.ShardId]uint64 { - shards := map[types.ShardId]uint64{ - 1: 10, - 2: 8, - 3: 12, - 4: 11, - } - return shards -} - func buildShardState3() map[types.ShardId]uint64 { shards := map[types.ShardId]uint64{ 1: 10, diff --git a/engine/file_downloader_test.go b/engine/file_downloader_test.go index be081c4..1b1e0d9 100644 --- a/engine/file_downloader_test.go +++ b/engine/file_downloader_test.go @@ -2,6 +2,7 @@ package engine import ( "bytes" + "context" "fmt" "github.com/pierrec/lz4/v4" "io" @@ -17,7 +18,7 @@ import ( func TestFileUploader(t *testing.T) { database := mosaicdb.NewDatabase("") storage := storage.NewStorage() - engine, err := NewEngine("/tmp/masaic/temp", database, storage, 10) + engine, err := NewEngine(context.Background(), "/tmp/masaic/temp", database, storage, 10) if err != nil { panic(err) } diff --git a/mosaicdb/db.go b/mosaicdb/db.go index 4452da6..749b8f4 100644 --- a/mosaicdb/db.go +++ b/mosaicdb/db.go @@ -33,9 +33,9 @@ func NewDatabase(dbPath string) *Database { slog.Info("Initialize db", "path", dbPath) os.MkdirAll(dbPath, 0774) db, err = pebble.Open(dbPath, &pebble.Options{}) - if err != nil { - panic(err) - } + } + if err != nil { + panic(err) } return &Database{ db: db, diff --git a/utils/utils.go b/utils/utils.go index e13a979..703bb92 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -33,7 +33,9 @@ func RandomString(count int) string { func RandomBytes(count int) []byte { buf := make([]byte, count) - cp.Read(buf) + if _, err := cp.Read(buf); err != nil { + panic(err) + } return buf } @@ -81,6 +83,8 @@ func Btou32(val []byte) uint32 { func HashFrom(data []byte) types.H256 { hasher := blake3.New() - hasher.Write(data) + if _, err := hasher.Write(data); err != nil { + panic(err) + } return hasher.Sum(nil) }