fixed file checker

This commit is contained in:
2026-03-30 14:06:58 +02:00
parent e444a9b7ff
commit 856ea529a7
5 changed files with 78 additions and 6 deletions
+10 -2
View File
@@ -6,6 +6,7 @@ package fileoper
import ( import (
"context" "context"
"net/http" "net/http"
"path/filepath"
"mstore/pkg/descr" "mstore/pkg/descr"
) )
@@ -21,9 +22,11 @@ type CheckFilesResult struct {
func (oper *Operator) CheckFiles(ctx context.Context, operatorID string, params *CheckFilesParams) (int, *CheckFilesResult, error) { func (oper *Operator) CheckFiles(ctx context.Context, operatorID string, params *CheckFilesParams) (int, *CheckFilesResult, error) {
var code int var code int
res := &CheckFilesResult{} res := &CheckFilesResult{
Files: make([]descr.File, 0),
}
var err error var err error
code = http.StatusOK
// Check existing and size // Check existing and size
files, err := oper.listFiles(ctx, params.PathType, params.Path) files, err := oper.listFiles(ctx, params.PathType, params.Path)
if err != nil { if err != nil {
@@ -36,7 +39,9 @@ func (oper *Operator) CheckFiles(ctx context.Context, operatorID string, params
code := http.StatusInternalServerError code := http.StatusInternalServerError
return code, res, err return code, res, err
} }
fullpath := filepath.Join(file.Collection, file.Name)
if !exists { if !exists {
oper.logg.Warningf("File not exists: %s", fullpath)
res.Files = append(res.Files, file) res.Files = append(res.Files, file)
err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name) err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name)
if err != nil { if err != nil {
@@ -45,6 +50,7 @@ func (oper *Operator) CheckFiles(ctx context.Context, operatorID string, params
} }
} }
if size != file.Size { if size != file.Size {
oper.logg.Warningf("File has incorrect size: %s", fullpath)
res.Files = append(res.Files, file) res.Files = append(res.Files, file)
err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name) err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name)
if err != nil { if err != nil {
@@ -70,7 +76,9 @@ func (oper *Operator) CheckFiles(ctx context.Context, operatorID string, params
code := http.StatusInternalServerError code := http.StatusInternalServerError
return code, res, err return code, res, err
} }
fullpath := filepath.Join(file.Collection, file.Name)
if sum != file.Checksum { if sum != file.Checksum {
oper.logg.Warningf("File has incorrect digest: %s", fullpath)
res.Files = append(res.Files, file) res.Files = append(res.Files, file)
err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name) err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name)
if err != nil { if err != nil {
+1 -2
View File
@@ -60,6 +60,7 @@ func NewServer() (*Server, error) {
var err error var err error
srv := &Server{} srv := &Server{}
srv.logg = logger.NewLoggerWithSubject("server") srv.logg = logger.NewLoggerWithSubject("server")
srv.ctx, srv.cancel = context.WithCancel(context.Background())
return srv, err return srv, err
} }
@@ -425,9 +426,7 @@ func (srv *Server) Run() error {
} }
srv.logg.Infof("Server run with user: %s", usr.Username) srv.logg.Infof("Server run with user: %s", usr.Username)
srv.ctx, srv.cancel = context.WithCancel(context.Background())
svcDone := make(chan error, 1) svcDone := make(chan error, 1)
// Service run // Service run
srv.logg.Infof("Start service") srv.logg.Infof("Start service")
startService := func(svc *service.Service, done chan error) { startService := func(svc *service.Service, done chan error) {
+65
View File
@@ -0,0 +1,65 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
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
}
+1 -1
View File
@@ -131,7 +131,7 @@ func (util *FileUtil) MakeFileCmds() *cobra.Command {
Use: "check [user:pass@]hostname[:port]/catalog", Use: "check [user:pass@]hostname[:port]/catalog",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Short: "Ckeck files in storage", 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.Detail, "detail", "D", false, "Show detail file information")
checkFilesCmd.Flags().BoolVarP(&util.checkFilesParams.Prefix, "prefix", "P", true, "Use path as collection path prefix") checkFilesCmd.Flags().BoolVarP(&util.checkFilesParams.Prefix, "prefix", "P", true, "Use path as collection path prefix")
+1 -1
View File
@@ -19,7 +19,7 @@ func (cli *Client) CheckFiles(ctx context.Context, rawpath string) ([]byte, erro
if err != nil { if err != nil {
return list, err return list, err
} }
uri := ref.FilesEP() uri := ref.CheckEP()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil { if err != nil {