Browse Source

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
Guillaume Dube 1 year ago
parent
commit
8798cf4e3b
5 changed files with 73 additions and 74 deletions
  1. 1 4
      cmd/dump.go
  2. 3 3
      cmd/serv.go
  3. 9 14
      models/models.go
  4. 58 53
      modules/setting/setting.go
  5. 2 0
      routers/init.go

+ 1 - 4
cmd/dump.go

@@ -126,10 +126,7 @@ func runDump(ctx *cli.Context) error {
126 126
 
127 127
 		var sessionAbsPath string
128 128
 		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
133 130
 		}
134 131
 		if err := zipAddDirectoryExclude(z, "data", setting.AppDataPath, sessionAbsPath); err != nil {
135 132
 			log.Fatalf("Failed to include data directory: %v", err)

+ 3 - 3
cmd/serv.go

@@ -50,9 +50,9 @@ func setup(logPath string) error {
50 50
 	models.LoadConfigs()
51 51
 
52 52
 	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)
56 56
 		}
57 57
 	}
58 58
 

+ 9 - 14
models/models.go

@@ -11,11 +11,11 @@ import (
11 11
 	"net/url"
12 12
 	"os"
13 13
 	"path"
14
+	"path/filepath"
14 15
 	"strings"
15 16
 
16 17
 	"code.gitea.io/gitea/modules/log"
17 18
 	"code.gitea.io/gitea/modules/setting"
18
-	"code.gitea.io/gitea/modules/util"
19 19
 
20 20
 	// Needed for the MySQL driver
21 21
 	_ "github.com/go-sql-driver/mysql"
@@ -152,11 +152,15 @@ func LoadConfigs() {
152 152
 	DbCfg.Timeout = sec.Key("SQLITE_TIMEOUT").MustInt(500)
153 153
 
154 154
 	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
+	}
157 159
 	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
+	}
160 164
 	setting.Indexer.UpdateQueueLength = sec.Key("UPDATE_BUFFER_LEN").MustInt(20)
161 165
 	setting.Indexer.MaxIndexerFileSize = sec.Key("MAX_FILE_SIZE").MustInt64(512 * 1024 * 1024)
162 166
 }
@@ -343,12 +347,3 @@ func DumpDatabase(filePath string, dbType string) error {
343 347
 	}
344 348
 	return x.DumpTablesToFile(tbs, filePath)
345 349
 }
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
-}

+ 58 - 53
modules/setting/setting.go

@@ -71,6 +71,7 @@ var (
71 71
 	AppSubURLDepth int // Number of slashes
72 72
 	AppPath        string
73 73
 	AppDataPath    string
74
+	AppWorkPath    string
74 75
 
75 76
 	// Server settings
76 77
 	Protocol             Scheme
@@ -525,17 +526,46 @@ func DateLang(lang string) string {
525 526
 	return "en"
526 527
 }
527 528
 
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])
533 536
 	}
534
-	file, err := exec.LookPath(execFile)
537
+
535 538
 	if err != nil {
536 539
 		return "", err
537 540
 	}
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)
539 569
 }
540 570
 
541 571
 func init() {
@@ -543,35 +573,10 @@ func init() {
543 573
 	log.NewLogger(0, "console", `{"level": 0}`)
544 574
 
545 575
 	var err error
546
-	if AppPath, err = execPath(); err != nil {
576
+	if AppPath, err = getAppPath(); err != nil {
547 577
 		log.Fatal(4, "Failed to get app path: %v", err)
548 578
 	}
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)
575 580
 }
576 581
 
577 582
 func forcePathSeparator(path string) {
@@ -612,16 +617,13 @@ func createPIDFile(pidPath string) {
612 617
 // NewContext initializes configuration context.
613 618
 // NOTE: do not print any log except error.
614 619
 func NewContext() {
615
-	workDir, err := WorkDir()
616
-	if err != nil {
617
-		log.Fatal(4, "Failed to get work directory: %v", err)
618
-	}
619
-
620 620
 	Cfg = ini.Empty()
621 621
 
622 622
 	CustomPath = os.Getenv("GITEA_CUSTOM")
623 623
 	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)
625 627
 	}
626 628
 
627 629
 	if len(CustomPID) > 0 {
@@ -629,13 +631,13 @@ func NewContext() {
629 631
 	}
630 632
 
631 633
 	if len(CustomConf) == 0 {
632
-		CustomConf = CustomPath + "/conf/app.ini"
634
+		CustomConf = path.Join(CustomPath, "conf/app.ini")
633 635
 	} else if !filepath.IsAbs(CustomConf) {
634
-		CustomConf = filepath.Join(workDir, CustomConf)
636
+		CustomConf = path.Join(CustomPath, CustomConf)
635 637
 	}
636 638
 
637 639
 	if com.IsFile(CustomConf) {
638
-		if err = Cfg.Append(CustomConf); err != nil {
640
+		if err := Cfg.Append(CustomConf); err != nil {
639 641
 			log.Fatal(4, "Failed to load custom conf '%s': %v", CustomConf, err)
640 642
 		}
641 643
 	} else {
@@ -649,7 +651,7 @@ func NewContext() {
649 651
 	}
650 652
 	homeDir = strings.Replace(homeDir, "\\", "/", -1)
651 653
 
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"))
653 655
 	forcePathSeparator(LogRootPath)
654 656
 
655 657
 	sec := Cfg.Section("server")
@@ -716,8 +718,8 @@ func NewContext() {
716 718
 	LocalURL = sec.Key("LOCAL_ROOT_URL").MustString(defaultLocalURL)
717 719
 	OfflineMode = sec.Key("OFFLINE_MODE").MustBool()
718 720
 	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"))
721 723
 	EnableGzip = sec.Key("ENABLE_GZIP").MustBool()
722 724
 	EnablePprof = sec.Key("ENABLE_PPROF").MustBool(false)
723 725
 
@@ -783,7 +785,7 @@ func NewContext() {
783 785
 	}
784 786
 	LFS.ContentPath = sec.Key("LFS_CONTENT_PATH").MustString(filepath.Join(AppDataPath, "lfs"))
785 787
 	if !filepath.IsAbs(LFS.ContentPath) {
786
-		LFS.ContentPath = filepath.Join(workDir, LFS.ContentPath)
788
+		LFS.ContentPath = filepath.Join(AppWorkPath, LFS.ContentPath)
787 789
 	}
788 790
 
789 791
 	if LFS.StartServer {
@@ -915,7 +917,7 @@ func NewContext() {
915 917
 	sec = Cfg.Section("attachment")
916 918
 	AttachmentPath = sec.Key("PATH").MustString(path.Join(AppDataPath, "attachments"))
917 919
 	if !filepath.IsAbs(AttachmentPath) {
918
-		AttachmentPath = path.Join(workDir, AttachmentPath)
920
+		AttachmentPath = path.Join(AppWorkPath, AttachmentPath)
919 921
 	}
920 922
 	AttachmentAllowedTypes = strings.Replace(sec.Key("ALLOWED_TYPES").MustString("image/jpeg,image/png,application/zip,application/gzip"), "|", ",", -1)
921 923
 	AttachmentMaxSize = sec.Key("MAX_SIZE").MustInt64(4)
@@ -969,7 +971,7 @@ func NewContext() {
969 971
 	RepoRootPath = sec.Key("ROOT").MustString(path.Join(homeDir, "gitea-repositories"))
970 972
 	forcePathSeparator(RepoRootPath)
971 973
 	if !filepath.IsAbs(RepoRootPath) {
972
-		RepoRootPath = path.Join(workDir, RepoRootPath)
974
+		RepoRootPath = path.Join(AppWorkPath, RepoRootPath)
973 975
 	} else {
974 976
 		RepoRootPath = path.Clean(RepoRootPath)
975 977
 	}
@@ -985,14 +987,14 @@ func NewContext() {
985 987
 	}
986 988
 
987 989
 	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)
989 991
 	}
990 992
 
991 993
 	sec = Cfg.Section("picture")
992 994
 	AvatarUploadPath = sec.Key("AVATAR_UPLOAD_PATH").MustString(path.Join(AppDataPath, "avatars"))
993 995
 	forcePathSeparator(AvatarUploadPath)
994 996
 	if !filepath.IsAbs(AvatarUploadPath) {
995
-		AvatarUploadPath = path.Join(workDir, AvatarUploadPath)
997
+		AvatarUploadPath = path.Join(AppWorkPath, AvatarUploadPath)
996 998
 	}
997 999
 	switch source := sec.Key("GRAVATAR_SOURCE").MustString("gravatar"); source {
998 1000
 	case "duoshuo":
@@ -1254,7 +1256,7 @@ func NewXORMLogService(disableConsole bool) {
1254 1256
 			if err = os.MkdirAll(path.Dir(logPath), os.ModePerm); err != nil {
1255 1257
 				panic(err.Error())
1256 1258
 			}
1257
-			logPath = filepath.Join(filepath.Dir(logPath), "xorm.log")
1259
+			logPath = path.Join(filepath.Dir(logPath), "xorm.log")
1258 1260
 
1259 1261
 			logConfigs = fmt.Sprintf(
1260 1262
 				`{"level":%s,"filename":"%s","rotate":%v,"maxlines":%d,"maxsize":%d,"daily":%v,"maxdays":%d}`, level,
@@ -1341,7 +1343,10 @@ func newCacheService() {
1341 1343
 func newSessionService() {
1342 1344
 	SessionConfig.Provider = Cfg.Section("session").Key("PROVIDER").In("memory",
1343 1345
 		[]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
+	}
1345 1350
 	SessionConfig.CookieName = Cfg.Section("session").Key("COOKIE_NAME").MustString("i_like_gitea")
1346 1351
 	SessionConfig.CookiePath = AppSubURL
1347 1352
 	SessionConfig.Secure = Cfg.Section("session").Key("COOKIE_SECURE").MustBool(false)

+ 2 - 0
routers/init.go

@@ -45,6 +45,8 @@ func NewServices() {
45 45
 // GlobalInit is for global configuration reload-able.
46 46
 func GlobalInit() {
47 47
 	setting.NewContext()
48
+	log.Trace("AppPath: %s", setting.AppPath)
49
+	log.Trace("AppWorkPath: %s", setting.AppWorkPath)
48 50
 	log.Trace("Custom path: %s", setting.CustomPath)
49 51
 	log.Trace("Log path: %s", setting.LogRootPath)
50 52
 	models.LoadConfigs()