| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame^] | 1 | // Copyright The Monogon Project Authors. |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 3 | |
| 4 | package fsxattrs |
| 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "unsafe" |
| 10 | |
| 11 | "golang.org/x/sys/unix" |
| 12 | ) |
| 13 | |
| 14 | type FSXAttrFlag uint32 |
| 15 | |
| 16 | // Defined in uapi/linux/fs.h |
| 17 | const ( |
| 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 Windelschmidt | a3cd52c | 2024-04-18 23:21:16 +0200 | [diff] [blame] | 36 | // 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. |
| 39 | const ( |
| 40 | FS_IOC_FSGETXATTR = 0x801c581f |
| 41 | FS_IOC_FSSETXATTR = 0x401c5820 |
| 42 | ) |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 43 | |
| 44 | type FSXAttrs struct { |
| 45 | Flags FSXAttrFlag |
| 46 | ExtentSize uint32 |
| 47 | ExtentCount uint32 |
| 48 | ProjectID uint32 |
| 49 | CoWExtentSize uint32 |
| 50 | _pad [8]byte |
| 51 | } |
| 52 | |
| 53 | func 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 Windelschmidt | 5f1a7de | 2024-09-19 02:00:14 +0200 | [diff] [blame] | 57 | return nil, fmt.Errorf("failed to execute getFSXAttrs: %w", errno) |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 58 | } |
| 59 | return &attrs, nil |
| 60 | } |
| 61 | |
| 62 | func 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 Windelschmidt | 5f1a7de | 2024-09-19 02:00:14 +0200 | [diff] [blame] | 65 | return fmt.Errorf("failed to execute setFSXAttrs: %w", errno) |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 66 | } |
| 67 | return nil |
| 68 | } |