commit d30dbb4f7fa09e657772ecd84423b33968584f7c
parent 71591b179b8665ee7e40531f92b5051c47a78297
Author: Matsuda Kenji <info@mtkn.jp>
Date: Fri, 3 Nov 2023 17:58:40 +0900
add Remove
Diffstat:
| M | cmd/semfs/fs.go | | | 60 | ++++++++++++++++++++++++++++++++++++++++-------------------- |
1 file changed, 40 insertions(+), 20 deletions(-)
diff --git a/cmd/semfs/fs.go b/cmd/semfs/fs.go
@@ -5,8 +5,8 @@ import (
"errors"
"io"
"io/fs"
- "strings"
"strconv"
+ "strings"
"time"
"git.mtkn.jp/lib9p"
@@ -27,6 +27,9 @@ func (fsys *semFS) OpenFile(name string, omode lib9p.OpenMode, perm fs.FileMode)
return fsys, nil
default:
for _, f := range fsys.semfiles {
+ if f == nil {
+ continue
+ }
if f.name == wnames[0] {
return f, nil
}
@@ -56,39 +59,45 @@ func (root *semFS) Create(name string, uid string, mode lib9p.OpenMode, perm lib
}
ctx, cancel := context.WithCancel(context.Background())
newfile := &semFile{
- name: name,
- sem: 0,
- path: uint64(len(root.semfiles)+1),
+ fs: root,
+ name: name,
+ sem: 0,
+ path: uint64(len(root.semfiles) + 1),
cancel: cancel,
}
newfile.start(ctx)
root.semfiles = append(root.semfiles, newfile)
return newfile, nil
}
+
func (root *semFS) Read(p []byte) (n int, err error) {
return 0, errors.New("is a directory")
}
func (root *semFS) ReadDir(n int) ([]*lib9p.DirEntry, error) {
- de := make([]*lib9p.DirEntry, len(root.semfiles))
- for i, f := range root.semfiles {
+ de := make([]*lib9p.DirEntry, 0, len(root.semfiles))
+ for _, f := range root.semfiles {
+ if f == nil {
+ continue
+ }
fi, err := f.Stat()
if err != nil {
return nil, err
}
- de[i] = fi
+ de = append(de, fi)
}
return de, nil
}
type semFile struct {
- name string
- sem int
- path uint64
+ fs *semFS
+ name string
+ sem int
+ path uint64
offset int
- rchan chan<- chan struct{}
- wchan chan<- int
- queue []chan struct{}
+ rchan chan<- chan struct{}
+ wchan chan<- int
+ queue []chan struct{}
cancel context.CancelFunc
}
@@ -101,13 +110,16 @@ func (f *semFile) start(ctx context.Context) {
case <-ctx.Done():
return
case c := <-rchan:
+ if c == nil {
+ return
+ }
if f.sem > 0 {
f.sem--
close(c)
} else {
f.queue = append(f.queue, c)
}
- case n := <- wchan:
+ case n := <-wchan:
f.sem += n
for len(f.queue) > 0 && f.sem > 0 {
close(f.queue[0])
@@ -124,12 +136,12 @@ func (f *semFile) start(ctx context.Context) {
func (f *semFile) Stat() (*lib9p.FileInfo, error) {
t := uint32(time.Now().Unix())
return &lib9p.FileInfo{Stat: lib9p.Stat{
- Qid: lib9p.Qid{Type: lib9p.QTFILE, Vers: 0, Path: f.path},
- Mode: 0666,
- Atime: t,
- Mtime: t,
+ Qid: lib9p.Qid{Type: lib9p.QTFILE, Vers: 0, Path: f.path},
+ Mode: 0666,
+ Atime: t,
+ Mtime: t,
Length: int64(f.sem),
- Name: f.name,
+ Name: f.name,
}}, nil
}
@@ -139,7 +151,7 @@ func (f *semFile) Close() error {
return nil
}
-func (f *semFile) Read(p []byte) (n int, err error) {
+func (f *semFile) Read(p []byte) (n int, err error) {
if len(p) > 0 {
log.Println("chan")
c := make(chan struct{})
@@ -158,3 +170,11 @@ func (f *semFile) Write(p []byte) (int, error) {
f.wchan <- n
return len(p), nil
}
+
+func (f *semFile) Remove() error {
+ f.cancel()
+ close(f.rchan)
+ close(f.wchan)
+ f.fs.semfiles[f.path-1] = nil
+ return nil
+}