overlay/071: Don't compare inode numbers in lower overlay and nested overlay
[xfstests-dev.git] / src / attr-list-by-handle-cursor-test.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2016 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <darrick.wong@oracle.com>
5  */
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <attr/attributes.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <asm/types.h>
14 #include <xfs/xfs.h>
15 #include <xfs/handle.h>
16
17 #define ATTRBUFSZ               1024
18 #define BSTATBUF_NR             32
19
20 /* Read all the extended attributes of a file handle. */
21 void
22 read_handle_xattrs(
23         struct xfs_handle       *handle)
24 {
25         struct attrlist_cursor  cur;
26         char                    attrbuf[ATTRBUFSZ];
27         char                    *firstname = NULL;
28         struct attrlist         *attrlist = (struct attrlist *)attrbuf;
29         struct attrlist_ent     *ent;
30         int                     i;
31         int                     flags = 0;
32         int                     error;
33
34         memset(&cur, 0, sizeof(cur));
35         while ((error = attr_list_by_handle(handle, sizeof(*handle),
36                                             attrbuf, ATTRBUFSZ, flags,
37                                             &cur)) == 0) {
38                 for (i = 0; i < attrlist->al_count; i++) {
39                         ent = ATTR_ENTRY(attrlist, i);
40
41                         if (i != 0)
42                                 continue;
43
44                         if (firstname == NULL) {
45                                 firstname = malloc(ent->a_valuelen);
46                                 memcpy(firstname, ent->a_name, ent->a_valuelen);
47                         } else {
48                                 if (memcmp(firstname, ent->a_name,
49                                            ent->a_valuelen) == 0)
50                                         fprintf(stderr,
51                                                 "Saw duplicate xattr \"%s\", buggy XFS?\n",
52                                                 ent->a_name);
53                                 else
54                                         fprintf(stderr,
55                                                 "Test passes.\n");
56                                 goto out;
57                         }
58                 }
59
60                 if (!attrlist->al_more)
61                         break;
62         }
63
64 out:
65         if (firstname)
66                 free(firstname);
67         if (error)
68                 perror("attr_list_by_handle");
69         return;
70 }
71
72 int main(
73         int                     argc,
74         char                    *argv[])
75 {
76         struct xfs_handle       *fshandle;
77         size_t                  fshandle_len;
78         struct xfs_handle       *handle;
79         size_t                  handle_len;
80         int                     error;
81
82         if (argc != 2) {
83                 fprintf(stderr, "Usage: %s filename\n", argv[0]);
84                 return 1;
85         }
86
87         error = path_to_fshandle(argv[1], (void **)&fshandle, &fshandle_len);
88         if (error) {
89                 perror("getting fshandle");
90                 return 2;
91         }
92
93         error = path_to_handle(argv[1], (void **)&handle, &handle_len);
94         if (error) {
95                 perror("getting handle");
96                 return 3;
97         }
98
99         read_handle_xattrs(handle);
100
101         free_handle(handle, handle_len);
102         free_handle(fshandle, fshandle_len);
103         return 0;
104 }