Skip to content

Commit

Permalink
refactor: 重构 tools.Chown 函数
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Nov 11, 2023
1 parent edd526d commit 1c5b32a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 30 deletions.
4 changes: 3 additions & 1 deletion app/http/controllers/plugins/pureftpd_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
10 changes: 7 additions & 3 deletions app/http/controllers/plugins/toolbox_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion app/http/controllers/setting_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion app/services/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 6 additions & 2 deletions app/services/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
23 changes: 5 additions & 18 deletions pkg/tools/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 判断路径是否存在
Expand Down Expand Up @@ -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)
}
7 changes: 3 additions & 4 deletions pkg/tools/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
}

0 comments on commit 1c5b32a

Please sign in to comment.