From c805a855bd6f65bf6799378f952e7cafe5f6161f Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 1 Sep 2016 15:52:24 -0400 Subject: [PATCH] keywords: fix xattrs for broken symlinks Signed-off-by: Vincent Batts --- keywords_linux.go | 3 ++ keywords_linux_test.go | 79 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 keywords_linux_test.go diff --git a/keywords_linux.go b/keywords_linux.go index ab77d5f..daaa9a9 100644 --- a/keywords_linux.go +++ b/keywords_linux.go @@ -59,6 +59,9 @@ var ( } return strings.Join(klist, " "), nil } + if !info.Mode().IsRegular() && !info.Mode().IsDir() { + return "", nil + } xlist, err := xattr.List(path) if err != nil { diff --git a/keywords_linux_test.go b/keywords_linux_test.go new file mode 100644 index 0000000..d467109 --- /dev/null +++ b/keywords_linux_test.go @@ -0,0 +1,79 @@ +// +build linux + +package mtree + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "testing" + + "github.com/vbatts/go-mtree/xattr" +) + +func TestXattr(t *testing.T) { + // a bit dirty to create/destory a directory in cwd, but often /tmp is + // mounted tmpfs and doesn't support xattrs + dir, err := ioutil.TempDir(".", "test.xattrs.") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + fh, err := os.Create(filepath.Join(dir, "file")) + if err != nil { + t.Fatal(err) + } + fh.WriteString("howdy") + fh.Sync() + if _, err := fh.Seek(0, 0); err != nil { + t.Fatal(err) + } + + if err := os.Symlink("./no/such/path", filepath.Join(dir, "symlink")); err != nil { + t.Fatal(err) + } + + if err := xattr.Set(dir, "user.test", []byte("directory")); err != nil { + t.Skip(fmt.Sprintf("skipping: %q does not support xattrs", dir)) + } + if err := xattr.Set(filepath.Join(dir, "file"), "user.test", []byte("regular file")); err != nil { + t.Fatal(err) + } + + dirstat, err := os.Lstat(dir) + if err != nil { + t.Fatal(err) + } + // Check the directory + str, err := xattrKeywordFunc(dir, dirstat, nil) + if err != nil { + t.Error(err) + } + if str == "" { + t.Errorf("expected a keyval; got %q", str) + } + + filestat, err := fh.Stat() + if err != nil { + t.Fatal(err) + } + // Check the regular file + str, err = xattrKeywordFunc(filepath.Join(dir, "file"), filestat, fh) + if err != nil { + t.Error(err) + } + if str == "" { + t.Errorf("expected a keyval; got %q", str) + } + + linkstat, err := os.Lstat(filepath.Join(dir, "symlink")) + if err != nil { + t.Fatal(err) + } + // Check a broken symlink + _, err = xattrKeywordFunc(filepath.Join(dir, "symlink"), linkstat, nil) + if err != nil { + t.Error(err) + } +}