mapDirOpenError maps the provided non-nil error from opening name to a possibly better non-nil error. In particular, it turns OS-specific errors about opening files in non-directories into os.ErrNotExist. See golang/go#18984. Adapted from the Go standard library; originally written by Nathaniel Caza
(fileSystem fs.FS, originalErr error, name string)
| 634 | // https://go-review.googlesource.com/c/go/+/36635/ |
| 635 | // https://go-review.googlesource.com/c/go/+/36804/ |
| 636 | func (fsrv *FileServer) mapDirOpenError(fileSystem fs.FS, originalErr error, name string) error { |
| 637 | if errors.Is(originalErr, fs.ErrNotExist) || errors.Is(originalErr, fs.ErrPermission) { |
| 638 | return originalErr |
| 639 | } |
| 640 | |
| 641 | var pathErr *fs.PathError |
| 642 | if errors.As(originalErr, &pathErr) { |
| 643 | return fs.ErrInvalid |
| 644 | } |
| 645 | |
| 646 | parts := strings.Split(name, separator) |
| 647 | for i := range parts { |
| 648 | if parts[i] == "" { |
| 649 | continue |
| 650 | } |
| 651 | fi, err := fs.Stat(fileSystem, strings.Join(parts[:i+1], separator)) |
| 652 | if err != nil { |
| 653 | return originalErr |
| 654 | } |
| 655 | if !fi.IsDir() { |
| 656 | return fs.ErrNotExist |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | return originalErr |
| 661 | } |
| 662 | |
| 663 | // transformHidePaths performs replacements for all the elements of fsrv.Hide and |
| 664 | // makes them absolute paths (if they contain a path separator), then returns a |