lib9p

Go 9P library.
Log | Files | Refs | LICENSE

commit 0c30f8deceab890728459eb52601f676467393c4
parent 8d7da12281052fa4ee2245a54974cbdd84937937
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Tue,  9 Jan 2024 11:39:22 +0900

return fs.PathError

Diffstat:
Mdiskfs/fs.go | 60++++++++++++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 52 insertions(+), 8 deletions(-)

diff --git a/diskfs/fs.go b/diskfs/fs.go @@ -4,6 +4,7 @@ package diskfs import ( "fmt" + "io/fs" "os" "path/filepath" "strings" @@ -23,8 +24,13 @@ func Open(name string) (*FS, error) { if err != nil { return nil, err } - if err := f.Close(); err != nil { - panic(err) + defer f.Close() + fi, err := f.Stat() + if err != nil { + return nil, err + } + if !fi.IsDir() { + return nil, fmt.Errorf("not a directory") } root := &File{ path: ".", @@ -39,6 +45,13 @@ func Open(name string) (*FS, error) { // OpenFile opens the named file with specified omode by calling os.OpenFile. func (fsys *FS) OpenFile(name string, omode lib9p.OpenMode) (lib9p.File, error) { + if !fs.ValidPath(name) { + return nil, &fs.PathError{ + Op: "openfile", + Path: name, + Err: fs.ErrInvalid, + } + } fp := filepath.Join(fsys.rootPath, name) var m int switch omode & 3 { @@ -53,11 +66,19 @@ func (fsys *FS) OpenFile(name string, omode lib9p.OpenMode) (lib9p.File, error) m |= os.O_TRUNC } if omode&lib9p.ORCLOSE != 0 { - return nil, fmt.Errorf("orclose not implemented") + return nil, &fs.PathError{ + Op: "openfile", + Path: name, + Err: fmt.Errorf("orclose not implemented"), + } } osf, err := os.OpenFile(fp, m, 0) if err != nil { - return nil, err + return nil, &fs.PathError{ + Op: "openfile", + Path: name, + Err: err, + } } return &File{fs: fsys, path: name, file: osf}, nil } @@ -65,6 +86,13 @@ func (fsys *FS) OpenFile(name string, omode lib9p.OpenMode) (lib9p.File, error) // TODO: check uid // BUG: check uid. it can be a security hole. func (fsys *FS) Create(name string, uid string, omode lib9p.OpenMode, perm lib9p.FileMode) (lib9p.File, error) { + if !fs.ValidPath(name) { + return nil, &fs.PathError{ + Op: "create", + Path: name, + Err: fs.ErrInvalid, + } + } paths := append([]string{fsys.rootPath}, strings.Split(name, "/")...) ospath := filepath.Join(paths...) var flag int @@ -80,7 +108,11 @@ func (fsys *FS) Create(name string, uid string, omode lib9p.OpenMode, perm lib9p flag |= os.O_TRUNC } if omode&lib9p.ORCLOSE != 0 { - return nil, fmt.Errorf("orclose not implemented in os package") + return nil, &fs.PathError{ + Op: "create", + Path: name, + Err: fmt.Errorf("orclose not implemented"), + } } var ( osfile *os.File @@ -88,17 +120,29 @@ func (fsys *FS) Create(name string, uid string, omode lib9p.OpenMode, perm lib9p ) if perm&os.ModeDir != 0 { if err := os.Mkdir(ospath, perm); err != nil { - return nil, fmt.Errorf("mkdir: %v", err) + return nil, &fs.PathError{ + Op: "create", + Path: name, + Err: fmt.Errorf("mkdir: %v", err), + } } osfile, err = os.OpenFile(ospath, flag, 0) if err != nil { - return nil, fmt.Errorf("openfile: %v", err) + return nil, &fs.PathError{ + Op: "create", + Path: name, + Err: err, + } } } else { flag |= os.O_CREATE osfile, err = os.OpenFile(ospath, flag, perm) if err != nil { - return nil, fmt.Errorf("openfile: %v", err) + return nil, &fs.PathError{ + Op: "create", + Path: name, + Err: err, + } } } return &File{