carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1//go:build linux
2
3package host
4
5import (
6 "errors"
7 "os"
8
9 "github.com/carlosframework/platform/internal/hostwrite"
10 "github.com/carlosframework/platform/internal/safefile"
11 "golang.org/x/sys/unix"
12)
13
14func prepareClaimDatabaseOwner(path string, uid, gid int) error {
15 for _, suffix := range []string{"", "-wal", "-shm"} {
16 name := path + suffix
17 f, err := safefile.OpenFile(name, os.O_RDWR, 0)
18 created := false
19 if errors.Is(err, unix.ENOENT) {
20 if suffix != "" {
21 continue
22 }
23 f, err = safefile.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
24 created = err == nil
25 }
26 if err != nil {
27 return err
28 }
29 err = func() error {
30 defer f.Close()
31 fd := int(f.Fd())
32 var stat unix.Stat_t
33 if err := unix.Fstat(fd, &stat); err != nil {
34 return err
35 }
36 if stat.Mode&unix.S_IFMT != unix.S_IFREG || stat.Nlink != 1 || (!created && int(stat.Uid) != uid) {
37 return hostwrite.ErrUnavailable
38 }
39 if created && os.Geteuid() == 0 {
40 if err := unix.Fchown(fd, uid, gid); err != nil {
41 return err
42 }
43 }
44 return unix.Fchmod(fd, 0600)
45 }()
46 if err != nil {
47 return err
48 }
49 }
50 return nil
51}
52