-
Notifications
You must be signed in to change notification settings - Fork 137
tapcfg: validate SQLite temp dir at startup and add opt out flag #1886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package tapcfg | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "runtime" | ||
| "strings" | ||
| ) | ||
|
|
||
| // ensureDirWritable verifies that the provided directory exists, is a directory | ||
| // and is writable by creating a temporary file within it. | ||
| func ensureDirWritable(dir string) error { | ||
| dirInfo, err := os.Stat(dir) | ||
| if err != nil { | ||
| return fmt.Errorf("not accessible (dir=%s): %w", dir, err) | ||
| } | ||
|
|
||
| if !dirInfo.IsDir() { | ||
| return fmt.Errorf("not a directory (dir=%s)", dir) | ||
| } | ||
|
|
||
| tmpFile, err := os.CreateTemp(dir, "tapd-tmpdir-check-*") | ||
| if err != nil { | ||
| return fmt.Errorf("not writable (dir=%s): %w", dir, err) | ||
| } | ||
| defer func() { _ = os.Remove(tmpFile.Name()) }() | ||
|
|
||
| if err := tmpFile.Close(); err != nil { | ||
| return fmt.Errorf("not writable (dir=%s): %w", dir, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // checkSQLiteTempDir checks temp directory locations on Linux/Darwin | ||
| // and verifies the first writable option. SQLite honors SQLITE_TMPDIR first, | ||
| // then TMPDIR, then falls back to /var/tmp, /usr/tmp and /tmp. | ||
| // | ||
| // NOTE: SQLite requires a writable temp directory because several internal | ||
| // operations need temporary files when they cannot be done purely in memory. | ||
| func checkSQLiteTempDir() error { | ||
| // This check only runs for Linux/Darwin. | ||
| if runtime.GOOS != "linux" && runtime.GOOS != "darwin" { | ||
| return nil | ||
| } | ||
|
|
||
| // SQLite will use the first available temp directory; we mirror that | ||
| // behavior by trying environment variables and standard fallback | ||
| // directories in order. | ||
| var errs []string | ||
|
|
||
| type dirSource struct { | ||
| path string | ||
| source string | ||
| } | ||
|
|
||
| sources := []dirSource{ | ||
| {path: os.Getenv("SQLITE_TMPDIR"), source: "env=SQLITE_TMPDIR"}, | ||
| {path: os.Getenv("TMPDIR"), source: "env=TMPDIR"}, | ||
| {path: "/var/tmp", source: "fallback=/var/tmp"}, | ||
| {path: "/usr/tmp", source: "fallback=/usr/tmp"}, | ||
| {path: "/tmp", source: "fallback=/tmp"}, | ||
| } | ||
|
|
||
| for _, s := range sources { | ||
| if s.path == "" { | ||
| continue | ||
| } | ||
|
|
||
| err := ensureDirWritable(s.path) | ||
| if err != nil { | ||
| err = fmt.Errorf("(%s) %w", s.source, err) | ||
| errs = append(errs, err.Error()) | ||
| continue | ||
| } | ||
|
|
||
| // Found a writable temp directory. | ||
| return nil | ||
| } | ||
|
|
||
| return fmt.Errorf("no writable temp directory found; attempts=%s", | ||
| strings.Join(errs, "; ")) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.