Skip to content

Commit

Permalink
Clone file owner and mode on compressed log.
Browse files Browse the repository at this point in the history
Clone the log file owner and the log file mode to the compressed
log file. Add tests to ensure that this is handled correctly.
  • Loading branch information
Joel Sing committed May 31, 2017
1 parent 8b24c4a commit 4cd920d
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
92 changes: 92 additions & 0 deletions linux_test.go
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"syscall"
"testing"
"time"
)

func TestMaintainMode(t *testing.T) {
Expand Down Expand Up @@ -83,6 +84,97 @@ func TestMaintainOwner(t *testing.T) {
equals(666, fakeFS.files[filename].gid, t)
}

func TestCompressMaintainMode(t *testing.T) {
currentTime = fakeTime

dir := makeTempDir("TestCompressMaintainMode", t)
defer os.RemoveAll(dir)

filename := logFile(dir)

mode := os.FileMode(0600)
f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, mode)
isNil(err, t)
f.Close()

l := &Logger{
Compress: true,
Filename: filename,
MaxBackups: 1,
MaxSize: 100, // megabytes
}
defer l.Close()
b := []byte("boo!")
n, err := l.Write(b)
isNil(err, t)
equals(len(b), n, t)

newFakeTime()

err = l.Rotate()
isNil(err, t)

// we need to wait a little bit since the files get compressed on a different
// goroutine.
<-time.After(10 * time.Millisecond)

// a compressed version of the log file should now exist with the correct
// mode.
filename2 := backupFile(dir)
info, err := os.Stat(filename)
isNil(err, t)
info2, err := os.Stat(filename2+compressSuffix)
isNil(err, t)
equals(mode, info.Mode(), t)
equals(mode, info2.Mode(), t)
}

func TestCompressMaintainOwner(t *testing.T) {
fakeFS := newFakeFS()
os_Chown = fakeFS.Chown
os_Stat = fakeFS.Stat
defer func() {
os_Chown = os.Chown
os_Stat = os.Stat
}()
currentTime = fakeTime
dir := makeTempDir("TestCompressMaintainOwner", t)
defer os.RemoveAll(dir)

filename := logFile(dir)

f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0644)
isNil(err, t)
f.Close()

l := &Logger{
Compress: true,
Filename: filename,
MaxBackups: 1,
MaxSize: 100, // megabytes
}
defer l.Close()
b := []byte("boo!")
n, err := l.Write(b)
isNil(err, t)
equals(len(b), n, t)

newFakeTime()

err = l.Rotate()
isNil(err, t)

// we need to wait a little bit since the files get compressed on a different
// goroutine.
<-time.After(10 * time.Millisecond)

// a compressed version of the log file should now exist with the correct
// owner.
filename2 := backupFile(dir)
equals(555, fakeFS.files[filename2+compressSuffix].uid, t)
equals(666, fakeFS.files[filename2+compressSuffix].gid, t)
}

type fakeFile struct {
uid int
gid int
Expand Down
11 changes: 10 additions & 1 deletion lumberjack.go
Expand Up @@ -472,9 +472,18 @@ func compressLogFile(src, dst string) (err error) {
}
defer f.Close()

fi, err := os_Stat(src)
if err != nil {
return fmt.Errorf("failed to stat log file: %v", err)
}

if err := chown(dst, fi); err != nil {
return fmt.Errorf("failed to chown compressed log file: %v", err)
}

// If this file already exists, we presume it was created by
// a previous attempt to compress the log file.
gzf, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
gzf, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, fi.Mode())
if err != nil {
return fmt.Errorf("failed to open compressed log file: %v", err)
}
Expand Down

0 comments on commit 4cd920d

Please sign in to comment.