diff --git a/app/http/controllers/plugins/pureftpd_controller.go b/app/http/controllers/plugins/pureftpd_controller.go index ccb7c88954..999d77ed33 100644 --- a/app/http/controllers/plugins/pureftpd_controller.go +++ b/app/http/controllers/plugins/pureftpd_controller.go @@ -185,7 +185,9 @@ func (r *PureFtpdController) Add(ctx http.Context) http.Response { if err = tools.Chmod(path, 0755); err != nil { return controllers.Error(ctx, http.StatusUnprocessableEntity, "修改目录权限失败") } - tools.Chown(path, "www", "www") + if err = tools.Chown(path, "www", "www"); err != nil { + return nil + } tools.Exec(`yes '` + password + `' | pure-pw useradd ` + username + ` -u www -g www -d ` + path) tools.Exec("pure-pw mkdb") diff --git a/app/http/controllers/plugins/toolbox_controller.go b/app/http/controllers/plugins/toolbox_controller.go index e2f4592532..5977b951ec 100644 --- a/app/http/controllers/plugins/toolbox_controller.go +++ b/app/http/controllers/plugins/toolbox_controller.go @@ -74,9 +74,13 @@ func (r *ToolBoxController) GetSWAP(ctx http.Context) http.Response { var total, used, free string var size int64 if tools.Exists("/www/swap") { - s, _ := tools.FileSize("/www/swap") - size = s / 1024 / 1024 - total = tools.FormatBytes(float64(s)) + file, err := tools.FileInfo("/www/swap") + if err != nil { + return controllers.Error(ctx, http.StatusUnprocessableEntity, "获取 SWAP 信息失败") + } + + size = file.Size() / 1024 / 1024 + total = tools.FormatBytes(float64(file.Size())) } else { size = 0 total = "0.00 B" diff --git a/app/http/controllers/setting_controller.go b/app/http/controllers/setting_controller.go index 3dda19dde9..ff63fcc5d9 100644 --- a/app/http/controllers/setting_controller.go +++ b/app/http/controllers/setting_controller.go @@ -101,7 +101,9 @@ func (r *SettingController) Update(ctx http.Context) http.Response { if err = tools.Mkdir(updateRequest.WebsitePath, 0755); err != nil { return ErrorSystem(ctx) } - tools.Chown(updateRequest.WebsitePath, "www", "www") + if err = tools.Chown(updateRequest.WebsitePath, "www", "www"); err != nil { + return ErrorSystem(ctx) + } } err = r.setting.Set(models.SettingKeyWebsitePath, updateRequest.WebsitePath) if err != nil { diff --git a/app/services/backup.go b/app/services/backup.go index cffe9c64c2..a283d74bc1 100644 --- a/app/services/backup.go +++ b/app/services/backup.go @@ -117,7 +117,9 @@ func (s *BackupImpl) WebsiteRestore(website models.Website, backupFile string) e if err := tools.Chmod(website.Path, 0755); err != nil { return err } - tools.Chown(website.Path, "www", "www") + if err := tools.Chown(website.Path, "www", "www"); err != nil { + return err + } return nil } diff --git a/app/services/website.go b/app/services/website.go index fd70862c0d..486afea34e 100644 --- a/app/services/website.go +++ b/app/services/website.go @@ -288,8 +288,12 @@ server if err := tools.Chmod(website.Path, 0755); err != nil { return models.Website{}, err } - tools.Chown(r.setting.Get(models.SettingKeyWebsitePath), "www", "www") - tools.Chown(website.Path, "www", "www") + if err := tools.Chown(r.setting.Get(models.SettingKeyWebsitePath), "www", "www"); err != nil { + return models.Website{}, err + } + if err := tools.Chown(website.Path, "www", "www"); err != nil { + return models.Website{}, err + } tools.Exec("systemctl reload openresty") diff --git a/pkg/tools/system.go b/pkg/tools/system.go index 4a3fcd6c88..92a84e511b 100644 --- a/pkg/tools/system.go +++ b/pkg/tools/system.go @@ -111,21 +111,9 @@ func Chmod(path string, permission os.FileMode) error { } // Chown 修改文件/目录所有者 -func Chown(path, user, group string) bool { +func Chown(path, user, group string) error { cmd := exec.Command("chown", "-R", user+":"+group, path) - - err := cmd.Run() - if err != nil { - facades.Log().With(map[string]any{ - "path": path, - "user": user, - "group": group, - "error": err.Error(), - }).Tags("面板", "工具函数").Info("修改文件/目录所有者失败") - return false - } - - return true + return cmd.Run() } // Exists 判断路径是否存在 @@ -173,8 +161,7 @@ func Size(path string) (int64, error) { return size, err } -// FileSize 获取文件大小 -func FileSize(path string) (int64, error) { - info, err := os.Stat(path) - return info.Size(), err +// FileInfo 获取文件大小 +func FileInfo(path string) (os.FileInfo, error) { + return os.Stat(path) } diff --git a/pkg/tools/system_test.go b/pkg/tools/system_test.go index 643fa16b01..308a884bd2 100644 --- a/pkg/tools/system_test.go +++ b/pkg/tools/system_test.go @@ -93,7 +93,7 @@ func (s *SystemHelperTestSuite) TestChown() { groups, err := currentUser.GroupIds() s.Nil(err) - s.True(Chown(filePath, currentUser.Username, groups[0])) + s.Nil(Chown(filePath, currentUser.Username, groups[0])) } func (s *SystemHelperTestSuite) TestExists() { @@ -134,8 +134,7 @@ func (s *SystemHelperTestSuite) TestSize() { s.Error(err) } -func (s *SystemHelperTestSuite) TestFileSize() { - size, err := FileSize("/tmp/123") - s.Equal(int64(0), size) +func (s *SystemHelperTestSuite) TestFileInfo() { + _, err := FileInfo("/tmp/123") s.Error(err) }