fix: fix hanging when reading a named pipe file (closes #1155)

This commit is contained in:
Oleg Lobanov 2020-11-24 11:32:23 +01:00
parent 9515ceeb42
commit 586d198d47
No known key found for this signature in database
GPG Key ID: 7CC64E41212621B0
3 changed files with 35 additions and 7 deletions

View File

@ -135,6 +135,10 @@ func (i *FileInfo) Checksum(algo string) error {
//nolint:goconst //nolint:goconst
//TODO: use constants //TODO: use constants
func (i *FileInfo) detectType(modify, saveContent bool) error { func (i *FileInfo) detectType(modify, saveContent bool) error {
if IsNamedPipe(i.Mode) {
i.Type = "blob"
return nil
}
// failing to detect the type should not return error. // failing to detect the type should not return error.
// imagine the situation where a file in a dir with thousands // imagine the situation where a file in a dir with thousands
// of files couldn't be opened: we'd have immediately // of files couldn't be opened: we'd have immediately
@ -232,9 +236,9 @@ func (i *FileInfo) readListing(checker rules.Checker) error {
continue continue
} }
if strings.HasPrefix(f.Mode().String(), "L") { if IsSymlink(f.Mode()) {
// It's a symbolic link. We try to follow it. If it doesn't work, // It's a symbolic link. We try to follow it. If it doesn't work,
// we stay with the link information instead if the target's. // we stay with the link information instead of the target's.
info, err := i.Fs.Stat(fPath) info, err := i.Fs.Stat(fPath)
if err == nil { if err == nil {
f = info f = info

View File

@ -1,6 +1,7 @@
package files package files
import ( import (
"os"
"unicode/utf8" "unicode/utf8"
) )
@ -48,3 +49,11 @@ func isBinary(content []byte, _ int) bool {
} }
return false return false
} }
func IsNamedPipe(mode os.FileMode) bool {
return mode&os.ModeNamedPipe != 0
}
func IsSymlink(mode os.FileMode) bool {
return mode&os.ModeSymlink != 0
}

View File

@ -1,7 +1,9 @@
package http package http
import ( import (
"bytes"
"errors" "errors"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
gopath "path" gopath "path"
@ -9,6 +11,7 @@ import (
"strings" "strings"
"github.com/mholt/archiver" "github.com/mholt/archiver"
"github.com/spf13/afero"
"github.com/filebrowser/filebrowser/v2/files" "github.com/filebrowser/filebrowser/v2/files"
"github.com/filebrowser/filebrowser/v2/fileutils" "github.com/filebrowser/filebrowser/v2/fileutils"
@ -91,6 +94,11 @@ var rawHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data)
return errToStatus(err), err return errToStatus(err), err
} }
if files.IsNamedPipe(file.Mode) {
setContentDisposition(w, r, file)
return 0, nil
}
if !file.IsDir { if !file.IsDir {
return rawFileHandler(w, r, file) return rawFileHandler(w, r, file)
} }
@ -110,11 +118,18 @@ func addFile(ar archiver.Writer, d *data, path, commonPath string) error {
return err return err
} }
file, err := d.user.Fs.Open(path) var (
if err != nil { file afero.File
return err arcReadCloser = ioutil.NopCloser(&bytes.Buffer{})
)
if !files.IsNamedPipe(info.Mode()) {
file, err = d.user.Fs.Open(path)
if err != nil {
return err
}
defer file.Close()
arcReadCloser = file
} }
defer file.Close()
if path != commonPath { if path != commonPath {
filename := strings.TrimPrefix(path, commonPath) filename := strings.TrimPrefix(path, commonPath)
@ -124,7 +139,7 @@ func addFile(ar archiver.Writer, d *data, path, commonPath string) error {
FileInfo: info, FileInfo: info,
CustomName: filename, CustomName: filename,
}, },
ReadCloser: file, ReadCloser: arcReadCloser,
}) })
if err != nil { if err != nil {
return err return err