Skip to content

Commit

Permalink
feat(common): Standardize Permissions for Creating Folders and Files
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengkunwang223 authored and maninhill committed Dec 20, 2024
1 parent 7008774 commit 75142b3
Show file tree
Hide file tree
Showing 48 changed files with 153 additions and 133 deletions.
8 changes: 4 additions & 4 deletions agent/app/api/v2/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ func mergeChunks(fileName string, fileDir string, dstDir string, chunkCount int,
dstDir = strings.TrimSpace(dstDir)
mode, _ := files.GetParentMode(dstDir)
if mode == 0 {
mode = 0755
mode = constant.DirPerm
}
if _, err := os.Stat(dstDir); err != nil && os.IsNotExist(err) {
if err = op.CreateDir(dstDir, mode); err != nil {
Expand Down Expand Up @@ -681,7 +681,7 @@ func (b *BaseApi) UploadChunkFiles(c *gin.Context) {
fileOp := files.NewFileOp()
tmpDir := path.Join(global.CONF.System.TmpDir, "upload")
if !fileOp.Stat(tmpDir) {
if err := fileOp.CreateDir(tmpDir, 0755); err != nil {
if err := fileOp.CreateDir(tmpDir, constant.DirPerm); err != nil {
helper.BadRequest(c, err)
return
}
Expand All @@ -692,7 +692,7 @@ func (b *BaseApi) UploadChunkFiles(c *gin.Context) {
if fileOp.Stat(fileDir) {
_ = fileOp.DeleteDir(fileDir)
}
_ = os.MkdirAll(fileDir, 0755)
_ = os.MkdirAll(fileDir, constant.DirPerm)
}
filePath := filepath.Join(fileDir, filename)

Expand Down Expand Up @@ -720,7 +720,7 @@ func (b *BaseApi) UploadChunkFiles(c *gin.Context) {
}

chunkPath := filepath.Join(fileDir, fmt.Sprintf("%s.%d", filename, chunkIndex))
err = os.WriteFile(chunkPath, chunkData, 0644)
err = os.WriteFile(chunkPath, chunkData, constant.DirPerm)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, buserr.WithMap(constant.ErrFileUpload, map[string]interface{}{"name": filename, "detail": err.Error()}, err))
return
Expand Down
6 changes: 3 additions & 3 deletions agent/app/service/app_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,10 @@ func (a *AppInstallService) Update(req request.AppInstalledUpdate) error {
return err
}
fileOp := files.NewFileOp()
_ = fileOp.WriteFile(installed.GetComposePath(), strings.NewReader(installed.DockerCompose), 0755)
_ = fileOp.WriteFile(installed.GetComposePath(), strings.NewReader(installed.DockerCompose), constant.DirPerm)
if err := rebuildApp(installed); err != nil {
_ = env.Write(backupEnvMaps, envPath)
_ = fileOp.WriteFile(installed.GetComposePath(), strings.NewReader(backupDockerCompose), 0755)
_ = fileOp.WriteFile(installed.GetComposePath(), strings.NewReader(backupDockerCompose), constant.DirPerm)
return err
}
installed.Status = constant.Running
Expand Down Expand Up @@ -856,7 +856,7 @@ func updateInstallInfoInDB(appKey, appName, param string, value interface{}) err
newFiles = append(newFiles, line)
}
}
file, err := os.OpenFile(envPath, os.O_WRONLY|os.O_TRUNC, 0666)
file, err := os.OpenFile(envPath, os.O_WRONLY|os.O_TRUNC, constant.FilePerm)
if err != nil {
return err
}
Expand Down
14 changes: 7 additions & 7 deletions agent/app/service/app_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ func upgradeInstall(req request.AppInstallUpgrade) error {
if fileOp.Stat(sourceScripts) {
dstScripts := path.Join(install.GetPath(), "scripts")
_ = fileOp.DeleteDir(dstScripts)
_ = fileOp.CreateDir(dstScripts, 0755)
_ = fileOp.CreateDir(dstScripts, constant.DirPerm)
scriptCmd := exec.Command("cp", "-rf", sourceScripts+"/.", dstScripts+"/")
_, _ = scriptCmd.CombinedOutput()
}
Expand Down Expand Up @@ -678,7 +678,7 @@ func upgradeInstall(req request.AppInstallUpgrade) error {
return err
}
newConfDir := path.Join(constant.DataDir, "www", "conf.d")
_ = fileOp.CreateDir(newConfDir, 0644)
_ = fileOp.CreateDir(newConfDir, constant.DirPerm)
oldConfDir := path.Join(install.GetPath(), "conf/conf.d")
items, err := os.ReadDir(oldConfDir)
if err != nil {
Expand Down Expand Up @@ -732,7 +732,7 @@ func upgradeInstall(req request.AppInstallUpgrade) error {
return err
}

if err = fileOp.WriteFile(install.GetComposePath(), strings.NewReader(install.DockerCompose), 0775); err != nil {
if err = fileOp.WriteFile(install.GetComposePath(), strings.NewReader(install.DockerCompose), constant.FilePerm); err != nil {
return err
}

Expand Down Expand Up @@ -871,10 +871,10 @@ func downloadApp(app model.App, appDetail model.AppDetail, appInstall *model.App
return
}
if !fileOp.Stat(appDownloadDir) {
_ = fileOp.CreateDir(appDownloadDir, 0755)
_ = fileOp.CreateDir(appDownloadDir, constant.DirPerm)
}
if !fileOp.Stat(appVersionDir) {
_ = fileOp.CreateDir(appVersionDir, 0755)
_ = fileOp.CreateDir(appVersionDir, constant.DirPerm)
}
if logger == nil {
global.LOG.Infof("download app[%s] from %s", app.Name, appDetail.DownloadUrl)
Expand Down Expand Up @@ -938,7 +938,7 @@ func copyData(task *task.Task, app model.App, appDetail model.AppDetail, appInst
resourceDir := path.Join(appResourceDir, appKey, appDetail.Version)

if !fileOp.Stat(installAppDir) {
if err = fileOp.CreateDir(installAppDir, 0755); err != nil {
if err = fileOp.CreateDir(installAppDir, constant.DirPerm); err != nil {
return
}
}
Expand All @@ -962,7 +962,7 @@ func copyData(task *task.Task, app model.App, appDetail model.AppDetail, appInst
if err = env.Write(envParams, envPath); err != nil {
return
}
if err := fileOp.WriteFile(appInstall.GetComposePath(), strings.NewReader(appInstall.DockerCompose), 0755); err != nil {
if err := fileOp.WriteFile(appInstall.GetComposePath(), strings.NewReader(appInstall.DockerCompose), constant.DirPerm); err != nil {
return err
}
return
Expand Down
2 changes: 1 addition & 1 deletion agent/app/service/backup_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func handleAppRecover(install *model.AppInstall, parentTask *task.Task, recoverF
appDir := install.GetPath()
backPath := fmt.Sprintf("%s_bak", appDir)
_ = fileOp.Rename(appDir, backPath)
_ = fileOp.CreateDir(appDir, 0755)
_ = fileOp.CreateDir(appDir, constant.DirPerm)

deCompressName := i18n.GetWithName("DeCompressFile", "app.tar.gz")
t.LogStart(deCompressName)
Expand Down
2 changes: 1 addition & 1 deletion agent/app/service/backup_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func handleRuntimeRecover(runtime *model.Runtime, recoverFile string, isRollback
runtimeDir := runtime.GetPath()
backPath := fmt.Sprintf("%s_bak", runtimeDir)
_ = fileOp.Rename(runtimeDir, backPath)
_ = fileOp.CreateDir(runtimeDir, 0755)
_ = fileOp.CreateDir(runtimeDir, constant.DirPerm)

if err := handleUnTar(tmpPath+"/runtime.tar.gz", fmt.Sprintf("%s/%s", constant.RuntimeDir, runtime.Type), secret); err != nil {
global.LOG.Errorf("handle recover from runtime.tar.gz failed, err: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion agent/app/service/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ func (u *ContainerService) ContainerLogClean(req dto.OperationWithName) error {
if err := client.ContainerStop(ctx, containerItem.ID, container.StopOptions{}); err != nil {
return err
}
file, err := os.OpenFile(containerItem.LogPath, os.O_RDWR|os.O_CREATE, 0666)
file, err := os.OpenFile(containerItem.LogPath, os.O_RDWR|os.O_CREATE, constant.FilePerm)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions agent/app/service/container_compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (u *ContainerService) loadPath(req *dto.ComposeCreate) error {
}

path := fmt.Sprintf("%s/docker-compose.yml", dir)
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, constant.FilePerm)
if err != nil {
return err
}
Expand Down Expand Up @@ -350,7 +350,7 @@ func newComposeEnv(pathItem string, env []string) error {
return nil
}
envFilePath := path.Join(path.Dir(pathItem), "1panel.env")
file, err := os.OpenFile(envFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
file, err := os.OpenFile(envFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, constant.FilePerm)
if err != nil {
global.LOG.Errorf("failed to create env file: %v", err)
return err
Expand Down
2 changes: 1 addition & 1 deletion agent/app/service/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func mkdirAndWriteFile(cronjob *model.Cronjob, startTime time.Time, msg []byte)

path := fmt.Sprintf("%s/%s.log", dir, startTime.Format(constant.DateTimeSlimLayout))
global.LOG.Infof("cronjob %s has generated some logs %s", cronjob.Name, path)
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, constant.FilePerm)
if err != nil {
return "", err
}
Expand Down
8 changes: 4 additions & 4 deletions agent/app/service/cronjob_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (u *CronjobService) handleShell(cronjob model.Cronjob, logPath string) erro
if cronjob.ScriptMode == "input" {
fileItem := pathUtils.Join(global.CONF.System.BaseDir, "1panel", "task", "shell", cronjob.Name, cronjob.Name+".sh")
_ = os.MkdirAll(pathUtils.Dir(fileItem), os.ModePerm)
shellFile, err := os.OpenFile(fileItem, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
shellFile, err := os.OpenFile(fileItem, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, constant.FilePerm)
if err != nil {
return err
}
Expand Down Expand Up @@ -234,7 +234,7 @@ func (u *CronjobService) handleCutWebsiteLog(cronjob *model.Cronjob, startTime t
srcErrorLogPath := pathUtils.Join(websiteLogDir, "error.log")
dstLogDir := pathUtils.Join(global.CONF.System.Backup, "log", "website", website.Alias)
if !fileOp.Stat(dstLogDir) {
_ = os.MkdirAll(dstLogDir, 0755)
_ = os.MkdirAll(dstLogDir, constant.DirPerm)
}

dstName := fmt.Sprintf("%s_log_%s.gz", website.PrimaryDomain, startTime.Format(constant.DateTimeSlimLayout))
Expand All @@ -248,8 +248,8 @@ func (u *CronjobService) handleCutWebsiteLog(cronjob *model.Cronjob, startTime t
global.LOG.Error(websiteErr.Error())
continue
} else {
_ = fileOp.WriteFile(srcAccessLogPath, strings.NewReader(""), 0755)
_ = fileOp.WriteFile(srcErrorLogPath, strings.NewReader(""), 0755)
_ = fileOp.WriteFile(srcAccessLogPath, strings.NewReader(""), constant.DirPerm)
_ = fileOp.WriteFile(srcErrorLogPath, strings.NewReader(""), constant.DirPerm)
}
msg := i18n.GetMsgWithMap("CutWebsiteLogSuccess", map[string]interface{}{"name": website.PrimaryDomain, "path": dstFilePath})
msgs = append(msgs, msg)
Expand Down
2 changes: 1 addition & 1 deletion agent/app/service/database_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ func (u *MysqlService) UpdateVariables(req dto.MysqlVariablesUpdate) error {
files = updateMyCnf(files, group, info.Param, info.Value)
}
}
file, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0666)
file, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, constant.FilePerm)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion agent/app/service/database_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func confSet(redisName string, updateType string, changeConf []redisConfig) erro
}
newFiles = append(newFiles, "# End Redis configuration rewrite by 1Panel")

file, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0666)
file, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, constant.FilePerm)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion agent/app/service/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (f *FileService) Create(op request.FileCreate) error {
if err == nil {
mode = int64(fileInfo.Mode().Perm())
} else {
mode = 0755
mode = constant.DirPerm
}
}
if op.IsDir {
Expand Down
2 changes: 1 addition & 1 deletion agent/app/service/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ func (u *FirewallService) updatePingStatus(enable string) error {
if !hasLine {
newFiles = append(newFiles, "net/ipv4/icmp_echo_ignore_all="+enable)
}
file, err := os.OpenFile(confPath, os.O_WRONLY|os.O_TRUNC, 0666)
file, err := os.OpenFile(confPath, os.O_WRONLY|os.O_TRUNC, constant.FilePerm)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions agent/app/service/host_tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ func (h *HostToolService) CreateToolConfig(req request.HostToolCreate) error {
supervisorDir := path.Join(global.CONF.System.BaseDir, "1panel", "tools", "supervisord")
includeDir := path.Join(supervisorDir, "supervisor.d")
if !fileOp.Stat(includeDir) {
if err = fileOp.CreateDir(includeDir, 0755); err != nil {
if err = fileOp.CreateDir(includeDir, constant.DirPerm); err != nil {
return err
}
}
logDir := path.Join(supervisorDir, "log")
if !fileOp.Stat(logDir) {
if err = fileOp.CreateDir(logDir, 0755); err != nil {
if err = fileOp.CreateDir(logDir, constant.DirPerm); err != nil {
return err
}
}
Expand Down Expand Up @@ -466,7 +466,7 @@ func handleSupervisorFile(req request.SupervisorProcessFileReq, includeDir, cont
}
return string(content), nil
case "clear":
if err = fileOp.WriteFile(logFile, strings.NewReader(""), 0755); err != nil {
if err = fileOp.WriteFile(logFile, strings.NewReader(""), constant.DirPerm); err != nil {
return "", err
}
}
Expand All @@ -486,7 +486,7 @@ func handleSupervisorFile(req request.SupervisorProcessFileReq, includeDir, cont
}
return string(content), nil
case "clear":
if err = fileOp.WriteFile(logFile, strings.NewReader(""), 0755); err != nil {
if err = fileOp.WriteFile(logFile, strings.NewReader(""), constant.DirPerm); err != nil {
return "", err
}
}
Expand All @@ -503,7 +503,7 @@ func handleSupervisorFile(req request.SupervisorProcessFileReq, includeDir, cont
if req.Content == "" {
return "", buserr.New("ErrConfigIsNull")
}
if err := fileOp.WriteFile(configPath, strings.NewReader(req.Content), 0755); err != nil {
if err := fileOp.WriteFile(configPath, strings.NewReader(req.Content), constant.DirPerm); err != nil {
return "", err
}
return "", operateSupervisorCtl("update", "", req.Name, includeDir, containerName)
Expand Down
4 changes: 2 additions & 2 deletions agent/app/service/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (u *ImageService) ImageBuild(req dto.ImageBuild) error {
}

pathItem := fmt.Sprintf("%s/Dockerfile", dir)
file, err := os.OpenFile(pathItem, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
file, err := os.OpenFile(pathItem, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, constant.FilePerm)
if err != nil {
return err
}
Expand Down Expand Up @@ -317,7 +317,7 @@ func (u *ImageService) ImageSave(req dto.ImageSave) error {
return err
}
defer out.Close()
file, err := os.OpenFile(fmt.Sprintf("%s/%s.tar", req.Path, req.Name), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
file, err := os.OpenFile(fmt.Sprintf("%s/%s.tar", req.Path, req.Name), os.O_WRONLY|os.O_CREATE|os.O_EXCL, constant.FilePerm)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions agent/app/service/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (n NginxService) UpdateConfigFile(req request.NginxConfigFileUpdate) error
if req.Backup {
backupPath := path.Join(path.Dir(filePath), "bak")
if !fileOp.Stat(backupPath) {
if err := fileOp.CreateDir(backupPath, 0755); err != nil {
if err := fileOp.CreateDir(backupPath, constant.DirPerm); err != nil {
return err
}
}
Expand All @@ -152,12 +152,12 @@ func (n NginxService) UpdateConfigFile(req request.NginxConfigFileUpdate) error
if err != nil {
return err
}
if err = fileOp.WriteFile(filePath, strings.NewReader(req.Content), 0644); err != nil {
if err = fileOp.WriteFile(filePath, strings.NewReader(req.Content), constant.DirPerm); err != nil {
return err
}
if status, err := checkContainerStatus(nginxInstall.ContainerName); err == nil && status != "running" {
if out, err := compose.DownAndUp(nginxInstall.GetComposePath()); err != nil {
_ = fileOp.SaveFile(filePath, string(oldContent), 0644)
_ = fileOp.SaveFile(filePath, string(oldContent), constant.DirPerm)
return fmt.Errorf("nginx restart failed: %v", out)
} else {
return nginxCheckAndReload(string(oldContent), filePath, nginxInstall.ContainerName)
Expand Down Expand Up @@ -207,7 +207,7 @@ func (n NginxService) Build(req request.NginxBuildReq) error {
)
if len(moduleContent) > 0 {
_ = json.Unmarshal(moduleContent, &modules)
bashFile, err := os.OpenFile(path.Join(buildPath, "tmp", "pre.sh"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
bashFile, err := os.OpenFile(path.Join(buildPath, "tmp", "pre.sh"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, constant.DirPerm)
if err != nil {
return err
}
Expand Down Expand Up @@ -358,5 +358,5 @@ func (n NginxService) UpdateModule(req request.NginxModuleUpdate) error {
if err != nil {
return err
}
return fileOp.SaveFileWithByte(moduleConfigPath, moduleByte, 0644)
return fileOp.SaveFileWithByte(moduleConfigPath, moduleByte, constant.DirPerm)
}
4 changes: 2 additions & 2 deletions agent/app/service/nginx_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@ func opNginx(containerName, operate string) error {

func nginxCheckAndReload(oldContent string, filePath string, containerName string) error {
if err := opNginx(containerName, constant.NginxCheck); err != nil {
_ = files.NewFileOp().WriteFile(filePath, strings.NewReader(oldContent), 0644)
_ = files.NewFileOp().WriteFile(filePath, strings.NewReader(oldContent), constant.DirPerm)
return err
}
if err := opNginx(containerName, constant.NginxReload); err != nil {
_ = files.NewFileOp().WriteFile(filePath, strings.NewReader(oldContent), 0644)
_ = files.NewFileOp().WriteFile(filePath, strings.NewReader(oldContent), constant.DirPerm)
return err
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion agent/app/service/recycle_bin.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func getClashDir(realPath string) (string, error) {
func createClashDir(clashDir string) error {
op := files.NewFileOp()
if !op.Stat(clashDir) {
if err := op.CreateDir(clashDir, 0755); err != nil {
if err := op.CreateDir(clashDir, constant.DirPerm); err != nil {
return err
}
}
Expand Down
6 changes: 3 additions & 3 deletions agent/app/service/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,13 +928,13 @@ func (r *RuntimeService) UpdatePHPConfig(req request.PHPConfigUpdate) (err error
}
}
updatedContent := strings.Join(lines, "\n")
if err := fileOp.WriteFile(phpConfigPath, strings.NewReader(updatedContent), 0755); err != nil {
if err := fileOp.WriteFile(phpConfigPath, strings.NewReader(updatedContent), constant.DirPerm); err != nil {
return err
}

err = restartRuntime(runtime)
if err != nil {
_ = fileOp.WriteFile(phpConfigPath, strings.NewReader(string(contentBytes)), 0755)
_ = fileOp.WriteFile(phpConfigPath, strings.NewReader(string(contentBytes)), constant.DirPerm)
return err
}
return
Expand Down Expand Up @@ -973,7 +973,7 @@ func (r *RuntimeService) UpdatePHPConfigFile(req request.PHPFileUpdate) error {
} else {
configPath = path.Join(runtime.GetPath(), "conf", "php.ini")
}
if err := files.NewFileOp().WriteFile(configPath, strings.NewReader(req.Content), 0755); err != nil {
if err := files.NewFileOp().WriteFile(configPath, strings.NewReader(req.Content), constant.DirPerm); err != nil {
return err
}
if _, err := compose.Restart(runtime.GetComposePath()); err != nil {
Expand Down
Loading

0 comments on commit 75142b3

Please sign in to comment.