commit ea1501ebf1c9693cab43910e78eec1573a8a06bd
parent c1205be0a090bc1b1a06cee65dad8490a8783174
Author: Matsuda Kenji <info@mtkn.jp>
Date: Fri, 18 Aug 2023 11:27:17 +0900
change stat
Diffstat:
| M | file.go | | | 57 | ++++++++++++++++++++++++++++++++++++++++----------------- |
| M | fs.go | | | 31 | ++++++++++++++++++++++++------- |
| M | testdir/a | | | 0 | |
3 files changed, 64 insertions(+), 24 deletions(-)
diff --git a/file.go b/file.go
@@ -4,15 +4,17 @@ import (
"fmt"
"io"
"io/fs"
- "time"
"path"
+ "time"
)
+type FileMode uint32
+
const (
- DMDIR = 0x80000000
- DMAPPEND = 0x40000000
- DMEXCL = 0x20000000
- DMTMP = 0x04000000
+ DMDIR FileMode = 0x80000000
+ DMAPPEND = 0x40000000
+ DMEXCL = 0x20000000
+ DMTMP = 0x04000000
)
const (
@@ -54,7 +56,7 @@ type stat struct {
t uint16
dev uint32
qid *Qid
- mode uint32
+ mode FileMode
aTime uint32
mTime uint32
length int64
@@ -89,7 +91,7 @@ func (s *stat) marshal() []byte {
cur += 4
pbit64(msg[cur:cur+8], s.qid.Path())
cur += 8
- pbit32(msg[cur:cur+4], s.mode)
+ pbit32(msg[cur:cur+4], uint32(s.mode))
cur += 4
pbit32(msg[cur:cur+4], s.aTime)
cur += 4
@@ -148,12 +150,29 @@ func (fi *FileInfo) IsDir() bool { return fi.info.IsDir() }
func (fi *FileInfo) Sys() any { return fi.stat }
type File struct {
- fs *FS // file system to which this file belongs
+ fs *FS // file system to which this file belongs
file fs.File // underlying file
- qid *Qid
- path string // absolute path from the root of the fs.
+ path string // absolute path from the root of the fs.
+
+ // System-modified data. Not in use.
+ Type uint16
+ Dev uint32
+
+ // File data
+ Qid *Qid // unique id from server
+ Mode uint32 // permissions
+ Atime uint32 // last read time
+ Mtime uint32 // last write time
+ Length int64 // file length
+ Name string // last element of path
+ Uid string // owner name
+ Gid string // group name
+ Muid string // last modifier name
}
+func (f *File) Marshal(b []byte) (n int, err error) {
+ panic("not inplemented")
+}
func newFile(fsys *FS, path string) (*File, error) {
file, err := fsys.fs.Open(path)
if err != nil {
@@ -176,7 +195,7 @@ func newFile(fsys *FS, path string) (*File, error) {
return nil, fmt.Errorf("alloc qid for file %v: %v", file, err)
}
}
- f.qid = q
+ f.Qid = q
return f, nil
}
@@ -188,9 +207,15 @@ func (f *File) Stat() (*FileInfo, error) {
fi := &FileInfo{
info: fsfi,
stat: &stat{
- qid: f.qid,
+ qid: f.Qid,
name: path.Base(f.path),
- mode: uint32(fsfi.Mode()), // TODO: convert
+ mode: fsModeTo9Mode(fsfi.Mode()),
+ aTime: uint32(fsfi.ModTime().Unix()),
+ mTime: uint32(fsfi.ModTime().Unix()),
+ length: fsfi.Size(),
+ uid: "kenji",
+ gid: "kenji",
+ muid: "kenji",
},
// TODO: ..., uid, gid, muid
}
@@ -257,8 +282,8 @@ func (f *File) ReadDir(n int) ([]*DirEntry, error) {
}
de[i] = &DirEntry{
dirEnt: fsde[i],
- info: info,
- file: file,
+ info: info,
+ file: file,
}
}
return de, nil
@@ -274,5 +299,3 @@ func (e *DirEntry) Name() string { return e.dirEnt.Name() }
func (e *DirEntry) IsDir() bool { return e.dirEnt.IsDir() }
func (e *DirEntry) Type() fs.FileMode { return e.dirEnt.Type() }
func (e *DirEntry) Info() (*FileInfo, error) { return e.info, nil }
-
-
diff --git a/fs.go b/fs.go
@@ -26,7 +26,7 @@ func (fsys *FS) Open(name string) (*File, error) {
if err != nil {
return nil, fmt.Errorf("stat file %s: %v", name, err)
}
- file.qid.SetType(fsModeToQidType(fi.Mode()))
+ file.Qid.SetType(fsModeToQidType(fi.Mode()))
return file, nil
}
@@ -39,7 +39,7 @@ func (fsys *FS) walk(root string, wnames []string) ([]*Qid, error) {
if err != nil {
return wqids, err
}
- qid, ok := fsys.qidPool.lookup(f.path)
+ qid, ok := fsys.qidPool.lookup(f.path) // TODO: use os.SameFile
if !ok {
s, err := f.file.Stat()
if err != nil {
@@ -58,19 +58,19 @@ func (fsys *FS) walk(root string, wnames []string) ([]*Qid, error) {
func fsModeToQidType(fm fs.FileMode) QidType {
var qt QidType
- if fm|fs.ModeDir != 0 {
+ if fm&fs.ModeDir != 0 {
qt |= QTDIR
}
- if fm|fs.ModeAppend != 0 {
+ if fm&fs.ModeAppend != 0 {
qt |= QTAPPEND
}
- if fm|fs.ModeExclusive != 0 {
+ if fm&fs.ModeExclusive != 0 {
qt |= QTEXCL
}
- if fm|fs.ModeTemporary != 0 {
+ if fm&fs.ModeTemporary != 0 {
qt |= QTTMP
}
- if fm|fs.ModeSymlink != 0 {
+ if fm&fs.ModeSymlink != 0 {
qt |= QTSYMLINK
}
// QTMOUNT is not in fs.FileMode.
@@ -79,3 +79,20 @@ func fsModeToQidType(fm fs.FileMode) QidType {
// are not in QidType.
return qt
}
+
+func fsModeTo9Mode(mode fs.FileMode) FileMode {
+ mode9 := FileMode(mode & fs.ModePerm)
+ if mode&fs.ModeDir != 0 {
+ mode9 |= DMDIR
+ }
+ if mode&fs.ModeAppend != 0 {
+ mode9 |= DMAPPEND
+ }
+ if mode&fs.ModeExclusive != 0 {
+ mode9 |= DMEXCL
+ }
+ if mode&fs.ModeTemporary != 0 {
+ mode9 |= DMTMP
+ }
+ return mode9
+}
diff --git a/testdir/a b/testdir/a