Skip to content

Commit

Permalink
fix: fix hanging when reading a named pipe file (closes #1155)
Browse files Browse the repository at this point in the history
  • Loading branch information
o1egl committed Nov 24, 2020
1 parent 9515cee commit 586d198
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
8 changes: 6 additions & 2 deletions files/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ func (i *FileInfo) Checksum(algo string) error {
//nolint:goconst
//TODO: use constants
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.
// imagine the situation where a file in a dir with thousands
// of files couldn't be opened: we'd have immediately
Expand Down Expand Up @@ -232,9 +236,9 @@ func (i *FileInfo) readListing(checker rules.Checker) error {
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,
// 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)
if err == nil {
f = info
Expand Down
9 changes: 9 additions & 0 deletions files/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package files

import (
"os"
"unicode/utf8"
)

Expand Down Expand Up @@ -48,3 +49,11 @@ func isBinary(content []byte, _ int) bool {
}
return false
}

func IsNamedPipe(mode os.FileMode) bool {
return mode&os.ModeNamedPipe != 0
}

func IsSymlink(mode os.FileMode) bool {
return mode&os.ModeSymlink != 0
}
25 changes: 20 additions & 5 deletions http/raw.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package http

import (
"bytes"
"errors"
"io/ioutil"
"net/http"
"net/url"
gopath "path"
"path/filepath"
"strings"

"github.com/mholt/archiver"
"github.com/spf13/afero"

"github.com/filebrowser/filebrowser/v2/files"
"github.com/filebrowser/filebrowser/v2/fileutils"
Expand Down Expand Up @@ -91,6 +94,11 @@ var rawHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data)
return errToStatus(err), err
}

if files.IsNamedPipe(file.Mode) {
setContentDisposition(w, r, file)
return 0, nil
}

if !file.IsDir {
return rawFileHandler(w, r, file)
}
Expand All @@ -110,11 +118,18 @@ func addFile(ar archiver.Writer, d *data, path, commonPath string) error {
return err
}

file, err := d.user.Fs.Open(path)
if err != nil {
return err
var (
file afero.File
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 {
filename := strings.TrimPrefix(path, commonPath)
Expand All @@ -124,7 +139,7 @@ func addFile(ar archiver.Writer, d *data, path, commonPath string) error {
FileInfo: info,
CustomName: filename,
},
ReadCloser: file,
ReadCloser: arcReadCloser,
})
if err != nil {
return err
Expand Down

0 comments on commit 586d198

Please sign in to comment.