source: MondoRescue/branches/stable/mindi-busybox/libbb/copy_file.c

Last change on this file was 1770, checked in by Bruno Cornec, 16 years ago
  • Better output for mindi-busybox revision
  • Remove dummy file created on NFS - report from Arnaud Tiger <arnaud.tiger_at_hp.com>
  • strace useful for debug
  • fix new versions for pb (2.0.0 for mindi and 1.7.2 for mindi-busybox)
  • fix build process for mindi-busybox + options used in that version (dd for label-partitions-as-necessary)
  • fix typo in label-partitions-as-necessary which doesn't seem to work
  • Update to busybox 1.7.2
  • perl is now required at restore time to support uuid swap partitions (and will be used for many other thigs

in the future for sure)

  • next mindi version will be 2.0.0 due to all the changes made in it (udev may break working distros)
  • small optimization in mindi on keyboard handling (one single find instead of multiple)
  • better interaction for USB device when launching mindi manually
  • attempt to automatically guess block disk size for ramdisk
  • fix typos in bkphw
  • Fix the remaining problem with UUID support for swap partitions
  • Updates mondoarchive man page for USB support
  • Adds preliminary Hardware support to mindi (Proliant SSSTK)
  • Tries to add udev support also for rhel4
  • Fix UUID support which was still broken.
  • Be conservative in test for the start-nfs script
  • Update config file for mindi-busybox for 1.7.2 migration
  • Try to run around a busybox bug (1.2.2 pb on inexistant links)
  • Add build content for mindi-busybox in pb
  • Remove distributions content for mindi-busybox
  • Fix a warning on inexistant raidtab
  • Solve problem on tmpfs in restore init (Problem of inexistant symlink and busybox)
  • Create MONDO_CACHE and use it everywhere + creation at start
  • Really never try to eject a USB device
  • Fix a issue with &> usage (replaced with 1> and 2>)
  • Adds magic file to depllist in order to have file working + ldd which helps for debugging issues
  • tty modes correct to avoid sh error messages
  • Use ext3 normally and not ext2 instead
  • USB device should be corrected after reading (take 1st part)
  • Adds a mount_USB_here function derived from mount_CDROM_here
  • usb detection place before /dev detection in device name at restore time
  • Fix when restoring from USB: media is asked in interactive mode
  • Adds USB support for mondorestore
  • mount_cdrom => mount_media
  • elilo.efi is now searched throughout /boot/efi and not in a fixed place as there is no standard
  • untar-and-softlink => untar (+ interface change)
  • suppress useless softlinks creation/removal in boot process
  • avoids udevd messages on groups
  • Increase # of disks to 99 as in mindi at restore time (should be a conf file parameter)
  • skip existing big file creation
  • seems to work correctly for USB mindi boot
  • Adds group and tty link to udev conf
  • Always load usb-torage (even 2.6) to initiate USB bus discovery
  • Better printing of messages
  • Attempt to fix a bug in supporting OpenSusE 10.3 kernel for initramfs (mindi may now use multiple regex for kernel initrd detection)
  • Links were not correctly done as non relative for modules in mindi
  • exclusion of modules denied now works
  • Also create modules in their ordinary place, so that classical modprobe works + copy modules.dep
  • Fix bugs for DENY_MODS handling
  • Add device /dev/console for udev
  • ide-generic should now really be excluded
  • Fix a bug in major number for tty
  • If udev then adds modprobe/insmod to rootfs
  • tty0 is also cretaed with udev
  • ide-generic put rather in DENY_MODS
  • udevd remove from deplist s handled in mindi directly
  • better default for mindi when using --usb
  • Handles dynamically linked busybox (in case we want to use it soon ;-)
  • Adds fixed devices to create for udev
  • ide-generic should not be part of the initrd when using libata v2
  • support a dynamically linked udev (case on Ubuntu 7.10 and Mandriva 2008.0 so should be quite generic) This will give incitation to move to dyn. linked binaries in the initrd which will help for other tasks (ia6 4)
  • Improvement in udev support (do not use cl options not available in busybox)
  • Udev in mindi
    • auto creation of the right links at boot time with udev-links.conf(from Mandriva 2008.0)
    • rework startup of udev as current makes kernel crash (from Mandriva 2008.0)
    • add support for 64 bits udev
  • Try to render MyInsmod silent at boot time
  • Adds udev support (mandatory for newest distributions to avoid remapping of devices in a different way as on the original system)
  • We also need vaft format support for USB boot
  • Adds libusual support (Ubuntu 7.10 needs it for USB)
  • Improve Ubuntu/Debian keyboard detection and support
  • pbinit adapted to new pb (0.8.10). Filtering of docs done in it
  • Suppress some mondo warnings and errors on USB again
  • Tries to fix lack of files in deb mindi package
  • Verify should now work for USB devices
  • More log/mesages improvement for USB support
  • - Supress g_erase_tmpdir_and_scratchdir
  • Improve some log messages for USB support
  • Try to improve install in mindi to avoid issues with isolinux.cfg not installed vene if in the pkg :-(
  • Improve mindi-busybox build
  • In conformity with pb 0.8.9
  • Add support for Ubuntu 7.10 in build process
  • Add USB Key button to Menu UI (CD streamer removed)
  • Attempt to fix error messages on tmp/scratch files at the end by removing those dir at the latest possible.
  • Fix a bug linked to the size of the -E param which could be used (Arnaud Tiger/René Ribaud).
  • Integrate ~/.pbrc content into mondorescue.pb (required project-builder >= 0.8.7)
  • Put mondorescue in conformity with new pb filtering rules
  • Add USB support at restore time (no test done yet). New start-usb script PB varibale added where useful
  • Unmounting USB device before removal of temporary scratchdir
  • Stil refining USB copy back to mondo (one command was not executed)
  • No need to have the image subdor in the csratchdir when USB.
  • umount the USB partition before attempting to use it
  • Remove useless copy from mindi to mondo at end of USB handling

(risky merge, we are raising the limits of 2 diverging branches. The status of stable is not completely sure as such. Will need lots of tests, but it's not yet done :-()
(merge -r1692:1769 $SVN_M/branches/2.2.5)

File size: 10.5 KB
RevLine 
[821]1/* vi: set sw=4 ts=4: */
2/*
3 * Mini copy_file implementation for busybox
4 *
5 * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
[1770]6 * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
[821]7 *
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9 *
10 */
11
12#include "libbb.h"
13
[1770]14// POSIX: if exists and -i, ask (w/o -i assume yes).
15// Then open w/o EXCL (yes, not unlink!).
16// If open still fails and -f, try unlink, then try open again.
17// Result: a mess:
18// If dest is a softlink, we overwrite softlink's destination!
19// (or fail, if it points to dir/nonexistent location/etc).
20// This is strange, but POSIX-correct.
21// coreutils cp has --remove-destination to override this...
22//
23// NB: we have special code which still allows for "cp file /dev/node"
24// to work POSIX-ly (the only realistic case where it makes sense)
25
26#define DO_POSIX_CP 0 /* 1 - POSIX behavior, 0 - safe behavior */
27
28// errno must be set to relevant value ("why we cannot create dest?")
29// for POSIX mode to give reasonable error message
30static int ask_and_unlink(const char *dest, int flags)
31{
32#if DO_POSIX_CP
33 if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) {
34 // Either it exists, or the *path* doesnt exist
35 bb_perror_msg("cannot create '%s'", dest);
36 return -1;
37 }
38#endif
39 // If !DO_POSIX_CP, act as if -f is always in effect - we don't want
40 // "cannot create" msg, we want unlink to be done (silently unless -i).
41
42 // TODO: maybe we should do it only if ctty is present?
43 if (flags & FILEUTILS_INTERACTIVE) {
44 // We would not do POSIX insanity. -i asks,
45 // then _unlinks_ the offender. Presto.
46 // (No "opening without O_EXCL", no "unlink only if -f")
47 // Or else we will end up having 3 open()s!
48 fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest);
49 if (!bb_ask_confirmation())
50 return 0; // not allowed to overwrite
51 }
52 if (unlink(dest) < 0) {
53 bb_perror_msg("cannot remove '%s'", dest);
54 return -1; // error
55 }
56 return 1; // ok (to try again)
57}
58
59/* Return:
60 * -1 error, copy not made
61 * 0 copy is made or user answered "no" in interactive mode
62 * (failures to preserve mode/owner/times are not reported in exit code)
63 */
[821]64int copy_file(const char *source, const char *dest, int flags)
65{
[1770]66 /* This is a recursive function, try to minimize stack usage */
67 /* NB: each struct stat is ~100 bytes */
[821]68 struct stat source_stat;
69 struct stat dest_stat;
[1770]70 signed char retval = 0;
71 signed char dest_exists = 0;
72 signed char ovr;
[821]73
[1770]74#define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE)
75
76 if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
77 // This may be a dangling symlink.
78 // Making [sym]links to dangling symlinks works, so...
79 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK))
80 goto make_links;
81 bb_perror_msg("cannot stat '%s'", source);
[821]82 return -1;
83 }
84
85 if (lstat(dest, &dest_stat) < 0) {
86 if (errno != ENOENT) {
[1770]87 bb_perror_msg("cannot stat '%s'", dest);
[821]88 return -1;
89 }
90 } else {
[1770]91 if (source_stat.st_dev == dest_stat.st_dev
92 && source_stat.st_ino == dest_stat.st_ino
93 ) {
94 bb_error_msg("'%s' and '%s' are the same file", source, dest);
[821]95 return -1;
96 }
97 dest_exists = 1;
98 }
99
[1770]100#if ENABLE_SELINUX
101 if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0) {
102 security_context_t con;
103 if (lgetfilecon(source, &con) >= 0) {
104 if (setfscreatecon(con) < 0) {
105 bb_perror_msg("cannot set setfscreatecon %s", con);
106 freecon(con);
107 return -1;
108 }
109 } else if (errno == ENOTSUP || errno == ENODATA) {
110 setfscreatecon_or_die(NULL);
111 } else {
112 bb_perror_msg("cannot lgetfilecon %s", source);
113 return -1;
114 }
115 }
116#endif
117
[821]118 if (S_ISDIR(source_stat.st_mode)) {
119 DIR *dp;
[1770]120 const char *tp;
[821]121 struct dirent *d;
122 mode_t saved_umask = 0;
123
124 if (!(flags & FILEUTILS_RECUR)) {
[1770]125 bb_error_msg("omitting directory '%s'", source);
[821]126 return -1;
127 }
128
[1770]129 /* Did we ever create source ourself before? */
130 tp = is_in_ino_dev_hashtable(&source_stat);
131 if (tp) {
132 /* We did! it's a recursion! man the lifeboats... */
133 bb_error_msg("recursion detected, omitting directory '%s'",
134 source);
135 return -1;
136 }
137
138 /* Create DEST */
[821]139 if (dest_exists) {
140 if (!S_ISDIR(dest_stat.st_mode)) {
[1770]141 bb_error_msg("target '%s' is not a directory", dest);
[821]142 return -1;
143 }
[1770]144 /* race here: user can substitute a symlink between
145 * this check and actual creation of files inside dest */
[821]146 } else {
147 mode_t mode;
148 saved_umask = umask(0);
149
150 mode = source_stat.st_mode;
151 if (!(flags & FILEUTILS_PRESERVE_STATUS))
152 mode = source_stat.st_mode & ~saved_umask;
[1770]153 /* Allow owner to access new dir (at least for now) */
[821]154 mode |= S_IRWXU;
155 if (mkdir(dest, mode) < 0) {
156 umask(saved_umask);
[1770]157 bb_perror_msg("cannot create directory '%s'", dest);
[821]158 return -1;
159 }
160 umask(saved_umask);
[1770]161 /* need stat info for add_to_ino_dev_hashtable */
162 if (lstat(dest, &dest_stat) < 0) {
163 bb_perror_msg("cannot stat '%s'", dest);
164 return -1;
165 }
[821]166 }
[1770]167 /* remember (dev,inode) of each created dir.
168 * NULL: name is not remembered */
169 add_to_ino_dev_hashtable(&dest_stat, NULL);
[821]170
[1770]171 /* Recursively copy files in SOURCE */
172 dp = opendir(source);
173 if (dp == NULL) {
174 retval = -1;
175 goto preserve_mode_ugid_time;
[821]176 }
177
178 while ((d = readdir(dp)) != NULL) {
179 char *new_source, *new_dest;
180
181 new_source = concat_subpath_file(source, d->d_name);
[1770]182 if (new_source == NULL)
[821]183 continue;
184 new_dest = concat_path_file(dest, d->d_name);
185 if (copy_file(new_source, new_dest, flags) < 0)
[1770]186 retval = -1;
[821]187 free(new_source);
188 free(new_dest);
189 }
190 closedir(dp);
191
[1770]192 if (!dest_exists
193 && chmod(dest, source_stat.st_mode & ~saved_umask) < 0
194 ) {
195 bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);
196 /* retval = -1; - WRONG! copy *WAS* made */
[821]197 }
[1770]198 goto preserve_mode_ugid_time;
199 }
200
201 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) {
202 int (*lf)(const char *oldpath, const char *newpath);
203 make_links:
204 // Hmm... maybe
205 // if (DEREF && MAKE_SOFTLINK) source = realpath(source) ?
206 // (but realpath returns NULL on dangling symlinks...)
207 lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link;
208 if (lf(source, dest) < 0) {
209 ovr = ask_and_unlink(dest, flags);
210 if (ovr <= 0)
211 return ovr;
212 if (lf(source, dest) < 0) {
213 bb_perror_msg("cannot create link '%s'", dest);
214 return -1;
215 }
216 }
217 /* _Not_ jumping to preserve_mode_ugid_time:
218 * hard/softlinks don't have those */
219 return 0;
220 }
221
222 if (S_ISREG(source_stat.st_mode)
223 /* DEREF uses stat, which never returns S_ISLNK() == true. */
224 /* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */
225 ) {
[821]226 int src_fd;
227 int dst_fd;
228
[1770]229 if (ENABLE_FEATURE_PRESERVE_HARDLINKS && !FLAGS_DEREF) {
230 const char *link_target;
231 link_target = is_in_ino_dev_hashtable(&source_stat);
232 if (link_target) {
233 if (link(link_target, dest) < 0) {
234 ovr = ask_and_unlink(dest, flags);
235 if (ovr <= 0)
236 return ovr;
237 if (link(link_target, dest) < 0) {
238 bb_perror_msg("cannot create link '%s'", dest);
239 return -1;
240 }
[821]241 }
242 return 0;
243 }
244 add_to_ino_dev_hashtable(&source_stat, dest);
245 }
246
[1770]247 src_fd = open_or_warn(source, O_RDONLY);
248 if (src_fd < 0)
249 return -1;
[821]250
[1770]251 /* POSIX way is a security problem versus symlink attacks,
252 * we do it only for non-symlinks, and only for non-recursive,
253 * non-interactive cp. NB: it is still racy
254 * for "cp file /home/bad_user/file" case
255 * (user can rm file and create a link to /etc/passwd) */
256 if (DO_POSIX_CP
257 || (dest_exists && !(flags & (FILEUTILS_RECUR|FILEUTILS_INTERACTIVE))
258 && !S_ISLNK(dest_stat.st_mode))
259 ) {
260 dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
261 } else /* safe way: */
262 dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
263 if (dst_fd == -1) {
264 ovr = ask_and_unlink(dest, flags);
265 if (ovr <= 0) {
266 close(src_fd);
267 return ovr;
[821]268 }
[1770]269 /* It shouldn't exist. If it exists, do not open (symlink attack?) */
270 dst_fd = open3_or_warn(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
271 if (dst_fd < 0) {
[821]272 close(src_fd);
[1770]273 return -1;
[821]274 }
275 }
276
[1770]277#if ENABLE_SELINUX
278 if (((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT)
279 || (flags & FILEUTILS_SET_SECURITY_CONTEXT))
280 && is_selinux_enabled() > 0
281 ) {
282 security_context_t con;
283 if (getfscreatecon(&con) == -1) {
284 bb_perror_msg("getfscreatecon");
[821]285 return -1;
286 }
[1770]287 if (con) {
288 if (setfilecon(dest, con) == -1) {
289 bb_perror_msg("setfilecon:%s,%s", dest, con);
290 freecon(con);
291 return -1;
292 }
293 freecon(con);
[821]294 }
295 }
[1770]296#endif
297 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
298 retval = -1;
299 /* Ok, writing side I can understand... */
300 if (close(dst_fd) < 0) {
301 bb_perror_msg("cannot close '%s'", dest);
302 retval = -1;
303 }
304 /* ...but read size is already checked by bb_copyfd_eof */
305 close(src_fd);
306 goto preserve_mode_ugid_time;
307 }
[821]308
[1770]309 /* Source is a symlink or a special file */
310 /* We are lazy here, a bit lax with races... */
311 if (dest_exists) {
312 errno = EEXIST;
313 ovr = ask_and_unlink(dest, flags);
314 if (ovr <= 0)
315 return ovr;
316 }
317 if (S_ISLNK(source_stat.st_mode)) {
318 char *lpath = xmalloc_readlink_or_warn(source);
319 if (lpath) {
320 int r = symlink(lpath, dest);
321 free(lpath);
322 if (r < 0) {
323 bb_perror_msg("cannot create symlink '%s'", dest);
[821]324 return -1;
325 }
326 if (flags & FILEUTILS_PRESERVE_STATUS)
327 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
[1770]328 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
[821]329 }
[1770]330 /* _Not_ jumping to preserve_mode_ugid_time:
331 * symlinks don't have those */
332 return 0;
333 }
334 if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode)
335 || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode)
336 ) {
337 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
338 bb_perror_msg("cannot create '%s'", dest);
339 return -1;
340 }
[821]341 } else {
[1770]342 bb_error_msg("unrecognized file '%s' with mode %x", source, source_stat.st_mode);
[821]343 return -1;
344 }
345
[1770]346 preserve_mode_ugid_time:
[821]347
[1770]348 if (flags & FILEUTILS_PRESERVE_STATUS
349 /* Cannot happen: */
350 /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */
351 ) {
[821]352 struct utimbuf times;
353
354 times.actime = source_stat.st_atime;
355 times.modtime = source_stat.st_mtime;
[1770]356 /* BTW, utimes sets usec-precision time - just FYI */
[821]357 if (utime(dest, &times) < 0)
[1770]358 bb_perror_msg("cannot preserve %s of '%s'", "times", dest);
[821]359 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
360 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
[1770]361 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
[821]362 }
363 if (chmod(dest, source_stat.st_mode) < 0)
[1770]364 bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);
[821]365 }
366
[1770]367 return retval;
[821]368}
Note: See TracBrowser for help on using the repository browser.