Whamcloud - gitweb
Revert "b=19808 2.6.29-fc11 patchless client support"
[fs/lustre-release.git] / libsysio / tests / test_copy.c
1 /*
2  *    This Cplant(TM) source code is the property of Sandia National
3  *    Laboratories.
4  *
5  *    This Cplant(TM) source code is copyrighted 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-2003 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 copy 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 <unistd.h>
47 #include <errno.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <fcntl.h>
51 #include <sys/uio.h>
52 #include <sys/queue.h>
53 #include <getopt.h>
54
55 #if defined(SYSIO_LABEL_NAMES)
56 #include "sysio.h"
57 #endif
58 #include "xtio.h"
59 #include "test.h"
60
61 /*
62  * Copy one file to another.
63  *
64  * Usage: test_copy [-o] <src> <dest>
65  *
66  * Destination will not be overwritten if it already exist.
67  */
68
69 static int overwrite = 0;                               /* over-write? */
70
71 void    usage(void);
72 int     copy_file(const char *spath, const char *dpath);
73
74 int
75 main(int argc, char * const argv[])
76 {
77         int     i;
78         int     err;
79         const char *spath, *dpath;
80
81         /*
82          * Parse command-line args.
83          */
84         while ((i = getopt(argc,
85                            argv,
86                            "o"
87                            )) != -1)
88                 switch (i) {
89
90                 case 'o':
91                         overwrite = 1;
92                         break;
93                 default:
94                         usage();
95                 }
96
97         if (!(argc - optind))
98                 usage();
99         err = _test_sysio_startup();
100         if (err) {
101                 errno = -err;
102                 perror("sysio startup");
103                 exit(1);
104         }
105
106         /*
107          * Source
108          */
109         spath = argv[optind++];
110         if (!(argc - optind))
111                 usage();
112         /*
113          * Destination
114          */
115         dpath = argv[optind++];
116         if (argc - optind)
117                 usage();
118
119         err = copy_file(spath, dpath);
120
121         _test_sysio_shutdown();
122
123         return err;
124 }
125
126 void
127 usage()
128 {
129
130         (void )fprintf(stderr,
131                        "Usage: test_copy "
132                        " source destination\n");
133         exit(1);
134 }
135
136 int
137 open_file(const char *path, int flags, mode_t mode)
138 {
139         int     fd;
140
141         fd = SYSIO_INTERFACE_NAME(open)(path, flags, mode);
142         if (fd < 0)
143                 perror(path);
144
145         return fd;
146 }
147
148 int
149 copy_file(const char *spath, const char *dpath)
150 {
151         int     sfd, dfd;
152         int     flags;
153         int     rtn;
154         struct stat stat;
155         char    *buf;
156         size_t  bufsiz;
157         ssize_t cc, wcc;
158
159         sfd = dfd = -1;
160         rtn = -1;
161         buf = NULL;
162
163         sfd = open_file(spath, O_RDONLY, 0);
164         if (sfd < 0)
165                 goto out;
166         flags = O_CREAT|O_WRONLY;
167         if (!overwrite)
168                 flags |= O_EXCL;
169         dfd = open_file(dpath, flags, 0666);
170         if (dfd < 0)
171                 goto out;
172
173         rtn = SYSIO_INTERFACE_NAME(fstat)(dfd, &stat);
174         if (rtn != 0) {
175                 perror(dpath);
176                 goto out;
177         }
178         bufsiz = stat.st_blksize;
179         if (bufsiz < (64 * 1024))
180                 bufsiz =
181                     (((64 * 1024) / stat.st_blksize - 1) + 1) * (64 * 1024);
182         buf = malloc(bufsiz);
183         if (!buf) {
184                 perror(dpath);
185                 goto out;
186         }
187
188         while ((cc = SYSIO_INTERFACE_NAME(read)(sfd, buf, bufsiz)) > 0)
189                 if ((wcc = SYSIO_INTERFACE_NAME(write)(dfd, buf, cc)) != cc) {
190                         if (wcc < 0) {
191                                 perror(dpath);
192                                 break;
193                         }
194                         (void )fprintf(stderr,
195                                        "%s: short write (%u/%u)\n",
196                                        dpath,
197                                        (unsigned )wcc,
198                                        (unsigned )cc);
199                         break;
200                 }
201         if (cc < 0) {
202                 perror(spath);
203                 rtn = -1;
204         }
205
206 out:
207         if (buf)
208                 free(buf);
209         if (sfd >= 0 && SYSIO_INTERFACE_NAME(close)(sfd) != 0)
210                 perror(spath);
211         if (dfd >= 0 &&
212             (SYSIO_INTERFACE_NAME(fsync)(dfd) != 0 ||
213              SYSIO_INTERFACE_NAME(close)(dfd) != 0))
214                 perror(dpath);
215
216         return rtn;
217 }