blob: 740ee3bd3aea1a26ef737c42c4c8dfc14cfb728f [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
Lorenz Brun1d801752020-04-02 09:24:51 +02002// SPDX-License-Identifier: Apache-2.0
Lorenz Brun1d801752020-04-02 09:24:51 +02003
4package fsxattrs
5
6import (
7 "fmt"
8 "os"
9 "unsafe"
10
11 "golang.org/x/sys/unix"
12)
13
14type FSXAttrFlag uint32
15
16// Defined in uapi/linux/fs.h
17const (
18 FlagRealtime FSXAttrFlag = 0x00000001
19 FlagPreallocated FSXAttrFlag = 0x00000002
20 FlagImmutable FSXAttrFlag = 0x00000008
21 FlagAppend FSXAttrFlag = 0x00000010
22 FlagSync FSXAttrFlag = 0x00000020
23 FlagNoATime FSXAttrFlag = 0x00000040
24 FlagNoDump FSXAttrFlag = 0x00000080
25 FlagRealtimeInherit FSXAttrFlag = 0x00000100
26 FlagProjectInherit FSXAttrFlag = 0x00000200
27 FlagNoSymlinks FSXAttrFlag = 0x00000400
28 FlagExtentSize FSXAttrFlag = 0x00000800
29 FlagNoDefragment FSXAttrFlag = 0x00002000
30 FlagFilestream FSXAttrFlag = 0x00004000
31 FlagDAX FSXAttrFlag = 0x00008000
32 FlagCOWExtentSize FSXAttrFlag = 0x00010000
33 FlagHasAttribute FSXAttrFlag = 0x80000000
34)
35
Tim Windelschmidta3cd52c2024-04-18 23:21:16 +020036// FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR are defined in uapi/linux/fs.h
37// and normally would be imported from x/sys/unix. Since they don't exist
38// there define them here for now.
39const (
40 FS_IOC_FSGETXATTR = 0x801c581f
41 FS_IOC_FSSETXATTR = 0x401c5820
42)
Lorenz Brun1d801752020-04-02 09:24:51 +020043
44type FSXAttrs struct {
45 Flags FSXAttrFlag
46 ExtentSize uint32
47 ExtentCount uint32
48 ProjectID uint32
49 CoWExtentSize uint32
50 _pad [8]byte
51}
52
53func Get(file *os.File) (*FSXAttrs, error) {
54 var attrs FSXAttrs
55 _, _, errno := unix.Syscall(unix.SYS_IOCTL, file.Fd(), FS_IOC_FSGETXATTR, uintptr(unsafe.Pointer(&attrs)))
56 if errno != 0 {
Tim Windelschmidt5f1a7de2024-09-19 02:00:14 +020057 return nil, fmt.Errorf("failed to execute getFSXAttrs: %w", errno)
Lorenz Brun1d801752020-04-02 09:24:51 +020058 }
59 return &attrs, nil
60}
61
62func Set(file *os.File, attrs *FSXAttrs) error {
63 _, _, errno := unix.Syscall(unix.SYS_IOCTL, file.Fd(), FS_IOC_FSSETXATTR, uintptr(unsafe.Pointer(attrs)))
64 if errno != 0 {
Tim Windelschmidt5f1a7de2024-09-19 02:00:14 +020065 return fmt.Errorf("failed to execute setFSXAttrs: %w", errno)
Lorenz Brun1d801752020-04-02 09:24:51 +020066 }
67 return nil
68}