diff --git a/libs/autofile/autofile.go b/libs/autofile/autofile.go index 3a6244894..061726f7d 100644 --- a/libs/autofile/autofile.go +++ b/libs/autofile/autofile.go @@ -3,6 +3,7 @@ package autofile import ( "os" "os/signal" + "path/filepath" "sync" "syscall" "time" @@ -57,6 +58,11 @@ type AutoFile struct { // an error, it will be of type *PathError or *ErrPermissionsChanged (if file's // permissions got changed (should be 0600)). func OpenAutoFile(path string) (*AutoFile, error) { + var err error + path, err = filepath.Abs(path) + if err != nil { + return nil, err + } af := &AutoFile{ ID: tmrand.Str(12) + ":" + path, Path: path, diff --git a/libs/autofile/autofile_test.go b/libs/autofile/autofile_test.go index 664264cdc..24ca6343d 100644 --- a/libs/autofile/autofile_test.go +++ b/libs/autofile/autofile_test.go @@ -3,26 +3,34 @@ package autofile import ( "io/ioutil" "os" + "path/filepath" "syscall" "testing" "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" tmos "github.com/tendermint/tendermint/libs/os" ) func TestSIGHUP(t *testing.T) { - // First, create an AutoFile writing to a tempfile dir - file, err := ioutil.TempFile("", "sighup_test") + origDir, err := os.Getwd() + require.NoError(t, err) + defer os.Chdir(origDir) + + // First, create a temporary directory and move into it + dir, err := ioutil.TempDir("", "sighup_test") require.NoError(t, err) - err = file.Close() + defer os.RemoveAll(dir) + err = os.Chdir(dir) require.NoError(t, err) - name := file.Name() - // Here is the actual AutoFile + // Create an AutoFile in the temporary directory + name := "sighup_test" af, err := OpenAutoFile(name) require.NoError(t, err) + require.True(t, filepath.IsAbs(af.Path)) // Write to the file. _, err = af.Write([]byte("Line 1\n")) @@ -34,6 +42,13 @@ func TestSIGHUP(t *testing.T) { err = os.Rename(name, name+"_old") require.NoError(t, err) + // Move into a different temporary directory + otherDir, err := ioutil.TempDir("", "sighup_test_other") + require.NoError(t, err) + defer os.RemoveAll(otherDir) + err = os.Chdir(otherDir) + require.NoError(t, err) + // Send SIGHUP to self. syscall.Kill(syscall.Getpid(), syscall.SIGHUP) @@ -49,12 +64,17 @@ func TestSIGHUP(t *testing.T) { require.NoError(t, err) // Both files should exist - if body := tmos.MustReadFile(name + "_old"); string(body) != "Line 1\nLine 2\n" { + if body := tmos.MustReadFile(filepath.Join(dir, name+"_old")); string(body) != "Line 1\nLine 2\n" { t.Errorf("unexpected body %s", body) } - if body := tmos.MustReadFile(name); string(body) != "Line 3\nLine 4\n" { + if body := tmos.MustReadFile(filepath.Join(dir, name)); string(body) != "Line 3\nLine 4\n" { t.Errorf("unexpected body %s", body) } + + // The current directory should be empty + files, err := ioutil.ReadDir(".") + require.NoError(t, err) + assert.Empty(t, files) } // // Manually modify file permissions, close, and reopen using autofile: diff --git a/libs/autofile/group.go b/libs/autofile/group.go index e859149b9..ca9f57003 100644 --- a/libs/autofile/group.go +++ b/libs/autofile/group.go @@ -6,7 +6,6 @@ import ( "fmt" "io" "os" - "path" "path/filepath" "regexp" "strconv" @@ -79,7 +78,10 @@ type Group struct { // OpenGroup creates a new Group with head at headPath. It returns an error if // it fails to open head file. func OpenGroup(headPath string, groupOptions ...func(*Group)) (g *Group, err error) { - dir := path.Dir(headPath) + dir, err := filepath.Abs(filepath.Dir(headPath)) + if err != nil { + return nil, err + } head, err := OpenAutoFile(headPath) if err != nil { return nil, err diff --git a/libs/autofile/group_test.go b/libs/autofile/group_test.go index a91ef0633..de29a0fba 100644 --- a/libs/autofile/group_test.go +++ b/libs/autofile/group_test.go @@ -4,6 +4,7 @@ import ( "io" "io/ioutil" "os" + "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -105,6 +106,23 @@ func TestCheckHeadSizeLimit(t *testing.T) { func TestRotateFile(t *testing.T) { g := createTestGroupWithHeadSizeLimit(t, 0) + + // Create a different temporary directory and move into it, to make sure + // relative paths are resolved at Group creation + origDir, err := os.Getwd() + require.NoError(t, err) + defer os.Chdir(origDir) + + dir, err := ioutil.TempDir("", "rotate_test") + require.NoError(t, err) + defer os.RemoveAll(dir) + err = os.Chdir(dir) + require.NoError(t, err) + + require.True(t, filepath.IsAbs(g.Head.Path)) + require.True(t, filepath.IsAbs(g.Dir)) + + // Create and rotate files g.WriteLine("Line 1") g.WriteLine("Line 2") g.WriteLine("Line 3") @@ -129,6 +147,11 @@ func TestRotateFile(t *testing.T) { t.Errorf("got unexpected contents: [%v]", string(body2)) } + // Make sure there are no files in the current, temporary directory + files, err := ioutil.ReadDir(".") + require.NoError(t, err) + assert.Empty(t, files) + // Cleanup destroyTestGroup(t, g) }