diff options
author | Gusted <williamzijl7@hotmail.com> | 2022-02-08 02:34:37 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-08 09:34:37 +0800 |
commit | 7b25a010c89835e036f1b0cc5af65249bfd2781b (patch) | |
tree | 33df1f1462c96cf8dbc2afa00dffa433ab231a44 | |
parent | 8422b1c55c979d3077614eb9bb027876695f3240 (diff) | |
download | gitea-7b25a010c89835e036f1b0cc5af65249bfd2781b.tar.gz gitea-7b25a010c89835e036f1b0cc5af65249bfd2781b.zip |
Only request write when necessary (#18657)
* Only request write when necessary
- Only request write for `INTERNAL_TOKEN_URI` when no token was found.
- Resolves #18655
* Fix perm
* Update setting.go
* Update setting.go
* Update setting.go
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: zeripath <art27@cantab.net>
-rw-r--r-- | modules/setting/setting.go | 15 |
1 files changed, 4 insertions, 11 deletions
diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 7841fbcdc3..ee2821df07 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -8,7 +8,6 @@ package setting import ( "encoding/base64" "fmt" - "io" "math" "net" "net/url" @@ -1082,28 +1081,22 @@ func loadInternalToken(sec *ini.Section) string { } switch tempURI.Scheme { case "file": - fp, err := os.OpenFile(tempURI.RequestURI(), os.O_RDWR, 0o600) - if err != nil { + buf, err := os.ReadFile(tempURI.RequestURI()) + if err != nil && !os.IsNotExist(err) { log.Fatal("Failed to open InternalTokenURI (%s): %v", uri, err) } - defer fp.Close() - - buf, err := io.ReadAll(fp) - if err != nil { - log.Fatal("Failed to read InternalTokenURI (%s): %v", uri, err) - } // No token in the file, generate one and store it. if len(buf) == 0 { token, err := generate.NewInternalToken() if err != nil { log.Fatal("Error generate internal token: %v", err) } - if _, err := io.WriteString(fp, token); err != nil { + err = os.WriteFile(tempURI.RequestURI(), []byte(token), 0o600) + if err != nil { log.Fatal("Error writing to InternalTokenURI (%s): %v", uri, err) } return token } - return strings.TrimSpace(string(buf)) default: log.Fatal("Unsupported URI-Scheme %q (INTERNAL_TOKEN_URI = %q)", tempURI.Scheme, uri) |