From 856ea529a7284363ce61a5e6077c13c9c9c6a1cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=BB=D0=B5=D0=B3=20=D0=91=D0=BE=D1=80=D0=BE=D0=B4?= =?UTF-8?q?=D0=B8=D0=BD?= Date: Mon, 30 Mar 2026 14:06:58 +0200 Subject: [PATCH] fixed file checker --- app/fileoper/check.go | 12 +++++- app/server/server.go | 3 +- cmd/mstorectl/filecmd/checkfiles.go | 65 +++++++++++++++++++++++++++++ cmd/mstorectl/filecmd/filecmd.go | 2 +- pkg/filecli/checkfiles.go | 2 +- 5 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 cmd/mstorectl/filecmd/checkfiles.go diff --git a/app/fileoper/check.go b/app/fileoper/check.go index 7d3bcb8..9524f81 100644 --- a/app/fileoper/check.go +++ b/app/fileoper/check.go @@ -6,6 +6,7 @@ package fileoper import ( "context" "net/http" + "path/filepath" "mstore/pkg/descr" ) @@ -21,9 +22,11 @@ type CheckFilesResult struct { func (oper *Operator) CheckFiles(ctx context.Context, operatorID string, params *CheckFilesParams) (int, *CheckFilesResult, error) { var code int - res := &CheckFilesResult{} + res := &CheckFilesResult{ + Files: make([]descr.File, 0), + } var err error - + code = http.StatusOK // Check existing and size files, err := oper.listFiles(ctx, params.PathType, params.Path) if err != nil { @@ -36,7 +39,9 @@ func (oper *Operator) CheckFiles(ctx context.Context, operatorID string, params code := http.StatusInternalServerError return code, res, err } + fullpath := filepath.Join(file.Collection, file.Name) if !exists { + oper.logg.Warningf("File not exists: %s", fullpath) res.Files = append(res.Files, file) err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name) if err != nil { @@ -45,6 +50,7 @@ func (oper *Operator) CheckFiles(ctx context.Context, operatorID string, params } } if size != file.Size { + oper.logg.Warningf("File has incorrect size: %s", fullpath) res.Files = append(res.Files, file) err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name) if err != nil { @@ -70,7 +76,9 @@ func (oper *Operator) CheckFiles(ctx context.Context, operatorID string, params code := http.StatusInternalServerError return code, res, err } + fullpath := filepath.Join(file.Collection, file.Name) if sum != file.Checksum { + oper.logg.Warningf("File has incorrect digest: %s", fullpath) res.Files = append(res.Files, file) err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name) if err != nil { diff --git a/app/server/server.go b/app/server/server.go index 527abcf..4985e59 100644 --- a/app/server/server.go +++ b/app/server/server.go @@ -60,6 +60,7 @@ func NewServer() (*Server, error) { var err error srv := &Server{} srv.logg = logger.NewLoggerWithSubject("server") + srv.ctx, srv.cancel = context.WithCancel(context.Background()) return srv, err } @@ -425,9 +426,7 @@ func (srv *Server) Run() error { } srv.logg.Infof("Server run with user: %s", usr.Username) - srv.ctx, srv.cancel = context.WithCancel(context.Background()) svcDone := make(chan error, 1) - // Service run srv.logg.Infof("Start service") startService := func(svc *service.Service, done chan error) { diff --git a/cmd/mstorectl/filecmd/checkfiles.go b/cmd/mstorectl/filecmd/checkfiles.go new file mode 100644 index 0000000..568530a --- /dev/null +++ b/cmd/mstorectl/filecmd/checkfiles.go @@ -0,0 +1,65 @@ +/* + * Copyright 2026 Oleg Borodin + */ +package filecmd + +import ( + "context" + "encoding/json" + "time" + + "github.com/spf13/cobra" + + "mstore/pkg/descr" + "mstore/pkg/filecli" +) + +// CheckFiles +type CheckFilesParams struct { + Filepath string + Detail bool + Prefix bool + Regexp bool +} + +type CheckFilesResult struct { + Files []descr.File `json:"files,omitempty"` + Filenames []string `json:"filenames,omitempty"` +} + +func (util *FileUtil) CheckFiles(cmd *cobra.Command, args []string) { + util.checkFilesParams.Filepath = args[0] + res, err := util.checkFiles(&util.commonFileParams, &util.checkFilesParams) + printResponse(res, err) +} + +func (util *FileUtil) checkFiles(common *CommonFileParams, params *CheckFilesParams) (*CheckFilesResult, error) { + var err error + res := &CheckFilesResult{} + timeout := time.Duration(common.Timeout) * time.Second + ctx, _ := context.WithTimeout(context.Background(), timeout) + ref, err := filecli.ParsePath(params.Filepath) + if err != nil { + return res, err + } + ref.SetUserinfo(common.Username, common.Password) + ref.PathType(pathType(params.Regexp, params.Prefix)) + mw := filecli.NewBasicAuthMiddleware(ref.Userinfo()) + + cli := filecli.NewClient(nil, mw) + list, err := cli.CheckFiles(ctx, ref.Raw()) + if err != nil { + return res, err + } + files := descr.NewFiles() + err = json.Unmarshal(list, files.ArrayPtr()) + if err != nil { + return res, err + } + if !params.Detail { + res.Filenames = files.List() + } else { + res.Files = files.Array() + } + return res, err +} diff --git a/cmd/mstorectl/filecmd/filecmd.go b/cmd/mstorectl/filecmd/filecmd.go index 8b252e6..ffda573 100644 --- a/cmd/mstorectl/filecmd/filecmd.go +++ b/cmd/mstorectl/filecmd/filecmd.go @@ -131,7 +131,7 @@ func (util *FileUtil) MakeFileCmds() *cobra.Command { Use: "check [user:pass@]hostname[:port]/catalog", Args: cobra.ExactArgs(1), Short: "Ckeck files in storage", - Run: util.ListFiles, + Run: util.CheckFiles, } checkFilesCmd.Flags().BoolVarP(&util.checkFilesParams.Detail, "detail", "D", false, "Show detail file information") checkFilesCmd.Flags().BoolVarP(&util.checkFilesParams.Prefix, "prefix", "P", true, "Use path as collection path prefix") diff --git a/pkg/filecli/checkfiles.go b/pkg/filecli/checkfiles.go index 2df8706..e6c8fef 100644 --- a/pkg/filecli/checkfiles.go +++ b/pkg/filecli/checkfiles.go @@ -19,7 +19,7 @@ func (cli *Client) CheckFiles(ctx context.Context, rawpath string) ([]byte, erro if err != nil { return list, err } - uri := ref.FilesEP() + uri := ref.CheckEP() req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) if err != nil {