diff options
Diffstat (limited to 'back/cmd/authfs/sessdir.go')
| -rw-r--r-- | back/cmd/authfs/sessdir.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/back/cmd/authfs/sessdir.go b/back/cmd/authfs/sessdir.go new file mode 100644 index 0000000..186484f --- /dev/null +++ b/back/cmd/authfs/sessdir.go @@ -0,0 +1,60 @@ +package main + +import ( + "sync" + + "git.samanthony.xyz/buth/back/auth" + "git.samanthony.xyz/buth/back/qver" +) + +type SessionsDir struct { + *qver.Version + + mu sync.Mutex // guards below + sessions map[auth.SessId]*Session +} + +func NewSessionsDir(rootVer *qver.Version) *SessionsDir { + return &SessionsDir{ + qver.New(qver.Parent(rootVer)), + sync.Mutex{}, + make(map[auth.SessId]*Session), + } +} + +func (dir *SessionsDir) Close() { + dir.mu.Lock() + // Don't unlock + + dir.Version.Close() + for id, sess := range dir.sessions { + sess.Close() + delete(dir.sessions, id) + } +} + +// Get returns the session with the given ID if it exists, or false if it doesn't. +func (dir *SessionsDir) Get(id auth.SessId) (*Session, bool) { + dir.mu.Lock() + defer dir.mu.Unlock() + sess, ok := dir.sessions[id] + return sess, ok +} + +// Owner returns the owner of the given session if it exists, or false if it doesn't. +func (dir *SessionsDir) Owner(id auth.SessId) (auth.Uname, bool) { + if sess, ok := dir.Get(id); ok { + return sess.Uname, ok + } + return "", false +} + +func (dir *SessionsDir) Kill(id auth.SessId) { + dir.mu.Lock() + defer dir.mu.Unlock() + if sess, ok := dir.sessions[id]; ok { + sess.Close() + delete(dir.sessions, id) + dir.Version.Bump() + } +} |