Whamcloud - gitweb
9ac97b170ca6665781e477d98d6834f5732ec0e4
[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 #define _BSD_SOURCE
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.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 <sys/queue.h>
55
56 #include "xtio.h"
57 #include "test.h"
58
59 /*
60  * Copy one file to another.
61  *
62  * Usage: test_copy [-o] <src> <dest>
63  *
64  * Destination will not be overwritten if it already exist.
65  */
66
67 static int overwrite = 0;                               /* over-write? */
68
69 void    usage(void);
70 int     copy_file(const char *spath, const char *dpath);
71
72 int
73 main(int argc, char * const argv[])
74 {
75         int     i;
76         int     err;
77         const char *spath, *dpath;
78
79         /*
80          * Parse command-line args.
81          */
82         while ((i = getopt(argc,
83                            argv,
84                            "o"
85                            )) != -1)
86                 switch (i) {
87
88                 case 'o':
89                         overwrite = 1;
90                         break;
91                 default:
92                         usage();
93                 }
94
95         if (!(argc - optind))
96                 usage();
97         err = _test_sysio_startup();
98         if (err) {
99                 errno = -err;
100                 perror("sysio startup");
101                 exit(1);
102         }
103
104         /*
105          * Source
106          */
107         spath = argv[optind++];
108         if (!(argc - optind))
109                 usage();
110         /*
111          * Destination
112          */
113         dpath = argv[optind++];
114         if (argc - optind)
115                 usage();
116
117         err = copy_file(spath, dpath);
118
119         _test_sysio_shutdown();
120
121         return err;
122 }
123
124 void
125 usage()
126 {
127
128         (void )fprintf(stderr,
129                        "Usage: test_copy "
130                        " source destination\n");
131         exit(1);
132 }
133
134 int
135 open_file(const char *path, int flags, mode_t mode)
136 {
137         int     fd;
138
139         fd = open(path, flags, mode);
140         if (fd < 0)
141                 perror(path);
142
143         return fd;
144 }
145
146 int
147 copy_file(const char *spath, const char *dpath)
148 {
149         int     sfd, dfd;
150         int     flags;
151         int     rtn;
152         static char buf[1024];
153         ssize_t cc, wcc;
154
155         sfd = dfd = -1;
156         rtn = -1;
157
158         sfd = open_file(spath, O_RDONLY, 0);
159         if (sfd < 0)
160                 goto out;
161         flags = O_CREAT|O_WRONLY;
162         if (!overwrite)
163                 flags |= O_EXCL;
164         dfd = open_file(dpath, flags, 0666);
165         if (dfd < 0)
166                 goto out;
167
168         while ((cc = read(sfd, buf, sizeof(buf))) > 0)
169                 if ((wcc = write(dfd, buf, cc)) != cc) {
170                         if (wcc < 0) {
171                                 perror(dpath);
172                                 break;
173                         }
174                         (void )fprintf(stderr,
175                                        "%s: short write (%u/%u)\n",
176                                        dpath,
177                                        (unsigned )wcc,
178                                        (unsigned )cc);
179                         break;
180                 }
181         if (cc < 0)
182                 perror(spath);
183
184 out:
185         if (sfd >= 0 && close(sfd) != 0)
186                 perror(spath);
187         if (dfd >= 0 && (fsync(dfd) != 0 || close(dfd) != 0))
188                 perror(dpath);
189
190         return 0;
191 }