1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * http://www.illumos.org/license/CDDL.
11 */
12
13 /*
14 * Copyright 2022 iXsystems, Inc.
15 */
16
17 /*
18 * FreeBSD exposes additional file attributes via ls -o and chflags.
19 * Under Linux, we provide ZFS_IOC_[GS]ETDOSFLAGS ioctl()s.
20 *
21 * This application is equivalent to FreeBSD chflags.
22 */
23
24 #include <err.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdbool.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/ioctl.h>
32 #include <sys/fs/zfs.h>
33 #include "dos_attributes.h"
34
35 int
main(int argc,const char * const * argv)36 main(int argc, const char *const *argv)
37 {
38 if (argc != 3)
39 errx(EXIT_FAILURE, "usage: %s flag file", argv[0]);
40
41 bool unset = false;
42 uint64_t attr = 0;
43 const char *flag = argv[1];
44 if (strcmp(flag, "0") == 0)
45 ;
46 else if (strcmp(flag, SU_NODUMP) == 0)
47 attr = ZFS_NODUMP;
48 else if (strcmp(flag, UNSET_NODUMP) == 0) {
49 attr = ZFS_NODUMP;
50 unset = true;
51 } else {
52 if (strncmp(flag, "no", 2) == 0) {
53 unset = true;
54 flag += 2;
55 }
56 for (size_t i = 0; i < ARRAY_SIZE(all_dos_attribute_names); ++i)
57 for (const char *const *nm = all_dos_attribute_names[i];
58 *nm; ++nm)
59 if (strcmp(flag, *nm) == 0) {
60 attr = all_dos_attributes[i];
61 goto found;
62 }
63
64 errx(EXIT_FAILURE, "%s: unknown flag", argv[1]);
65 found:;
66 }
67
68 int fd = open(argv[2], O_RDWR | O_APPEND | O_CLOEXEC);
69 if (fd == -1)
70 err(EXIT_FAILURE, "%s", argv[2]);
71
72 uint64_t flags;
73 if (ioctl(fd, ZFS_IOC_GETDOSFLAGS, &flags) == -1)
74 err(EXIT_FAILURE, "ZFS_IOC_GETDOSFLAGS");
75
76 if (attr == 0)
77 flags = 0;
78 else if (unset)
79 flags &= ~attr;
80 else
81 flags |= attr;
82
83 if (ioctl(fd, ZFS_IOC_SETDOSFLAGS, &flags) == -1)
84 err(EXIT_FAILURE, "ZFS_IOC_SETDOSFLAGS");
85
86 uint64_t newflags;
87 if (ioctl(fd, ZFS_IOC_GETDOSFLAGS, &newflags) == -1)
88 err(EXIT_FAILURE, "second ZFS_IOC_GETDOSFLAGS");
89
90 if (newflags != flags)
91 errx(EXIT_FAILURE, "expecting %#" PRIx64 ", got %#" PRIx64
92 "; %ssetting %#" PRIx64 "",
93 flags, newflags, unset ? "un" : "", attr);
94
95 (void) printf("%#" PRIx64 "\n", flags);
96 }
97