1 /* $NetBSD: utils.c,v 1.44 2015/03/03 00:20:38 enami Exp $ */
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94";
37 __RCSID("$NetBSD: utils.c,v 1.44 2015/03/03 00:20:38 enami Exp $");
42 #include <sys/param.h>
45 #include <sys/extattr.h>
59 #define MMAP_MAX_SIZE (8 * 1048576)
60 #define MMAP_MAX_WRITE (64 * 1024)
63 set_utimes(const char *file
, struct stat
*fs
)
65 struct timespec ts
[2];
67 ts
[0] = fs
->st_atimespec
;
68 ts
[1] = fs
->st_mtimespec
;
70 if (lutimens(file
, ts
)) {
71 warn("lutimens: %s", file
);
84 progress(const struct finfo
*fi
, size_t written
)
86 int pcent
= (int)((100.0 * written
) / fi
->size
);
89 (void)fprintf(stderr
, "%s => %s %zu/%zu bytes %d%% written\n",
90 fi
->from
, fi
->to
, written
, fi
->size
, pcent
);
94 copy_file(FTSENT
*entp
, int dne
)
96 static char buf
[MAXBSIZE
];
97 struct stat to_stat
, *fs
;
98 int ch
, checkch
, from_fd
, rcount
, rval
, to_fd
, tolnk
, wcount
;
102 if ((from_fd
= open(entp
->fts_path
, O_RDONLY
, 0)) == -1) {
103 warn("%s", entp
->fts_path
);
108 fs
= entp
->fts_statp
;
109 tolnk
= ((Rflag
&& !(Lflag
|| Hflag
)) || Pflag
);
112 * If the file exists and we're interactive, verify with the user.
113 * If the file DNE, set the mode to be the from file, minus setuid
114 * bits, modified by the umask; arguably wrong, but it makes copying
115 * executables work right and it's been that way forever. (The
116 * other choice is 666 or'ed with the execute bits on the from file
117 * modified by the umask.)
124 (void)fprintf(stderr
, "overwrite %s? ", to
.p_path
);
125 checkch
= ch
= getchar();
126 while (ch
!= '\n' && ch
!= EOF
)
128 if (checkch
!= 'y' && checkch
!= 'Y') {
129 (void)close(from_fd
);
135 lstat(to
.p_path
, &sb
) : stat(to
.p_path
, &sb
);
137 warn("stat: %s", to
.p_path
);
138 (void)close(from_fd
);
142 if (!(tolnk
&& S_ISLNK(sb
.st_mode
)))
143 to_fd
= open(to
.p_path
, O_WRONLY
| O_TRUNC
, 0);
145 to_fd
= open(to
.p_path
, O_WRONLY
| O_TRUNC
| O_CREAT
,
146 fs
->st_mode
& ~(S_ISUID
| S_ISGID
));
148 if (to_fd
== -1 && (fflag
|| tolnk
)) {
150 * attempt to remove existing destination file name and
153 (void)unlink(to
.p_path
);
154 to_fd
= open(to
.p_path
, O_WRONLY
| O_TRUNC
| O_CREAT
,
155 fs
->st_mode
& ~(S_ISUID
| S_ISGID
));
159 warn("%s", to
.p_path
);
160 (void)close(from_fd
);
166 /* if hard linking then simply close the open fds, link and return */
168 (void)close(from_fd
);
170 (void)unlink(to
.p_path
);
171 if (link(entp
->fts_path
, to
.p_path
)) {
172 warn("%s", to
.p_path
);
180 * There's no reason to do anything other than close the file
181 * now if it's empty, so let's not bother.
183 if (fs
->st_size
> 0) {
186 fi
.from
= entp
->fts_path
;
188 fi
.size
= (size_t)fs
->st_size
;
191 * Mmap and write if less than 8M (the limit is so
192 * we don't totally trash memory on big files).
193 * This is really a minor hack, but it wins some CPU back.
198 if (fs
->st_size
<= MMAP_MAX_SIZE
) {
199 size_t fsize
= (size_t)fs
->st_size
;
200 p
= mmap(NULL
, fsize
, PROT_READ
, MAP_FILE
|MAP_SHARED
,
202 if (p
!= MAP_FAILED
) {
207 #if !defined(__minix)
208 (void) madvise(p
, (size_t)fs
->st_size
,
210 #endif /* !defined(__minix) */
213 * Write out the data in small chunks to
214 * avoid locking the output file for a
215 * long time if the reading the data from
216 * the source is slow.
222 chunk
= (remainder
> MMAP_MAX_WRITE
) ?
223 MMAP_MAX_WRITE
: remainder
;
224 if (write(to_fd
, &p
[fsize
- remainder
],
226 warn("%s", to
.p_path
);
233 progress(&fi
, ptotal
);
234 } while (remainder
> 0);
236 if (munmap(p
, fsize
) < 0) {
237 warn("%s", entp
->fts_path
);
244 while ((rcount
= read(from_fd
, buf
, MAXBSIZE
)) > 0) {
245 wcount
= write(to_fd
, buf
, (size_t)rcount
);
246 if (rcount
!= wcount
|| wcount
== -1) {
247 warn("%s", to
.p_path
);
253 progress(&fi
, ptotal
);
256 warn("%s", entp
->fts_path
);
262 #if !defined(__minix)
263 if (pflag
&& (fcpxattr(from_fd
, to_fd
) != 0))
264 warn("%s: error copying extended attributes", to
.p_path
);
265 #endif /* !defined(__minix) */
267 (void)close(from_fd
);
274 if (pflag
&& setfile(fs
, to_fd
))
277 * If the source was setuid or setgid, lose the bits unless the
278 * copy is owned by the same user and group.
281 (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
283 && fs
->st_mode
& (S_ISUID
| S_ISGID
) && fs
->st_uid
== myuid
) {
284 if (fstat(to_fd
, &to_stat
)) {
285 warn("%s", to
.p_path
);
287 } else if (fs
->st_gid
== to_stat
.st_gid
&&
288 fchmod(to_fd
, fs
->st_mode
& RETAINBITS
& ~myumask
)) {
289 warn("%s", to
.p_path
);
294 warn("%s", to
.p_path
);
297 /* set the mod/access times now after close of the fd */
298 if (pflag
&& set_utimes(to
.p_path
, fs
)) {
305 copy_link(FTSENT
*p
, int exists
)
308 char target
[MAXPATHLEN
];
310 if ((len
= readlink(p
->fts_path
, target
, sizeof(target
)-1)) == -1) {
311 warn("readlink: %s", p
->fts_path
);
315 if (exists
&& unlink(to
.p_path
)) {
316 warn("unlink: %s", to
.p_path
);
319 if (symlink(target
, to
.p_path
)) {
320 warn("symlink: %s", target
);
323 return (pflag
? setfile(p
->fts_statp
, 0) : 0);
327 copy_fifo(struct stat
*from_stat
, int exists
)
329 if (exists
&& unlink(to
.p_path
)) {
330 warn("unlink: %s", to
.p_path
);
333 if (mkfifo(to
.p_path
, from_stat
->st_mode
)) {
334 warn("mkfifo: %s", to
.p_path
);
337 return (pflag
? setfile(from_stat
, 0) : 0);
341 copy_special(struct stat
*from_stat
, int exists
)
343 if (exists
&& unlink(to
.p_path
)) {
344 warn("unlink: %s", to
.p_path
);
347 if (mknod(to
.p_path
, from_stat
->st_mode
, from_stat
->st_rdev
)) {
348 warn("mknod: %s", to
.p_path
);
351 return (pflag
? setfile(from_stat
, 0) : 0);
359 * Set the owner/group/permissions for the "to" file to the information
360 * in the stat structure. If fd is zero, also call set_utimes() to set
361 * the mod/access times. If fd is non-zero, the caller must do a utimes
362 * itself after close(fd).
365 setfile(struct stat
*fs
, int fd
)
367 int rval
/* MINIX:, islink*/;
370 #if !defined(__minix)
371 islink
= S_ISLNK(fs
->st_mode
);
372 #endif /* !defined(__minix) */
373 fs
->st_mode
&= S_ISUID
| S_ISGID
| S_IRWXU
| S_IRWXG
| S_IRWXO
;
376 * Changing the ownership probably won't succeed, unless we're root
377 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
378 * the mode; current BSD behavior is to remove all setuid bits on
379 * chown. If chown fails, lose setuid/setgid bits.
381 if (fd
? fchown(fd
, fs
->st_uid
, fs
->st_gid
) :
382 lchown(to
.p_path
, fs
->st_uid
, fs
->st_gid
)) {
383 if (errno
!= EPERM
) {
384 warn("chown: %s", to
.p_path
);
387 fs
->st_mode
&= ~(S_ISUID
| S_ISGID
);
389 if (fd
? fchmod(fd
, fs
->st_mode
) : lchmod(to
.p_path
, fs
->st_mode
)) {
390 warn("chmod: %s", to
.p_path
);
394 #if !defined(__minix)
395 if (!islink
&& !Nflag
) {
396 unsigned long fflags
= fs
->st_flags
;
399 * NFS doesn't support chflags; ignore errors unless
400 * there's reason to believe we're losing bits.
401 * (Note, this still won't be right if the server
402 * supports flags and we were trying to *remove* flags
403 * on a file that we copied, i.e., that we didn't create.)
407 if ((fd
? fchflags(fd
, fflags
) :
408 chflags(to
.p_path
, fflags
)) == -1)
409 if (errno
!= EOPNOTSUPP
|| fs
->st_flags
!= 0) {
410 warn("chflags: %s", to
.p_path
);
414 #endif /* !defined(__minix) */
416 /* if fd is non-zero, caller must call set_utimes() after close() */
417 if (fd
== 0 && set_utimes(to
.p_path
, fs
))
425 (void)fprintf(stderr
,
426 "usage: %s [-R [-H | -L | -P]] [-f | -i] [-alNpv] src target\n"
427 " %s [-R [-H | -L | -P]] [-f | -i] [-alNpv] src1 ... srcN directory\n",
428 getprogname(), getprogname());