Get libhandle working on IRIX for qa. Put back fd_to_handle.
[xfstests-dev.git] / src / open_unlink.c
1 /*
2  * Copyright (c) 2005 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  * 
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * 
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  * 
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  *
32  *
33  * Create a file given path argument and then unlink it but
34  * keep fd open.
35  * Then do an attr_list for parentptrs. (supported on IRIX)
36  *
37  */
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #include <sys/attributes.h>
47 #include <sys/fs/xfs_itable.h>
48
49 #ifndef ATTR_PARENT
50 #define ATTR_PARENT 0x0040
51 #endif
52
53 #define EA_LISTBUF_SZ 16384
54
55 int
56 main(int argc, char *argv[])
57 {
58         char *path;
59         int fd;
60         char *prog = argv[0];
61         void *handle;
62         size_t hlen;
63         attrlist_cursor_t cursor;
64         attrlist_t *ea_listbuf = (attrlist_t *)malloc(EA_LISTBUF_SZ);
65         uint64_t parent_ino;
66         uint64_t link_cnt;
67         int sts;
68         int nameix;
69         attrlist_ent_t *entp;
70
71         if (argc < 2) {
72                 fprintf(stderr, "%s: missing pathname argument\n", prog);
73                 return 1;
74         }
75         path = argv[1];
76
77         if (argc > 2) {
78                 fprintf(stderr, "%s: too many arguments\n", prog);
79                 return 1;
80         }
81
82         /* if file already exists then error out */
83         if (access(path, F_OK) == 0) {
84                 fprintf(stderr, "%s: file \"%s\" already exists\n", prog, path);
85                 return 1;
86         }
87
88         fd = open(path, O_RDWR|O_CREAT|O_EXCL);
89         if (fd == -1) {
90                 fprintf(stderr, "%s: failed to create \"%s\": %s\n", prog, path, strerror(errno));
91                 return 1;
92         }
93
94
95         /* for linux libhandle version - to set libhandle fsfd cache */
96         {
97                 void *fshandle;
98                 size_t fshlen;
99
100                 if (path_to_fshandle(path, &fshandle, &fshlen) != 0) {
101                         fprintf(stderr, "%s: failed path_to_fshandle \"%s\": %s\n",
102                                 prog, path, strerror(errno));
103                         return 1;
104                 }
105         }
106
107
108         /* 
109          * look at parentptr EAs and see if the path exists now that
110          * it has been unlinked.
111          */ 
112         if (fd_to_handle(fd, &handle, &hlen) != 0) {
113                 fprintf(stderr, "%s: failed to fd_to_handle \"%s\": %s\n",
114                         prog, path, strerror(errno));
115                 return 1;
116         }
117
118         if (unlink(path) == -1) {
119                 fprintf(stderr, "%s: failed to unlink \"%s\": %s\n", prog, path, strerror(errno));
120                 return 1;
121         }
122
123         memset(&cursor, 0, sizeof(cursor));
124
125         /* just do one call - don't bother with continue logic */
126         sts = attr_list_by_handle(handle,
127                         hlen,
128                         (char*)ea_listbuf,
129                         EA_LISTBUF_SZ,
130                         ATTR_PARENT,
131                         &cursor);
132         if (sts != 0) {
133                 fprintf(stderr, "%s: failed to list attr for \"%s\": %s\n", prog, path, strerror(errno));
134                 return 1;
135         }
136
137         printf("ea count = %d\n", ea_listbuf->al_count);
138
139         /*
140          * process EA list
141          */
142         for (nameix = 0; nameix < ea_listbuf->al_count; nameix++) {
143                 entp = ATTR_ENTRY(ea_listbuf, nameix);
144
145                 sts = sscanf(entp->a_name, "%llx %llx", &parent_ino, &link_cnt);
146                 if (sts != 2) {
147                         fprintf(stderr,
148                                 "inode-path for \"%s\" is corrupted\n",
149                                 path);
150
151                         /* go onto next EA name */
152                         continue;
153                 }
154                 /* just print the info out */
155                 printf("[%d] parent_ino = %llu, link_cnt = %llu\n", nameix, parent_ino, link_cnt);
156         }
157
158
159         free_handle(handle, hlen);
160
161         if (close(fd) == -1) {
162                 fprintf(stderr, "%s: failed to close \"%s\": %s\n", prog, path, strerror(errno));
163                 return 1;
164         }
165
166         return 0;
167 }