]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. | |
3 | * All Rights Reserved. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU General Public License as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it would be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | */ | |
18 | ||
19 | #include <libxfs.h> | |
20 | ||
21 | int rtcp(char *, char *, int); | |
22 | int xfsrtextsize(char *path); | |
23 | ||
24 | int pflag; | |
25 | char *progname; | |
26 | ||
27 | void | |
28 | usage() | |
29 | { | |
30 | fprintf(stderr, _("%s [-e extsize] [-p] source target\n"), progname); | |
31 | exit(2); | |
32 | } | |
33 | ||
34 | int | |
35 | main(int argc, char **argv) | |
36 | { | |
37 | register int c, i, r, errflg = 0; | |
38 | struct stat64 s2; | |
39 | int extsize = - 1; | |
40 | ||
41 | progname = basename(argv[0]); | |
42 | setlocale(LC_ALL, ""); | |
43 | bindtextdomain(PACKAGE, LOCALEDIR); | |
44 | textdomain(PACKAGE); | |
45 | ||
46 | while ((c = getopt(argc, argv, "pe:V")) != EOF) { | |
47 | switch (c) { | |
48 | case 'e': | |
49 | extsize = atoi(optarg); | |
50 | break; | |
51 | case 'p': | |
52 | pflag = 1; | |
53 | break; | |
54 | case 'V': | |
55 | printf(_("%s version %s\n"), progname, VERSION); | |
56 | exit(0); | |
57 | default: | |
58 | errflg++; | |
59 | } | |
60 | } | |
61 | ||
62 | /* | |
63 | * Check for sufficient arguments or a usage error. | |
64 | */ | |
65 | argc -= optind; | |
66 | argv = &argv[optind]; | |
67 | ||
68 | if (argc < 2) { | |
69 | fprintf(stderr, _("%s: must specify files to copy\n"), | |
70 | progname); | |
71 | errflg++; | |
72 | } | |
73 | ||
74 | if (errflg) | |
75 | usage(); | |
76 | ||
77 | /* | |
78 | * If there is more than a source and target, | |
79 | * the last argument (the target) must be a directory | |
80 | * which really exists. | |
81 | */ | |
82 | if (argc > 2) { | |
83 | if (stat64(argv[argc-1], &s2) < 0) { | |
84 | fprintf(stderr, _("%s: stat64 of %s failed\n"), | |
85 | progname, argv[argc-1]); | |
86 | exit(2); | |
87 | } | |
88 | ||
89 | if (!S_ISDIR(s2.st_mode)) { | |
90 | fprintf(stderr, | |
91 | _("%s: final argument is not directory\n"), | |
92 | progname); | |
93 | usage(); | |
94 | } | |
95 | } | |
96 | ||
97 | /* | |
98 | * Perform a multiple argument rtcp by | |
99 | * multiple invocations of rtcp(). | |
100 | */ | |
101 | r = 0; | |
102 | for (i = 0; i < argc-1; i++) | |
103 | r += rtcp(argv[i], argv[argc-1], extsize); | |
104 | ||
105 | /* | |
106 | * Show errors by nonzero exit code. | |
107 | */ | |
108 | exit(r?2:0); | |
109 | } | |
110 | ||
111 | int | |
112 | rtcp( char *source, char *target, int fextsize) | |
113 | { | |
114 | int fromfd, tofd, readct, writect, iosz, reopen; | |
115 | int remove = 0, rtextsize; | |
116 | char *sp, *fbuf, *ptr; | |
117 | char tbuf[ PATH_MAX ]; | |
118 | struct stat64 s1, s2; | |
119 | struct fsxattr fsxattr; | |
120 | struct dioattr dioattr; | |
121 | ||
122 | /* | |
123 | * While source or target have trailing /, remove them | |
124 | * unless only "/". | |
125 | */ | |
126 | sp = source + strlen(source); | |
127 | if (sp) { | |
128 | while (*--sp == '/' && sp > source) | |
129 | *sp = '\0'; | |
130 | } | |
131 | sp = target + strlen(target); | |
132 | if (sp) { | |
133 | while (*--sp == '/' && sp > target) | |
134 | *sp = '\0'; | |
135 | } | |
136 | ||
137 | if ( stat64(source, &s1) ) { | |
138 | fprintf(stderr, _("%s: failed stat64 on %s: %s\n"), | |
139 | progname, source, strerror(errno)); | |
140 | return( -1); | |
141 | } | |
142 | ||
143 | /* | |
144 | * check for a realtime partition | |
145 | */ | |
146 | snprintf(tbuf, sizeof(tbuf), "%s", target); | |
147 | if ( stat64(target, &s2) ) { | |
148 | if (!S_ISDIR(s2.st_mode)) { | |
149 | /* take out target file name */ | |
150 | if ((ptr = strrchr(tbuf, '/')) != NULL) | |
151 | *ptr = '\0'; | |
152 | else | |
153 | snprintf(tbuf, sizeof(tbuf), "."); | |
154 | } | |
155 | } | |
156 | ||
157 | if ( (rtextsize = xfsrtextsize( tbuf )) <= 0 ) { | |
158 | fprintf(stderr, | |
159 | _("%s: %s filesystem has no realtime partition\n"), | |
160 | progname, tbuf); | |
161 | return( -1 ); | |
162 | } | |
163 | ||
164 | /* | |
165 | * check if target is a directory | |
166 | */ | |
167 | snprintf(tbuf, sizeof(tbuf), "%s", target); | |
168 | if ( !stat64(target, &s2) ) { | |
169 | if (S_ISDIR(s2.st_mode)) { | |
170 | snprintf(tbuf, sizeof(tbuf), "%s/%s", target, | |
171 | basename(source)); | |
172 | } | |
173 | } | |
174 | ||
175 | if ( stat64(tbuf, &s2) ) { | |
176 | /* | |
177 | * create the file if it does not exist | |
178 | */ | |
179 | if ( (tofd = open(tbuf, O_RDWR|O_CREAT|O_DIRECT, 0666)) < 0 ) { | |
180 | fprintf(stderr, _("%s: open of %s failed: %s\n"), | |
181 | progname, tbuf, strerror(errno)); | |
182 | return( -1 ); | |
183 | } | |
184 | remove = 1; | |
185 | ||
186 | /* | |
187 | * mark the file as a realtime file | |
188 | */ | |
189 | fsxattr.fsx_xflags = XFS_XFLAG_REALTIME; | |
190 | if (fextsize != -1 ) | |
191 | fsxattr.fsx_extsize = fextsize; | |
192 | else | |
193 | fsxattr.fsx_extsize = 0; | |
194 | ||
195 | if ( xfsctl(tbuf, tofd, XFS_IOC_FSSETXATTR, &fsxattr) ) { | |
196 | fprintf(stderr, | |
197 | _("%s: set attributes on %s failed: %s\n"), | |
198 | progname, tbuf, strerror(errno)); | |
199 | close( tofd ); | |
200 | unlink( tbuf ); | |
201 | return( -1 ); | |
202 | } | |
203 | } else { | |
204 | /* | |
205 | * open existing file | |
206 | */ | |
207 | if ( (tofd = open(tbuf, O_RDWR|O_DIRECT)) < 0 ) { | |
208 | fprintf(stderr, _("%s: open of %s failed: %s\n"), | |
209 | progname, tbuf, strerror(errno)); | |
210 | return( -1 ); | |
211 | } | |
212 | ||
213 | if ( xfsctl(tbuf, tofd, XFS_IOC_FSGETXATTR, &fsxattr) ) { | |
214 | fprintf(stderr, | |
215 | _("%s: get attributes of %s failed: %s\n"), | |
216 | progname, tbuf, strerror(errno)); | |
217 | close( tofd ); | |
218 | return( -1 ); | |
219 | } | |
220 | ||
221 | /* | |
222 | * check if the existing file is already a realtime file | |
223 | */ | |
224 | if ( !(fsxattr.fsx_xflags & XFS_XFLAG_REALTIME) ) { | |
225 | fprintf(stderr, _("%s: %s is not a realtime file.\n"), | |
226 | progname, tbuf); | |
227 | return( -1 ); | |
228 | } | |
229 | ||
230 | /* | |
231 | * check for matching extent size | |
232 | */ | |
233 | if ( (fextsize != -1) && (fsxattr.fsx_extsize != fextsize) ) { | |
234 | fprintf(stderr, _("%s: %s file extent size is %d, " | |
235 | "instead of %d.\n"), | |
236 | progname, tbuf, fsxattr.fsx_extsize, fextsize); | |
237 | return( -1 ); | |
238 | } | |
239 | } | |
240 | ||
241 | /* | |
242 | * open the source file | |
243 | */ | |
244 | reopen = 0; | |
245 | if ( (fromfd = open(source, O_RDONLY|O_DIRECT)) < 0 ) { | |
246 | fprintf(stderr, _("%s: open of %s source failed: %s\n"), | |
247 | progname, source, strerror(errno)); | |
248 | close( tofd ); | |
249 | if (remove) | |
250 | unlink( tbuf ); | |
251 | return( -1 ); | |
252 | } | |
253 | ||
254 | fsxattr.fsx_xflags = 0; | |
255 | fsxattr.fsx_extsize = 0; | |
256 | if ( xfsctl(source, fromfd, XFS_IOC_FSGETXATTR, &fsxattr) ) { | |
257 | reopen = 1; | |
258 | } else { | |
259 | if (! (fsxattr.fsx_xflags & XFS_XFLAG_REALTIME) ){ | |
260 | fprintf(stderr, _("%s: %s is not a realtime file.\n"), | |
261 | progname, source); | |
262 | reopen = 1; | |
263 | } | |
264 | } | |
265 | ||
266 | if (reopen) { | |
267 | close( fromfd ); | |
268 | if ( (fromfd = open(source, O_RDONLY )) < 0 ) { | |
269 | fprintf(stderr, _("%s: open of %s source failed: %s\n"), | |
270 | progname, source, strerror(errno)); | |
271 | close( tofd ); | |
272 | if (remove) | |
273 | unlink( tbuf ); | |
274 | return( -1 ); | |
275 | } | |
276 | } | |
277 | ||
278 | /* | |
279 | * get direct I/O parameters | |
280 | */ | |
281 | if ( xfsctl(tbuf, tofd, XFS_IOC_DIOINFO, &dioattr) ) { | |
282 | fprintf(stderr, | |
283 | _("%s: couldn't get direct I/O information: %s\n"), | |
284 | progname, strerror(errno)); | |
285 | close( fromfd ); | |
286 | close( tofd ); | |
287 | if ( remove ) | |
288 | unlink( tbuf ); | |
289 | return( -1 ); | |
290 | } | |
291 | ||
292 | if ( rtextsize % dioattr.d_miniosz ) { | |
293 | fprintf(stderr, _("%s: extent size %d not a multiple of %d.\n"), | |
294 | progname, rtextsize, dioattr.d_miniosz); | |
295 | close( fromfd ); | |
296 | close( tofd ); | |
297 | if ( remove ) | |
298 | unlink( tbuf ); | |
299 | return( -1 ); | |
300 | } | |
301 | ||
302 | /* | |
303 | * Check that the source file size is a multiple of the | |
304 | * file system block size. | |
305 | */ | |
306 | if ( s1.st_size % dioattr.d_miniosz ) { | |
307 | printf(_("The size of %s is not a multiple of %d.\n"), | |
308 | source, dioattr.d_miniosz); | |
309 | if ( pflag ) { | |
310 | printf(_("%s will be padded to %lld bytes.\n"), | |
311 | tbuf, (long long) | |
312 | (((s1.st_size / dioattr.d_miniosz) + 1) * | |
313 | dioattr.d_miniosz) ); | |
314 | ||
315 | } else { | |
316 | printf(_("Use the -p option to pad %s to a " | |
317 | "size which is a multiple of %d bytes.\n"), | |
318 | tbuf, dioattr.d_miniosz); | |
319 | close( fromfd ); | |
320 | close( tofd ); | |
321 | if ( remove ) | |
322 | unlink( tbuf ); | |
323 | return( -1 ); | |
324 | } | |
325 | } | |
326 | ||
327 | iosz = dioattr.d_miniosz; | |
328 | fbuf = memalign( dioattr.d_mem, iosz); | |
329 | memset(fbuf, 0, iosz); | |
330 | ||
331 | /* | |
332 | * read the entire source file | |
333 | */ | |
334 | while ( ( readct = read( fromfd, fbuf, iosz) ) != 0 ) { | |
335 | /* | |
336 | * if there is a read error - break | |
337 | */ | |
338 | if (readct < 0 ) { | |
339 | break; | |
340 | } | |
341 | ||
342 | /* | |
343 | * if there is a short read, pad to a block boundary | |
344 | */ | |
345 | if ( readct != iosz ) { | |
346 | if ( (readct % dioattr.d_miniosz) != 0 ) { | |
347 | readct = ( (readct/dioattr.d_miniosz) + 1 ) * | |
348 | dioattr.d_miniosz; | |
349 | } | |
350 | } | |
351 | ||
352 | /* | |
353 | * write to target file | |
354 | */ | |
355 | writect = write( tofd, fbuf, readct); | |
356 | ||
357 | if ( writect != readct ) { | |
358 | fprintf(stderr, _("%s: write error: %s\n"), | |
359 | progname, strerror(errno)); | |
360 | close(fromfd); | |
361 | close(tofd); | |
362 | free( fbuf ); | |
363 | return( -1 ); | |
364 | } | |
365 | ||
366 | memset( fbuf, 0, iosz); | |
367 | } | |
368 | ||
369 | close(fromfd); | |
370 | close(tofd); | |
371 | free( fbuf ); | |
372 | return( 0 ); | |
373 | } | |
374 | ||
375 | /* | |
376 | * Determine the realtime extent size of the XFS file system | |
377 | */ | |
378 | int | |
379 | xfsrtextsize( char *path) | |
380 | { | |
381 | int fd, rval, rtextsize; | |
382 | xfs_fsop_geom_v1_t geo; | |
383 | ||
384 | fd = open( path, O_RDONLY ); | |
385 | if ( fd < 0 ) { | |
386 | fprintf(stderr, _("%s: could not open %s: %s\n"), | |
387 | progname, path, strerror(errno)); | |
388 | return -1; | |
389 | } | |
390 | rval = xfsctl( path, fd, XFS_IOC_FSGEOMETRY_V1, &geo ); | |
391 | close(fd); | |
392 | if ( rval < 0 ) | |
393 | return -1; | |
394 | ||
395 | rtextsize = geo.rtextsize * geo.blocksize; | |
396 | ||
397 | return rtextsize; | |
398 | } |