Skip to content
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

fix(cli): write CACHEDIR.TAG to log directories #3147

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}