use os.ModePerm for files and directory creation

This commit is contained in:
Nicola Murino 2020-06-08 19:40:17 +02:00
parent cd380973df
commit 7807fa7cc2
4 changed files with 46 additions and 46 deletions

View file

@ -122,7 +122,7 @@ func TestMain(m *testing.M) {
httpd.SetBaseURLAndCredentials("http://127.0.0.1:8081", "", "")
backupsPath = filepath.Join(os.TempDir(), "test_backups")
httpdConf.BackupsPath = backupsPath
err = os.MkdirAll(backupsPath, 0777)
err = os.MkdirAll(backupsPath, os.ModePerm)
if err != nil {
logger.WarnToConsole("error creating backups path: %v", err)
os.Exit(1)
@ -1742,7 +1742,7 @@ func TestStartQuotaScanMock(t *testing.T) {
}
_, err = os.Stat(user.HomeDir)
if err != nil && os.IsNotExist(err) {
err = os.MkdirAll(user.HomeDir, 0777)
err = os.MkdirAll(user.HomeDir, os.ModePerm)
assert.NoError(t, err)
}
req, _ = http.NewRequest(http.MethodPost, quotaScanPath, bytes.NewBuffer(userAsJSON))
@ -1797,7 +1797,7 @@ func TestStartFolderQuotaScanMock(t *testing.T) {
// and now a real quota scan
_, err = os.Stat(mappedPath)
if err != nil && os.IsNotExist(err) {
err = os.MkdirAll(mappedPath, 0777)
err = os.MkdirAll(mappedPath, os.ModePerm)
assert.NoError(t, err)
}
req, _ = http.NewRequest(http.MethodPost, quotaScanVFolderPath, bytes.NewBuffer(folderAsJSON))
@ -2612,7 +2612,7 @@ func checkResponseCode(t *testing.T, expected, actual int) {
func createTestFile(path string, size int64) error {
baseDir := filepath.Dir(path)
if _, err := os.Stat(baseDir); os.IsNotExist(err) {
err = os.MkdirAll(baseDir, 0777)
err = os.MkdirAll(baseDir, os.ModePerm)
if err != nil {
return err
}

View file

@ -224,7 +224,7 @@ func TestPreDeleteAction(t *testing.T) {
Hook: hookCmd,
}
homeDir := filepath.Join(os.TempDir(), "test_user")
err = os.MkdirAll(homeDir, 0777)
err = os.MkdirAll(homeDir, os.ModePerm)
assert.NoError(t, err)
user := dataprovider.User{
Username: "username",
@ -1281,7 +1281,7 @@ func TestSCPTestDownloadProtocolMessages(t *testing.T) {
},
}
path := "testDir"
err := os.Mkdir(path, 0777)
err := os.Mkdir(path, os.ModePerm)
assert.NoError(t, err)
stat, err := os.Stat(path)
assert.NoError(t, err)
@ -1451,7 +1451,7 @@ func TestSCPRecursiveDownloadErrors(t *testing.T) {
},
}
path := "testDir"
err := os.Mkdir(path, 0777)
err := os.Mkdir(path, os.ModePerm)
assert.NoError(t, err)
stat, err := os.Stat(path)
assert.NoError(t, err)
@ -1818,7 +1818,7 @@ func TestLoadHostKeys(t *testing.T) {
err = os.Remove(testfile)
assert.NoError(t, err)
keysDir := filepath.Join(os.TempDir(), "keys")
err = os.MkdirAll(keysDir, 0777)
err = os.MkdirAll(keysDir, os.ModePerm)
assert.NoError(t, err)
rsaKeyName := filepath.Join(keysDir, defaultPrivateRSAKeyName)
ecdsaKeyName := filepath.Join(keysDir, defaultPrivateECDSAKeyName)
@ -1890,13 +1890,13 @@ func TestUpdateQuotaAfterRenameMissingFile(t *testing.T) {
request.Target = path.Join("vdir", "dir")
if runtime.GOOS != osWindows {
testDirPath := filepath.Join(mappedPath, "dir")
err := os.MkdirAll(testDirPath, 0777)
err := os.MkdirAll(testDirPath, os.ModePerm)
assert.NoError(t, err)
err = os.Chmod(testDirPath, 0001)
assert.NoError(t, err)
err = c.updateQuotaAfterRename(request, testDirPath, 0)
assert.Error(t, err)
err = os.Chmod(testDirPath, 0777)
err = os.Chmod(testDirPath, os.ModePerm)
assert.NoError(t, err)
err = os.RemoveAll(testDirPath)
assert.NoError(t, err)

View file

@ -132,7 +132,7 @@ func TestMain(m *testing.M) {
loginBannerFileName := "login_banner"
loginBannerFile := filepath.Join(configDir, loginBannerFileName)
logger.InitLogger(logFilePath, 5, 1, 28, false, zerolog.DebugLevel)
err := ioutil.WriteFile(loginBannerFile, []byte("simple login banner\n"), 0777)
err := ioutil.WriteFile(loginBannerFile, []byte("simple login banner\n"), os.ModePerm)
if err != nil {
logger.WarnToConsole("error creating login banner: %v", err)
}
@ -2126,7 +2126,7 @@ func TestVirtualFolders(t *testing.T) {
},
VirtualPath: vdirPath,
})
err := os.MkdirAll(mappedPath, 0777)
err := os.MkdirAll(mappedPath, os.ModePerm)
assert.NoError(t, err)
user, _, err := httpd.AddUser(u, http.StatusOK)
assert.NoError(t, err)
@ -2223,9 +2223,9 @@ func TestVirtualFoldersQuotaLimit(t *testing.T) {
})
users := []dataprovider.User{u1, u2}
for _, u := range users {
err = os.MkdirAll(mappedPath1, 0777)
err = os.MkdirAll(mappedPath1, os.ModePerm)
assert.NoError(t, err)
err = os.MkdirAll(mappedPath2, 0777)
err = os.MkdirAll(mappedPath2, os.ModePerm)
assert.NoError(t, err)
user, _, err := httpd.AddUser(u, http.StatusOK)
assert.NoError(t, err)
@ -2316,9 +2316,9 @@ func TestVirtualFoldersQuotaRenameOverwrite(t *testing.T) {
QuotaFiles: 0,
QuotaSize: testFileSize + testFileSize1 - 1,
})
err = os.MkdirAll(mappedPath1, 0777)
err = os.MkdirAll(mappedPath1, os.ModePerm)
assert.NoError(t, err)
err = os.MkdirAll(mappedPath2, 0777)
err = os.MkdirAll(mappedPath2, os.ModePerm)
assert.NoError(t, err)
user, _, err := httpd.AddUser(u, http.StatusOK)
assert.NoError(t, err)
@ -2397,9 +2397,9 @@ func TestVirtualFoldersQuotaValues(t *testing.T) {
QuotaFiles: 0,
QuotaSize: 0,
})
err := os.MkdirAll(mappedPath1, 0777)
err := os.MkdirAll(mappedPath1, os.ModePerm)
assert.NoError(t, err)
err = os.MkdirAll(mappedPath2, 0777)
err = os.MkdirAll(mappedPath2, os.ModePerm)
assert.NoError(t, err)
user, _, err := httpd.AddUser(u, http.StatusOK)
assert.NoError(t, err)
@ -2505,9 +2505,9 @@ func TestQuotaRenameInsideSameVirtualFolder(t *testing.T) {
QuotaFiles: 0,
QuotaSize: 0,
})
err := os.MkdirAll(mappedPath1, 0777)
err := os.MkdirAll(mappedPath1, os.ModePerm)
assert.NoError(t, err)
err = os.MkdirAll(mappedPath2, 0777)
err = os.MkdirAll(mappedPath2, os.ModePerm)
assert.NoError(t, err)
user, _, err := httpd.AddUser(u, http.StatusOK)
assert.NoError(t, err)
@ -2729,9 +2729,9 @@ func TestQuotaRenameBetweenVirtualFolder(t *testing.T) {
QuotaFiles: 0,
QuotaSize: 0,
})
err := os.MkdirAll(mappedPath1, 0777)
err := os.MkdirAll(mappedPath1, os.ModePerm)
assert.NoError(t, err)
err = os.MkdirAll(mappedPath2, 0777)
err = os.MkdirAll(mappedPath2, os.ModePerm)
assert.NoError(t, err)
user, _, err := httpd.AddUser(u, http.StatusOK)
assert.NoError(t, err)
@ -2973,9 +2973,9 @@ func TestQuotaRenameFromVirtualFolder(t *testing.T) {
QuotaFiles: 0,
QuotaSize: 0,
})
err := os.MkdirAll(mappedPath1, 0777)
err := os.MkdirAll(mappedPath1, os.ModePerm)
assert.NoError(t, err)
err = os.MkdirAll(mappedPath2, 0777)
err = os.MkdirAll(mappedPath2, os.ModePerm)
assert.NoError(t, err)
user, _, err := httpd.AddUser(u, http.StatusOK)
assert.NoError(t, err)
@ -3220,9 +3220,9 @@ func TestQuotaRenameToVirtualFolder(t *testing.T) {
QuotaFiles: 0,
QuotaSize: 0,
})
err := os.MkdirAll(mappedPath1, 0777)
err := os.MkdirAll(mappedPath1, os.ModePerm)
assert.NoError(t, err)
err = os.MkdirAll(mappedPath2, 0777)
err = os.MkdirAll(mappedPath2, os.ModePerm)
assert.NoError(t, err)
user, _, err := httpd.AddUser(u, http.StatusOK)
assert.NoError(t, err)
@ -3479,9 +3479,9 @@ func TestVirtualFoldersLink(t *testing.T) {
QuotaFiles: 0,
QuotaSize: 0,
})
err := os.MkdirAll(mappedPath1, 0777)
err := os.MkdirAll(mappedPath1, os.ModePerm)
assert.NoError(t, err)
err = os.MkdirAll(mappedPath2, 0777)
err = os.MkdirAll(mappedPath2, os.ModePerm)
assert.NoError(t, err)
user, _, err := httpd.AddUser(u, http.StatusOK)
assert.NoError(t, err)
@ -3549,7 +3549,7 @@ func TestVirtualFoldersLink(t *testing.T) {
func TestVirtualFolderQuotaScan(t *testing.T) {
mappedPath := filepath.Join(os.TempDir(), "mapped_dir")
err := os.MkdirAll(mappedPath, 0777)
err := os.MkdirAll(mappedPath, os.ModePerm)
assert.NoError(t, err)
testFileSize := int64(65535)
testFileName := "test_file.dat"
@ -3620,9 +3620,9 @@ func TestVFolderQuotaSize(t *testing.T) {
QuotaFiles: 1,
QuotaSize: testFileSize * 2,
})
err := os.MkdirAll(mappedPath1, 0777)
err := os.MkdirAll(mappedPath1, os.ModePerm)
assert.NoError(t, err)
err = os.MkdirAll(mappedPath2, 0777)
err = os.MkdirAll(mappedPath2, os.ModePerm)
assert.NoError(t, err)
testFileName := "test_file.dat"
testFilePath := filepath.Join(homeBasePath, testFileName)
@ -4603,7 +4603,7 @@ func TestResolvePaths(t *testing.T) {
Bucket: "bucket",
Region: "us-east-1",
}
err = os.MkdirAll(user.GetHomeDir(), 0777)
err = os.MkdirAll(user.GetHomeDir(), os.ModePerm)
assert.NoError(t, err)
s3fs, err := vfs.NewS3Fs("", user.GetHomeDir(), s3config)
assert.NoError(t, err)
@ -4653,7 +4653,7 @@ func TestVirtualRelativePaths(t *testing.T) {
},
VirtualPath: vdirPath,
})
err := os.MkdirAll(mappedPath, 0777)
err := os.MkdirAll(mappedPath, os.ModePerm)
assert.NoError(t, err)
fs := vfs.NewOsFs("", user.GetHomeDir(), user.VirtualFolders)
rel := fs.GetRelativePath(mappedPath)
@ -4679,7 +4679,7 @@ func TestResolveVirtualPaths(t *testing.T) {
},
VirtualPath: vdirPath,
})
err := os.MkdirAll(mappedPath, 0777)
err := os.MkdirAll(mappedPath, os.ModePerm)
assert.NoError(t, err)
osFs := vfs.NewOsFs("", user.GetHomeDir(), user.VirtualFolders).(vfs.OsFs)
b, f := osFs.GetFsPaths("/vdir/a.txt")
@ -5336,7 +5336,7 @@ func TestSCPVirtualFolders(t *testing.T) {
},
VirtualPath: vdirPath,
})
err := os.MkdirAll(mappedPath, 0777)
err := os.MkdirAll(mappedPath, os.ModePerm)
assert.NoError(t, err)
user, _, err := httpd.AddUser(u, http.StatusOK)
assert.NoError(t, err)
@ -5400,9 +5400,9 @@ func TestSCPVirtualFoldersQuota(t *testing.T) {
QuotaFiles: 0,
QuotaSize: 0,
})
err := os.MkdirAll(mappedPath1, 0777)
err := os.MkdirAll(mappedPath1, os.ModePerm)
assert.NoError(t, err)
err = os.MkdirAll(mappedPath2, 0777)
err = os.MkdirAll(mappedPath2, os.ModePerm)
assert.NoError(t, err)
user, _, err := httpd.AddUser(u, http.StatusOK)
assert.NoError(t, err)
@ -5487,7 +5487,7 @@ func TestSCPPermsSubDirs(t *testing.T) {
localPath := filepath.Join(homeBasePath, "scp_download.dat")
subPath := filepath.Join(user.GetHomeDir(), "somedir")
testFileSize := int64(65535)
err = os.MkdirAll(subPath, 0777)
err = os.MkdirAll(subPath, os.ModePerm)
assert.NoError(t, err)
remoteDownPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, "/somedir")
err = scpDownload(localPath, remoteDownPath, false, true)
@ -5667,7 +5667,7 @@ func TestSCPEscapeHomeDir(t *testing.T) {
usePubKey := true
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
assert.NoError(t, err)
err = os.MkdirAll(user.GetHomeDir(), 0777)
err = os.MkdirAll(user.GetHomeDir(), os.ModePerm)
assert.NoError(t, err)
testDir := "testDir"
linkPath := filepath.Join(homeBasePath, defaultUsername, testDir)
@ -5709,7 +5709,7 @@ func TestSCPUploadPaths(t *testing.T) {
testFileSize := int64(65535)
testDirName := "testDir"
testDirPath := filepath.Join(user.GetHomeDir(), testDirName)
err = os.MkdirAll(testDirPath, 0777)
err = os.MkdirAll(testDirPath, os.ModePerm)
assert.NoError(t, err)
err = createTestFile(testFilePath, testFileSize)
assert.NoError(t, err)
@ -5744,7 +5744,7 @@ func TestSCPOverwriteDirWithFile(t *testing.T) {
testFilePath := filepath.Join(homeBasePath, testFileName)
testFileSize := int64(65535)
testDirPath := filepath.Join(user.GetHomeDir(), testFileName)
err = os.MkdirAll(testDirPath, 0777)
err = os.MkdirAll(testDirPath, os.ModePerm)
assert.NoError(t, err)
err = createTestFile(testFilePath, testFileSize)
assert.NoError(t, err)
@ -6001,7 +6001,7 @@ func getCustomAuthSftpClient(user dataprovider.User, authMethods []ssh.AuthMetho
func createTestFile(path string, size int64) error {
baseDir := filepath.Dir(path)
if _, err := os.Stat(baseDir); os.IsNotExist(err) {
err = os.MkdirAll(baseDir, 0777)
err = os.MkdirAll(baseDir, os.ModePerm)
if err != nil {
return err
}
@ -6318,7 +6318,7 @@ func checkSystemCommands() {
}
func initGitRepo(path string) ([]byte, error) {
err := os.MkdirAll(path, 0777)
err := os.MkdirAll(path, os.ModePerm)
if err != nil {
return nil, err
}

View file

@ -88,7 +88,7 @@ func (OsFs) Remove(name string, isDir bool) error {
// Mkdir creates a new directory with the specified name and default permissions
func (OsFs) Mkdir(name string) error {
return os.Mkdir(name, 0777)
return os.Mkdir(name, os.ModePerm)
}
// Symlink creates source as a symbolic link to target.
@ -152,7 +152,7 @@ func (OsFs) IsPermission(err error) bool {
func (fs OsFs) CheckRootPath(username string, uid int, gid int) bool {
var err error
if _, err = fs.Stat(fs.rootDir); fs.IsNotExist(err) {
err = os.MkdirAll(fs.rootDir, 0777)
err = os.MkdirAll(fs.rootDir, os.ModePerm)
fsLog(fs, logger.LevelDebug, "root directory %#v for user %#v does not exist, try to create, mkdir error: %v",
fs.rootDir, username, err)
if err == nil {
@ -383,7 +383,7 @@ func (fs *OsFs) createMissingDirs(filePath string, uid, gid int) error {
last := len(dirsToCreate) - 1
for i := range dirsToCreate {
d := dirsToCreate[last-i]
if err := os.Mkdir(d, 0777); err != nil {
if err := os.Mkdir(d, os.ModePerm); err != nil {
fsLog(fs, logger.LevelError, "error creating missing dir: %#v", d)
return err
}