Skip to content

Commit 50a70ef

Browse files
fix: skip large binary file history snapshots (#12683)
#### What this PR does / why we need it? Refs #12681 #### Summary of your change #### Please indicate you've done the following: - [ ] Made sure tests are passing and test coverage is added if needed. - [ ] Made sure commit message follow the rule of [Conventional Commits specification](https://www.conventionalcommits.org/). - [ ] Considered the docs impact and opened a new docs issue or PR with docs changes if needed.
1 parent 75266ef commit 50a70ef

1 file changed

Lines changed: 38 additions & 8 deletions

File tree

agent/app/service/file.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ import (
5050
type FileService struct {
5151
}
5252

53+
const fileHistorySnapshotMaxSize = 10 * 1024 * 1024
54+
5355
type IFileService interface {
5456
GetFileList(op request.FileOption) (response.FileInfo, error)
5557
SearchUploadWithPage(req request.SearchUploadWithPage) (int64, interface{}, error)
@@ -692,11 +694,11 @@ func (f *FileService) ChangeName(req request.FileRename) error {
692694
}
693695
fo := files.NewFileOp()
694696
info, _ := files.NewFileInfo(files.FileOption{Path: req.OldName, Expand: false})
695-
content, _ := os.ReadFile(req.OldName)
697+
content, shouldRecordHistory := readEditableFileHistoryContent(req.OldName, info)
696698
if err := fo.Rename(req.OldName, req.NewName); err != nil {
697699
return err
698700
}
699-
if info != nil && !info.IsDir {
701+
if shouldRecordHistory {
700702
if histErr := historyService.RecordOperation(fileHistoryOpRename, req.OldName, content, info.FileMode, req.OldName, req.NewName); histErr != nil {
701703
global.LOG.Warnf("record file rename history failed for %s: %v", req.OldName, histErr)
702704
}
@@ -727,18 +729,18 @@ func (f *FileService) MvFile(m request.FileMove) error {
727729
path string
728730
content []byte
729731
mode os.FileMode
730-
isDir bool
732+
record bool
731733
}
732734
snapshots := make([]moveSnapshot, 0, len(m.OldPaths))
733735
for _, oldPath := range m.OldPaths {
734-
content, _ := os.ReadFile(oldPath)
735736
mode := os.FileMode(0640)
736-
isDir := false
737+
record := false
738+
var content []byte
737739
if info, err := files.NewFileInfo(files.FileOption{Path: oldPath, Expand: false}); err == nil {
738740
mode = info.FileMode
739-
isDir = info.IsDir
741+
content, record = readEditableFileHistoryContent(oldPath, info)
740742
}
741-
snapshots = append(snapshots, moveSnapshot{path: oldPath, content: content, mode: mode, isDir: isDir})
743+
snapshots = append(snapshots, moveSnapshot{path: oldPath, content: content, mode: mode, record: record})
742744
}
743745
var errs []error
744746
if m.Type == "cut" {
@@ -754,7 +756,7 @@ func (f *FileService) MvFile(m request.FileMove) error {
754756
return err
755757
}
756758
for _, snapshot := range snapshots {
757-
if !snapshot.isDir {
759+
if snapshot.record {
758760
targetPath := buildHistoryMoveTargetPath(m.NewPath, m.Name, snapshot.path, len(m.OldPaths))
759761
if histErr := historyService.RecordOperation(fileHistoryOpMove, snapshot.path, snapshot.content, snapshot.mode, snapshot.path, targetPath); histErr != nil {
760762
global.LOG.Warnf("record file move history failed for %s: %v", snapshot.path, histErr)
@@ -790,6 +792,34 @@ func (f *FileService) MvFile(m request.FileMove) error {
790792
return nil
791793
}
792794

795+
func readEditableFileHistoryContent(filePath string, info *files.FileInfo) ([]byte, bool) {
796+
if info == nil || info.IsDir || files.IsBlockDevice(info.FileMode) || info.Size > fileHistorySnapshotMaxSize {
797+
return nil, false
798+
}
799+
file, err := os.Open(filePath)
800+
if err != nil {
801+
return nil, false
802+
}
803+
defer file.Close()
804+
805+
headBuf := make([]byte, 1024)
806+
n, err := file.Read(headBuf)
807+
if err != nil && err != io.EOF {
808+
return nil, false
809+
}
810+
if n > 0 && files.DetectBinary(headBuf[:n]) {
811+
return nil, false
812+
}
813+
if _, err := file.Seek(0, 0); err != nil {
814+
return nil, false
815+
}
816+
content, err := io.ReadAll(io.LimitReader(file, fileHistorySnapshotMaxSize+1))
817+
if err != nil || int64(len(content)) > fileHistorySnapshotMaxSize {
818+
return nil, false
819+
}
820+
return content, true
821+
}
822+
793823
func buildHistoryMoveTargetPath(dst, name, sourcePath string, sourceCount int) string {
794824
if strings.TrimSpace(dst) == "" {
795825
return sourcePath

0 commit comments

Comments
 (0)