From b1c0c32ba6b43bbdeaecbb66ca463b1a1e24c6fb Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 27 Apr 2020 06:30:12 -0700 Subject: [PATCH] fix: ignore symlinks in backend filesystems (#9457) fixes #9419 --- cmd/posix-list-dir_other.go | 22 +--------------------- cmd/posix-list-dir_test.go | 2 +- cmd/posix-list-dir_unix.go | 7 +++++-- cmd/posix-list-dir_windows.go | 29 ++++++----------------------- 4 files changed, 13 insertions(+), 47 deletions(-) diff --git a/cmd/posix-list-dir_other.go b/cmd/posix-list-dir_other.go index 5bdfaa1e6..febd16f48 100644 --- a/cmd/posix-list-dir_other.go +++ b/cmd/posix-list-dir_other.go @@ -21,10 +21,7 @@ package cmd import ( "io" "os" - "path" "strings" - - "github.com/minio/minio/cmd/logger" ) // Return all the entries at the directory dirPath. @@ -111,25 +108,8 @@ func readDirN(dirPath string, count int) (entries []string, err error) { } } for _, fi := range fis { - // Stat symbolic link and follow to get the final value. + // Not need to follow symlink. if fi.Mode()&os.ModeSymlink == os.ModeSymlink { - var st os.FileInfo - st, err = os.Stat(path.Join(dirPath, fi.Name())) - if err != nil { - reqInfo := (&logger.ReqInfo{}).AppendTags("path", path.Join(dirPath, fi.Name())) - ctx := logger.SetReqInfo(GlobalContext, reqInfo) - logger.LogIf(ctx, err) - continue - } - // Append to entries if symbolic link exists and is valid. - if st.IsDir() { - entries = append(entries, fi.Name()+SlashSeparator) - } else if st.Mode().IsRegular() { - entries = append(entries, fi.Name()) - } - if count > 0 { - remaining-- - } continue } if fi.Mode().IsDir() { diff --git a/cmd/posix-list-dir_test.go b/cmd/posix-list-dir_test.go index 1ad0225cd..2c6afb2b5 100644 --- a/cmd/posix-list-dir_test.go +++ b/cmd/posix-list-dir_test.go @@ -149,7 +149,7 @@ func setupTestReadDirSymlink(t *testing.T) (testResults []result) { } // Add to entries. entries = append(entries, name1) - entries = append(entries, name2) + // Symlinks are ignored. } if err := os.MkdirAll(filepath.Join(dir, "mydir"), 0777); err != nil { t.Fatalf("Unable to create \"mydir\", %s", err) diff --git a/cmd/posix-list-dir_unix.go b/cmd/posix-list-dir_unix.go index c4d101f98..be903b6cd 100644 --- a/cmd/posix-list-dir_unix.go +++ b/cmd/posix-list-dir_unix.go @@ -172,8 +172,8 @@ func readDirN(dirPath string, count int) (entries []string, err error) { // Fallback for filesystems (like old XFS) that don't // support Dirent.Type and have DT_UNKNOWN (0) there // instead. - if typ == unexpectedFileMode || typ&os.ModeSymlink == os.ModeSymlink { - fi, err := os.Stat(pathJoin(dirPath, name)) + if typ == unexpectedFileMode { + fi, err := os.Lstat(pathJoin(dirPath, name)) if err != nil { // It got deleted in the meantime, not found // or returns too many symlinks ignore this @@ -186,6 +186,9 @@ func readDirN(dirPath string, count int) (entries []string, err error) { } typ = fi.Mode() & os.ModeType } + if typ&os.ModeSymlink == os.ModeSymlink { + continue + } if typ.IsRegular() { entries = append(entries, name) } else if typ.IsDir() { diff --git a/cmd/posix-list-dir_windows.go b/cmd/posix-list-dir_windows.go index 158221ff9..cf211c842 100644 --- a/cmd/posix-list-dir_windows.go +++ b/cmd/posix-list-dir_windows.go @@ -121,17 +121,16 @@ func readDirN(dirPath string, count int) (entries []string, err error) { data := &syscall.Win32finddata{} for count != 0 { - e := syscall.FindNextFile(syscall.Handle(d.Fd()), data) - if e != nil { - if e == syscall.ERROR_NO_MORE_FILES { + err = syscall.FindNextFile(syscall.Handle(d.Fd()), data) + if err != nil { + if err == syscall.ERROR_NO_MORE_FILES { break } else { - err = &os.PathError{ + return nil, &os.PathError{ Op: "FindNextFile", Path: dirPath, - Err: e, + Err: err, } - return } } name := syscall.UTF16ToString(data.FileName[0:]) @@ -140,23 +139,7 @@ func readDirN(dirPath string, count int) (entries []string, err error) { } switch { case data.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT != 0: - // If its symbolic link, follow the link using os.Stat() - var fi os.FileInfo - fi, err = os.Stat(pathJoin(dirPath, name)) - if err != nil { - // If file does not exist, we continue and skip it. - // Could happen if it was deleted in the middle while - // this list was being performed. - if os.IsNotExist(err) { - continue - } - return nil, err - } - if fi.IsDir() { - entries = append(entries, name+SlashSeparator) - } else if fi.Mode().IsRegular() { - entries = append(entries, name) - } + continue case data.FileAttributes&syscall.FILE_ATTRIBUTE_DIRECTORY != 0: entries = append(entries, name+SlashSeparator) default: