Skip to content

Commit 71813fd

Browse files
committed
Create a user-specific temp dir to avoid permission problems on multi-user machines
In 3a9dbf7 we created a global /tmp/lazygit/ folder that contains the temp directories of each running instance, to avoid polluting /tmp with multiple folders. The problem with that approach was that the folder was created with 700 permissions, so if multiple users were using lazygit on the same machine (e.g. a server), all users except the first one would get fatal errors on startup. Fix this by creating temp folders containing the user's uid.
1 parent bcb95bd commit 71813fd

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

pkg/app/entry_point.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
_ "net/http/pprof"
99
"os"
1010
"os/exec"
11+
"os/user"
1112
"path/filepath"
1213
"runtime"
1314
"runtime/debug"
@@ -124,11 +125,7 @@ func Start(buildInfo *BuildInfo, integrationTest integrationTypes.IntegrationTes
124125
os.Exit(0)
125126
}
126127

127-
tmpDirBase := filepath.Join(os.TempDir(), "lazygit")
128-
if err := os.MkdirAll(tmpDirBase, 0o700); err != nil {
129-
log.Fatal(err.Error())
130-
}
131-
tempDir, err := os.MkdirTemp(tmpDirBase, "")
128+
tempDir, err := os.MkdirTemp(getTempDirBase(), "lazygit-*")
132129
if err != nil {
133130
log.Fatal(err.Error())
134131
}
@@ -314,3 +311,19 @@ func getGitVersionInfo() string {
314311
gitVersion := strings.Trim(strings.TrimPrefix(string(stdout), "git version "), " \r\n")
315312
return gitVersion
316313
}
314+
315+
func getTempDirBase() string {
316+
tempDir := os.TempDir()
317+
318+
user, err := user.Current()
319+
if err != nil || user.Uid == "" {
320+
return tempDir
321+
}
322+
323+
tmpDirBase := filepath.Join(tempDir, "lazygit-"+user.Uid)
324+
if err := os.MkdirAll(tmpDirBase, 0o700); err != nil {
325+
return tempDir
326+
}
327+
328+
return tmpDirBase
329+
}

0 commit comments

Comments
 (0)