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

import (
	"errors"
	"fmt"
	"io/fs"
	"os"

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

	"git.samanthony.xyz/buth/back/qver"
)

type UsersDir struct {
	path string
	*qver.Version
}

func NewUsersDir(path string, rootVer *qver.Version) (*UsersDir, error) {
	info, err := os.Stat(path)
	if errors.Is(err, fs.ErrNotExist) {
		if err := os.Mkdir(path, 0770); err != nil {
			return nil, err
		}
	} else if err != nil {
		return nil, err
	} else if !info.IsDir() {
		return nil, fmt.Errorf("%q is not a directory", path)
	}

	return &UsersDir{
		path,
		qver.New(qver.Parent(rootVer)),
	}, nil
}

func (dir *UsersDir) Close() {
	dir.Version.Close()
}

func (dir *UsersDir) 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:    usersQidPath,
	}, nil
}

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