generic/486: Get rid of the redundant error=%d printing
[xfstests-dev.git] / src / attr-list-by-handle-cursor-test.c
1 /*
2  * Copyright (C) 2016 Oracle.  All Rights Reserved.
3  *
4  * Author: Darrick J. Wong <darrick.wong@oracle.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it would be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write the Free Software Foundation,
18  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <attr/attributes.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <asm/types.h>
28 #include <xfs/xfs.h>
29 #include <xfs/handle.h>
30
31 #define ATTRBUFSZ               1024
32 #define BSTATBUF_NR             32
33
34 /* Read all the extended attributes of a file handle. */
35 void
36 read_handle_xattrs(
37         struct xfs_handle       *handle)
38 {
39         struct attrlist_cursor  cur;
40         char                    attrbuf[ATTRBUFSZ];
41         char                    *firstname = NULL;
42         struct attrlist         *attrlist = (struct attrlist *)attrbuf;
43         struct attrlist_ent     *ent;
44         int                     i;
45         int                     flags = 0;
46         int                     error;
47
48         memset(&cur, 0, sizeof(cur));
49         while ((error = attr_list_by_handle(handle, sizeof(*handle),
50                                             attrbuf, ATTRBUFSZ, flags,
51                                             &cur)) == 0) {
52                 for (i = 0; i < attrlist->al_count; i++) {
53                         ent = ATTR_ENTRY(attrlist, i);
54
55                         if (i != 0)
56                                 continue;
57
58                         if (firstname == NULL) {
59                                 firstname = malloc(ent->a_valuelen);
60                                 memcpy(firstname, ent->a_name, ent->a_valuelen);
61                         } else {
62                                 if (memcmp(firstname, ent->a_name,
63                                            ent->a_valuelen) == 0)
64                                         fprintf(stderr,
65                                                 "Saw duplicate xattr \"%s\", buggy XFS?\n",
66                                                 ent->a_name);
67                                 else
68                                         fprintf(stderr,
69                                                 "Test passes.\n");
70                                 goto out;
71                         }
72                 }
73
74                 if (!attrlist->al_more)
75                         break;
76         }
77
78 out:
79         if (firstname)
80                 free(firstname);
81         if (error)
82                 perror("attr_list_by_handle");
83         return;
84 }
85
86 int main(
87         int                     argc,
88         char                    *argv[])
89 {
90         struct xfs_handle       *fshandle;
91         size_t                  fshandle_len;
92         struct xfs_handle       *handle;
93         size_t                  handle_len;
94         int                     error;
95
96         if (argc != 2) {
97                 fprintf(stderr, "Usage: %s filename\n", argv[0]);
98                 return 1;
99         }
100
101         error = path_to_fshandle(argv[1], (void **)&fshandle, &fshandle_len);
102         if (error) {
103                 perror("getting fshandle");
104                 return 2;
105         }
106
107         error = path_to_handle(argv[1], (void **)&handle, &handle_len);
108         if (error) {
109                 perror("getting handle");
110                 return 3;
111         }
112
113         read_handle_xattrs(handle);
114
115         free_handle(handle, handle_len);
116         free_handle(fshandle, fshandle_len);
117         return 0;
118 }