Skip to content

Commit 9cc970a

Browse files
committed
chore: fix data directory handling
1 parent 4180613 commit 9cc970a

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

internal/profile/profile.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ type Profile struct {
3737
func checkDataDir(dataDir string) (string, error) {
3838
// Convert to absolute path if relative path is supplied.
3939
if !filepath.IsAbs(dataDir) {
40-
relativeDir := filepath.Join(filepath.Dir(os.Args[0]), dataDir)
41-
absDir, err := filepath.Abs(relativeDir)
40+
// Use current working directory, not the binary's directory
41+
// This ensures we use the actual working directory where the process runs
42+
absDir, err := filepath.Abs(dataDir)
4243
if err != nil {
4344
return "", err
4445
}
@@ -56,23 +57,24 @@ func checkDataDir(dataDir string) (string, error) {
5657
func (p *Profile) Validate() error {
5758
// Set default data directory if not specified
5859
if p.Data == "" {
59-
if p.Demo {
60-
// In demo mode, use current directory
61-
p.Data = "."
60+
if runtime.GOOS == "windows" {
61+
p.Data = filepath.Join(os.Getenv("ProgramData"), "memos")
6262
} else {
63-
// In production mode, use system directory
64-
if runtime.GOOS == "windows" {
65-
p.Data = filepath.Join(os.Getenv("ProgramData"), "memos")
66-
} else {
67-
// On Linux/macOS, check if /var/opt/memos exists (Docker scenario)
68-
// If not, fall back to current directory to avoid permission issues
69-
if _, err := os.Stat("/var/opt/memos"); err == nil {
63+
// On Linux/macOS, check if /var/opt/memos exists and is writable (Docker scenario)
64+
if info, err := os.Stat("/var/opt/memos"); err == nil && info.IsDir() {
65+
// Check if we can write to this directory
66+
testFile := filepath.Join("/var/opt/memos", ".write-test")
67+
if err := os.WriteFile(testFile, []byte("test"), 0600); err == nil {
68+
os.Remove(testFile)
7069
p.Data = "/var/opt/memos"
7170
} else {
72-
slog.Warn("default production data directory /var/opt/memos not accessible, using current directory. " +
73-
"Consider using --data flag to specify a data directory.")
71+
// /var/opt/memos exists but is not writable, use current directory
72+
slog.Warn("/var/opt/memos is not writable, using current directory")
7473
p.Data = "."
7574
}
75+
} else {
76+
// /var/opt/memos doesn't exist, use current directory (local development)
77+
p.Data = "."
7678
}
7779
}
7880
}

scripts/Dockerfile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,24 @@ RUN --mount=type=cache,target=/go/pkg/mod \
2727

2828
# Use minimal Alpine with security updates
2929
FROM alpine:3.21 AS monolithic
30-
WORKDIR /usr/local/memos
3130

3231
# Install runtime dependencies and create non-root user in single layer
3332
RUN apk add --no-cache tzdata ca-certificates && \
3433
addgroup -g 10001 -S nonroot && \
3534
adduser -u 10001 -S -G nonroot -h /var/opt/memos nonroot && \
36-
mkdir -p /var/opt/memos && \
35+
mkdir -p /var/opt/memos /usr/local/memos && \
3736
chown -R nonroot:nonroot /var/opt/memos
3837

39-
# Copy binary and entrypoint
38+
# Copy binary and entrypoint to /usr/local/memos
4039
COPY --from=backend /backend-build/memos /usr/local/memos/memos
4140
COPY --from=backend --chmod=755 /backend-build/scripts/entrypoint.sh /usr/local/memos/entrypoint.sh
4241

4342
# Switch to non-root user
4443
USER nonroot:nonroot
4544

45+
# Set working directory to the writable volume
46+
WORKDIR /var/opt/memos
47+
4648
# Data directory
4749
VOLUME /var/opt/memos
4850

0 commit comments

Comments
 (0)