From b7c3dc0d2a78d643c8b4da94762a16bd5011a138 Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Thu, 18 Apr 2024 22:40:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E5=88=9B=E5=BB=BA=20P?= =?UTF-8?q?HP=20=E7=BD=91=E7=AB=99=E9=A6=96=E9=A1=B5=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E8=AE=BF=E9=97=AE=E7=9A=84=20BUG=20(#4577)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/file.go | 2 +- backend/utils/files/file_op.go | 30 ++++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/backend/app/service/file.go b/backend/app/service/file.go index 06745bb60de4..0f188aa95018 100644 --- a/backend/app/service/file.go +++ b/backend/app/service/file.go @@ -133,7 +133,7 @@ func (f *FileService) Create(op request.FileCreate) error { } } if op.IsDir { - return fo.CreateDir(op.Path, fs.FileMode(mode)) + return fo.CreateDirWithMode(op.Path, fs.FileMode(mode)) } if op.IsLink { if !fo.Stat(op.LinkPath) { diff --git a/backend/utils/files/file_op.go b/backend/utils/files/file_op.go index a2ff326ad86a..b0ff04c9b3e4 100644 --- a/backend/utils/files/file_op.go +++ b/backend/utils/files/file_op.go @@ -54,17 +54,14 @@ func (f FileOp) GetContent(dst string) ([]byte, error) { } func (f FileOp) CreateDir(dst string, mode fs.FileMode) error { + return f.Fs.MkdirAll(dst, mode) +} + +func (f FileOp) CreateDirWithMode(dst string, mode fs.FileMode) error { if err := f.Fs.MkdirAll(dst, mode); err != nil { return err } - modStr := fmt.Sprintf("%o", mode) - - modeInt, err := strconv.ParseInt(modStr, 10, 64) - if err != nil { - modeInt = 0755 - } - - return f.ChmodR(dst, modeInt, true) + return f.ChmodRWithMode(dst, mode, true) } func (f FileOp) CreateFile(dst string) error { @@ -193,6 +190,23 @@ func (f FileOp) ChmodR(dst string, mode int64, sub bool) error { return nil } +func (f FileOp) ChmodRWithMode(dst string, mode fs.FileMode, sub bool) error { + cmdStr := fmt.Sprintf(`chmod %v "%s"`, fmt.Sprintf("%o", mode.Perm()), dst) + if sub { + cmdStr = fmt.Sprintf(`chmod -R %v "%s"`, fmt.Sprintf("%o", mode.Perm()), dst) + } + if cmd.HasNoPasswordSudo() { + cmdStr = fmt.Sprintf("sudo %s", cmdStr) + } + if msg, err := cmd.ExecWithTimeOut(cmdStr, 10*time.Second); err != nil { + if msg != "" { + return errors.New(msg) + } + return err + } + return nil +} + func (f FileOp) Rename(oldName string, newName string) error { return f.Fs.Rename(oldName, newName) }