2 * tst_getsize.c --- this function tests the getsize function
5 * This file may be redistributed under the terms of the GNU Public
11 #define _GNU_SOURCE /* for asprintf */
21 #include <sys/types.h>
22 #ifdef HAVE_ATTR_XATTR_H
23 #include <attr/xattr.h>
24 #elif HAVE_SYS_XATTR_H
25 #include <sys/xattr.h>
27 /* This is just a test program, let's try to work around the lack of header */
28 extern ssize_t fgetxattr (int __filedes, const char *__name,
29 void *__value, size_t __size);
30 extern int fsetxattr (int __filedes, const char *__name,
31 const void *__value, size_t __size, int __flags);
44 char tmpvalue[NR_XATTRS + 1];
53 static void init_ea_table(void)
57 ea_table = malloc(sizeof(struct ea) * NR_XATTRS);
58 if (ea_table == NULL) {
59 perror("maloc failed");
62 for (i = 0; i < NR_XATTRS; i ++) {
63 ea_table[i].name = malloc(i + 2 + strlen("user."));
64 if (ea_table[i].name == NULL) {
65 perror("malloc failed");
68 strcpy(ea_table[i].name, "user.");
69 memset(ea_table[i].name + strlen("user."), 'X', i + 1);
70 ea_table[i].name[i + 1 + strlen("user.")] = 0;
72 ea_table[i].value = malloc(NR_XATTRS - i + 1);
73 if (ea_table[i].value == NULL) {
74 perror("malloc failed");
77 memset(ea_table[i].value, 'Y', NR_XATTRS - i);
78 ea_table[i].value[NR_XATTRS - i] = 0;
82 static int set_xattrs(int fd)
86 for (i = 0; i < NR_XATTRS; i ++) {
87 if (fsetxattr(fd, ea_table[i].name, ea_table[i].value,
88 NR_XATTRS - i + 1, XATTR_CREATE) == -1) {
89 if (errno != ENOSPC) {
90 perror("fsetxattr failed");
96 printf("\t%d xattrs are set\n", i);
100 void get_xattrs1(int fd, int nr)
105 printf("\ttesting fgetxattr .. "); fflush(stdout);
107 for (i = 0; i < nr; i ++) {
108 size = fgetxattr(fd, ea_table[i].name, tmpvalue,
111 perror("fgetxattr failed");
114 if (memcmp(ea_table[i].value, tmpvalue, nr - i + 1)) {
115 fprintf(stderr, "value mismatch");
120 printf("%d xattrs are checked, ok\n", i);
123 void get_xattrs2(const char *device, ext2_ino_t ino, int nr)
127 struct ext2_inode *inode;
131 printf("\ttesting ext2fs_attr_get .. "); fflush(stdout);
133 err = ext2fs_open(device, 0, 0, 0, unix_io_manager, &fs);
136 err = ext2fs_get_mem(EXT2_INODE_SIZE(fs->super), &inode);
138 com_err("get_xattrs2", err, "allocating memory");
142 err = ext2fs_read_inode_full(fs, ino, inode,
143 EXT2_INODE_SIZE(fs->super));
145 com_err("get_xattrs2", err, "reading inode");
148 for (i = 0; i < nr; i ++) {
149 err = ext2fs_attr_get(fs, inode, EXT2_ATTR_INDEX_USER,
150 ea_table[i].name + strlen("user."),
151 tmpvalue, sizeof(tmpvalue), &size);
153 com_err("get_xattrs2", err, "getting xattr");
156 assert(size == (NR_XATTRS - i + 1));
158 if (memcmp(ea_table[i].value, tmpvalue, size)) {
159 fprintf(stderr, "value mismatch");
165 printf("%d xattrs are checked, ok\n", i);
167 #endif /* HAVE_MNTENT_H */
169 int main(int argc, const char *argv[])
182 initialize_ext2_error_table();
186 f = setmntent(MOUNTED, "r");
188 fprintf(stderr, "failed to setmntent\n");
192 while ((mnt = getmntent(f)) != NULL) {
193 if (hasmntopt(mnt, "user_xattr") == NULL)
195 err = ext2fs_open(mnt->mnt_fsname, 0, 0, 0,
196 unix_io_manager, &fs);
198 com_err("tst_read_ea", err, "opening fs %s:%s",
199 mnt->mnt_fsname, mnt->mnt_type);
204 printf("type=%s, fsname=%s, mtpt=%s\n", mnt->mnt_type,
205 mnt->mnt_fsname, mnt->mnt_dir);
207 asprintf(&name, "%s/readeaXXXXXX", mnt->mnt_dir);
210 fprintf(stderr, "tst_read_ea: mkstemp failed on %s: %s",
211 name, strerror(errno));
214 if (fstat(fd, &st)) {
215 fprintf(stderr, "tst_read_ea: fstat failed on %s: %s\n",
216 name, strerror(errno));
225 get_xattrs2(mnt->mnt_fsname, st.st_ino, nr);
235 "\tno ext2 based filesystems mounted with user_xattr\n"
236 "\thope it is ok\n");
237 #endif /* HAVE_MNTENT_H */