-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save files in local storage as umask (#21198)
Go creates temporary files as 600, but sometimes we want the group to be able to read them (for example, for another user to back up the storage) This PR applies the umask to the renamed tmp files in local storage. Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
- Loading branch information
1 parent
83680c9
commit cca189e
Showing
4 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build !windows | ||
|
||
package util | ||
|
||
import ( | ||
"os" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
var defaultUmask int | ||
|
||
func init() { | ||
// at the moment, the umask could only be gotten by calling unix.Umask(newUmask) | ||
// use 0o077 as temp new umask to reduce the risks if this umask is used anywhere else before the correct umask is recovered | ||
tempUmask := 0o077 | ||
defaultUmask = unix.Umask(tempUmask) | ||
unix.Umask(defaultUmask) | ||
} | ||
|
||
func ApplyUmask(f string, newMode os.FileMode) error { | ||
mod := newMode & ^os.FileMode(defaultUmask) | ||
return os.Chmod(f, mod) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build !windows | ||
|
||
package util | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestApplyUmask(t *testing.T) { | ||
f, err := os.CreateTemp(t.TempDir(), "test-filemode-") | ||
assert.NoError(t, err) | ||
|
||
err = os.Chmod(f.Name(), 0o777) | ||
assert.NoError(t, err) | ||
st, err := os.Stat(f.Name()) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 0o777, st.Mode().Perm()&0o777) | ||
|
||
oldDefaultUmask := defaultUmask | ||
defaultUmask = 0o037 | ||
defer func() { | ||
defaultUmask = oldDefaultUmask | ||
}() | ||
err = ApplyUmask(f.Name(), os.ModePerm) | ||
assert.NoError(t, err) | ||
st, err = os.Stat(f.Name()) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 0o740, st.Mode().Perm()&0o777) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build windows | ||
|
||
package util | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
func ApplyUmask(f string, newMode os.FileMode) error { | ||
// do nothing for Windows, because Windows doesn't use umask | ||
return nil | ||
} |