1 64abb42a 2018-04-10 noreply package ufs
6 64abb42a 2018-04-10 noreply "io/ioutil"
9 64abb42a 2018-04-10 noreply "path/filepath"
10 64abb42a 2018-04-10 noreply "strconv"
12 64abb42a 2018-04-10 noreply "syscall"
14 c55b00ed 2021-12-31 op "git.omarpolo.com/go-p9p"
17 64abb42a 2018-04-10 noreply type session struct {
18 64abb42a 2018-04-10 noreply sync.Mutex
19 64abb42a 2018-04-10 noreply rootRef *FileRef
20 64abb42a 2018-04-10 noreply refs map[p9p.Fid]*FileRef
23 64abb42a 2018-04-10 noreply func NewSession(ctx context.Context, root string) (p9p.Session, error) {
24 64abb42a 2018-04-10 noreply return &session{
25 64abb42a 2018-04-10 noreply rootRef: &FileRef{Path: root},
26 64abb42a 2018-04-10 noreply refs: make(map[p9p.Fid]*FileRef),
30 64abb42a 2018-04-10 noreply func (sess *session) getRef(fid p9p.Fid) (*FileRef, error) {
31 64abb42a 2018-04-10 noreply sess.Lock()
32 64abb42a 2018-04-10 noreply defer sess.Unlock()
34 64abb42a 2018-04-10 noreply if fid == p9p.NOFID {
35 64abb42a 2018-04-10 noreply return nil, p9p.ErrUnknownfid
38 64abb42a 2018-04-10 noreply ref, found := sess.refs[fid]
39 64abb42a 2018-04-10 noreply if !found {
40 64abb42a 2018-04-10 noreply return nil, p9p.ErrUnknownfid
43 64abb42a 2018-04-10 noreply if err := ref.Stat(); err != nil {
44 64abb42a 2018-04-10 noreply return nil, err
47 64abb42a 2018-04-10 noreply return ref, nil
50 64abb42a 2018-04-10 noreply func (sess *session) newRef(fid p9p.Fid, path string) (*FileRef, error) {
51 64abb42a 2018-04-10 noreply sess.Lock()
52 64abb42a 2018-04-10 noreply defer sess.Unlock()
54 64abb42a 2018-04-10 noreply if fid == p9p.NOFID {
55 64abb42a 2018-04-10 noreply return nil, p9p.ErrUnknownfid
58 64abb42a 2018-04-10 noreply _, found := sess.refs[fid]
59 64abb42a 2018-04-10 noreply if found {
60 64abb42a 2018-04-10 noreply return nil, p9p.ErrDupfid
63 64abb42a 2018-04-10 noreply ref := &FileRef{Path: path}
64 64abb42a 2018-04-10 noreply if err := ref.Stat(); err != nil {
65 64abb42a 2018-04-10 noreply return nil, err
68 64abb42a 2018-04-10 noreply sess.refs[fid] = ref
69 64abb42a 2018-04-10 noreply return ref, nil
72 64abb42a 2018-04-10 noreply func (sess *session) Auth(ctx context.Context, afid p9p.Fid, uname, aname string) (p9p.Qid, error) {
73 64abb42a 2018-04-10 noreply // TODO: AuthInit?
74 64abb42a 2018-04-10 noreply return p9p.Qid{}, nil //p9p.MessageRerror{Ename: "no auth"}
77 64abb42a 2018-04-10 noreply func (sess *session) Attach(ctx context.Context, fid, afid p9p.Fid, uname, aname string) (p9p.Qid, error) {
78 64abb42a 2018-04-10 noreply if uname == "" {
79 64abb42a 2018-04-10 noreply return p9p.Qid{}, p9p.MessageRerror{Ename: "no user"}
82 64abb42a 2018-04-10 noreply // TODO: AuthCheck?
84 64abb42a 2018-04-10 noreply // if afid > 0 {
85 64abb42a 2018-04-10 noreply // return p9p.Qid{}, p9p.MessageRerror{Ename: "attach: no auth"}
88 64abb42a 2018-04-10 noreply if aname == "" {
89 64abb42a 2018-04-10 noreply aname = sess.rootRef.Path
92 64abb42a 2018-04-10 noreply ref, err := sess.newRef(fid, aname)
93 64abb42a 2018-04-10 noreply if err != nil {
94 64abb42a 2018-04-10 noreply return p9p.Qid{}, err
97 64abb42a 2018-04-10 noreply return ref.Info.Qid, nil
100 64abb42a 2018-04-10 noreply func (sess *session) Clunk(ctx context.Context, fid p9p.Fid) error {
101 64abb42a 2018-04-10 noreply ref, err := sess.getRef(fid)
102 64abb42a 2018-04-10 noreply if err != nil {
103 64abb42a 2018-04-10 noreply return err
106 64abb42a 2018-04-10 noreply ref.Lock()
107 64abb42a 2018-04-10 noreply defer ref.Unlock()
108 64abb42a 2018-04-10 noreply if ref.File != nil {
109 64abb42a 2018-04-10 noreply ref.File.Close()
112 64abb42a 2018-04-10 noreply sess.Lock()
113 64abb42a 2018-04-10 noreply defer sess.Unlock()
114 64abb42a 2018-04-10 noreply delete(sess.refs, fid)
116 64abb42a 2018-04-10 noreply return nil
119 64abb42a 2018-04-10 noreply func (sess *session) Remove(ctx context.Context, fid p9p.Fid) error {
120 64abb42a 2018-04-10 noreply defer sess.Clunk(ctx, fid)
122 64abb42a 2018-04-10 noreply ref, err := sess.getRef(fid)
123 64abb42a 2018-04-10 noreply if err != nil {
124 64abb42a 2018-04-10 noreply return err
127 64abb42a 2018-04-10 noreply // TODO: check write perms on parent
129 64abb42a 2018-04-10 noreply return os.Remove(ref.Path)
132 64abb42a 2018-04-10 noreply func (sess *session) Walk(ctx context.Context, fid p9p.Fid, newfid p9p.Fid, names ...string) ([]p9p.Qid, error) {
133 64abb42a 2018-04-10 noreply var qids []p9p.Qid
135 64abb42a 2018-04-10 noreply ref, err := sess.getRef(fid)
136 64abb42a 2018-04-10 noreply if err != nil {
137 64abb42a 2018-04-10 noreply return qids, err
140 64abb42a 2018-04-10 noreply newref, err := sess.newRef(newfid, ref.Path)
141 64abb42a 2018-04-10 noreply if err != nil {
142 64abb42a 2018-04-10 noreply return qids, err
145 64abb42a 2018-04-10 noreply path := newref.Path
146 64abb42a 2018-04-10 noreply for _, name := range names {
147 64abb42a 2018-04-10 noreply newpath := filepath.Join(path, name)
148 64abb42a 2018-04-10 noreply r := &FileRef{Path: newpath}
149 64abb42a 2018-04-10 noreply if err := r.Stat(); err != nil {
152 64abb42a 2018-04-10 noreply qids = append(qids, r.Info.Qid)
153 64abb42a 2018-04-10 noreply path = newpath
156 64abb42a 2018-04-10 noreply newref.Path = path
157 64abb42a 2018-04-10 noreply return qids, nil
160 64abb42a 2018-04-10 noreply func (sess *session) Read(ctx context.Context, fid p9p.Fid, p []byte, offset int64) (n int, err error) {
161 64abb42a 2018-04-10 noreply ref, err := sess.getRef(fid)
162 64abb42a 2018-04-10 noreply if err != nil {
163 64abb42a 2018-04-10 noreply return 0, err
166 64abb42a 2018-04-10 noreply ref.Lock()
167 64abb42a 2018-04-10 noreply defer ref.Unlock()
169 64abb42a 2018-04-10 noreply if ref.IsDir() {
170 64abb42a 2018-04-10 noreply if offset == 0 && ref.Readdir == nil {
171 64abb42a 2018-04-10 noreply files, err := ioutil.ReadDir(ref.Path)
172 64abb42a 2018-04-10 noreply if err != nil {
173 64abb42a 2018-04-10 noreply return 0, err
175 64abb42a 2018-04-10 noreply var dirs []p9p.Dir
176 64abb42a 2018-04-10 noreply for _, info := range files {
177 64abb42a 2018-04-10 noreply dirs = append(dirs, dirFromInfo(info))
179 64abb42a 2018-04-10 noreply ref.Readdir = p9p.NewFixedReaddir(p9p.NewCodec(), dirs)
181 64abb42a 2018-04-10 noreply if ref.Readdir == nil {
182 64abb42a 2018-04-10 noreply return 0, p9p.ErrBadoffset
184 64abb42a 2018-04-10 noreply return ref.Readdir.Read(ctx, p, offset)
187 64abb42a 2018-04-10 noreply if ref.File == nil {
188 64abb42a 2018-04-10 noreply return 0, p9p.MessageRerror{Ename: "no file open"} //p9p.ErrClosed
191 64abb42a 2018-04-10 noreply n, err = ref.File.ReadAt(p, offset)
192 64abb42a 2018-04-10 noreply if err != nil && err != io.EOF {
193 64abb42a 2018-04-10 noreply return n, err
195 64abb42a 2018-04-10 noreply return n, nil
198 64abb42a 2018-04-10 noreply func (sess *session) Write(ctx context.Context, fid p9p.Fid, p []byte, offset int64) (n int, err error) {
199 64abb42a 2018-04-10 noreply ref, err := sess.getRef(fid)
200 64abb42a 2018-04-10 noreply if err != nil {
201 64abb42a 2018-04-10 noreply return 0, err
204 64abb42a 2018-04-10 noreply ref.Lock()
205 64abb42a 2018-04-10 noreply defer ref.Unlock()
206 64abb42a 2018-04-10 noreply if ref.File == nil {
207 64abb42a 2018-04-10 noreply return 0, p9p.ErrClosed
210 64abb42a 2018-04-10 noreply return ref.File.WriteAt(p, offset)
213 64abb42a 2018-04-10 noreply func (sess *session) Open(ctx context.Context, fid p9p.Fid, mode p9p.Flag) (p9p.Qid, uint32, error) {
214 64abb42a 2018-04-10 noreply ref, err := sess.getRef(fid)
215 64abb42a 2018-04-10 noreply if err != nil {
216 64abb42a 2018-04-10 noreply return p9p.Qid{}, 0, err
219 64abb42a 2018-04-10 noreply ref.Lock()
220 64abb42a 2018-04-10 noreply defer ref.Unlock()
221 64abb42a 2018-04-10 noreply f, err := os.OpenFile(ref.Path, oflags(mode), 0)
222 64abb42a 2018-04-10 noreply if err != nil {
223 64abb42a 2018-04-10 noreply return p9p.Qid{}, 0, err
225 64abb42a 2018-04-10 noreply ref.File = f
226 64abb42a 2018-04-10 noreply return ref.Info.Qid, 0, nil
229 64abb42a 2018-04-10 noreply func (sess *session) Create(ctx context.Context, parent p9p.Fid, name string, perm uint32, mode p9p.Flag) (p9p.Qid, uint32, error) {
230 64abb42a 2018-04-10 noreply ref, err := sess.getRef(parent)
231 64abb42a 2018-04-10 noreply if err != nil {
232 64abb42a 2018-04-10 noreply return p9p.Qid{}, 0, err
235 64abb42a 2018-04-10 noreply newpath := filepath.Join(ref.Path, name)
237 64abb42a 2018-04-10 noreply var file *os.File
238 64abb42a 2018-04-10 noreply switch {
239 64abb42a 2018-04-10 noreply case perm&p9p.DMDIR != 0:
240 64abb42a 2018-04-10 noreply err = os.Mkdir(newpath, os.FileMode(perm&0777))
242 64abb42a 2018-04-10 noreply case perm&p9p.DMSYMLINK != 0:
243 64abb42a 2018-04-10 noreply case perm&p9p.DMNAMEDPIPE != 0:
244 64abb42a 2018-04-10 noreply case perm&p9p.DMDEVICE != 0:
245 64abb42a 2018-04-10 noreply err = p9p.MessageRerror{Ename: "not implemented"}
247 64abb42a 2018-04-10 noreply default:
248 64abb42a 2018-04-10 noreply file, err = os.OpenFile(newpath, oflags(mode)|os.O_CREATE, os.FileMode(perm&0777))
251 64abb42a 2018-04-10 noreply if file == nil && err == nil {
252 64abb42a 2018-04-10 noreply file, err = os.OpenFile(newpath, oflags(mode), 0)
255 64abb42a 2018-04-10 noreply if err != nil {
256 64abb42a 2018-04-10 noreply return p9p.Qid{}, 0, err
259 64abb42a 2018-04-10 noreply ref.Lock()
260 64abb42a 2018-04-10 noreply defer ref.Unlock()
261 64abb42a 2018-04-10 noreply ref.Path = newpath
262 64abb42a 2018-04-10 noreply ref.File = file
263 64abb42a 2018-04-10 noreply if err := ref.statLocked(); err != nil {
264 64abb42a 2018-04-10 noreply return p9p.Qid{}, 0, err
266 64abb42a 2018-04-10 noreply return ref.Info.Qid, 0, err
269 64abb42a 2018-04-10 noreply func (sess *session) Stat(ctx context.Context, fid p9p.Fid) (p9p.Dir, error) {
270 64abb42a 2018-04-10 noreply ref, err := sess.getRef(fid)
271 64abb42a 2018-04-10 noreply if err != nil {
272 64abb42a 2018-04-10 noreply return p9p.Dir{}, err
274 64abb42a 2018-04-10 noreply return ref.Info, nil
277 64abb42a 2018-04-10 noreply func (sess *session) WStat(ctx context.Context, fid p9p.Fid, dir p9p.Dir) error {
278 64abb42a 2018-04-10 noreply ref, err := sess.getRef(fid)
279 64abb42a 2018-04-10 noreply if err != nil {
280 64abb42a 2018-04-10 noreply return err
283 64abb42a 2018-04-10 noreply if dir.Mode != ^uint32(0) {
284 64abb42a 2018-04-10 noreply // TODO: 9P2000.u: DMSETUID DMSETGID
285 64abb42a 2018-04-10 noreply err := os.Chmod(ref.Path, os.FileMode(dir.Mode&0777))
286 64abb42a 2018-04-10 noreply if err != nil {
287 64abb42a 2018-04-10 noreply return err
291 64abb42a 2018-04-10 noreply if dir.UID != "" || dir.GID != "" {
292 64abb42a 2018-04-10 noreply usr, err := user.Lookup(dir.UID)
293 64abb42a 2018-04-10 noreply if err != nil {
294 64abb42a 2018-04-10 noreply return err
296 64abb42a 2018-04-10 noreply uid, err := strconv.Atoi(usr.Uid)
297 64abb42a 2018-04-10 noreply if err != nil {
298 64abb42a 2018-04-10 noreply return err
300 64abb42a 2018-04-10 noreply grp, err := user.LookupGroup(dir.GID)
301 64abb42a 2018-04-10 noreply if err != nil {
302 64abb42a 2018-04-10 noreply return err
304 64abb42a 2018-04-10 noreply gid, err := strconv.Atoi(grp.Gid)
305 64abb42a 2018-04-10 noreply if err != nil {
306 64abb42a 2018-04-10 noreply return err
308 64abb42a 2018-04-10 noreply if err := os.Chown(ref.Path, uid, gid); err != nil {
309 64abb42a 2018-04-10 noreply return err
313 64abb42a 2018-04-10 noreply if dir.Name != "" {
314 64abb42a 2018-04-10 noreply newpath := filepath.Join(filepath.Dir(ref.Path), dir.Name)
315 64abb42a 2018-04-10 noreply if err := syscall.Rename(ref.Path, newpath); err != nil {
316 64abb42a 2018-04-10 noreply return nil
318 64abb42a 2018-04-10 noreply ref.Lock()
319 64abb42a 2018-04-10 noreply defer ref.Unlock()
320 64abb42a 2018-04-10 noreply ref.Path = newpath
323 64abb42a 2018-04-10 noreply if dir.Length != ^uint64(0) {
324 64abb42a 2018-04-10 noreply if err := os.Truncate(ref.Path, int64(dir.Length)); err != nil {
325 64abb42a 2018-04-10 noreply return err
329 64abb42a 2018-04-10 noreply // If either mtime or atime need to be changed, then
330 64abb42a 2018-04-10 noreply // we must change both.
331 64abb42a 2018-04-10 noreply //if dir.ModTime != time.Time{} || dir.AccessTime != ^uint32(0) {
332 64abb42a 2018-04-10 noreply // mt, at := time.Unix(int64(dir.Mtime), 0), time.Unix(int64(dir.Atime), 0)
333 64abb42a 2018-04-10 noreply // if cmt, cat := (dir.Mtime == ^uint32(0)), (dir.Atime == ^uint32(0)); cmt || cat {
334 64abb42a 2018-04-10 noreply // st, e := os.Stat(fid.path)
335 64abb42a 2018-04-10 noreply // if e != nil {
336 64abb42a 2018-04-10 noreply // req.RespondError(toError(e))
337 64abb42a 2018-04-10 noreply // return
339 64abb42a 2018-04-10 noreply // switch cmt {
340 64abb42a 2018-04-10 noreply // case true:
341 64abb42a 2018-04-10 noreply // mt = st.ModTime()
342 64abb42a 2018-04-10 noreply // default:
343 64abb42a 2018-04-10 noreply // at = atime(st.Sys().(*syscall.Stat_t))
346 64abb42a 2018-04-10 noreply // e := os.Chtimes(fid.path, at, mt)
347 64abb42a 2018-04-10 noreply // if e != nil {
348 64abb42a 2018-04-10 noreply // req.RespondError(toError(e))
349 64abb42a 2018-04-10 noreply // return
352 64abb42a 2018-04-10 noreply return nil
355 64abb42a 2018-04-10 noreply func (sess *session) Version() (msize int, version string) {
356 64abb42a 2018-04-10 noreply return p9p.DefaultMSize, p9p.DefaultVersion