Skip to content

Commit

Permalink
fix(cli): write cachedir.tag to log directories
Browse files Browse the repository at this point in the history
  • Loading branch information
NickIAm committed Aug 3, 2023
1 parent a620349 commit 3102ccd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
10 changes: 10 additions & 0 deletions internal/logfile/logfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/kopia/kopia/internal/zaplogutil"
"github.com/kopia/kopia/repo/content"
"github.com/kopia/kopia/repo/logging"
"github.com/kopia/kopia/internal/cachedir"
)

const logsDirMode = 0o700
Expand Down Expand Up @@ -213,6 +214,15 @@ func (c *loggingFlags) setupLogFileBasedLogger(now time.Time, subdir, suffix, lo
fmt.Fprintln(os.Stderr, "Unable to create logs directory:", err)
}

// write a cachetag.dir to the log path to keep log files out of the backup
cacheTagName := filepath.Join(logDir, "CACHEDIR.TAG")

if _, err := os.Stat(cacheTagName); errors.Is(err, os.ErrNotExist) {
if err := cachedir.WriteCacheMarker(logDir); err != nil {
fmt.Fprintln(os.Stderr, "Unable to write CACHETAG")
}
}

sweepLogWG := &sync.WaitGroup{}
doSweep := func() {}

Expand Down
30 changes: 26 additions & 4 deletions internal/logfile/logfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ func TestLogFileRotation(t *testing.T) {

// expected number of files per directory
subdirs := map[string]int{
"cli-logs": 3,
"content-logs": 4,
"cli-logs": 4,
"content-logs": 5,
}

for subdir, wantEntryCount := range subdirs {
Expand Down Expand Up @@ -189,8 +189,8 @@ func TestLogFileMaxTotalSize(t *testing.T) {

env.RunAndExpectSuccess(t, "snap", "ls", "--file-log-level=debug", "--log-dir", tmpLogDir, fmt.Sprintf("%s=%v", flag, size1MB/2))
size2 := getTotalDirSize(t, logSubdir)
require.LessOrEqual(t, size1, size0/2)
require.LessOrEqual(t, size2, size1/2)
require.LessOrEqual(t, size1, size0/2+291)
require.LessOrEqual(t, size2, size1/2+291)
require.Greater(t, size2, size1/4)
})
}
Expand Down Expand Up @@ -238,3 +238,25 @@ func getTotalDirSize(t *testing.T, dir string) int {

return totalSize
}

func TestCacheFileExists(t *testing.T) {
t.Parallel()

runner := testenv.NewInProcRunner(t)
runner.CustomizeApp = logfile.Attach

env := testenv.NewCLITest(t, testenv.RepoFormatNotImportant, runner)

tmpLogDir := testutil.TempDirectory(t)
cacheTagName := "CACHEDIR.TAG"

env.RunAndExpectSuccess(t, "repo", "create", "filesystem", "--path", env.RepoDir, "--log-dir", tmpLogDir)

if _, err := os.Stat(filepath.Join(tmpLogDir, "cli-logs", cacheTagName)); err != nil {
t.Fatal(err)
}

if _, err := os.Stat(filepath.Join(tmpLogDir, "content-logs", cacheTagName)); err != nil {
t.Fatal(err)
}
}

0 comments on commit 3102ccd

Please sign in to comment.