Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/app/service/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (f *FileService) Create(op request.FileCreate) error {
func (f *FileService) Delete(op request.FileDelete) error {
if op.IsDir {
excludeDir := global.CONF.System.DataDir
if strings.Contains(op.Path, ".1panel_clash") || op.Path == excludeDir {
if filepath.Base(op.Path) == ".1panel_clash" || op.Path == excludeDir {
return buserr.New(constant.ErrPathNotDelete)
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段Go代码的主要问题在于file.Delete()方法,当op是目录时需要处理的情况。目前,在逻辑上删除目录操作可能不符合预期:

func (f *FileService) Delete(op request.FileDelete) error {
 	if op.IsDir && !strings.Contains(global.CONF.System.DataDir, op.Path) { // 这行如果删除的是某个包含'.1panel_clash' 的路径或与系统默认数据目录的路径相同,则会导致逻辑错误。
    return buserr.New(constant.ErrPathNotDelete)
 }

 	...

这个逻辑可能导致某些特定情况下的文件被误删,如.1panel_clash的存在导致正常情况下会忽略但最后还是被删除。

建议调整为:

  1. 删除单个非空目录:

func (f *FileService) Delete(op request.FileDelete) error {
if !filepath.Walk(op.Path).Contains(".") {
return f.fileExistCheck(op.Path) // 如果不存在点则继续往下
}

 err := f.deleteDirectory(op.Path)
 if err != nil {
   return err
 }

Comment: suggestion func (f *FileService) Delete(op request.FileDelete) error { if !filepath.Walk(op.Path).Contains('.') { ... } else {

这样可以避免因为路径存在指定文件和排除路径而意外执行错误的操作了。

Expand Down