Revision 333238386631 () - Diff

Link to this snippet: https://friendpaste.com/7gUN4kB3ZfHwVdlDggodUY
Embed:
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
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/xattr.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

int main(int argc, char *argv[])
{
int fd;

if (argc != 2) {
printf("Use: %s <path>\n", argv[0]);
return 1;
}

/*
* with O_WRONLY or O_RDWR, opening a directory always fails, even when passing
* O_DIRECTORY. Opening with O_RDONLY, without O_DIRECTORY succeeds for both
* regular files and directories.
*/
fd = open(argv[1], O_WRONLY | O_DIRECTORY);
/* fd = open(argv[1], O_RDONLY); */
if (fd == -1) {
printf("open error %d: %s\n", errno, strerror(errno));
} else {
int e;

e = fsetxattr(fd, "user.x1", "abc", 3, 0);
printf("file open fd = %d, setxattr(fd) = %d, fsync(fd) = %d\n", fd, e, fsync(fd));
close(fd);
}
return 0;
}