From 0aff0d529eb948dbbdab122c7f92fa5ee7f0f000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=98=AD?= <81747598+lan-yonghui@users.noreply.github.com> Date: Fri, 15 May 2026 11:25:16 +0800 Subject: [PATCH] fix: improve error handling and refactor file history content retrieval (#12736) --- agent/app/service/file.go | 44 +++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/agent/app/service/file.go b/agent/app/service/file.go index 5f05ef873..f66101691 100644 --- a/agent/app/service/file.go +++ b/agent/app/service/file.go @@ -810,13 +810,12 @@ func (f *FileService) ChangeName(req request.FileRename) error { return buserr.New("ErrInvalidChar") } fo := files.NewFileOp() - info, _ := files.NewFileInfo(files.FileOption{Path: req.OldName, Expand: false}) - content, shouldRecordHistory := readEditableFileHistoryContent(req.OldName, info) + content, mode, shouldRecordHistory := readEditableFileHistoryContent(req.OldName) if err := fo.Rename(req.OldName, req.NewName); err != nil { return err } if shouldRecordHistory { - if histErr := historyService.RecordOperation(fileHistoryOpRename, req.OldName, content, info.FileMode, req.OldName, req.NewName); histErr != nil { + if histErr := historyService.RecordOperation(fileHistoryOpRename, req.OldName, content, mode, req.OldName, req.NewName); histErr != nil { global.LOG.Warnf("record file rename history failed for %s: %v", req.OldName, histErr) } } @@ -848,19 +847,13 @@ func (f *FileService) MvFile(m request.FileMove) error { mode os.FileMode record bool } - snapshots := make([]moveSnapshot, 0, len(m.OldPaths)) - for _, oldPath := range m.OldPaths { - mode := os.FileMode(0640) - record := false - var content []byte - if info, err := files.NewFileInfo(files.FileOption{Path: oldPath, Expand: false}); err == nil { - mode = info.FileMode - content, record = readEditableFileHistoryContent(oldPath, info) - } - snapshots = append(snapshots, moveSnapshot{path: oldPath, content: content, mode: mode, record: record}) - } var errs []error if m.Type == "cut" { + snapshots := make([]moveSnapshot, 0, len(m.OldPaths)) + for _, oldPath := range m.OldPaths { + content, mode, record := readEditableFileHistoryContent(oldPath) + snapshots = append(snapshots, moveSnapshot{path: oldPath, content: content, mode: mode, record: record}) + } if len(m.CoverPaths) > 0 { for _, src := range m.CoverPaths { if err := fo.CopyAndReName(src, m.NewPath, "", true); err != nil { @@ -909,32 +902,37 @@ func (f *FileService) MvFile(m request.FileMove) error { return nil } -func readEditableFileHistoryContent(filePath string, info *files.FileInfo) ([]byte, bool) { - if info == nil || info.IsDir || files.IsBlockDevice(info.FileMode) || info.Size > fileHistorySnapshotMaxSize { - return nil, false +func readEditableFileHistoryContent(filePath string) ([]byte, os.FileMode, bool) { + info, err := os.Lstat(filePath) + if err != nil { + return nil, 0640, false + } + mode := info.Mode() + if mode.IsDir() || mode&os.ModeSymlink != 0 || !mode.IsRegular() || files.IsBlockDevice(mode) || info.Size() > fileHistorySnapshotMaxSize { + return nil, mode, false } file, err := os.Open(filePath) if err != nil { - return nil, false + return nil, mode, false } defer file.Close() headBuf := make([]byte, 1024) n, err := file.Read(headBuf) if err != nil && err != io.EOF { - return nil, false + return nil, mode, false } if n > 0 && files.DetectBinary(headBuf[:n]) { - return nil, false + return nil, mode, false } if _, err := file.Seek(0, 0); err != nil { - return nil, false + return nil, mode, false } content, err := io.ReadAll(io.LimitReader(file, fileHistorySnapshotMaxSize+1)) if err != nil || int64(len(content)) > fileHistorySnapshotMaxSize { - return nil, false + return nil, mode, false } - return content, true + return content, mode, true } func buildHistoryMoveTargetPath(dst, name, sourcePath string, sourceCount int) string {