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

Prevent log purging of symlinks #5154

Merged
merged 1 commit into from
Sep 5, 2019
Merged
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
15 changes: 9 additions & 6 deletions go/vt/logutil/logutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ func TestPurgeByMtime(t *testing.T) {
t.Fatalf("os.Chtimes: %v", err)
}
}
now := time.Date(2020, 1, 1, 12, 15, 0, 0, time.UTC)
now := time.Date(2020, 1, 1, 12, 0, 0, 0, time.UTC)
filenameMtimeMap := map[string]string{
"vtadam.localhost.vitess.log.INFO.20200101-120000.00000": "2020-01-01T12:00:00.000Z",
"vtadam.localhost.vitess.log.INFO.20200101-113000.00000": "2020-01-01T11:30:00.000Z",
"vtadam.localhost.vitess.log.INFO.20200101-100000.00000": "2020-01-01T10:00:00.000Z",
"vtadam.localhost.vitess.log.INFO.20200101-090000.00000": "2020-01-01T09:00:00.000Z",
Expand All @@ -114,7 +113,11 @@ func TestPurgeByMtime(t *testing.T) {
for filename, mtimeStr := range filenameMtimeMap {
createFileWithMtime(filename, mtimeStr)
}
if err := os.Symlink("vtadam.localhost.vitess.log.INFO.20200101-120000.00000", path.Join(logDir, "vtadam.INFO")); err != nil {

// Create vtadam.INFO symlink to 100000. This is a contrived example in that
// current log (100000) is not the latest log (113000). This will not happen
// IRL but it helps us test edge cases of purging by mtime.
if err := os.Symlink("vtadam.localhost.vitess.log.INFO.20200101-100000.00000", path.Join(logDir, "vtadam.INFO")); err != nil {
t.Fatalf("os.Symlink: %v", err)
}

Expand All @@ -126,9 +129,9 @@ func TestPurgeByMtime(t *testing.T) {
}

if len(left) != 3 {
// 20200101-120000 is current
// 20200101-113000 is within 1 hour
// symlink remains
// 1. 113000 is within 1 hour
// 2. 100000 is current (vtadam.INFO)
// 3. vtadam.INFO symlink remains
// rest are removed
t.Errorf("wrong number of files remain: want %v, got %v", 3, len(left))
}
Expand Down
8 changes: 7 additions & 1 deletion go/vt/logutil/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ func purgeLogsOnce(now time.Time, dir, program string, ctimeDelta time.Duration,
return
}
for _, file := range files {
if current[file] {
statInfo, err := os.Lstat(file)
if err != nil {
// Failed to stat file
continue
}
if current[file] || !statInfo.Mode().IsRegular() {
// Do not purge current file or any non-regular files (symlinks etc)
continue
}
purgeFile := false
Expand Down