1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include <fcntl.h>
#include <sys/types.h>
#include <utime.h>
#include <sys/stat.h>
#include <locale.h>
#include <libintl.h>
#include <sys/mman.h>
/*
* consolidation pkg command library includes
*/
#include "pkglib.h"
/*
* local pkg command library includes
*/
#include "libinst.h"
#include "libadm.h"
#include "messages.h"
/*
* MAXMAPSIZE controls the largest mapping to use at a time; please refer
* to mmap(2) for details of how this size is incremented and rounded; briefly
* each mapping request has an additional 16Kb added to it - mappings over
* 4Mb will be rounded to a 4Mb boundary - thus if there were 8mb, adding
* in the 16Kb overhead the mapping would use another 4Mb-16kb - that is
* why there is 16Kb subtracted from the total
*/
#define MAXMAPSIZE (1024*1024*8)-(1024*16) /* map at most 8MB */
#define SMALLFILESIZE (32*1024) /* dont mmap files less than 32kb */
/*
* Name: copyF
* Description: fast copy of file - use mmap()/write() loop if possible
* Arguments: char *srcPath - name of source file to copy from
* char *dstPath - name of target file to copy to
* time_t a_mytime: control setting of access/modification times:
* == 0 - replicate source file access/modification times
* != 0 - use specified time for access/modification times
* Returns: int
* == 0 - successful
* != 0 - failure
*/
int
copyf(char *a_srcPath, char *a_dstPath, time_t a_mytime)
{
struct stat srcStatbuf;
struct utimbuf times;
int srcFd;
int dstFd;
int status;
char *pt;
/* open source file for reading */
srcFd = open(a_srcPath, O_RDONLY, 0);
if (srcFd < 0) {
progerr(ERR_OPEN_READ, a_srcPath, errno, strerror(errno));
return (-1);
}
/* obtain file status of source file */
if (fstat(srcFd, &srcStatbuf) != 0) {
progerr(ERR_FSTAT, srcFd, a_srcPath, errno, strerror(errno));
(void) close(srcFd);
return (-1);
}
/* open target file for writing */
dstFd = open(a_dstPath, O_WRONLY | O_TRUNC | O_CREAT,
srcStatbuf.st_mode);
if (dstFd < 0) {
/* create directory structure if missing */
pt = a_dstPath;
while (pt = strchr(pt+1, '/')) {
*pt = '\0';
if (isdir(a_dstPath)) {
if (mkdir(a_dstPath, 0755)) {
progerr(ERR_NODIR, a_dstPath,
errno, strerror(errno));
*pt = '/';
(void) close(srcFd);
return (-1);
}
}
*pt = '/';
}
/* attempt to create target file again */
dstFd = open(a_dstPath, O_WRONLY | O_TRUNC | O_CREAT,
srcStatbuf.st_mode);
if (dstFd < 0) {
progerr(ERR_OPEN_WRITE, a_dstPath, errno,
strerror(errno));
(void) close(srcFd);
return (-1);
}
}
/*
* source and target files are open: copy data
*/
status = copyFile(srcFd, dstFd, a_srcPath, a_dstPath, &srcStatbuf, 0);
(void) close(srcFd);
(void) close(dstFd);
/*
* determine how to set access/modification times for target:
* -- a_mytime == 0: replicate source file access/modification times
* -- otherwise: use a_mytime for file access/modification times
*/
if (a_mytime == 0) {
times.actime = srcStatbuf.st_atime;
times.modtime = srcStatbuf.st_mtime;
} else {
times.actime = a_mytime;
times.modtime = a_mytime;
}
/* set access/modification times for target */
if (utime(a_dstPath, ×) != 0) {
progerr(ERR_MODTIM, a_dstPath, errno, strerror(errno));
return (-1);
}
/* return error if copy failed */
if (status != 0) {
progerr(ERR_READ, a_srcPath, errno, strerror(errno));
return (-1);
}
/* success! */
return (0);
}
/*
* Name: copyFile
* Description: fast copy of file - use mmap()/write() loop if possible
* Arguments: int srcFd - file descriptor open on source file
* int dstFd - file descriptor open on target file
* char *srcPath - name of source file (for error messages)
* char *dstPath - name of target file (for error messages)
* struct stat *a_srcStatbuf - stat structure for source file
* long a_iosize - preferred io size for read/write loop
* Returns: int
* == 0 - successful
* != 0 - failure
*/
int
copyFile(int a_srcFd, int a_dstFd, char *a_srcPath, char *a_dstPath,
struct stat *a_srcStatbuf, long a_iosize)
{
caddr_t cp;
off_t filesize = a_srcStatbuf->st_size;
size_t mapsize = 0;
size_t munmapsize = 0;
off_t offset = 0;
echoDebug(DBG_COPY_FILE, a_srcPath, a_dstPath);
/*
* if the source is a regular file and is not "too small", then cause
* the file to be mapped into memory
*/
if (S_ISREG(a_srcStatbuf->st_mode) && (filesize > SMALLFILESIZE)) {
/*
* Determine size of initial mapping. This will determine the
* size of the address space chunk we work with. This initial
* mapping size will be used to perform munmap() in the future.
*/
mapsize = MAXMAPSIZE;
if (filesize < mapsize) {
mapsize = filesize;
}
/*
* remember size of mapping to "unmap" - if the source file
* exceeds MAXMAPSIZE bytes, then the final mapping of the
* source file will be less than MAXMAPSIZE, and we need to
* make sure that the entire mapping is unmapped when done.
*/
munmapsize = mapsize;
/* map the first segment of the source into memory */
cp = mmap((caddr_t)NULL, mapsize, PROT_READ,
(MAP_SHARED|MAP_ALIGN), a_srcFd, (off_t)0);
if (cp == MAP_FAILED) {
mapsize = 0; /* can't mmap today */
}
}
/*
* if the source was not mapped into memory, copy via read/write loop
*/
if (mapsize == 0) {
char *buf = (char *)NULL;
size_t blocksize;
int pagesize = getpagesize();
/* set blocksize for copy */
blocksize = a_iosize;
if ((blocksize == 0) || (blocksize > SMALLFILESIZE)) {
blocksize = SMALLFILESIZE;
} else if (blocksize < pagesize) {
blocksize = pagesize;
}
/* allocate i/o transfer buffer */
buf = memalign((size_t)pagesize, blocksize);
if (buf == (char *)NULL) {
progerr(ERR_COPY_MEMORY, a_srcPath, errno,
strerror(errno));
return (1);
}
/* copy the file contents */
for (;;) {
ssize_t n;
/* read next block of data */
n = read(a_srcFd, buf, blocksize);
if (n == 0) {
/* end of file - return success */
(void) free(buf);
return (0);
} else if (n < 0) {
/* read error - return error */
progerr(ERR_READ, a_srcPath,
errno, strerror(errno));
(void) free(buf);
return (1);
}
/* write out block of data just read in */
if (vfpSafeWrite(a_dstFd, buf, (size_t)n) != n) {
/* short write/write error - return error */
progerr(ERR_WRITE, a_dstPath,
errno, strerror(errno));
(void) free(buf);
return (1);
}
}
}
/*
* the source has been mapped into memory, copy via mappings
*/
for (;;) {
ssize_t nbytes;
/* write first mappings worth of data */
nbytes = write(a_dstFd, cp, mapsize);
/*
* if we write less than the mmaped size it's due to a
* media error on the input file or out of space on
* the output file. So, try again, and look for errno.
*/
if ((nbytes >= 0) && (nbytes != (ssize_t)mapsize)) {
size_t remains;
remains = mapsize - nbytes;
while (remains > 0) {
nbytes = write(a_dstFd,
(cp + mapsize - remains), remains);
if (nbytes >= 0) {
remains -= nbytes;
if (remains == 0) {
nbytes = mapsize;
}
continue;
}
/* i/o error - report and exit */
if (errno == ENOSPC) {
progerr(ERR_WRITE, a_dstPath,
errno, strerror(errno));
} else {
progerr(ERR_READ, a_srcPath,
errno, strerror(errno));
}
/* unmap source file mapping */
(void) munmap(cp, munmapsize);
return (1);
}
}
/*
* although the write manual page doesn't specify this
* as a possible errno, it is set when the nfs read
* via the mmap'ed file is accessed, so report the
* problem as a source access problem, not a target file
* problem
*/
if (nbytes < 0) {
if (errno == EACCES) {
progerr(ERR_READ, a_srcPath,
errno, strerror(errno));
} else {
progerr(ERR_WRITE, a_dstPath,
errno, strerror(errno));
}
/* unmap source file mapping */
(void) munmap(cp, munmapsize);
return (1);
}
filesize -= nbytes;
if (filesize == 0) {
break;
}
offset += nbytes;
if (filesize < mapsize) {
mapsize = filesize;
}
/* map next segment of file on top of existing mapping */
cp = mmap(cp, mapsize, PROT_READ, (MAP_SHARED|MAP_FIXED),
a_srcFd, offset);
if (cp == MAP_FAILED) {
progerr(ERR_MAPFAILED, a_srcPath, errno,
strerror(errno));
/* unmap source file mapping */
(void) munmap(cp, munmapsize);
return (1);
}
}
/* unmap source file mapping */
(void) munmap(cp, munmapsize);
return (0);
}
/*
* Name: openLocal
* Description: open a file and assure that the descriptor returned is open on
* a file that is local to the current system - if the file is not
* local to this system, copy the file to a temporary file first,
* and then pass a handle back opened on the temporary file
* Arguments: a_path - [RO, *RO] - (char *)
* Pointer to string representing the path to the file
* to open
* a_oflag - [RO, *RO] - (int)
* Integer representing the "mode" bits for an open(2) call
* a_tmpdir - [RO, *RO] - (char *)
* Pointer to string representing the path to a directory
* where a temporary copy of the file can be placed if
* the source file is not local to this system. If this is
* NULL or does not exist, P_tmpdir is used.
* Returns: int
* >= 0 - file descriptor opened on the file
* == -1 - failed to open - errno contains error code
* NOTE: If the file is not local and is copied locally, the file is
* setup in such a way that it will be removed when the last
* file descriptor opened on the file is closed - there is no need
* to know the path to the temporary file or to remove it
* when done.
*/
int
openLocal(char *a_path, int a_oflag, char *a_tmpdir)
{
char *bn;
char template[PATH_MAX];
int fd;
int lerrno;
int n;
int tmpFd;
struct stat statbuf;
/* open source file */
fd = open(a_path, a_oflag);
if (fd < 0) {
return (fd);
}
/* return open fd if the source file is not remote */
if (!isFdRemote(fd)) {
return (fd);
}
/*
* source file is remote - must make a local copy
*/
/* get the source file's status */
n = fstat(fd, &statbuf);
if (n < 0) {
lerrno = errno;
(void) close(fd);
errno = lerrno;
return (-1);
}
/* generate unique temporary file name */
if ((a_tmpdir == (char *)NULL) || (*a_tmpdir == '\0') ||
(isdir(a_tmpdir) != 0)) {
a_tmpdir = P_tmpdir;
}
bn = basename(a_path);
n = strlen(a_tmpdir);
n = snprintf(template, sizeof (template), "%s%s%sXXXXXX",
a_tmpdir, a_tmpdir[n-1] == '/' ? "" : "/", bn);
if (n > sizeof (template)) {
(void) close(fd);
return (EINVAL);
}
/* create the temporary file and open it */
tmpFd = mkstemp(template);
if (tmpFd < 0) {
lerrno = errno;
(void) close(fd);
errno = lerrno;
return (tmpFd);
}
/* unlink the file so when it is closed it is automatically deleted */
(void) unlink(template);
/* copy the source file to the temporary file */
n = copyFile(fd, tmpFd, a_path, template, &statbuf, 0L);
lerrno = errno;
(void) close(fd);
if (n != 0) {
(void) close(tmpFd);
errno = lerrno;
return (-1);
}
/* return handle to temporary file created */
return (tmpFd);
}
|