summaryrefslogtreecommitdiffstats
path: root/back/cmd/authfs/sessdir.go
blob: 22d8202fc19c9e7cb4dbca56a003011f9f38c6cd (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
package main

import (
	"sync"

	p9 "github.com/Harvey-OS/ninep/protocol"

	"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()
	}
}

func (dir *SessionsDir) Qid() (p9.QID, error) {
	ver, err := dir.Version.Get()
	if err != nil {
		return p9.QID{}, err
	}
	return p9.QID{
		Type:    p9.QTDIR,
		Version: ver,
		Path:    sessionsQidPath,
	}, nil
}

func (dir *SessionsDir) Open(mode p9.Mode) error {
	if mode&(p9.OEXEC|p9.OTRUNC|p9.ORCLOSE) != 0 {
		return ErrPerm
	}
	return nil
}