lib9p

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

diskfs.go (1033B)


      1 package diskfs
      2 
      3 import (
      4 	"fmt"
      5 	"io/fs"
      6 	"os"
      7 	"path/filepath"
      8 
      9 	"git.mtkn.jp/lib9p"
     10 )
     11 
     12 /*
     13 DiskFS is a file system opened by OpenDiskFS
     14 */
     15 type FS struct {
     16 	rootPath string
     17 	qidPool  *QidPool
     18 }
     19 
     20 func Open(name string) (*FS, error) {
     21 	f, err := os.Open(name)
     22 	if err != nil {
     23 		return nil, err
     24 	}
     25 	if err := f.Close(); err != nil {
     26 		panic(err)
     27 	}
     28 	root := &File{
     29 		path: ".",
     30 	}
     31 	fsys := &FS{
     32 		rootPath: name,
     33 		qidPool:  allocQidPool(),
     34 	}
     35 	root.fs = fsys
     36 	return fsys, nil
     37 }
     38 
     39 func (fsys *FS) OpenFile(name string, omode lib9p.OpenMode, perm fs.FileMode) (lib9p.File, error) {
     40 	fp := filepath.Join(fsys.rootPath, name)
     41 	var m int
     42 	switch omode & 3 {
     43 	case lib9p.OREAD:
     44 		m = os.O_RDONLY
     45 	case lib9p.OWRITE:
     46 		m = os.O_WRONLY
     47 	case lib9p.ORDWR:
     48 		m = os.O_RDWR
     49 	}
     50 	if omode&lib9p.OTRUNC != 0 {
     51 		m |= os.O_TRUNC
     52 	}
     53 	if omode&lib9p.ORCLOSE != 0 {
     54 		return nil, fmt.Errorf("orclose not implemented")
     55 	}
     56 	osf, err := os.OpenFile(fp, m, perm)
     57 	if err != nil {
     58 		return nil, err
     59 	}
     60 	return &File{fs: fsys, path: name, file: osf}, nil
     61 }