From e604ce6deaf0ba4407c54293a338673ed06f833a Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Tue, 5 Dec 2017 23:08:55 +0100 Subject: [PATCH] syscallcompat: OpenNofollow: fix relPath="" case Sometimes want to open baseDir itself. This case was broken, fix it. --- internal/syscallcompat/open_nofollow.go | 4 ++++ internal/syscallcompat/open_nofollow_test.go | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/internal/syscallcompat/open_nofollow.go b/internal/syscallcompat/open_nofollow.go index c804e12..1afa54c 100644 --- a/internal/syscallcompat/open_nofollow.go +++ b/internal/syscallcompat/open_nofollow.go @@ -27,6 +27,10 @@ func OpenNofollow(baseDir string, relPath string, flags int, mode uint32) (fd in if err != nil { return -1, err } + // Caller wanted to open baseDir itself? + if relPath == "" { + return dirfd, nil + } // Split the path into components and separate intermediate directories // and the final basename parts := strings.Split(relPath, "/") diff --git a/internal/syscallcompat/open_nofollow_test.go b/internal/syscallcompat/open_nofollow_test.go index e9cdf77..37ea76b 100644 --- a/internal/syscallcompat/open_nofollow_test.go +++ b/internal/syscallcompat/open_nofollow_test.go @@ -34,4 +34,11 @@ func TestOpenNofollow(t *testing.T) { if err != syscall.ELOOP { t.Errorf("expected ELOOP, got %v", err) } + // Check to see that the base dir can be opened as well + fd, err = OpenNofollow(tmpDir, "", syscall.O_RDONLY, 0) + if err != nil { + t.Errorf("cannot open base dir: %v", err) + } else { + syscall.Close(fd) + } }