diff --git a/cmd/posix.go b/cmd/posix.go index 29a9af498..b777a1b3e 100644 --- a/cmd/posix.go +++ b/cmd/posix.go @@ -74,24 +74,20 @@ func checkPathLength(pathName string) error { func isDirEmpty(dirname string) bool { f, err := os.Open(preparePath(dirname)) if err != nil { - errorIf(func() error { - if !os.IsNotExist(err) { - return err - } - return nil - }(), "Unable to access directory.") + if !os.IsNotExist(err) { + errorIf(err, "Unable to access directory") + } + return false } defer f.Close() // List one entry. _, err = f.Readdirnames(1) if err != io.EOF { - errorIf(func() error { - if !os.IsNotExist(err) { - return err - } - return nil - }(), "Unable to list directory.") + if !os.IsNotExist(err) { + errorIf(err, "Unable to list directory") + } + return false } // Returns true if we have reached EOF, directory is indeed empty. diff --git a/cmd/posix_test.go b/cmd/posix_test.go index 88ba71d24..d5d48c70d 100644 --- a/cmd/posix_test.go +++ b/cmd/posix_test.go @@ -73,6 +73,42 @@ func TestPosixGetDiskInfo(t *testing.T) { } } +func TestPosixIsDirEmpty(t *testing.T) { + tmp, err := ioutil.TempDir(globalTestTmpDir, "minio-") + if err != nil { + t.Fatal(err) + } + defer removeAll(tmp) + + // Should give false on non-existent directory. + dir1 := slashpath.Join(tmp, "non-existent-directory") + if isDirEmpty(dir1) != false { + t.Error("expected false for non-existent directory, got true") + } + + // Should give false for not-a-directory. + dir2 := slashpath.Join(tmp, "file") + err = ioutil.WriteFile(dir2, []byte("hello"), 0777) + if err != nil { + t.Fatal(err) + } + + if isDirEmpty(dir2) != false { + t.Error("expected false for a file, got true") + } + + // Should give true for a real empty directory. + dir3 := slashpath.Join(tmp, "empty") + err = os.Mkdir(dir3, 0777) + if err != nil { + t.Fatal(err) + } + + if isDirEmpty(dir3) != true { + t.Error("expected true for empty dir, got false") + } +} + // TestPosixReadAll - TestPosixs the functionality implemented by posix ReadAll storage API. func TestPosixReadAll(t *testing.T) { // create posix test setup