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)
34 # ifndef EACCESS /* seems to be missing from the Mingw headers */
43 /*#define DEBUG_MSGS*/
45 # define DBUG(x) printf x
47 # define DBUG(x) ((void)0)
50 #define FSSEP '/' /* filename separator char */
52 static int copyFileRecursive(const char* src, const char* dst, bool isCmdLine, unsigned int options);
55 * Returns true if the source file is newer than the destination file.
57 * The check is based on the modification date, whole seconds only. This
58 * also returns true if the file sizes don't match.
60 static bool isSourceNewer(const struct stat* pSrcStat, const struct stat* pDstStat)
62 return (pSrcStat->st_mtime > pDstStat->st_mtime) ||
63 (pSrcStat->st_size != pDstStat->st_size);
67 * Returns true if the source file has high resolution modification
68 * date. Cygwin/Mingw doesn't support st_mtim and always returns false.
70 static bool isHiresMtime(const struct stat* pSrcStat)
72 #if defined(__CYGWIN__) || defined(__MINGW32__)
74 #elif defined(MACOSX_RSRC)
75 return pSrcStat->st_mtimespec.tv_nsec > 0;
77 return pSrcStat->st_mtim.tv_nsec > 0;
82 * Returns true if the source and destination files are actually the
83 * same thing. We detect this by checking the inode numbers, which seems
86 static bool isSameFile(const struct stat* pSrcStat, const struct stat* pDstStat)
88 #ifndef HAVE_VALID_STAT_ST_INO
89 /* with MSVCRT.DLL, stat always sets st_ino to 0, and there is no simple way to */
90 /* get the equivalent information with Win32 (Cygwin does some weird stuff in */
91 /* its winsup/cygwin/fhandler_disk_file.cc to emulate this, too complex for us) */
94 return (pSrcStat->st_ino == pDstStat->st_ino);
98 static void printCopyMsg(const char* src, const char* dst, unsigned int options)
100 if ((options & COPY_VERBOSE_MASK) > 0)
101 printf(" '%s' --> '%s'\n", src, dst);
104 static void printNotNewerMsg(const char* src, const char* dst, unsigned int options)
106 if ((options & COPY_VERBOSE_MASK) > 1)
107 printf(" '%s' is up-to-date\n", dst);
111 * Copy the contents of one file to another.
113 * The files are assumed to be seeked to the start.
115 static int copyFileContents(const char* dst, int dstFd, const char* src, int srcFd)
117 unsigned char buf[8192];
118 ssize_t readCount, writeCount;
121 * Read a chunk, write it, and repeat.
124 readCount = read(srcFd, buf, sizeof(buf));
127 "acp: failed reading '%s': %s\n", src, strerror(errno));
132 writeCount = write(dstFd, buf, readCount);
133 if (writeCount < 0) {
135 "acp: failed writing '%s': %s\n", dst, strerror(errno));
138 if (writeCount != readCount) {
139 fprintf(stderr, "acp: partial write to '%s' (%zd of %zd)\n",
140 dst, writeCount, readCount);
145 if (readCount < (ssize_t) sizeof(buf))
153 * Set the permissions, owner, and timestamps on the destination file
154 * equal to those of the source file.
156 * Failures here are "soft"; they don't produce warning messages and don't
157 * cause the cp command to report a failure.
159 static int setPermissions(const char* dst, const struct stat* pSrcStat, unsigned int options)
163 if (options & COPY_TIMESTAMPS) {
165 * Start with timestamps. The access and mod dates are not affected
166 * by the next operations.
168 ut.actime = pSrcStat->st_atime;
169 ut.modtime = pSrcStat->st_mtime;
170 if (isHiresMtime(pSrcStat))
172 if (utime(dst, &ut) != 0) {
173 DBUG(("--- unable to set timestamps on '%s': %s\n",
174 dst, strerror(errno)));
178 if (options & COPY_PERMISSIONS) {
180 * Set the permissions.
182 if (chmod(dst, pSrcStat->st_mode & ~(S_IFMT)) != 0) {
183 DBUG(("--- unable to set perms on '%s' to 0%o: %s\n",
184 dst, pSrcStat->st_mode & ~(S_IFMT), strerror(errno)));
186 #ifndef HAVE_MS_C_RUNTIME
190 if (chown(dst, pSrcStat->st_uid, pSrcStat->st_gid) != 0) {
191 DBUG(("--- unable to set owner of '%s' to %d/%d: %s\n",
192 dst, pSrcStat->st_uid, pSrcStat->st_gid, strerror(errno)));
201 * Copy a regular file. If the destination file exists and is not a
202 * regular file, we fail. However, we use stat() rather than lstat(),
203 * because it's okay to write through a symlink (the noDereference stuff
204 * only applies to the source file).
206 * If the file doesn't exist, create it. If it does exist, truncate it.
208 static int copyRegular(const char* src, const char* dst, const struct stat* pSrcStat, unsigned int options)
211 int srcFd, dstFd, statResult, copyResult;
213 DBUG(("--- copying regular '%s' to '%s'\n", src, dst));
215 statResult = stat(dst, &dstStat);
216 if (statResult == 0 && !S_ISREG(dstStat.st_mode)) {
218 "acp: destination '%s' exists and is not regular file\n",
221 } else if (statResult != 0 && errno != ENOENT) {
222 fprintf(stderr, "acp: unable to stat destination '%s'\n", dst);
226 if (statResult == 0) {
227 if (isSameFile(pSrcStat, &dstStat)) {
228 fprintf(stderr, "acp: '%s' and '%s' are the same file\n",
232 if (options & COPY_UPDATE_ONLY) {
233 if (!isSourceNewer(pSrcStat, &dstStat)) {
234 DBUG(("--- source is not newer: '%s'\n", src));
235 printNotNewerMsg(src, dst, options);
242 srcFd = open(src, O_RDONLY | O_BINARY, 0);
244 fprintf(stderr, "acp: unable to open '%s': %s\n", src, strerror(errno));
248 /* open dest with O_CREAT | O_TRUNC */
249 DBUG(("--- opening '%s'\n", dst));
250 dstFd = open(dst, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644);
253 if (errno == ENOENT) {
254 /* this happens if the target directory doesn't exist */
256 "acp: cannot create '%s': %s\n", dst, strerror(errno));
261 /* if "force" is set, try removing the destination file and retry */
262 if (options & COPY_FORCE) {
263 if (unlink(dst) != 0) {
264 #ifdef HAVE_MS_C_RUNTIME
265 /* MSVCRT.DLL unlink will fail with EACCESS if the file is set read-only */
266 /* so try to change its mode, and unlink again */
267 if (errno == EACCESS) {
268 if (chmod(dst, S_IWRITE|S_IREAD) == 0 && unlink(dst) == 0)
272 fprintf(stderr, "acp: unable to remove '%s': %s\n",
273 dst, strerror(errno));
277 #ifdef HAVE_MS_C_RUNTIME
280 dstFd = open(dst, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644);
284 fprintf(stderr, "acp: unable to open '%s': %s\n",
285 dst, strerror(errno));
290 copyResult = copyFileContents(dst, dstFd, src, srcFd);
299 char* srcRsrcName = NULL;
300 char* dstRsrcName = NULL;
301 struct stat rsrcStat;
303 srcRsrcName = malloc(strlen(src) + 5 + 1);
304 strcpy(srcRsrcName, src);
305 strcat(srcRsrcName, "/rsrc");
307 dstRsrcName = malloc(strlen(dst) + 5 + 1);
308 strcpy(dstRsrcName, dst);
309 strcat(dstRsrcName, "/rsrc");
311 if (stat(srcRsrcName, &rsrcStat) == 0 && rsrcStat.st_size > 0) {
312 DBUG(("--- RSRC: %s --> %s\n", srcRsrcName, dstRsrcName));
314 srcFd = open(srcRsrcName, O_RDONLY);
315 dstFd = open(dstRsrcName, O_TRUNC | O_WRONLY, 0);
317 if (srcFd >= 0 && dstFd >= 0) {
318 copyResult = copyFileContents(dstRsrcName, dstFd,
333 setPermissions(dst, pSrcStat, options);
335 printCopyMsg(src, dst, options);
342 * Copy a symlink. This only happens if we're in "no derefence" mode,
343 * in which we copy the links rather than the files that are pointed at.
345 * We always discard the destination file. If it's a symlink already,
346 * we want to throw it out and replace it. If it's not a symlink, we
347 * need to trash it so we can create one.
350 extern int copySymlink(const char* src, const char* dst, const struct stat* pSrcStat, unsigned int options) __attribute__((error("no symlinks on Windows")));
352 static int copySymlink(const char* src, const char* dst, const struct stat* pSrcStat, unsigned int options)
355 char linkBuf[PATH_MAX+1];
356 int statResult, nameLen;
358 assert(options & COPY_NO_DEREFERENCE);
359 DBUG(("--- copying symlink '%s' to '%s'\n", src, dst));
361 /* NOTE: we use lstat() here */
362 statResult = lstat(dst, &dstStat);
363 if (statResult == 0 && !S_ISREG(dstStat.st_mode)
364 && !S_ISLNK(dstStat.st_mode)
368 "acp: destination '%s' exists and is not regular or symlink\n",
373 if (statResult == 0) {
374 if (isSameFile(pSrcStat, &dstStat)) {
375 fprintf(stderr, "acp: '%s' and '%s' are the same file\n",
379 if (options & COPY_UPDATE_ONLY) {
380 if (!isSourceNewer(pSrcStat, &dstStat)) {
381 DBUG(("--- source is not newer: '%s'\n", src));
382 printNotNewerMsg(src, dst, options);
388 /* extract the symlink contents */
389 nameLen = readlink(src, linkBuf, sizeof(linkBuf)-1);
391 fprintf(stderr, "acp: unable to read symlink '%s': %s\n",
392 src, strerror(errno));
395 linkBuf[nameLen] = '\0';
396 DBUG(("--- creating symlink file '%s' (--> %s)\n", dst, linkBuf));
398 if (statResult == 0) {
399 DBUG(("--- removing '%s'\n", dst));
400 if (unlink(dst) != 0) {
401 fprintf(stderr, "acp: unable to remove '%s': %s\n",
402 dst, strerror(errno));
407 if (symlink(linkBuf, dst) != 0) {
408 fprintf(stderr, "acp: unable to create symlink '%s' [%s]: %s\n",
409 dst, linkBuf, strerror(errno));
414 * There's no way to set the file date or access permissions, but
415 * it is possible to set the owner.
417 if (options & COPY_PERMISSIONS) {
418 if (lchown(dst, pSrcStat->st_uid, pSrcStat->st_gid) != 0)
419 DBUG(("--- lchown failed: %s\n", strerror(errno)));
422 printCopyMsg(src, dst, options);
429 * Copy the contents of one directory to another. Both "src" and "dst"
430 * must be directories. We will create "dst" if it does not exist.
432 int copyDirectory(const char* src, const char* dst, const struct stat* pSrcStat, unsigned int options)
439 DBUG(("--- copy dir '%s' to '%s'\n", src, dst));
441 statResult = stat(dst, &dstStat);
442 if (statResult == 0 && !S_ISDIR(dstStat.st_mode)) {
444 "acp: destination '%s' exists and is not a directory\n", dst);
446 } else if (statResult != 0 && errno != ENOENT) {
447 fprintf(stderr, "acp: unable to stat destination '%s'\n", dst);
451 if (statResult == 0) {
452 if (isSameFile(pSrcStat, &dstStat)) {
454 "acp: cannot copy directory into itself ('%s' and '%s')\n",
459 DBUG(("--- creating dir '%s'\n", dst));
460 cc = mkdir(dst, 0755);
462 fprintf(stderr, "acp: unable to create directory '%s': %s\n",
463 dst, strerror(errno));
467 /* only print on mkdir */
468 printCopyMsg(src, dst, options);
472 * Open the directory, and plow through its contents.
476 fprintf(stderr, "acp: unable to open directory '%s': %s\n",
477 src, strerror(errno));
485 int srcLen, dstLen, nameLen;
491 if (strcmp(ent->d_name, ".") == 0 ||
492 strcmp(ent->d_name, "..") == 0)
497 nameLen = strlen(ent->d_name);
498 srcLen = strlen(src);
499 dstLen = strlen(dst);
501 srcFile = malloc(srcLen +1 + nameLen +1);
502 memcpy(srcFile, src, srcLen);
503 srcFile[srcLen] = FSSEP;
504 memcpy(srcFile + srcLen+1, ent->d_name, nameLen +1);
506 dstFile = malloc(dstLen +1 + nameLen +1);
507 memcpy(dstFile, dst, dstLen);
508 dstFile[dstLen] = FSSEP;
509 memcpy(dstFile + dstLen+1, ent->d_name, nameLen +1);
511 if (copyFileRecursive(srcFile, dstFile, false, options) != 0)
512 retVal = -1; /* note failure and keep going */
519 setPermissions(dst, pSrcStat, options);
525 * Do the actual copy. This is called recursively from copyDirectory().
527 * "dst" should only be a directory if "src" is also a directory.
529 * Returns 0 on success.
531 static int copyFileRecursive(const char* src, const char* dst, bool isCmdLine, unsigned int options)
538 int statResult, statErrno;
541 * Stat the source file. If it doesn't exist, fail.
543 if (options & COPY_NO_DEREFERENCE)
544 statResult = lstat(src, &srcStat);
546 statResult = stat(src, &srcStat);
547 statErrno = errno; /* preserve across .exe attempt */
551 * Here's the interesting part. Under Cygwin, if you have a file
552 * called "foo.exe", stat("foo", ...) will succeed, but open("foo", ...)
553 * will fail. We need to figure out what its name is supposed to be
554 * so we can create the correct destination file.
556 * If we don't have the "-e" flag set, we want "acp foo bar" to fail,
557 * not automatically find "foo.exe". That way, if we really were
558 * trying to copy "foo", it doesn't grab something we don't want.
560 if (isCmdLine && statResult == 0) {
562 tmpFd = open(src, O_RDONLY | O_BINARY, 0);
572 * If we didn't find the file, try it again with ".exe".
574 if (isCmdLine && statResult < 0 && statErrno == ENOENT && (options & COPY_TRY_EXE)) {
575 srcExe = malloc(strlen(src) + 4 +1);
577 strcat(srcExe, ".exe");
579 if (options & COPY_NO_DEREFERENCE)
580 statResult = lstat(srcExe, &srcStat);
582 statResult = stat(srcExe, &srcStat);
584 if (statResult == 0 && !S_ISREG(srcStat.st_mode))
585 statResult = -1; /* fail, use original statErrno below */
587 if (statResult == 0) {
588 /* found a .exe, copy that instead */
589 dstExe = malloc(strlen(dst) + 4 +1);
591 strcat(dstExe, ".exe");
596 DBUG(("--- couldn't find '%s' either\n", srcExe));
600 if (statResult < 0) {
601 if (statErrno == ENOENT)
602 fprintf(stderr, "acp: file '%s' does not exist\n", src);
604 fprintf(stderr, "acp: unable to stat '%s': %s\n",
605 src, strerror(statErrno));
611 * If "src" is a directory, ignore it if "recursive" isn't set.
613 * We want to create "dst" as a directory (or verify that it already
614 * exists as a directory), and then copy its contents.
616 if (S_ISDIR(srcStat.st_mode)) {
617 if (!(options & COPY_RECURSIVE)) {
618 fprintf(stderr, "acp: omitting directory '%s'\n", src);
620 retVal = copyDirectory(src, dst, &srcStat, options);
622 } else if (S_ISLNK(srcStat.st_mode)) {
623 retVal = copySymlink(src, dst, &srcStat, options);
624 } else if (S_ISREG(srcStat.st_mode)) {
625 retVal = copyRegular(src, dst, &srcStat, options);
627 fprintf(stderr, "acp: skipping unusual file '%s' (mode=0%o)\n",
628 src, srcStat.st_mode);
639 int copyFile(const char* src, const char* dst, unsigned int options)
641 return copyFileRecursive(src, dst, true, options);