2 * Copyright 2005 The Android Open Source Project
4 * Android "cp" replacement.
6 * The GNU/Linux "cp" uses O_LARGEFILE in its open() calls, utimes() instead
7 * of utime(), and getxattr()/setxattr() instead of chmod(). These are
8 * probably "better", but are non-portable, and not necessary for our
11 #include <host/CopyFile.h>
17 #include <sys/types.h>
27 #ifdef HAVE_MS_C_RUNTIME
28 # define mkdir(path,mode) _mkdir(path)
33 # ifndef EACCESS /* seems to be missing from the Mingw headers */
42 /*#define DEBUG_MSGS*/
44 # define DBUG(x) printf x
46 # define DBUG(x) ((void)0)
49 #define FSSEP '/' /* filename separator char */
51 static int copyFileRecursive(const char* src, const char* dst, bool isCmdLine, unsigned int options);
54 * Returns true if the source file is newer than the destination file.
56 * The check is based on the modification date, whole seconds only. This
57 * also returns true if the file sizes don't match.
59 static bool isSourceNewer(const struct stat* pSrcStat, const struct stat* pDstStat)
61 return (pSrcStat->st_mtime > pDstStat->st_mtime) ||
62 (pSrcStat->st_size != pDstStat->st_size);
66 * Returns true if the source file has high resolution modification
67 * date. Cygwin/Mingw doesn't support st_mtim and always returns false.
69 static bool isHiresMtime(const struct stat* pSrcStat)
72 #if defined(MACOSX_RSRC)
73 return pSrcStat->st_mtimespec.tv_nsec > 0;
75 return pSrcStat->st_mtim.tv_nsec > 0;
83 * Returns true if the source and destination files are actually the
84 * same thing. We detect this by checking the inode numbers, which seems
87 static bool isSameFile(const struct stat* pSrcStat, const struct stat* pDstStat)
89 #ifndef HAVE_VALID_STAT_ST_INO
90 /* with MSVCRT.DLL, stat always sets st_ino to 0, and there is no simple way to */
91 /* get the equivalent information with Win32 (Cygwin does some weird stuff in */
92 /* its winsup/cygwin/fhandler_disk_file.cc to emulate this, too complex for us) */
95 return (pSrcStat->st_ino == pDstStat->st_ino);
99 static void printCopyMsg(const char* src, const char* dst, unsigned int options)
101 if ((options & COPY_VERBOSE_MASK) > 0)
102 printf(" '%s' --> '%s'\n", src, dst);
105 static void printNotNewerMsg(const char* src, const char* dst, unsigned int options)
107 if ((options & COPY_VERBOSE_MASK) > 1)
108 printf(" '%s' is up-to-date\n", dst);
112 * Copy the contents of one file to another.
114 * The files are assumed to be seeked to the start.
116 static int copyFileContents(const char* dst, int dstFd, const char* src, int srcFd)
118 unsigned char buf[8192];
119 ssize_t readCount, writeCount;
122 * Read a chunk, write it, and repeat.
125 readCount = read(srcFd, buf, sizeof(buf));
128 "acp: failed reading '%s': %s\n", src, strerror(errno));
133 writeCount = write(dstFd, buf, readCount);
134 if (writeCount < 0) {
136 "acp: failed writing '%s': %s\n", dst, strerror(errno));
139 if (writeCount != readCount) {
140 fprintf(stderr, "acp: partial write to '%s' (%d of %d)\n",
141 dst, writeCount, readCount);
146 if (readCount < (ssize_t) sizeof(buf))
154 * Set the permissions, owner, and timestamps on the destination file
155 * equal to those of the source file.
157 * Failures here are "soft"; they don't produce warning messages and don't
158 * cause the cp command to report a failure.
160 static int setPermissions(const char* dst, const struct stat* pSrcStat, unsigned int options)
164 if (options & COPY_TIMESTAMPS) {
166 * Start with timestamps. The access and mod dates are not affected
167 * by the next operations.
169 ut.actime = pSrcStat->st_atime;
170 ut.modtime = pSrcStat->st_mtime;
171 if (isHiresMtime(pSrcStat))
173 if (utime(dst, &ut) != 0) {
174 DBUG(("--- unable to set timestamps on '%s': %s\n",
175 dst, strerror(errno)));
179 if (options & COPY_PERMISSIONS) {
181 * Set the permissions.
183 if (chmod(dst, pSrcStat->st_mode & ~(S_IFMT)) != 0) {
184 DBUG(("--- unable to set perms on '%s' to 0%o: %s\n",
185 dst, pSrcStat->st_mode & ~(S_IFMT), strerror(errno)));
187 #ifndef HAVE_MS_C_RUNTIME
191 if (chown(dst, pSrcStat->st_uid, pSrcStat->st_gid) != 0) {
192 DBUG(("--- unable to set owner of '%s' to %d/%d: %s\n",
193 dst, pSrcStat->st_uid, pSrcStat->st_gid, strerror(errno)));
202 * Copy a regular file. If the destination file exists and is not a
203 * regular file, we fail. However, we use stat() rather than lstat(),
204 * because it's okay to write through a symlink (the noDereference stuff
205 * only applies to the source file).
207 * If the file doesn't exist, create it. If it does exist, truncate it.
209 static int copyRegular(const char* src, const char* dst, const struct stat* pSrcStat, unsigned int options)
212 int srcFd, dstFd, statResult, copyResult;
214 DBUG(("--- copying regular '%s' to '%s'\n", src, dst));
216 statResult = stat(dst, &dstStat);
217 if (statResult == 0 && !S_ISREG(dstStat.st_mode)) {
219 "acp: destination '%s' exists and is not regular file\n",
222 } else if (statResult != 0 && errno != ENOENT) {
223 fprintf(stderr, "acp: unable to stat destination '%s'\n", dst);
227 if (statResult == 0) {
228 if (isSameFile(pSrcStat, &dstStat)) {
229 fprintf(stderr, "acp: '%s' and '%s' are the same file\n",
233 if (options & COPY_UPDATE_ONLY) {
234 if (!isSourceNewer(pSrcStat, &dstStat)) {
235 DBUG(("--- source is not newer: '%s'\n", src));
236 printNotNewerMsg(src, dst, options);
243 srcFd = open(src, O_RDONLY | O_BINARY, 0);
245 fprintf(stderr, "acp: unable to open '%s': %s\n", src, strerror(errno));
249 /* open dest with O_CREAT | O_TRUNC */
250 DBUG(("--- opening '%s'\n", dst));
251 dstFd = open(dst, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644);
254 if (errno == ENOENT) {
255 /* this happens if the target directory doesn't exist */
257 "acp: cannot create '%s': %s\n", dst, strerror(errno));
262 /* if "force" is set, try removing the destination file and retry */
263 if (options & COPY_FORCE) {
264 if (unlink(dst) != 0) {
265 #ifdef HAVE_MS_C_RUNTIME
266 /* MSVCRT.DLL unlink will fail with EACCESS if the file is set read-only */
267 /* so try to change its mode, and unlink again */
268 if (errno == EACCESS) {
269 if (chmod(dst, S_IWRITE|S_IREAD) == 0 && unlink(dst) == 0)
273 fprintf(stderr, "acp: unable to remove '%s': %s\n",
274 dst, strerror(errno));
278 #ifdef HAVE_MS_C_RUNTIME
281 dstFd = open(dst, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644);
285 fprintf(stderr, "acp: unable to open '%s': %s\n",
286 dst, strerror(errno));
291 copyResult = copyFileContents(dst, dstFd, src, srcFd);
300 char* srcRsrcName = NULL;
301 char* dstRsrcName = NULL;
302 struct stat rsrcStat;
304 srcRsrcName = malloc(strlen(src) + 5 + 1);
305 strcpy(srcRsrcName, src);
306 strcat(srcRsrcName, "/rsrc");
308 dstRsrcName = malloc(strlen(dst) + 5 + 1);
309 strcpy(dstRsrcName, dst);
310 strcat(dstRsrcName, "/rsrc");
312 if (stat(srcRsrcName, &rsrcStat) == 0 && rsrcStat.st_size > 0) {
313 DBUG(("--- RSRC: %s --> %s\n", srcRsrcName, dstRsrcName));
315 srcFd = open(srcRsrcName, O_RDONLY);
316 dstFd = open(dstRsrcName, O_TRUNC | O_WRONLY, 0);
318 if (srcFd >= 0 && dstFd >= 0) {
319 copyResult = copyFileContents(dstRsrcName, dstFd,
334 setPermissions(dst, pSrcStat, options);
336 printCopyMsg(src, dst, options);
344 * Copy a symlink. This only happens if we're in "no derefence" mode,
345 * in which we copy the links rather than the files that are pointed at.
347 * We always discard the destination file. If it's a symlink already,
348 * we want to throw it out and replace it. If it's not a symlink, we
349 * need to trash it so we can create one.
351 static int copySymlink(const char* src, const char* dst, const struct stat* pSrcStat, unsigned int options)
354 char linkBuf[PATH_MAX+1];
355 int statResult, nameLen;
357 assert(options & COPY_NO_DEREFERENCE);
358 DBUG(("--- copying symlink '%s' to '%s'\n", src, dst));
360 /* NOTE: we use lstat() here */
361 statResult = lstat(dst, &dstStat);
362 if (statResult == 0 && !S_ISREG(dstStat.st_mode)
363 && !S_ISLNK(dstStat.st_mode)
367 "acp: destination '%s' exists and is not regular or symlink\n",
372 if (statResult == 0) {
373 if (isSameFile(pSrcStat, &dstStat)) {
374 fprintf(stderr, "acp: '%s' and '%s' are the same file\n",
378 if (options & COPY_UPDATE_ONLY) {
379 if (!isSourceNewer(pSrcStat, &dstStat)) {
380 DBUG(("--- source is not newer: '%s'\n", src));
381 printNotNewerMsg(src, dst, options);
387 /* extract the symlink contents */
388 nameLen = readlink(src, linkBuf, sizeof(linkBuf)-1);
390 fprintf(stderr, "acp: unable to read symlink '%s': %s\n",
391 src, strerror(errno));
394 linkBuf[nameLen] = '\0';
395 DBUG(("--- creating symlink file '%s' (--> %s)\n", dst, linkBuf));
397 if (statResult == 0) {
398 DBUG(("--- removing '%s'\n", dst));
399 if (unlink(dst) != 0) {
400 fprintf(stderr, "acp: unable to remove '%s': %s\n",
401 dst, strerror(errno));
406 if (symlink(linkBuf, dst) != 0) {
407 fprintf(stderr, "acp: unable to create symlink '%s' [%s]: %s\n",
408 dst, linkBuf, strerror(errno));
413 * There's no way to set the file date or access permissions, but
414 * it is possible to set the owner.
416 if (options & COPY_PERMISSIONS) {
417 if (lchown(dst, pSrcStat->st_uid, pSrcStat->st_gid) != 0)
418 DBUG(("--- lchown failed: %s\n", strerror(errno)));
421 printCopyMsg(src, dst, options);
425 #endif /* HAVE_SYMLINKS */
428 * Copy the contents of one directory to another. Both "src" and "dst"
429 * must be directories. We will create "dst" if it does not exist.
431 int copyDirectory(const char* src, const char* dst, const struct stat* pSrcStat, unsigned int options)
438 DBUG(("--- copy dir '%s' to '%s'\n", src, dst));
440 statResult = stat(dst, &dstStat);
441 if (statResult == 0 && !S_ISDIR(dstStat.st_mode)) {
443 "acp: destination '%s' exists and is not a directory\n", dst);
445 } else if (statResult != 0 && errno != ENOENT) {
446 fprintf(stderr, "acp: unable to stat destination '%s'\n", dst);
450 if (statResult == 0) {
451 if (isSameFile(pSrcStat, &dstStat)) {
453 "acp: cannot copy directory into itself ('%s' and '%s')\n",
458 DBUG(("--- creating dir '%s'\n", dst));
459 cc = mkdir(dst, 0755);
461 fprintf(stderr, "acp: unable to create directory '%s': %s\n",
462 dst, strerror(errno));
466 /* only print on mkdir */
467 printCopyMsg(src, dst, options);
471 * Open the directory, and plow through its contents.
475 fprintf(stderr, "acp: unable to open directory '%s': %s\n",
476 src, strerror(errno));
484 int srcLen, dstLen, nameLen;
490 if (strcmp(ent->d_name, ".") == 0 ||
491 strcmp(ent->d_name, "..") == 0)
496 nameLen = strlen(ent->d_name);
497 srcLen = strlen(src);
498 dstLen = strlen(dst);
500 srcFile = malloc(srcLen +1 + nameLen +1);
501 memcpy(srcFile, src, srcLen);
502 srcFile[srcLen] = FSSEP;
503 memcpy(srcFile + srcLen+1, ent->d_name, nameLen +1);
505 dstFile = malloc(dstLen +1 + nameLen +1);
506 memcpy(dstFile, dst, dstLen);
507 dstFile[dstLen] = FSSEP;
508 memcpy(dstFile + dstLen+1, ent->d_name, nameLen +1);
510 if (copyFileRecursive(srcFile, dstFile, false, options) != 0)
511 retVal = -1; /* note failure and keep going */
518 setPermissions(dst, pSrcStat, options);
524 * Do the actual copy. This is called recursively from copyDirectory().
526 * "dst" should only be a directory if "src" is also a directory.
528 * Returns 0 on success.
530 static int copyFileRecursive(const char* src, const char* dst, bool isCmdLine, unsigned int options)
537 int statResult, statErrno;
540 * Stat the source file. If it doesn't exist, fail.
542 if (options & COPY_NO_DEREFERENCE)
543 statResult = lstat(src, &srcStat);
545 statResult = stat(src, &srcStat);
546 statErrno = errno; /* preserve across .exe attempt */
550 * Here's the interesting part. Under Cygwin, if you have a file
551 * called "foo.exe", stat("foo", ...) will succeed, but open("foo", ...)
552 * will fail. We need to figure out what its name is supposed to be
553 * so we can create the correct destination file.
555 * If we don't have the "-e" flag set, we want "acp foo bar" to fail,
556 * not automatically find "foo.exe". That way, if we really were
557 * trying to copy "foo", it doesn't grab something we don't want.
559 if (isCmdLine && statResult == 0) {
561 tmpFd = open(src, O_RDONLY | O_BINARY, 0);
571 * If we didn't find the file, try it again with ".exe".
573 if (isCmdLine && statResult < 0 && statErrno == ENOENT && (options & COPY_TRY_EXE)) {
574 srcExe = malloc(strlen(src) + 4 +1);
576 strcat(srcExe, ".exe");
578 if (options & COPY_NO_DEREFERENCE)
579 statResult = lstat(srcExe, &srcStat);
581 statResult = stat(srcExe, &srcStat);
583 if (statResult == 0 && !S_ISREG(srcStat.st_mode))
584 statResult = -1; /* fail, use original statErrno below */
586 if (statResult == 0) {
587 /* found a .exe, copy that instead */
588 dstExe = malloc(strlen(dst) + 4 +1);
590 strcat(dstExe, ".exe");
595 DBUG(("--- couldn't find '%s' either\n", srcExe));
599 if (statResult < 0) {
600 if (statErrno == ENOENT)
601 fprintf(stderr, "acp: file '%s' does not exist\n", src);
603 fprintf(stderr, "acp: unable to stat '%s': %s\n",
604 src, strerror(statErrno));
610 * If "src" is a directory, ignore it if "recursive" isn't set.
612 * We want to create "dst" as a directory (or verify that it already
613 * exists as a directory), and then copy its contents.
615 if (S_ISDIR(srcStat.st_mode)) {
616 if (!(options & COPY_RECURSIVE)) {
617 fprintf(stderr, "acp: omitting directory '%s'\n", src);
619 retVal = copyDirectory(src, dst, &srcStat, options);
622 } else if (S_ISLNK(srcStat.st_mode)) {
623 retVal = copySymlink(src, dst, &srcStat, options);
625 } else if (S_ISREG(srcStat.st_mode)) {
626 retVal = copyRegular(src, dst, &srcStat, options);
628 fprintf(stderr, "acp: skipping unusual file '%s' (mode=0%o)\n",
629 src, srcStat.st_mode);
640 int copyFile(const char* src, const char* dst, unsigned int options)
642 return copyFileRecursive(src, dst, true, options);