mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 10:17:17 +08:00
refactor: 重构 tools.Chown 函数
This commit is contained in:
@@ -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")
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user