os_unix.go

 1// Copyright (c) 2017, Andrey Nering <andrey.nering@gmail.com>
 2// See LICENSE for licensing information
 3
 4//go:build unix
 5
 6package interp
 7
 8import (
 9	"context"
10	"os/user"
11	"strconv"
12	"syscall"
13
14	"golang.org/x/sys/unix"
15	"mvdan.cc/sh/v3/syntax"
16)
17
18func mkfifo(path string, mode uint32) error {
19	return unix.Mkfifo(path, mode)
20}
21
22// access is similar to checking the permission bits from [io/fs.FileInfo],
23// but it also takes into account the current user's role.
24func (r *Runner) access(ctx context.Context, path string, mode uint32) error {
25	// TODO(v4): "access" may need to become part of a handler, like "open" or "stat".
26	return unix.Access(path, mode)
27}
28
29// unTestOwnOrGrp implements the -O and -G unary tests. If the file does not
30// exist, or the current user cannot be retrieved, returns false.
31func (r *Runner) unTestOwnOrGrp(ctx context.Context, op syntax.UnTestOperator, x string) bool {
32	info, err := r.stat(ctx, x)
33	if err != nil {
34		return false
35	}
36	u, err := user.Current()
37	if err != nil {
38		return false
39	}
40	if op == syntax.TsUsrOwn {
41		uid, _ := strconv.Atoi(u.Uid)
42		return uint32(uid) == info.Sys().(*syscall.Stat_t).Uid
43	}
44	gid, _ := strconv.Atoi(u.Gid)
45	return uint32(gid) == info.Sys().(*syscall.Stat_t).Gid
46}
47
48type waitStatus = syscall.WaitStatus