Whamcloud - gitweb
LU-1934 ofd: implement precreate batching
[fs/lustre-release.git] / libsysio / tests / test_regions.c
1 /*
2  *    This Cplant(TM) source code is the property of Sandia National
3  *    Laboratories.
4  *
5  *    This Cplant(TM) source code is regionsrighted by Sandia National
6  *    Laboratories.
7  *
8  *    The redistribution of this Cplant(TM) source code is subject to the
9  *    terms of the GNU Lesser General Public License
10  *    (see cit/LGPL or http://www.gnu.org/licenses/lgpl.html)
11  *
12  *    Cplant(TM) Copyright 1998-2004 Sandia Corporation. 
13  *    Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
14  *    license for use of this work by or on behalf of the US Government.
15  *    Export of this program may require a license from the United States
16  *    Government.
17  */
18
19 /*
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Lesser General Public
22  * License as published by the Free Software Foundation; either
23  * version 2.1 of the License, or (at your option) any later version.
24  * 
25  * This library is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28  * Lesser General Public License for more details.
29  * 
30  * You should have received a regions of the GNU Lesser General Public
31  * License along with this library; if not, write to the Free Software
32  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33  *
34  * Questions or comments about this library should be sent to:
35  *
36  * Lee Ward
37  * Sandia National Laboratories, New Mexico
38  * P.O. Box 5800
39  * Albuquerque, NM 87185-1110
40  *
41  * lee@sandia.gov
42  */
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <limits.h>
49 #include <errno.h>
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #include <fcntl.h>
53 #include <sys/uio.h>
54 #include <getopt.h>
55
56 #if defined(SYSIO_LABEL_NAMES)
57 #include "sysio.h"
58 #endif
59 #include "xtio.h"
60 #include "test.h"
61
62 /*
63  * Copy one file to another.
64  *
65  * Usage: test_regions [-x] \
66  *              {r,w} <off> <count> <path>
67  *
68  * Destination will not be overwritten if it already exist.
69  */
70
71 #if defined(_LARGEFILE64_SOURCE) && _LARGEFILE64_SOURCE
72 #define GO64
73 #else
74 #warning Cannot prompt the 64-bit interface
75 #endif
76
77 char    which;
78 #ifdef GO64
79 int     use64 = 0;                                      /* 64-bit interface? */
80 #endif
81
82 void    usage(void);
83
84 int
85 main(int argc, char * const argv[])
86 {
87         int     i;
88         int     err;
89         long    l;
90         off_t   off;
91 #ifdef GO64
92         long long ll;
93         off64_t off64;
94 #endif
95         char    *cp;
96         unsigned long nbytes;
97         const char *path;
98         char    *buf;
99         int     flags;
100         int     fd;
101         ssize_t cc;
102         extern int _test_sysio_startup(void);
103
104         /*
105          * Parse command-line args.
106          */
107         while ((i = getopt(argc,
108                            argv,
109 #ifdef __GLIBC__
110                            "+"
111 #endif
112 #ifdef GO64
113                            "x"
114 #endif
115                            "")) != -1)
116                 switch (i) {
117
118 #ifdef GO64
119                 case 'x':
120                         use64 = 1;
121                         break;
122 #endif
123                 default:
124                         usage();
125                 }
126
127         if (argc - optind != 4)
128                 usage();
129
130         which = *argv[optind];
131         if (strlen(argv[optind]) != 1 || !(which == 'r' || which == 'w')) {
132                 (void )fprintf(stderr, "Which op?\n");
133                 exit(1);
134         }
135         optind++;
136         off = l =
137 #ifdef GO64
138             ll = strtoll(argv[optind++], &cp, 0);
139 #else
140             strtol(argv[optind++], &cp, 0);
141 #endif
142 #ifdef GO64
143         off64 = ll;
144 #endif
145         if (*cp != '\0' ||
146 #ifdef GO64
147             ((ll == LLONG_MIN || ll == LLONG_MAX) && errno == ERANGE) ||
148             off64 != ll || (!use64 && off != ll)
149 #else
150             ((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE) ||
151             off != l
152 #endif
153            ) {
154                 (void )fprintf(stderr, "Offset out of range\n");
155                 exit(1);
156         }
157         nbytes = strtoul(argv[optind++], &cp, 0);
158         if (*cp != '\0' || (nbytes == ULONG_MAX && errno == ERANGE)) {
159                 (void )fprintf(stderr, "Transfer count out of range\n");
160                 exit(1);
161         }
162         if (!(argc - optind))
163                 usage();
164         path = argv[optind++];
165
166         err = _test_sysio_startup();
167         if (err) {
168                 errno = -err;
169                 perror("sysio startup");
170                 exit(1);
171         }       
172
173         (void )umask(022);
174
175         buf = malloc(nbytes);
176         if (!buf) {
177                 perror("malloc");
178                 err = 1;
179                 goto out;
180         }
181         (void )memset(buf, 0, nbytes);
182
183         err = 0;
184         flags = which == 'r' ? O_RDONLY : (O_WRONLY|O_CREAT|O_EXCL);
185 #ifdef GO64
186         if (use64)
187                 flags |= O_LARGEFILE;
188 #endif
189         fd = SYSIO_INTERFACE_NAME(open)(path, flags, 0666);
190         if (fd < 0) {
191                 perror(path);
192                 err = 1;
193                 goto error;
194         }
195 #ifdef GO64
196         if (use64)
197                 off64 = SYSIO_INTERFACE_NAME(lseek64)(fd, off64, SEEK_SET);
198         else
199                 off64 =
200 #endif
201                   off = SYSIO_INTERFACE_NAME(lseek)(fd, off, SEEK_SET);
202 #ifdef GO64
203         if ((use64 && off64 < 0) || (!use64 && off < 0)) {
204                 perror(use64 ? "lseek64" : "lseek");
205                 err = 1;
206                 goto error;
207         }
208 #else
209         if (off < 0) {
210                 perror("lseek");
211                 err = 1;
212                 goto error;
213         }
214 #endif
215         if (which == 'r')
216                 cc = SYSIO_INTERFACE_NAME(read)(fd, buf, nbytes);
217         else
218                 cc = SYSIO_INTERFACE_NAME(write)(fd, buf, nbytes);
219         if (cc < 0) {
220                 perror(path);
221                 err = 1;
222                 goto error;
223         }
224 #ifdef GO64
225         if (use64) {
226                 off64 = SYSIO_INTERFACE_NAME(lseek64)(fd, 0, SEEK_CUR);
227         } else
228                 off64 =
229 #endif
230                   off = SYSIO_INTERFACE_NAME(lseek)(fd, 0, SEEK_CUR);
231         (void )printf(("%s%s@"
232 #ifdef GO64
233                        "%lld"
234 #else
235                        "%ld"
236 #endif
237                        ": %ld, off "
238 #ifdef GO64
239                        "%lld"
240 #else
241                        "%ld"
242 #endif
243                        "\n"),
244                       which == 'r' ? "read" : "write",
245 #ifdef GO64
246                       use64 ? "64" : "",
247                       ll,
248 #else
249                       "",
250                       l,
251 #endif
252                       (long )cc,
253 #ifdef GO64
254                       (long long int)off64
255 #else
256                       off
257 #endif
258                       );
259
260 error:
261         if (fd > 0 && SYSIO_INTERFACE_NAME(close)(fd) != 0)
262                 perror(path);
263         free(buf);
264 out:
265         _test_sysio_shutdown();
266
267         return err;
268 }
269
270 void
271 usage()
272 {
273
274         (void )fprintf(stderr,
275                        "Usage: test_regions "
276 #ifdef GO64
277                        "[-x] "
278 #endif
279                        " {r,w} <offset> <nbytes> <path>\n");
280         exit(1);
281 }