summaryrefslogtreecommitdiffstats
path: root/back/cmd/authfs/session.go
blob: 2d4eb98294f109a6bb937da555cad7f24ba341b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main

import (
	"context"
	"sync"

	"github.com/docker-archive/go-p9p"
)

// Session implements the p9p.Session interface.
type Session struct {
	mu   sync.Mutex
	fids map[p9p.Fid]Fid
	*Root
}

type Fid struct {
	File
	uname string
}

type File interface {
	Qid() p9p.Qid
	Stat() (p9p.Dir, error)
	Open(mode p9p.Flag) error
	Remove() error
	Read(ctx context.Context, p []byte, offset int64) (n int, err error)
	Write(ctx context.Context, p []byte, offset int64) (n int, err error)
}

type Dir interface {
	File
	Create(ctx context.Context, name string, perm uint32, mode p9p.Flag) (File, error)
	Walk(ctx context.Context, name string) (File, error)
}

func NewSession(root *Root) p9p.Session {
	return &Session{
		fids: make(map[p9p.Fid]Fid),
		Root: root,
	}
}

func (s *Session) Auth(ctx context.Context, afid p9p.Fid, uname, aname string) (p9p.Qid, error) {
	return p9p.Qid{}, p9p.MessageRerror{"authfs: no authentication required"}
}

func (s *Session) Attach(ctx context.Context, fid, afid p9p.Fid, uname, aname string) (p9p.Qid, error) {
	s.mu.Lock()
	defer s.mu.Unlock()

	if _, exists := s.fids[fid]; exists {
		return p9p.Qid{}, p9p.ErrDupfid
	}
	f := Fid{s.root, uname}
	s.fids[fid] = f
	return f.Qid(), nil
}

func (s *Session) Clunk(ctx context.Context, fid p9p.Fid) error {
	s.mu.Lock()
	defer s.mu.Unlock()

	if _, ok := s.fids[fid]; ok {
		delete(s.fids, fid)
		return nil
	} else {
		return p9p.ErrUnknownfid
	}
}

func (s *Session) Remove(ctx context.Context, fid p9p.Fid) error {
	s.mu.Lock()
	defer s.mu.Unlock()

	f, ok := s.fids[fid]
	if !ok {
		return p9p.ErrUnknownfid
	}
	delete(s.fids, fid) // clunk
	return f.Remove()
}

func (s *Session) Walk(ctx context.Context, fid, newfid p9p.Fid, names ...string) ([]p9p.Qid, error) {
	s.mu.Lock()
	defer s.mu.Unlock()

	f, ok := s.fids[fid]
	if !ok {
		return nil, p9p.ErrUnknownfid
	}

	if _, exists := s.fids[newfid]; exists {
		return nil, p9p.ErrDupfid
	}

	var (
		qids []p9p.Qid
		err  error
		dot  = f
	)
	defer func() { s.fids[newfid] = dot }()
	for _, name := range names {
		dir, ok := dot.File.(Dir)
		if !ok {
			return qids, p9p.ErrWalknodir
		}
		dot.File, err = dir.Walk(ctx, name)
		if err != nil {
			return qids, err
		}
		qids = append(qids, dot.File.Qid())
	}
	return qids, nil
}

func (s *Session) Read(ctx context.Context, fid p9p.Fid, p []byte, offset int64) (n int, err error) {
	s.mu.Lock()
	defer s.mu.Unlock()

	if f, ok := s.fids[fid]; ok {
		return f.File.Read(ctx, p, offset)
	} else {
		return 0, p9p.ErrUnknownfid
	}
}

func (s *Session) Write(ctx context.Context, fid p9p.Fid, p []byte, offset int64) (n int, err error) {
	s.mu.Lock()
	defer s.mu.Unlock()

	if f, ok := s.fids[fid]; ok {
		return f.File.Write(ctx, p, offset)
	} else {
		return 0, p9p.ErrUnknownfid
	}
}

func (s *Session) Open(ctx context.Context, fid p9p.Fid, mode p9p.Flag) (p9p.Qid, uint32, error) {
	s.mu.Lock()
	defer s.mu.Unlock()

	f, ok := s.fids[fid]
	if !ok {
		return p9p.Qid{}, 0, p9p.ErrUnknownfid
	}
	if err := f.Open(mode); err != nil {
		return p9p.Qid{}, 0, err
	}
	return f.File.Qid(), 0, nil
}

func (s *Session) Create(ctx context.Context, parent p9p.Fid, name string, perm uint32, mode p9p.Flag) (p9p.Qid, uint32, error) {
	s.mu.Lock()
	defer s.mu.Unlock()

	f, ok := s.fids[parent]
	if !ok {
		return p9p.Qid{}, 0, p9p.ErrUnknownfid
	}
	dir, ok := f.File.(Dir)
	if !ok {
		return p9p.Qid{}, 0, p9p.ErrCreatenondir
	}

	child, err := dir.Create(ctx, name, perm, mode)
	return child.Qid(), 0, err
}

func (s *Session) Stat(ctx context.Context, fid p9p.Fid) (p9p.Dir, error) {
	s.mu.Lock()
	defer s.mu.Unlock()

	if f, ok := s.fids[fid]; ok {
		return f.File.Stat()
	} else {
		return p9p.Dir{}, p9p.ErrUnknownfid
	}
}

func (s *Session) WStat(ctx context.Context, fid p9p.Fid, dir p9p.Dir) error {
	return p9p.ErrPerm
}

func (s *Session) Version() (msize int, version string) {
	return p9p.DefaultMSize, p9p.DefaultVersion
}