Skip to content

Commit 8798cf4

Browse files
dubeglafriks
authored andcommitted
Set session and indexers' data files rel to AppDataPath (#2192)
* Set session and indexers' data files rel to AppDataPath The setting AppDataPath is now relative to the working directory. The session svc's PROVIDER_CONFIG now defaults to AppDataPath/data/sessions. The issue indexer's IssuePath now defaults to AppDataPath/indexers/issues.bleves. * fix bug
1 parent 95637e0 commit 8798cf4

File tree

5 files changed

+73
-74
lines changed

5 files changed

+73
-74
lines changed

cmd/dump.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,7 @@ func runDump(ctx *cli.Context) error {
126126

127127
var sessionAbsPath string
128128
if setting.SessionConfig.Provider == "file" {
129-
if len(setting.SessionConfig.ProviderConfig) == 0 {
130-
setting.SessionConfig.ProviderConfig = "data/sessions"
131-
}
132-
sessionAbsPath, _ = filepath.Abs(setting.SessionConfig.ProviderConfig)
129+
sessionAbsPath = setting.SessionConfig.ProviderConfig
133130
}
134131
if err := zipAddDirectoryExclude(z, "data", setting.AppDataPath, sessionAbsPath); err != nil {
135132
log.Fatalf("Failed to include data directory: %v", err)

cmd/serv.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ func setup(logPath string) error {
5050
models.LoadConfigs()
5151

5252
if setting.UseSQLite3 || setting.UseTiDB {
53-
workDir, _ := setting.WorkDir()
54-
if err := os.Chdir(workDir); err != nil {
55-
log.GitLogger.Fatal(4, "Failed to change directory %s: %v", workDir, err)
53+
workPath := setting.AppWorkPath
54+
if err := os.Chdir(workPath); err != nil {
55+
log.GitLogger.Fatal(4, "Failed to change directory %s: %v", workPath, err)
5656
}
5757
}
5858

models/models.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import (
1111
"net/url"
1212
"os"
1313
"path"
14+
"path/filepath"
1415
"strings"
1516

1617
"code.gitea.io/gitea/modules/log"
1718
"code.gitea.io/gitea/modules/setting"
18-
"code.gitea.io/gitea/modules/util"
1919

2020
// Needed for the MySQL driver
2121
_ "github.com/go-sql-driver/mysql"
@@ -152,11 +152,15 @@ func LoadConfigs() {
152152
DbCfg.Timeout = sec.Key("SQLITE_TIMEOUT").MustInt(500)
153153

154154
sec = setting.Cfg.Section("indexer")
155-
setting.Indexer.IssuePath = absolutePath(
156-
sec.Key("ISSUE_INDEXER_PATH").MustString("indexers/issues.bleve"))
155+
setting.Indexer.IssuePath = sec.Key("ISSUE_INDEXER_PATH").MustString(path.Join(setting.AppDataPath, "indexers/issues.bleve"))
156+
if !filepath.IsAbs(setting.Indexer.IssuePath) {
157+
setting.Indexer.IssuePath = path.Join(setting.AppWorkPath, setting.Indexer.IssuePath)
158+
}
157159
setting.Indexer.RepoIndexerEnabled = sec.Key("REPO_INDEXER_ENABLED").MustBool(false)
158-
setting.Indexer.RepoPath = absolutePath(
159-
sec.Key("REPO_INDEXER_PATH").MustString("indexers/repos.bleve"))
160+
setting.Indexer.RepoPath = sec.Key("REPO_INDEXER_PATH").MustString(path.Join(setting.AppDataPath, "indexers/repos.bleve"))
161+
if !filepath.IsAbs(setting.Indexer.RepoPath) {
162+
setting.Indexer.RepoPath = path.Join(setting.AppWorkPath, setting.Indexer.RepoPath)
163+
}
160164
setting.Indexer.UpdateQueueLength = sec.Key("UPDATE_BUFFER_LEN").MustInt(20)
161165
setting.Indexer.MaxIndexerFileSize = sec.Key("MAX_FILE_SIZE").MustInt64(512 * 1024 * 1024)
162166
}
@@ -343,12 +347,3 @@ func DumpDatabase(filePath string, dbType string) error {
343347
}
344348
return x.DumpTablesToFile(tbs, filePath)
345349
}
346-
347-
// absolutePath make path absolute if it is relative
348-
func absolutePath(path string) string {
349-
workDir, err := setting.WorkDir()
350-
if err != nil {
351-
log.Fatal(4, "Failed to get work directory: %v", err)
352-
}
353-
return util.EnsureAbsolutePath(path, workDir)
354-
}

modules/setting/setting.go

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ var (
7171
AppSubURLDepth int // Number of slashes
7272
AppPath string
7373
AppDataPath string
74+
AppWorkPath string
7475

7576
// Server settings
7677
Protocol Scheme
@@ -525,53 +526,57 @@ func DateLang(lang string) string {
525526
return "en"
526527
}
527528

528-
// execPath returns the executable path.
529-
func execPath() (string, error) {
530-
execFile := os.Args[0]
531-
if IsWindows && filepath.IsAbs(execFile) {
532-
return filepath.Clean(execFile), nil
529+
func getAppPath() (string, error) {
530+
var appPath string
531+
var err error
532+
if IsWindows && filepath.IsAbs(os.Args[0]) {
533+
appPath = filepath.Clean(os.Args[0])
534+
} else {
535+
appPath, err = exec.LookPath(os.Args[0])
533536
}
534-
file, err := exec.LookPath(execFile)
537+
535538
if err != nil {
536539
return "", err
537540
}
538-
return filepath.Abs(file)
541+
appPath, err = filepath.Abs(appPath)
542+
if err != nil {
543+
return "", err
544+
}
545+
// Note: we don't use path.Dir here because it does not handle case
546+
// which path starts with two "/" in Windows: "//psf/Home/..."
547+
return strings.Replace(appPath, "\\", "/", -1), err
548+
}
549+
550+
func getWorkPath(appPath string) string {
551+
workPath := ""
552+
giteaWorkPath := os.Getenv("GITEA_WORK_DIR")
553+
gogsWorkPath := os.Getenv("GOGS_WORK_DIR")
554+
555+
if len(giteaWorkPath) > 0 {
556+
workPath = giteaWorkPath
557+
} else if len(gogsWorkPath) > 0 {
558+
log.Warn(`Usage of GOGS_WORK_DIR is deprecated and will be *removed* in a future release, please consider changing to GITEA_WORK_DIR`)
559+
workPath = gogsWorkPath
560+
} else {
561+
i := strings.LastIndex(appPath, "/")
562+
if i == -1 {
563+
workPath = appPath
564+
} else {
565+
workPath = appPath[:i]
566+
}
567+
}
568+
return strings.Replace(workPath, "\\", "/", -1)
539569
}
540570

541571
func init() {
542572
IsWindows = runtime.GOOS == "windows"
543573
log.NewLogger(0, "console", `{"level": 0}`)
544574

545575
var err error
546-
if AppPath, err = execPath(); err != nil {
576+
if AppPath, err = getAppPath(); err != nil {
547577
log.Fatal(4, "Failed to get app path: %v", err)
548578
}
549-
550-
// Note: we don't use path.Dir here because it does not handle case
551-
// which path starts with two "/" in Windows: "//psf/Home/..."
552-
AppPath = strings.Replace(AppPath, "\\", "/", -1)
553-
}
554-
555-
// WorkDir returns absolute path of work directory.
556-
func WorkDir() (string, error) {
557-
wd := os.Getenv("GITEA_WORK_DIR")
558-
if len(wd) > 0 {
559-
return wd, nil
560-
}
561-
// Use GOGS_WORK_DIR if available, for backward compatibility
562-
// TODO: drop in 1.1.0 ?
563-
wd = os.Getenv("GOGS_WORK_DIR")
564-
if len(wd) > 0 {
565-
log.Warn(`Usage of GOGS_WORK_DIR is deprecated and will be *removed* in a future release,
566-
please consider changing to GITEA_WORK_DIR`)
567-
return wd, nil
568-
}
569-
570-
i := strings.LastIndex(AppPath, "/")
571-
if i == -1 {
572-
return AppPath, nil
573-
}
574-
return AppPath[:i], nil
579+
AppWorkPath = getWorkPath(AppPath)
575580
}
576581

577582
func forcePathSeparator(path string) {
@@ -612,30 +617,27 @@ func createPIDFile(pidPath string) {
612617
// NewContext initializes configuration context.
613618
// NOTE: do not print any log except error.
614619
func NewContext() {
615-
workDir, err := WorkDir()
616-
if err != nil {
617-
log.Fatal(4, "Failed to get work directory: %v", err)
618-
}
619-
620620
Cfg = ini.Empty()
621621

622622
CustomPath = os.Getenv("GITEA_CUSTOM")
623623
if len(CustomPath) == 0 {
624-
CustomPath = workDir + "/custom"
624+
CustomPath = path.Join(AppWorkPath, "custom")
625+
} else if !filepath.IsAbs(CustomPath) {
626+
CustomPath = path.Join(AppWorkPath, CustomPath)
625627
}
626628

627629
if len(CustomPID) > 0 {
628630
createPIDFile(CustomPID)
629631
}
630632

631633
if len(CustomConf) == 0 {
632-
CustomConf = CustomPath + "/conf/app.ini"
634+
CustomConf = path.Join(CustomPath, "conf/app.ini")
633635
} else if !filepath.IsAbs(CustomConf) {
634-
CustomConf = filepath.Join(workDir, CustomConf)
636+
CustomConf = path.Join(CustomPath, CustomConf)
635637
}
636638

637639
if com.IsFile(CustomConf) {
638-
if err = Cfg.Append(CustomConf); err != nil {
640+
if err := Cfg.Append(CustomConf); err != nil {
639641
log.Fatal(4, "Failed to load custom conf '%s': %v", CustomConf, err)
640642
}
641643
} else {
@@ -649,7 +651,7 @@ func NewContext() {
649651
}
650652
homeDir = strings.Replace(homeDir, "\\", "/", -1)
651653

652-
LogRootPath = Cfg.Section("log").Key("ROOT_PATH").MustString(path.Join(workDir, "log"))
654+
LogRootPath = Cfg.Section("log").Key("ROOT_PATH").MustString(path.Join(AppWorkPath, "log"))
653655
forcePathSeparator(LogRootPath)
654656

655657
sec := Cfg.Section("server")
@@ -716,8 +718,8 @@ func NewContext() {
716718
LocalURL = sec.Key("LOCAL_ROOT_URL").MustString(defaultLocalURL)
717719
OfflineMode = sec.Key("OFFLINE_MODE").MustBool()
718720
DisableRouterLog = sec.Key("DISABLE_ROUTER_LOG").MustBool()
719-
StaticRootPath = sec.Key("STATIC_ROOT_PATH").MustString(workDir)
720-
AppDataPath = sec.Key("APP_DATA_PATH").MustString("data")
721+
StaticRootPath = sec.Key("STATIC_ROOT_PATH").MustString(AppWorkPath)
722+
AppDataPath = sec.Key("APP_DATA_PATH").MustString(path.Join(AppWorkPath, "data"))
721723
EnableGzip = sec.Key("ENABLE_GZIP").MustBool()
722724
EnablePprof = sec.Key("ENABLE_PPROF").MustBool(false)
723725

@@ -783,7 +785,7 @@ func NewContext() {
783785
}
784786
LFS.ContentPath = sec.Key("LFS_CONTENT_PATH").MustString(filepath.Join(AppDataPath, "lfs"))
785787
if !filepath.IsAbs(LFS.ContentPath) {
786-
LFS.ContentPath = filepath.Join(workDir, LFS.ContentPath)
788+
LFS.ContentPath = filepath.Join(AppWorkPath, LFS.ContentPath)
787789
}
788790

789791
if LFS.StartServer {
@@ -915,7 +917,7 @@ func NewContext() {
915917
sec = Cfg.Section("attachment")
916918
AttachmentPath = sec.Key("PATH").MustString(path.Join(AppDataPath, "attachments"))
917919
if !filepath.IsAbs(AttachmentPath) {
918-
AttachmentPath = path.Join(workDir, AttachmentPath)
920+
AttachmentPath = path.Join(AppWorkPath, AttachmentPath)
919921
}
920922
AttachmentAllowedTypes = strings.Replace(sec.Key("ALLOWED_TYPES").MustString("image/jpeg,image/png,application/zip,application/gzip"), "|", ",", -1)
921923
AttachmentMaxSize = sec.Key("MAX_SIZE").MustInt64(4)
@@ -969,7 +971,7 @@ func NewContext() {
969971
RepoRootPath = sec.Key("ROOT").MustString(path.Join(homeDir, "gitea-repositories"))
970972
forcePathSeparator(RepoRootPath)
971973
if !filepath.IsAbs(RepoRootPath) {
972-
RepoRootPath = path.Join(workDir, RepoRootPath)
974+
RepoRootPath = path.Join(AppWorkPath, RepoRootPath)
973975
} else {
974976
RepoRootPath = path.Clean(RepoRootPath)
975977
}
@@ -985,14 +987,14 @@ func NewContext() {
985987
}
986988

987989
if !filepath.IsAbs(Repository.Upload.TempPath) {
988-
Repository.Upload.TempPath = path.Join(workDir, Repository.Upload.TempPath)
990+
Repository.Upload.TempPath = path.Join(AppWorkPath, Repository.Upload.TempPath)
989991
}
990992

991993
sec = Cfg.Section("picture")
992994
AvatarUploadPath = sec.Key("AVATAR_UPLOAD_PATH").MustString(path.Join(AppDataPath, "avatars"))
993995
forcePathSeparator(AvatarUploadPath)
994996
if !filepath.IsAbs(AvatarUploadPath) {
995-
AvatarUploadPath = path.Join(workDir, AvatarUploadPath)
997+
AvatarUploadPath = path.Join(AppWorkPath, AvatarUploadPath)
996998
}
997999
switch source := sec.Key("GRAVATAR_SOURCE").MustString("gravatar"); source {
9981000
case "duoshuo":
@@ -1254,7 +1256,7 @@ func NewXORMLogService(disableConsole bool) {
12541256
if err = os.MkdirAll(path.Dir(logPath), os.ModePerm); err != nil {
12551257
panic(err.Error())
12561258
}
1257-
logPath = filepath.Join(filepath.Dir(logPath), "xorm.log")
1259+
logPath = path.Join(filepath.Dir(logPath), "xorm.log")
12581260

12591261
logConfigs = fmt.Sprintf(
12601262
`{"level":%s,"filename":"%s","rotate":%v,"maxlines":%d,"maxsize":%d,"daily":%v,"maxdays":%d}`, level,
@@ -1341,7 +1343,10 @@ func newCacheService() {
13411343
func newSessionService() {
13421344
SessionConfig.Provider = Cfg.Section("session").Key("PROVIDER").In("memory",
13431345
[]string{"memory", "file", "redis", "mysql"})
1344-
SessionConfig.ProviderConfig = strings.Trim(Cfg.Section("session").Key("PROVIDER_CONFIG").String(), "\" ")
1346+
SessionConfig.ProviderConfig = strings.Trim(Cfg.Section("session").Key("PROVIDER_CONFIG").MustString(path.Join(AppDataPath, "sessions")), "\" ")
1347+
if !filepath.IsAbs(SessionConfig.ProviderConfig) {
1348+
SessionConfig.ProviderConfig = path.Join(AppWorkPath, SessionConfig.ProviderConfig)
1349+
}
13451350
SessionConfig.CookieName = Cfg.Section("session").Key("COOKIE_NAME").MustString("i_like_gitea")
13461351
SessionConfig.CookiePath = AppSubURL
13471352
SessionConfig.Secure = Cfg.Section("session").Key("COOKIE_SECURE").MustBool(false)

routers/init.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ func NewServices() {
4545
// GlobalInit is for global configuration reload-able.
4646
func GlobalInit() {
4747
setting.NewContext()
48+
log.Trace("AppPath: %s", setting.AppPath)
49+
log.Trace("AppWorkPath: %s", setting.AppWorkPath)
4850
log.Trace("Custom path: %s", setting.CustomPath)
4951
log.Trace("Log path: %s", setting.LogRootPath)
5052
models.LoadConfigs()

0 commit comments

Comments
 (0)