From 31c608f595e2a7b37cebfe685fa1462af3d5bbbe Mon Sep 17 00:00:00 2001 From: LTLA Date: Sun, 20 Oct 2024 15:07:00 -0700 Subject: [PATCH] Bugfix to follow symlink when stat'ing file for HEAD retrieval. Also avoid using Lstat to reduce potential for confusion when the only (purported) advantage is for efficiency by reducing a few steps of indirection. --- handlers.go | 2 +- list.go | 4 +++- load_test.go | 2 +- verify.go | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/handlers.go b/handlers.go index ebb23b3..dcde69a 100644 --- a/handlers.go +++ b/handlers.go @@ -534,7 +534,7 @@ func newRetrieveFileHandler(db *sql.DB) func(http.ResponseWriter, *http.Request) return } - info, err := os.Lstat(path) // intential Lstat() to avoid unnecessary link following. + info, err := os.Stat(path) if err != nil { if errors.Is(err, os.ErrNotExist) { err = newHttpError(http.StatusNotFound, errors.New("path does not exist")) diff --git a/list.go b/list.go index 2dd8acd..f2a7c4f 100644 --- a/list.go +++ b/list.go @@ -93,7 +93,7 @@ func listMetadata(dir string, base_names []string) (map[string]fs.FileInfo, []st return fs.SkipDir } - _, err := os.Lstat(filepath.Join(path, ".SewerRatignore")) + _, err := os.Stat(filepath.Join(path, ".SewerRatignore")) if err != nil && errors.Is(err, fs.ErrNotExist) { return nil } else { @@ -114,6 +114,8 @@ func listMetadata(dir string, base_names []string) (map[string]fs.FileInfo, []st // our index, so that the index is updated when the target is changed // (rather than when the link itself is changed). info, err = os.Stat(path) + + // We don't recurse into symlinked directories, though. if err == nil && info.IsDir() { return nil } diff --git a/load_test.go b/load_test.go index 7aaa12b..38d4d6e 100644 --- a/load_test.go +++ b/load_test.go @@ -84,7 +84,7 @@ func TestLoadMetadata(t *testing.T) { t.Fatalf("failed to create a symlink; %v", err) } - info, err := os.Lstat(path) // check that symlinks are correctly loaded. + info, err := os.Stat(path) if err != nil { t.Fatal(err) } diff --git a/verify.go b/verify.go index 367be4f..afedb0d 100644 --- a/verify.go +++ b/verify.go @@ -48,7 +48,7 @@ func (vr *verificationRegistry) Provision(path string) (string, error) { candidate = ".sewer_" + base64.RawURLEncoding.EncodeToString(buff) - _, err = os.Lstat(filepath.Join(path, candidate)) // intential Lstat() to avoid unnecessary link following. + _, err = os.Stat(filepath.Join(path, candidate)) if err != nil { if errors.Is(err, os.ErrNotExist) { found = true