Skip to content

Commit

Permalink
Bugfix to follow symlink when stat'ing file for HEAD retrieval.
Browse files Browse the repository at this point in the history
Also avoid using Lstat to reduce potential for confusion when the only
(purported) advantage is for efficiency by reducing a few steps of indirection.
  • Loading branch information
LTLA committed Oct 20, 2024
1 parent b1ce4f8 commit 31c608f
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
4 changes: 3 additions & 1 deletion list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 31c608f

Please sign in to comment.