File: utils_linux.go

package info (click to toggle)
rust-pathrs 0.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,912 kB
  • sloc: python: 1,138; sh: 371; ansic: 259; makefile: 151
file content (56 lines) | stat: -rw-r--r-- 1,356 bytes parent folder | download | duplicates (2)
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
//go:build linux

// SPDX-License-Identifier: MPL-2.0
/*
 * libpathrs: safe path resolution on Linux
 * Copyright (C) 2019-2025 Aleksa Sarai <cyphar@cyphar.com>
 * Copyright (C) 2019-2025 SUSE LLC
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package pathrs

import (
	"fmt"
	"os"

	"golang.org/x/sys/unix"
)

//nolint:cyclop // this function needs to handle a lot of cases
func toUnixMode(mode os.FileMode, needsType bool) (uint32, error) {
	sysMode := uint32(mode.Perm())
	switch mode & os.ModeType { //nolint:exhaustive // we only care about ModeType bits
	case 0:
		if needsType {
			sysMode |= unix.S_IFREG
		}
	case os.ModeDir:
		sysMode |= unix.S_IFDIR
	case os.ModeSymlink:
		sysMode |= unix.S_IFLNK
	case os.ModeCharDevice | os.ModeDevice:
		sysMode |= unix.S_IFCHR
	case os.ModeDevice:
		sysMode |= unix.S_IFBLK
	case os.ModeNamedPipe:
		sysMode |= unix.S_IFIFO
	case os.ModeSocket:
		sysMode |= unix.S_IFSOCK
	default:
		return 0, fmt.Errorf("invalid mode filetype %+o", mode)
	}
	if mode&os.ModeSetuid != 0 {
		sysMode |= unix.S_ISUID
	}
	if mode&os.ModeSetgid != 0 {
		sysMode |= unix.S_ISGID
	}
	if mode&os.ModeSticky != 0 {
		sysMode |= unix.S_ISVTX
	}
	return sysMode, nil
}