diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index d24d668440d..5fc4fa48cef 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -290,7 +290,9 @@ ;; Default path for App data ;APP_DATA_PATH = data ; relative paths will be made absolute with _`AppWorkPath`_ ;; -;; Base path for App's temp files, leave empty to use the managed tmp directory in APP_DATA_PATH +;; Base path for temp files, relative to APP_DATA_PATH, leave empty to use the default managed tmp directory. +;; * For NFS instances: prefer an absolute local path for better writing performance. +;; * For HA and load-balanced instances: use a shared directory for all instances. ;APP_TEMP_PATH = ;; ;; Enable gzip compression for runtime-generated content, static resources excluded diff --git a/modules/setting/path.go b/modules/setting/path.go index 545fa0efb78..15aad75c377 100644 --- a/modules/setting/path.go +++ b/modules/setting/path.go @@ -211,9 +211,13 @@ func MockBuiltinPaths(workPath, customPath, customConf string) func() { // AppDataTempDir returns a managed temporary directory for the application data. // Using empty sub will get the managed base temp directory, and it's safe to delete it. -// Gitea only creates subdirectories under it, but not the APP_TEMP_PATH directory itself. +// Gitea only creates subdirectories under it, but not the APP_TEMP_PATH directory itself, +// so all temp directories and files can be deleted safely, Gitea also cleans up them when it starts up. // * When APP_TEMP_PATH="/tmp": the managed temp directory is "/tmp/gitea-tmp" // * When APP_TEMP_PATH is not set: the managed temp directory is "/{APP_DATA_PATH}/tmp" +// In some cases, the temp directory should be carefully chosen: +// * For NFS instances: prefer an absolute local path, because NFS may have issues with file writing performance. +// * For HA and load-balanced instances: use a shared directory, temp files may be needed by different requests (e.g.: package uploading) func AppDataTempDir(sub string) *tempdir.TempDir { if appTempPathInternal != "" { return tempdir.New(appTempPathInternal, "gitea-tmp/"+sub) diff --git a/modules/setting/path_test.go b/modules/setting/path_test.go index fc6a2116dc9..fd4cf8f43d0 100644 --- a/modules/setting/path_test.go +++ b/modules/setting/path_test.go @@ -115,6 +115,15 @@ func TestInitWorkPathAndCommonConfig(t *testing.T) { assert.True(t, AppWorkPathMismatch) }) + t.Run("AppTempPathRelativeToAppDataPath", func(t *testing.T) { + iniTempPath := fp(tmpDir, "app-temppath.ini") + _ = os.WriteFile(iniTempPath, []byte("[server]\nAPP_TEMP_PATH=temp"), 0o644) + _ = os.MkdirAll(fp(dirFoo, "data/temp"), 0o755) + testInit(dirFoo, "", "") + InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniTempPath}) + assert.Equal(t, fp(dirFoo, "data/temp/gitea-tmp"), AppDataTempDir("").JoinPath()) + }) + t.Run("Builtin", func(t *testing.T) { testInit(dirFoo, dirBar, dirXxx) InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{}) diff --git a/modules/setting/server.go b/modules/setting/server.go index e1b62418270..402224abb4e 100644 --- a/modules/setting/server.go +++ b/modules/setting/server.go @@ -265,6 +265,9 @@ func loadServerFrom(rootCfg ConfigProvider) { appTempPathInternal = sec.Key("APP_TEMP_PATH").String() if appTempPathInternal != "" { + if !filepath.IsAbs(appTempPathInternal) { + appTempPathInternal = filepath.Join(AppDataPath, appTempPathInternal) + } if _, err := os.Stat(appTempPathInternal); err != nil { log.Fatal("APP_TEMP_PATH %q is not accessible: %v", appTempPathInternal, err) }