From 31414a7536171770124b0f2ca5cd8985ab56123e Mon Sep 17 00:00:00 2001 From: Mike Sawka Date: Fri, 20 Sep 2024 12:25:46 -0700 Subject: [PATCH] don't open files when stating directory (#797) --- pkg/util/utilfn/utilfn.go | 5 ++++- pkg/wshrpc/wshremote/wshremote.go | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/pkg/util/utilfn/utilfn.go b/pkg/util/utilfn/utilfn.go index 23540d168..1f854d8ff 100644 --- a/pkg/util/utilfn/utilfn.go +++ b/pkg/util/utilfn/utilfn.go @@ -629,7 +629,7 @@ func CopyToChannel(outputCh chan<- []byte, reader io.Reader) error { // on error just returns "" // does not return "application/octet-stream" as this is considered a detection failure // can pass an existing fileInfo to avoid re-statting the file -func DetectMimeType(path string, fileInfo fs.FileInfo) string { +func DetectMimeType(path string, fileInfo fs.FileInfo, extended bool) string { if fileInfo == nil { statRtn, err := os.Stat(path) if err != nil { @@ -657,6 +657,9 @@ func DetectMimeType(path string, fileInfo fs.FileInfo) string { if mimeType := mime.TypeByExtension(ext); mimeType != "" { return mimeType } + if !extended { + return "" + } fd, err := os.Open(path) if err != nil { return "" diff --git a/pkg/wshrpc/wshremote/wshremote.go b/pkg/wshrpc/wshremote/wshremote.go index c31513267..811923392 100644 --- a/pkg/wshrpc/wshremote/wshremote.go +++ b/pkg/wshrpc/wshremote/wshremote.go @@ -104,7 +104,7 @@ func (impl *ServerImpl) remoteStreamFileDir(ctx context.Context, path string, by if err != nil { continue } - innerFileInfo := statToFileInfo(filepath.Join(path, innerFileInfoInt.Name()), innerFileInfoInt) + innerFileInfo := statToFileInfo(filepath.Join(path, innerFileInfoInt.Name()), innerFileInfoInt, false) fileInfoArr = append(fileInfoArr, innerFileInfo) if len(fileInfoArr) >= DirChunkSize { dataCallback(fileInfoArr, nil) @@ -200,8 +200,8 @@ func (impl *ServerImpl) RemoteStreamFileCommand(ctx context.Context, data wshrpc return ch } -func statToFileInfo(fullPath string, finfo fs.FileInfo) *wshrpc.FileInfo { - mimeType := utilfn.DetectMimeType(fullPath, finfo) +func statToFileInfo(fullPath string, finfo fs.FileInfo, extended bool) *wshrpc.FileInfo { + mimeType := utilfn.DetectMimeType(fullPath, finfo, extended) rtn := &wshrpc.FileInfo{ Path: wavebase.ReplaceHomeDir(fullPath), Dir: computeDirPart(fullPath, finfo.IsDir()), @@ -272,7 +272,7 @@ func (*ServerImpl) fileInfoInternal(path string, extended bool) (*wshrpc.FileInf if err != nil { return nil, fmt.Errorf("cannot stat file %q: %w", path, err) } - rtn := statToFileInfo(cleanedPath, finfo) + rtn := statToFileInfo(cleanedPath, finfo, extended) if extended { rtn.ReadOnly = checkIsReadOnly(cleanedPath, finfo, true) }