From 7b695af99672622d58eb0f03c63cf49dfc2bbc15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=98=AD?= Date: Wed, 15 Jul 2026 15:54:05 +0800 Subject: [PATCH] fix: Fix the issue of file permissions changing after decompressing files (#13267) --- agent/app/service/file.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/agent/app/service/file.go b/agent/app/service/file.go index f1b60d7cd..373e25ef7 100644 --- a/agent/app/service/file.go +++ b/agent/app/service/file.go @@ -621,9 +621,16 @@ func copyDecompressEntry(ctx context.Context, srcPath, dstPath string) (retErr e return os.Chtimes(dstPath, info.ModTime(), info.ModTime()) } - if err := os.RemoveAll(dstPath); err != nil { + dstInfo, err := os.Lstat(dstPath) + keepExistingFile := err == nil && dstInfo.Mode().IsRegular() + if err != nil && !os.IsNotExist(err) { return err } + if !keepExistingFile { + if err := os.RemoveAll(dstPath); err != nil { + return err + } + } if err := os.MkdirAll(filepath.Dir(dstPath), constant.DirPerm); err != nil { return err } @@ -647,8 +654,10 @@ func copyDecompressEntry(ctx context.Context, srcPath, dstPath string) (retErr e if _, err := io.Copy(dstFile, srcFile); err != nil { return err } - if err := applyDecompressOwnership(srcPath, dstPath); err != nil { - return err + if !keepExistingFile { + if err := applyDecompressOwnership(srcPath, dstPath); err != nil { + return err + } } return os.Chtimes(dstPath, info.ModTime(), info.ModTime()) }