Skip to content

Commit 0d8a3ca

Browse files
committed
feat(file): add overwrite option for file decompression to manage existing entries
1 parent 6d8d3b2 commit 0d8a3ca

21 files changed

Lines changed: 657 additions & 19 deletions

File tree

‎agent/app/dto/request/file.go‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ type FileCompressStopReq struct {
8686
}
8787

8888
type FileDeCompress struct {
89-
Dst string `json:"dst" validate:"required"`
90-
Type string `json:"type" validate:"required"`
91-
Path string `json:"path" validate:"required"`
92-
Secret string `json:"secret"`
93-
TaskID string `json:"taskID"`
89+
Dst string `json:"dst" validate:"required"`
90+
Type string `json:"type" validate:"required"`
91+
Path string `json:"path" validate:"required"`
92+
Secret string `json:"secret"`
93+
TaskID string `json:"taskID"`
94+
Overwrite *bool `json:"overwrite,omitempty" default:"true"`
9495
}
9596

9697
type FileDeCompressStopReq struct {

‎agent/app/service/file.go‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ func (f *FileService) StopDeCompress(taskID string) error {
602602

603603
func (f *FileService) DeCompress(c request.FileDeCompress) error {
604604
fo := files.NewFileOp()
605+
overwrite := c.Overwrite == nil || *c.Overwrite
605606
if c.Type == "tar" && len(c.Secret) != 0 {
606607
c.Type = "tar.gz"
607608
}
@@ -631,7 +632,7 @@ func (f *FileService) DeCompress(c request.FileDeCompress) error {
631632
success := false
632633
defer func() {
633634
_ = os.RemoveAll(tempDst)
634-
if !success && !dstExisted {
635+
if !success && !dstExisted && overwrite {
635636
_ = os.RemoveAll(c.Dst)
636637
}
637638
}()
@@ -641,10 +642,12 @@ func (f *FileService) DeCompress(c request.FileDeCompress) error {
641642
}); err != nil {
642643
return err
643644
}
644-
if err := fo.CreateDir(c.Dst, constant.DirPerm); err != nil {
645-
return err
645+
if overwrite {
646+
if err := fo.CreateDir(c.Dst, constant.DirPerm); err != nil {
647+
return err
648+
}
646649
}
647-
if err := copyDecompressTree(t.TaskCtx, tempDst, c.Dst); err != nil {
650+
if err := copyDecompressTree(t.TaskCtx, tempDst, c.Dst, overwrite); err != nil {
648651
return err
649652
}
650653
success = true
@@ -655,7 +658,13 @@ func (f *FileService) DeCompress(c request.FileDeCompress) error {
655658
return nil
656659
}
657660

658-
func copyDecompressTree(ctx context.Context, srcDir, dstDir string) error {
661+
func copyDecompressTree(ctx context.Context, srcDir, dstDir string, overwrite ...bool) error {
662+
if err := ctx.Err(); err != nil {
663+
return err
664+
}
665+
if len(overwrite) > 0 && !overwrite[0] {
666+
return copyDecompressTreeKeep(ctx, srcDir, dstDir)
667+
}
659668
state := decompressCopyState{hardlinks: make(map[decompressFileIdentity]string)}
660669
entries, err := os.ReadDir(srcDir)
661670
if err != nil {
@@ -686,7 +695,10 @@ func decompressHardlinkIdentity(info os.FileInfo) (decompressFileIdentity, bool)
686695
return decompressFileIdentity{device: uint64(stat.Dev), inode: uint64(stat.Ino)}, true
687696
}
688697

689-
func copyDecompressEntry(ctx context.Context, srcPath, dstPath string) (retErr error) {
698+
func copyDecompressEntry(ctx context.Context, srcPath, dstPath string, overwrite ...bool) (retErr error) {
699+
if len(overwrite) > 0 && !overwrite[0] {
700+
return copyDecompressEntryKeep(ctx, srcPath, dstPath)
701+
}
690702
state := decompressCopyState{hardlinks: make(map[decompressFileIdentity]string)}
691703
return copyDecompressEntryWithState(ctx, srcPath, dstPath, &state)
692704
}

0 commit comments

Comments
 (0)