Skip to content

Commit b0a053b

Browse files
authored
fix: Fix batch modification file/folder permission timeout issue (#11775)
#10737
1 parent a8b3390 commit b0a053b

File tree

3 files changed

+89
-14
lines changed

3 files changed

+89
-14
lines changed

agent/app/service/file.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -315,19 +315,18 @@ func (f *FileService) ChangeMode(op request.FileCreate) error {
315315

316316
func (f *FileService) BatchChangeModeAndOwner(op request.FileRoleReq) error {
317317
fo := files.NewFileOp()
318-
for _, path := range op.Paths {
319-
if !fo.Stat(path) {
318+
for _, p := range op.Paths {
319+
if !fo.Stat(p) {
320320
return buserr.New("ErrPathNotFound")
321321
}
322-
if err := fo.ChownR(path, op.User, op.Group, op.Sub); err != nil {
323-
return err
324-
}
325-
if err := fo.ChmodR(path, op.Mode, op.Sub); err != nil {
326-
return err
327-
}
322+
}
323+
if err := fo.ChownRPaths(op.Paths, op.User, op.Group, op.Sub); err != nil {
324+
return err
325+
}
326+
if err := fo.ChmodRPaths(op.Paths, op.Mode, op.Sub); err != nil {
327+
return err
328328
}
329329
return nil
330-
331330
}
332331

333332
func (f *FileService) ChangeOwner(req request.FileRoleUpdate) error {

agent/utils/files/file_op.go

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ import (
3333
"github.com/spf13/afero"
3434
)
3535

36+
const (
37+
cmdDefaultTimeout = 10 * time.Second
38+
cmdRecursiveTimeout = 5 * time.Minute
39+
)
40+
3641
var protectedPaths = []string{
3742
"/",
3843
"/bin",
@@ -222,7 +227,11 @@ func (f FileOp) ChownR(dst string, uid string, gid string, sub bool) error {
222227
if sub {
223228
cmdStr = fmt.Sprintf(`chown -R %s:%s "%s"`, uid, gid, dst)
224229
}
225-
cmdMgr := cmd.NewCommandMgr(cmd.WithTimeout(10 * time.Second))
230+
timeout := cmdDefaultTimeout
231+
if sub {
232+
timeout = cmdRecursiveTimeout
233+
}
234+
cmdMgr := cmd.NewCommandMgr(cmd.WithTimeout(timeout))
226235
if err := cmdMgr.RunBashC(cmdStr); err != nil {
227236
return err
228237
}
@@ -234,7 +243,11 @@ func (f FileOp) ChmodR(dst string, mode int64, sub bool) error {
234243
if sub {
235244
cmdStr = fmt.Sprintf(`%s chmod -R %v "%s"`, cmd.SudoHandleCmd(), fmt.Sprintf("%04o", mode), dst)
236245
}
237-
cmdMgr := cmd.NewCommandMgr(cmd.WithTimeout(10 * time.Second))
246+
timeout := cmdDefaultTimeout
247+
if sub {
248+
timeout = cmdRecursiveTimeout
249+
}
250+
cmdMgr := cmd.NewCommandMgr(cmd.WithTimeout(timeout))
238251
if err := cmdMgr.RunBashC(cmdStr); err != nil {
239252
return err
240253
}
@@ -246,7 +259,70 @@ func (f FileOp) ChmodRWithMode(dst string, mode fs.FileMode, sub bool) error {
246259
if sub {
247260
cmdStr = fmt.Sprintf(`%s chmod -R %v "%s"`, cmd.SudoHandleCmd(), fmt.Sprintf("%o", mode.Perm()), dst)
248261
}
249-
cmdMgr := cmd.NewCommandMgr(cmd.WithTimeout(10 * time.Second))
262+
timeout := cmdDefaultTimeout
263+
if sub {
264+
timeout = cmdRecursiveTimeout
265+
}
266+
cmdMgr := cmd.NewCommandMgr(cmd.WithTimeout(timeout))
267+
if err := cmdMgr.RunBashC(cmdStr); err != nil {
268+
return err
269+
}
270+
return nil
271+
}
272+
273+
func (f FileOp) ChownRPaths(paths []string, uid string, gid string, sub bool) error {
274+
if len(paths) == 0 {
275+
return nil
276+
}
277+
if len(paths) == 1 {
278+
return f.ChownR(paths[0], uid, gid, sub)
279+
}
280+
quoted := make([]string, len(paths))
281+
for i, p := range paths {
282+
quoted[i] = fmt.Sprintf(`"%s"`, p)
283+
}
284+
args := strings.Join(quoted, " ")
285+
var cmdStr string
286+
if sub {
287+
cmdStr = fmt.Sprintf(`chown -R %s:%s %s`, uid, gid, args)
288+
} else {
289+
cmdStr = fmt.Sprintf(`%s chown %s:%s %s`, cmd.SudoHandleCmd(), uid, gid, args)
290+
}
291+
timeout := cmdDefaultTimeout
292+
if sub {
293+
timeout = cmdRecursiveTimeout
294+
}
295+
cmdMgr := cmd.NewCommandMgr(cmd.WithTimeout(timeout))
296+
if err := cmdMgr.RunBashC(cmdStr); err != nil {
297+
return err
298+
}
299+
return nil
300+
}
301+
302+
func (f FileOp) ChmodRPaths(paths []string, mode int64, sub bool) error {
303+
if len(paths) == 0 {
304+
return nil
305+
}
306+
if len(paths) == 1 {
307+
return f.ChmodR(paths[0], mode, sub)
308+
}
309+
quoted := make([]string, len(paths))
310+
for i, p := range paths {
311+
quoted[i] = fmt.Sprintf(`"%s"`, p)
312+
}
313+
args := strings.Join(quoted, " ")
314+
modeStr := fmt.Sprintf("%04o", mode)
315+
var cmdStr string
316+
if sub {
317+
cmdStr = fmt.Sprintf(`%s chmod -R %s %s`, cmd.SudoHandleCmd(), modeStr, args)
318+
} else {
319+
cmdStr = fmt.Sprintf(`%s chmod %s %s`, cmd.SudoHandleCmd(), modeStr, args)
320+
}
321+
timeout := cmdDefaultTimeout
322+
if sub {
323+
timeout = cmdRecursiveTimeout
324+
}
325+
cmdMgr := cmd.NewCommandMgr(cmd.WithTimeout(timeout))
250326
if err := cmdMgr.RunBashC(cmdStr); err != nil {
251327
return err
252328
}

frontend/src/api/modules/files.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const batchDeleteFile = (form: File.FileBatchDelete) => {
3939
};
4040

4141
export const changeFileMode = (form: File.FileCreate) => {
42-
return http.post<File.File>('files/mode', form);
42+
return http.post<File.File>('files/mode', form, TimeoutEnum.T_5M);
4343
};
4444

4545
export const compressFile = (form: File.FileCompress) => {
@@ -148,7 +148,7 @@ export const removeFavorite = (id: number) => {
148148
};
149149

150150
export const batchChangeRole = (params: File.FileRole) => {
151-
return http.post<any>('files/batch/role', params);
151+
return http.post<any>('files/batch/role', params, TimeoutEnum.T_5M);
152152
};
153153

154154
export const getRecycleStatus = () => {

0 commit comments

Comments
 (0)