Add procfd.c with gpfs_set_winattrs_path()
[slow/toolbox.git] / procfd.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdarg.h>
5 #include <stdbool.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <dirent.h>
10 #include <sys/types.h>
11 #include <sys/xattr.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <sys/acl.h>
15 #include <gpfs.h>
16
17 static const char *sys_proc_fd_path(int fd, char *buf, size_t bufsize)
18 {
19         int written;
20
21         written = snprintf(buf,
22                            bufsize,
23                            "/proc/self/fd/%d",
24                            fd);
25         if (written >= bufsize) {
26                 return NULL;
27         }
28
29         return buf;
30 }
31
32 int main(int argc, char **argv)
33 {
34         const char *p = NULL;
35         char buf[PATH_MAX];
36         int fd;
37         int ret;
38         struct gpfs_winattr attrs = {0};
39
40         if (argc < 2) {
41                 printf("Usage: %s FILE\n");
42                 return 1;
43         }
44
45         fd = open(argv[1], O_PATH);
46         if (fd == -1) {
47                 return 1;
48         }
49
50         p = sys_proc_fd_path(fd, buf, sizeof(buf));
51         if (p == NULL) {
52                 return 1;
53         }
54
55         attrs.winAttrs = GPFS_WINATTR_HIDDEN;
56
57         ret = gpfs_set_winattrs_path(p,
58                                      GPFS_WINATTR_SET_ATTRS,
59                                      &attrs);
60         if (ret != 0) {
61                 printf("gpfswrap_set_winattrs_path [%s] failed: %s\n",
62                         p, strerror(errno));
63                 return 1;
64         }
65
66         return 0;
67 }
68