xref: /aosp_15_r20/build/make/tools/libhost/CopyFile.c (revision 9e94795a3d4ef5c1d47486f9a02bb378756cea8a)
1*9e94795aSAndroid Build Coastguard Worker /*
2*9e94795aSAndroid Build Coastguard Worker  * Copyright 2005 The Android Open Source Project
3*9e94795aSAndroid Build Coastguard Worker  *
4*9e94795aSAndroid Build Coastguard Worker  * Android "cp" replacement.
5*9e94795aSAndroid Build Coastguard Worker  *
6*9e94795aSAndroid Build Coastguard Worker  * The GNU/Linux "cp" uses O_LARGEFILE in its open() calls, utimes() instead
7*9e94795aSAndroid Build Coastguard Worker  * of utime(), and getxattr()/setxattr() instead of chmod().  These are
8*9e94795aSAndroid Build Coastguard Worker  * probably "better", but are non-portable, and not necessary for our
9*9e94795aSAndroid Build Coastguard Worker  * purposes.
10*9e94795aSAndroid Build Coastguard Worker  */
11*9e94795aSAndroid Build Coastguard Worker #include <host/CopyFile.h>
12*9e94795aSAndroid Build Coastguard Worker 
13*9e94795aSAndroid Build Coastguard Worker #include <stdlib.h>
14*9e94795aSAndroid Build Coastguard Worker #include <stdio.h>
15*9e94795aSAndroid Build Coastguard Worker #include <string.h>
16*9e94795aSAndroid Build Coastguard Worker #include <unistd.h>
17*9e94795aSAndroid Build Coastguard Worker #include <sys/types.h>
18*9e94795aSAndroid Build Coastguard Worker #include <sys/stat.h>
19*9e94795aSAndroid Build Coastguard Worker #include <getopt.h>
20*9e94795aSAndroid Build Coastguard Worker #include <dirent.h>
21*9e94795aSAndroid Build Coastguard Worker #include <fcntl.h>
22*9e94795aSAndroid Build Coastguard Worker #include <utime.h>
23*9e94795aSAndroid Build Coastguard Worker #include <limits.h>
24*9e94795aSAndroid Build Coastguard Worker #include <errno.h>
25*9e94795aSAndroid Build Coastguard Worker #include <assert.h>
26*9e94795aSAndroid Build Coastguard Worker 
27*9e94795aSAndroid Build Coastguard Worker #if defined(_WIN32)
28*9e94795aSAndroid Build Coastguard Worker #include <direct.h>  /* For _mkdir() */
29*9e94795aSAndroid Build Coastguard Worker #  define mkdir(path,mode)   _mkdir(path)
30*9e94795aSAndroid Build Coastguard Worker #  define S_ISLNK(s) 0
31*9e94795aSAndroid Build Coastguard Worker #  define lstat stat
32*9e94795aSAndroid Build Coastguard Worker #  ifndef EACCESS   /* seems to be missing from the Mingw headers */
33*9e94795aSAndroid Build Coastguard Worker #    define  EACCESS            13
34*9e94795aSAndroid Build Coastguard Worker #  endif
35*9e94795aSAndroid Build Coastguard Worker #endif
36*9e94795aSAndroid Build Coastguard Worker 
37*9e94795aSAndroid Build Coastguard Worker #ifndef O_BINARY
38*9e94795aSAndroid Build Coastguard Worker #  define  O_BINARY  0
39*9e94795aSAndroid Build Coastguard Worker #endif
40*9e94795aSAndroid Build Coastguard Worker 
41*9e94795aSAndroid Build Coastguard Worker /*#define DEBUG_MSGS*/
42*9e94795aSAndroid Build Coastguard Worker #ifdef DEBUG_MSGS
43*9e94795aSAndroid Build Coastguard Worker # define DBUG(x) printf x
44*9e94795aSAndroid Build Coastguard Worker #else
45*9e94795aSAndroid Build Coastguard Worker # define DBUG(x) ((void)0)
46*9e94795aSAndroid Build Coastguard Worker #endif
47*9e94795aSAndroid Build Coastguard Worker 
48*9e94795aSAndroid Build Coastguard Worker #define FSSEP '/'       /* filename separator char */
49*9e94795aSAndroid Build Coastguard Worker 
50*9e94795aSAndroid Build Coastguard Worker static int copyFileRecursive(const char* src, const char* dst, bool isCmdLine, unsigned int options);
51*9e94795aSAndroid Build Coastguard Worker 
52*9e94795aSAndroid Build Coastguard Worker /*
53*9e94795aSAndroid Build Coastguard Worker  * Returns true if the source file is newer than the destination file.
54*9e94795aSAndroid Build Coastguard Worker  *
55*9e94795aSAndroid Build Coastguard Worker  * The check is based on the modification date, whole seconds only.  This
56*9e94795aSAndroid Build Coastguard Worker  * also returns true if the file sizes don't match.
57*9e94795aSAndroid Build Coastguard Worker  */
isSourceNewer(const struct stat * pSrcStat,const struct stat * pDstStat)58*9e94795aSAndroid Build Coastguard Worker static bool isSourceNewer(const struct stat* pSrcStat, const struct stat* pDstStat)
59*9e94795aSAndroid Build Coastguard Worker {
60*9e94795aSAndroid Build Coastguard Worker     return (pSrcStat->st_mtime > pDstStat->st_mtime) ||
61*9e94795aSAndroid Build Coastguard Worker            (pSrcStat->st_size != pDstStat->st_size);
62*9e94795aSAndroid Build Coastguard Worker }
63*9e94795aSAndroid Build Coastguard Worker 
64*9e94795aSAndroid Build Coastguard Worker /*
65*9e94795aSAndroid Build Coastguard Worker  * Returns true if the source file has high resolution modification
66*9e94795aSAndroid Build Coastguard Worker  * date. Cygwin/Mingw doesn't support st_mtim and always returns false.
67*9e94795aSAndroid Build Coastguard Worker  */
isHiresMtime(const struct stat * pSrcStat)68*9e94795aSAndroid Build Coastguard Worker static bool isHiresMtime(const struct stat* pSrcStat)
69*9e94795aSAndroid Build Coastguard Worker {
70*9e94795aSAndroid Build Coastguard Worker #if defined(_WIN32)
71*9e94795aSAndroid Build Coastguard Worker     return 0;
72*9e94795aSAndroid Build Coastguard Worker #elif defined(__APPLE__)
73*9e94795aSAndroid Build Coastguard Worker     return pSrcStat->st_mtimespec.tv_nsec > 0;
74*9e94795aSAndroid Build Coastguard Worker #else
75*9e94795aSAndroid Build Coastguard Worker     return pSrcStat->st_mtim.tv_nsec > 0;
76*9e94795aSAndroid Build Coastguard Worker #endif
77*9e94795aSAndroid Build Coastguard Worker }
78*9e94795aSAndroid Build Coastguard Worker 
79*9e94795aSAndroid Build Coastguard Worker /*
80*9e94795aSAndroid Build Coastguard Worker  * Returns true if the source and destination files are actually the
81*9e94795aSAndroid Build Coastguard Worker  * same thing.  We detect this by checking the inode numbers, which seems
82*9e94795aSAndroid Build Coastguard Worker  * to work on Cygwin.
83*9e94795aSAndroid Build Coastguard Worker  */
isSameFile(const struct stat * pSrcStat,const struct stat * pDstStat)84*9e94795aSAndroid Build Coastguard Worker static bool isSameFile(const struct stat* pSrcStat, const struct stat* pDstStat)
85*9e94795aSAndroid Build Coastguard Worker {
86*9e94795aSAndroid Build Coastguard Worker #if defined(_WIN32)
87*9e94795aSAndroid Build Coastguard Worker   (void)pSrcStat;
88*9e94795aSAndroid Build Coastguard Worker   (void)pDstStat;
89*9e94795aSAndroid Build Coastguard Worker     /* with MSVCRT.DLL, stat always sets st_ino to 0, and there is no simple way to */
90*9e94795aSAndroid Build Coastguard Worker 	/* get the equivalent information with Win32 (Cygwin does some weird stuff in   */
91*9e94795aSAndroid Build Coastguard Worker 	/* its winsup/cygwin/fhandler_disk_file.cc to emulate this, too complex for us) */
92*9e94795aSAndroid Build Coastguard Worker 	return 0;
93*9e94795aSAndroid Build Coastguard Worker #else
94*9e94795aSAndroid Build Coastguard Worker     return (pSrcStat->st_ino == pDstStat->st_ino);
95*9e94795aSAndroid Build Coastguard Worker #endif
96*9e94795aSAndroid Build Coastguard Worker }
97*9e94795aSAndroid Build Coastguard Worker 
printCopyMsg(const char * src,const char * dst,unsigned int options)98*9e94795aSAndroid Build Coastguard Worker static void printCopyMsg(const char* src, const char* dst, unsigned int options)
99*9e94795aSAndroid Build Coastguard Worker {
100*9e94795aSAndroid Build Coastguard Worker     if ((options & COPY_VERBOSE_MASK) > 0)
101*9e94795aSAndroid Build Coastguard Worker         printf("    '%s' --> '%s'\n", src, dst);
102*9e94795aSAndroid Build Coastguard Worker }
103*9e94795aSAndroid Build Coastguard Worker 
printNotNewerMsg(const char * src,const char * dst,unsigned int options)104*9e94795aSAndroid Build Coastguard Worker static void printNotNewerMsg(const char* src, const char* dst, unsigned int options)
105*9e94795aSAndroid Build Coastguard Worker {
106*9e94795aSAndroid Build Coastguard Worker     (void)src;
107*9e94795aSAndroid Build Coastguard Worker     if ((options & COPY_VERBOSE_MASK) > 1)
108*9e94795aSAndroid Build Coastguard Worker         printf("    '%s' is up-to-date\n", dst);
109*9e94795aSAndroid Build Coastguard Worker }
110*9e94795aSAndroid Build Coastguard Worker 
111*9e94795aSAndroid Build Coastguard Worker /*
112*9e94795aSAndroid Build Coastguard Worker  * Copy the contents of one file to another.
113*9e94795aSAndroid Build Coastguard Worker  *
114*9e94795aSAndroid Build Coastguard Worker  * The files are assumed to be seeked to the start.
115*9e94795aSAndroid Build Coastguard Worker  */
copyFileContents(const char * dst,int dstFd,const char * src,int srcFd)116*9e94795aSAndroid Build Coastguard Worker static int copyFileContents(const char* dst, int dstFd, const char* src, int srcFd)
117*9e94795aSAndroid Build Coastguard Worker {
118*9e94795aSAndroid Build Coastguard Worker     unsigned char buf[8192];
119*9e94795aSAndroid Build Coastguard Worker     ssize_t readCount, writeCount;
120*9e94795aSAndroid Build Coastguard Worker 
121*9e94795aSAndroid Build Coastguard Worker     /*
122*9e94795aSAndroid Build Coastguard Worker      * Read a chunk, write it, and repeat.
123*9e94795aSAndroid Build Coastguard Worker      */
124*9e94795aSAndroid Build Coastguard Worker     while (1) {
125*9e94795aSAndroid Build Coastguard Worker         readCount = read(srcFd, buf, sizeof(buf));
126*9e94795aSAndroid Build Coastguard Worker         if (readCount < 0) {
127*9e94795aSAndroid Build Coastguard Worker             fprintf(stderr,
128*9e94795aSAndroid Build Coastguard Worker                 "acp: failed reading '%s': %s\n", src, strerror(errno));
129*9e94795aSAndroid Build Coastguard Worker             return -1;
130*9e94795aSAndroid Build Coastguard Worker         }
131*9e94795aSAndroid Build Coastguard Worker 
132*9e94795aSAndroid Build Coastguard Worker         if (readCount > 0) {
133*9e94795aSAndroid Build Coastguard Worker             writeCount = write(dstFd, buf, readCount);
134*9e94795aSAndroid Build Coastguard Worker             if (writeCount < 0) {
135*9e94795aSAndroid Build Coastguard Worker                 fprintf(stderr,
136*9e94795aSAndroid Build Coastguard Worker                     "acp: failed writing '%s': %s\n", dst, strerror(errno));
137*9e94795aSAndroid Build Coastguard Worker                 return -1;
138*9e94795aSAndroid Build Coastguard Worker             }
139*9e94795aSAndroid Build Coastguard Worker             if (writeCount != readCount) {
140*9e94795aSAndroid Build Coastguard Worker                 fprintf(stderr, "acp: partial write to '%s' (%zd of %zd)\n",
141*9e94795aSAndroid Build Coastguard Worker                     dst, writeCount, readCount);
142*9e94795aSAndroid Build Coastguard Worker                 return -1;
143*9e94795aSAndroid Build Coastguard Worker             }
144*9e94795aSAndroid Build Coastguard Worker         }
145*9e94795aSAndroid Build Coastguard Worker 
146*9e94795aSAndroid Build Coastguard Worker         if (readCount < (ssize_t) sizeof(buf))
147*9e94795aSAndroid Build Coastguard Worker             break;
148*9e94795aSAndroid Build Coastguard Worker     }
149*9e94795aSAndroid Build Coastguard Worker 
150*9e94795aSAndroid Build Coastguard Worker     return 0;
151*9e94795aSAndroid Build Coastguard Worker }
152*9e94795aSAndroid Build Coastguard Worker 
153*9e94795aSAndroid Build Coastguard Worker /*
154*9e94795aSAndroid Build Coastguard Worker  * Set the permissions, owner, and timestamps on the destination file
155*9e94795aSAndroid Build Coastguard Worker  * equal to those of the source file.
156*9e94795aSAndroid Build Coastguard Worker  *
157*9e94795aSAndroid Build Coastguard Worker  * Failures here are "soft"; they don't produce warning messages and don't
158*9e94795aSAndroid Build Coastguard Worker  * cause the cp command to report a failure.
159*9e94795aSAndroid Build Coastguard Worker  */
setPermissions(const char * dst,const struct stat * pSrcStat,unsigned int options)160*9e94795aSAndroid Build Coastguard Worker static int setPermissions(const char* dst, const struct stat* pSrcStat, unsigned int options)
161*9e94795aSAndroid Build Coastguard Worker {
162*9e94795aSAndroid Build Coastguard Worker     struct utimbuf ut;
163*9e94795aSAndroid Build Coastguard Worker 
164*9e94795aSAndroid Build Coastguard Worker     if (options & COPY_TIMESTAMPS) {
165*9e94795aSAndroid Build Coastguard Worker         /*
166*9e94795aSAndroid Build Coastguard Worker          * Start with timestamps.  The access and mod dates are not affected
167*9e94795aSAndroid Build Coastguard Worker          * by the next operations.
168*9e94795aSAndroid Build Coastguard Worker          */
169*9e94795aSAndroid Build Coastguard Worker         ut.actime = pSrcStat->st_atime;
170*9e94795aSAndroid Build Coastguard Worker         ut.modtime = pSrcStat->st_mtime;
171*9e94795aSAndroid Build Coastguard Worker         if (isHiresMtime(pSrcStat))
172*9e94795aSAndroid Build Coastguard Worker             ut.modtime += 1;
173*9e94795aSAndroid Build Coastguard Worker         if (utime(dst, &ut) != 0) {
174*9e94795aSAndroid Build Coastguard Worker             DBUG(("---   unable to set timestamps on '%s': %s\n",
175*9e94795aSAndroid Build Coastguard Worker                 dst, strerror(errno)));
176*9e94795aSAndroid Build Coastguard Worker         }
177*9e94795aSAndroid Build Coastguard Worker     }
178*9e94795aSAndroid Build Coastguard Worker 
179*9e94795aSAndroid Build Coastguard Worker     if (options & COPY_PERMISSIONS) {
180*9e94795aSAndroid Build Coastguard Worker         /*
181*9e94795aSAndroid Build Coastguard Worker          * Set the permissions.
182*9e94795aSAndroid Build Coastguard Worker          */
183*9e94795aSAndroid Build Coastguard Worker         if (chmod(dst, pSrcStat->st_mode & ~(S_IFMT)) != 0) {
184*9e94795aSAndroid Build Coastguard Worker             DBUG(("---   unable to set perms on '%s' to 0%o: %s\n",
185*9e94795aSAndroid Build Coastguard Worker                 dst, pSrcStat->st_mode & ~(S_IFMT), strerror(errno)));
186*9e94795aSAndroid Build Coastguard Worker         }
187*9e94795aSAndroid Build Coastguard Worker #ifndef _WIN32
188*9e94795aSAndroid Build Coastguard Worker         /*
189*9e94795aSAndroid Build Coastguard Worker          * Set the owner.
190*9e94795aSAndroid Build Coastguard Worker          */
191*9e94795aSAndroid Build Coastguard Worker         if (chown(dst, pSrcStat->st_uid, pSrcStat->st_gid) != 0) {
192*9e94795aSAndroid Build Coastguard Worker             DBUG(("---   unable to set owner of '%s' to %d/%d: %s\n",
193*9e94795aSAndroid Build Coastguard Worker                 dst, pSrcStat->st_uid, pSrcStat->st_gid, strerror(errno)));
194*9e94795aSAndroid Build Coastguard Worker         }
195*9e94795aSAndroid Build Coastguard Worker #endif
196*9e94795aSAndroid Build Coastguard Worker     }
197*9e94795aSAndroid Build Coastguard Worker 
198*9e94795aSAndroid Build Coastguard Worker     return 0;
199*9e94795aSAndroid Build Coastguard Worker }
200*9e94795aSAndroid Build Coastguard Worker 
201*9e94795aSAndroid Build Coastguard Worker /*
202*9e94795aSAndroid Build Coastguard Worker  * Copy a regular file.  If the destination file exists and is not a
203*9e94795aSAndroid Build Coastguard Worker  * regular file, we fail.  However, we use stat() rather than lstat(),
204*9e94795aSAndroid Build Coastguard Worker  * because it's okay to write through a symlink (the noDereference stuff
205*9e94795aSAndroid Build Coastguard Worker  * only applies to the source file).
206*9e94795aSAndroid Build Coastguard Worker  *
207*9e94795aSAndroid Build Coastguard Worker  * If the file doesn't exist, create it.  If it does exist, truncate it.
208*9e94795aSAndroid Build Coastguard Worker  */
copyRegular(const char * src,const char * dst,const struct stat * pSrcStat,unsigned int options)209*9e94795aSAndroid Build Coastguard Worker static int copyRegular(const char* src, const char* dst, const struct stat* pSrcStat, unsigned int options)
210*9e94795aSAndroid Build Coastguard Worker {
211*9e94795aSAndroid Build Coastguard Worker     struct stat dstStat;
212*9e94795aSAndroid Build Coastguard Worker     int srcFd, dstFd, statResult, copyResult;
213*9e94795aSAndroid Build Coastguard Worker 
214*9e94795aSAndroid Build Coastguard Worker     DBUG(("--- copying regular '%s' to '%s'\n", src, dst));
215*9e94795aSAndroid Build Coastguard Worker 
216*9e94795aSAndroid Build Coastguard Worker     statResult = stat(dst, &dstStat);
217*9e94795aSAndroid Build Coastguard Worker     if (statResult == 0 && !S_ISREG(dstStat.st_mode)) {
218*9e94795aSAndroid Build Coastguard Worker         fprintf(stderr,
219*9e94795aSAndroid Build Coastguard Worker             "acp: destination '%s' exists and is not regular file\n",
220*9e94795aSAndroid Build Coastguard Worker             dst);
221*9e94795aSAndroid Build Coastguard Worker         return -1;
222*9e94795aSAndroid Build Coastguard Worker     } else if (statResult != 0 && errno != ENOENT) {
223*9e94795aSAndroid Build Coastguard Worker         fprintf(stderr, "acp: unable to stat destination '%s'\n", dst);
224*9e94795aSAndroid Build Coastguard Worker         return -1;
225*9e94795aSAndroid Build Coastguard Worker     }
226*9e94795aSAndroid Build Coastguard Worker 
227*9e94795aSAndroid Build Coastguard Worker     if (statResult == 0) {
228*9e94795aSAndroid Build Coastguard Worker         if (isSameFile(pSrcStat, &dstStat)) {
229*9e94795aSAndroid Build Coastguard Worker             fprintf(stderr, "acp: '%s' and '%s' are the same file\n",
230*9e94795aSAndroid Build Coastguard Worker                 src, dst);
231*9e94795aSAndroid Build Coastguard Worker             return -1;
232*9e94795aSAndroid Build Coastguard Worker         }
233*9e94795aSAndroid Build Coastguard Worker         if (options & COPY_UPDATE_ONLY) {
234*9e94795aSAndroid Build Coastguard Worker             if (!isSourceNewer(pSrcStat, &dstStat)) {
235*9e94795aSAndroid Build Coastguard Worker                 DBUG(("---  source is not newer: '%s'\n", src));
236*9e94795aSAndroid Build Coastguard Worker                 printNotNewerMsg(src, dst, options);
237*9e94795aSAndroid Build Coastguard Worker                 return 0;
238*9e94795aSAndroid Build Coastguard Worker             }
239*9e94795aSAndroid Build Coastguard Worker         }
240*9e94795aSAndroid Build Coastguard Worker     }
241*9e94795aSAndroid Build Coastguard Worker 
242*9e94795aSAndroid Build Coastguard Worker     /* open src */
243*9e94795aSAndroid Build Coastguard Worker     srcFd = open(src, O_RDONLY | O_BINARY, 0);
244*9e94795aSAndroid Build Coastguard Worker     if (srcFd < 0) {
245*9e94795aSAndroid Build Coastguard Worker         fprintf(stderr, "acp: unable to open '%s': %s\n", src, strerror(errno));
246*9e94795aSAndroid Build Coastguard Worker         return -1;
247*9e94795aSAndroid Build Coastguard Worker     }
248*9e94795aSAndroid Build Coastguard Worker 
249*9e94795aSAndroid Build Coastguard Worker     /* open dest with O_CREAT | O_TRUNC */
250*9e94795aSAndroid Build Coastguard Worker     DBUG(("---  opening '%s'\n", dst));
251*9e94795aSAndroid Build Coastguard Worker     dstFd = open(dst, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644);
252*9e94795aSAndroid Build Coastguard Worker 
253*9e94795aSAndroid Build Coastguard Worker     if (dstFd < 0) {
254*9e94795aSAndroid Build Coastguard Worker         if (errno == ENOENT) {
255*9e94795aSAndroid Build Coastguard Worker             /* this happens if the target directory doesn't exist */
256*9e94795aSAndroid Build Coastguard Worker             fprintf(stderr,
257*9e94795aSAndroid Build Coastguard Worker                 "acp: cannot create '%s': %s\n", dst, strerror(errno));
258*9e94795aSAndroid Build Coastguard Worker             (void) close(srcFd);
259*9e94795aSAndroid Build Coastguard Worker             return -1;
260*9e94795aSAndroid Build Coastguard Worker         }
261*9e94795aSAndroid Build Coastguard Worker 
262*9e94795aSAndroid Build Coastguard Worker         /* if "force" is set, try removing the destination file and retry */
263*9e94795aSAndroid Build Coastguard Worker         if (options & COPY_FORCE) {
264*9e94795aSAndroid Build Coastguard Worker             if (unlink(dst) != 0) {
265*9e94795aSAndroid Build Coastguard Worker #ifdef _WIN32
266*9e94795aSAndroid Build Coastguard Worker 				/* MSVCRT.DLL unlink will fail with EACCESS if the file is set read-only */
267*9e94795aSAndroid Build Coastguard Worker 				/* so try to change its mode, and unlink again                           */
268*9e94795aSAndroid Build Coastguard Worker 				if (errno == EACCESS) {
269*9e94795aSAndroid Build Coastguard Worker 					if (chmod(dst, S_IWRITE|S_IREAD) == 0 && unlink(dst) == 0)
270*9e94795aSAndroid Build Coastguard Worker 						goto Open_File;
271*9e94795aSAndroid Build Coastguard Worker 				}
272*9e94795aSAndroid Build Coastguard Worker #endif
273*9e94795aSAndroid Build Coastguard Worker                 fprintf(stderr, "acp: unable to remove '%s': %s\n",
274*9e94795aSAndroid Build Coastguard Worker                     dst, strerror(errno));
275*9e94795aSAndroid Build Coastguard Worker                 (void) close(srcFd);
276*9e94795aSAndroid Build Coastguard Worker                 return -1;
277*9e94795aSAndroid Build Coastguard Worker             }
278*9e94795aSAndroid Build Coastguard Worker #ifdef _WIN32
279*9e94795aSAndroid Build Coastguard Worker         Open_File:
280*9e94795aSAndroid Build Coastguard Worker #endif
281*9e94795aSAndroid Build Coastguard Worker             dstFd = open(dst, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644);
282*9e94795aSAndroid Build Coastguard Worker         }
283*9e94795aSAndroid Build Coastguard Worker     }
284*9e94795aSAndroid Build Coastguard Worker     if (dstFd < 0) {
285*9e94795aSAndroid Build Coastguard Worker         fprintf(stderr, "acp: unable to open '%s': %s\n",
286*9e94795aSAndroid Build Coastguard Worker             dst, strerror(errno));
287*9e94795aSAndroid Build Coastguard Worker         (void) close(srcFd);
288*9e94795aSAndroid Build Coastguard Worker         return -1;
289*9e94795aSAndroid Build Coastguard Worker     }
290*9e94795aSAndroid Build Coastguard Worker 
291*9e94795aSAndroid Build Coastguard Worker     copyResult = copyFileContents(dst, dstFd, src, srcFd);
292*9e94795aSAndroid Build Coastguard Worker 
293*9e94795aSAndroid Build Coastguard Worker     (void) close(srcFd);
294*9e94795aSAndroid Build Coastguard Worker     (void) close(dstFd);
295*9e94795aSAndroid Build Coastguard Worker     if (copyResult != 0)
296*9e94795aSAndroid Build Coastguard Worker         return -1;
297*9e94795aSAndroid Build Coastguard Worker 
298*9e94795aSAndroid Build Coastguard Worker #if defined(__APPLE__)
299*9e94795aSAndroid Build Coastguard Worker     // Copy Mac OS X resource forks too.
300*9e94795aSAndroid Build Coastguard Worker     {
301*9e94795aSAndroid Build Coastguard Worker         char* srcRsrcName = NULL;
302*9e94795aSAndroid Build Coastguard Worker         char* dstRsrcName = NULL;
303*9e94795aSAndroid Build Coastguard Worker         struct stat rsrcStat;
304*9e94795aSAndroid Build Coastguard Worker 
305*9e94795aSAndroid Build Coastguard Worker         srcRsrcName = malloc(strlen(src) + 5 + 1);
306*9e94795aSAndroid Build Coastguard Worker         strcpy(srcRsrcName, src);
307*9e94795aSAndroid Build Coastguard Worker         strcat(srcRsrcName, "/rsrc");
308*9e94795aSAndroid Build Coastguard Worker 
309*9e94795aSAndroid Build Coastguard Worker         dstRsrcName = malloc(strlen(dst) + 5 + 1);
310*9e94795aSAndroid Build Coastguard Worker         strcpy(dstRsrcName, dst);
311*9e94795aSAndroid Build Coastguard Worker         strcat(dstRsrcName, "/rsrc");
312*9e94795aSAndroid Build Coastguard Worker 
313*9e94795aSAndroid Build Coastguard Worker         if (stat(srcRsrcName, &rsrcStat) == 0 && rsrcStat.st_size > 0) {
314*9e94795aSAndroid Build Coastguard Worker             DBUG(("---  RSRC: %s --> %s\n", srcRsrcName, dstRsrcName));
315*9e94795aSAndroid Build Coastguard Worker 
316*9e94795aSAndroid Build Coastguard Worker             srcFd = open(srcRsrcName, O_RDONLY);
317*9e94795aSAndroid Build Coastguard Worker             dstFd = open(dstRsrcName, O_TRUNC | O_WRONLY, 0);
318*9e94795aSAndroid Build Coastguard Worker             copyResult = -1;
319*9e94795aSAndroid Build Coastguard Worker             if (srcFd >= 0 && dstFd >= 0) {
320*9e94795aSAndroid Build Coastguard Worker                 copyResult = copyFileContents(dstRsrcName, dstFd,
321*9e94795aSAndroid Build Coastguard Worker                     srcRsrcName, srcFd);
322*9e94795aSAndroid Build Coastguard Worker                 (void) close(srcFd);
323*9e94795aSAndroid Build Coastguard Worker                 (void) close(dstFd);
324*9e94795aSAndroid Build Coastguard Worker             }
325*9e94795aSAndroid Build Coastguard Worker 
326*9e94795aSAndroid Build Coastguard Worker             if (copyResult != 0) {
327*9e94795aSAndroid Build Coastguard Worker                 free(srcRsrcName);
328*9e94795aSAndroid Build Coastguard Worker                 free(dstRsrcName);
329*9e94795aSAndroid Build Coastguard Worker                 return -1;
330*9e94795aSAndroid Build Coastguard Worker             }
331*9e94795aSAndroid Build Coastguard Worker         }
332*9e94795aSAndroid Build Coastguard Worker 
333*9e94795aSAndroid Build Coastguard Worker         free(srcRsrcName);
334*9e94795aSAndroid Build Coastguard Worker         free(dstRsrcName);
335*9e94795aSAndroid Build Coastguard Worker     }
336*9e94795aSAndroid Build Coastguard Worker #endif
337*9e94795aSAndroid Build Coastguard Worker 
338*9e94795aSAndroid Build Coastguard Worker     setPermissions(dst, pSrcStat, options);
339*9e94795aSAndroid Build Coastguard Worker 
340*9e94795aSAndroid Build Coastguard Worker     printCopyMsg(src, dst, options);
341*9e94795aSAndroid Build Coastguard Worker 
342*9e94795aSAndroid Build Coastguard Worker     return 0;
343*9e94795aSAndroid Build Coastguard Worker }
344*9e94795aSAndroid Build Coastguard Worker 
345*9e94795aSAndroid Build Coastguard Worker 
346*9e94795aSAndroid Build Coastguard Worker /*
347*9e94795aSAndroid Build Coastguard Worker  * Copy a symlink.  This only happens if we're in "no derefence" mode,
348*9e94795aSAndroid Build Coastguard Worker  * in which we copy the links rather than the files that are pointed at.
349*9e94795aSAndroid Build Coastguard Worker  *
350*9e94795aSAndroid Build Coastguard Worker  * We always discard the destination file.  If it's a symlink already,
351*9e94795aSAndroid Build Coastguard Worker  * we want to throw it out and replace it.  If it's not a symlink, we
352*9e94795aSAndroid Build Coastguard Worker  * need to trash it so we can create one.
353*9e94795aSAndroid Build Coastguard Worker  */
354*9e94795aSAndroid Build Coastguard Worker #if defined(_WIN32)
355*9e94795aSAndroid Build Coastguard Worker extern int copySymlink(const char* src, const char* dst, const struct stat* pSrcStat, unsigned int options)
356*9e94795aSAndroid Build Coastguard Worker #ifdef __clang__
357*9e94795aSAndroid Build Coastguard Worker   __attribute__((unavailable("no symlinks on Windows")));
358*9e94795aSAndroid Build Coastguard Worker #else
359*9e94795aSAndroid Build Coastguard Worker   __attribute__((error("no symlinks on Windows")));
360*9e94795aSAndroid Build Coastguard Worker #endif
361*9e94795aSAndroid Build Coastguard Worker #else
copySymlink(const char * src,const char * dst,const struct stat * pSrcStat,unsigned int options)362*9e94795aSAndroid Build Coastguard Worker static int copySymlink(const char* src, const char* dst, const struct stat* pSrcStat, unsigned int options)
363*9e94795aSAndroid Build Coastguard Worker {
364*9e94795aSAndroid Build Coastguard Worker     struct stat dstStat;
365*9e94795aSAndroid Build Coastguard Worker     char linkBuf[PATH_MAX+1];
366*9e94795aSAndroid Build Coastguard Worker     int statResult, nameLen;
367*9e94795aSAndroid Build Coastguard Worker 
368*9e94795aSAndroid Build Coastguard Worker     assert(options & COPY_NO_DEREFERENCE);
369*9e94795aSAndroid Build Coastguard Worker     DBUG(("--- copying symlink '%s' to '%s'\n", src, dst));
370*9e94795aSAndroid Build Coastguard Worker 
371*9e94795aSAndroid Build Coastguard Worker     /* NOTE: we use lstat() here */
372*9e94795aSAndroid Build Coastguard Worker     statResult = lstat(dst, &dstStat);
373*9e94795aSAndroid Build Coastguard Worker     if (statResult == 0 && !S_ISREG(dstStat.st_mode)
374*9e94795aSAndroid Build Coastguard Worker                          && !S_ISLNK(dstStat.st_mode)
375*9e94795aSAndroid Build Coastguard Worker 						 )
376*9e94795aSAndroid Build Coastguard Worker     {
377*9e94795aSAndroid Build Coastguard Worker         fprintf(stderr,
378*9e94795aSAndroid Build Coastguard Worker             "acp: destination '%s' exists and is not regular or symlink\n",
379*9e94795aSAndroid Build Coastguard Worker             dst);
380*9e94795aSAndroid Build Coastguard Worker         return -1;
381*9e94795aSAndroid Build Coastguard Worker     }
382*9e94795aSAndroid Build Coastguard Worker 
383*9e94795aSAndroid Build Coastguard Worker     if (statResult == 0) {
384*9e94795aSAndroid Build Coastguard Worker         if (isSameFile(pSrcStat, &dstStat)) {
385*9e94795aSAndroid Build Coastguard Worker             fprintf(stderr, "acp: '%s' and '%s' are the same file\n",
386*9e94795aSAndroid Build Coastguard Worker                 src, dst);
387*9e94795aSAndroid Build Coastguard Worker             return -1;
388*9e94795aSAndroid Build Coastguard Worker         }
389*9e94795aSAndroid Build Coastguard Worker         if (options & COPY_UPDATE_ONLY) {
390*9e94795aSAndroid Build Coastguard Worker             if (!isSourceNewer(pSrcStat, &dstStat)) {
391*9e94795aSAndroid Build Coastguard Worker                 DBUG(("---  source is not newer: '%s'\n", src));
392*9e94795aSAndroid Build Coastguard Worker                 printNotNewerMsg(src, dst, options);
393*9e94795aSAndroid Build Coastguard Worker                 return 0;
394*9e94795aSAndroid Build Coastguard Worker             }
395*9e94795aSAndroid Build Coastguard Worker         }
396*9e94795aSAndroid Build Coastguard Worker     }
397*9e94795aSAndroid Build Coastguard Worker 
398*9e94795aSAndroid Build Coastguard Worker     /* extract the symlink contents */
399*9e94795aSAndroid Build Coastguard Worker     nameLen = readlink(src, linkBuf, sizeof(linkBuf)-1);
400*9e94795aSAndroid Build Coastguard Worker     if (nameLen <= 0) {
401*9e94795aSAndroid Build Coastguard Worker         fprintf(stderr, "acp: unable to read symlink '%s': %s\n",
402*9e94795aSAndroid Build Coastguard Worker             src, strerror(errno));
403*9e94795aSAndroid Build Coastguard Worker         return -1;
404*9e94795aSAndroid Build Coastguard Worker     }
405*9e94795aSAndroid Build Coastguard Worker     linkBuf[nameLen] = '\0';
406*9e94795aSAndroid Build Coastguard Worker     DBUG(("--- creating symlink file '%s' (--> %s)\n", dst, linkBuf));
407*9e94795aSAndroid Build Coastguard Worker 
408*9e94795aSAndroid Build Coastguard Worker     if (statResult == 0) {
409*9e94795aSAndroid Build Coastguard Worker         DBUG(("---  removing '%s'\n", dst));
410*9e94795aSAndroid Build Coastguard Worker         if (unlink(dst) != 0) {
411*9e94795aSAndroid Build Coastguard Worker             fprintf(stderr, "acp: unable to remove '%s': %s\n",
412*9e94795aSAndroid Build Coastguard Worker                 dst, strerror(errno));
413*9e94795aSAndroid Build Coastguard Worker             return -1;
414*9e94795aSAndroid Build Coastguard Worker         }
415*9e94795aSAndroid Build Coastguard Worker     }
416*9e94795aSAndroid Build Coastguard Worker 
417*9e94795aSAndroid Build Coastguard Worker     if (symlink(linkBuf, dst) != 0) {
418*9e94795aSAndroid Build Coastguard Worker         fprintf(stderr, "acp: unable to create symlink '%s' [%s]: %s\n",
419*9e94795aSAndroid Build Coastguard Worker             dst, linkBuf, strerror(errno));
420*9e94795aSAndroid Build Coastguard Worker         return -1;
421*9e94795aSAndroid Build Coastguard Worker     }
422*9e94795aSAndroid Build Coastguard Worker 
423*9e94795aSAndroid Build Coastguard Worker     /*
424*9e94795aSAndroid Build Coastguard Worker      * There's no way to set the file date or access permissions, but
425*9e94795aSAndroid Build Coastguard Worker      * it is possible to set the owner.
426*9e94795aSAndroid Build Coastguard Worker      */
427*9e94795aSAndroid Build Coastguard Worker     if (options & COPY_PERMISSIONS) {
428*9e94795aSAndroid Build Coastguard Worker         if (lchown(dst, pSrcStat->st_uid, pSrcStat->st_gid) != 0)
429*9e94795aSAndroid Build Coastguard Worker             DBUG(("---  lchown failed: %s\n", strerror(errno)));
430*9e94795aSAndroid Build Coastguard Worker     }
431*9e94795aSAndroid Build Coastguard Worker 
432*9e94795aSAndroid Build Coastguard Worker     printCopyMsg(src, dst, options);
433*9e94795aSAndroid Build Coastguard Worker 
434*9e94795aSAndroid Build Coastguard Worker     return 0;
435*9e94795aSAndroid Build Coastguard Worker }
436*9e94795aSAndroid Build Coastguard Worker #endif
437*9e94795aSAndroid Build Coastguard Worker 
438*9e94795aSAndroid Build Coastguard Worker /*
439*9e94795aSAndroid Build Coastguard Worker  * Copy the contents of one directory to another.  Both "src" and "dst"
440*9e94795aSAndroid Build Coastguard Worker  * must be directories.  We will create "dst" if it does not exist.
441*9e94795aSAndroid Build Coastguard Worker  */
copyDirectory(const char * src,const char * dst,const struct stat * pSrcStat,unsigned int options)442*9e94795aSAndroid Build Coastguard Worker int copyDirectory(const char* src, const char* dst, const struct stat* pSrcStat, unsigned int options)
443*9e94795aSAndroid Build Coastguard Worker {
444*9e94795aSAndroid Build Coastguard Worker     int retVal = 0;
445*9e94795aSAndroid Build Coastguard Worker     struct stat dstStat;
446*9e94795aSAndroid Build Coastguard Worker     DIR* dir;
447*9e94795aSAndroid Build Coastguard Worker     int cc, statResult;
448*9e94795aSAndroid Build Coastguard Worker 
449*9e94795aSAndroid Build Coastguard Worker     DBUG(("--- copy dir '%s' to '%s'\n", src, dst));
450*9e94795aSAndroid Build Coastguard Worker 
451*9e94795aSAndroid Build Coastguard Worker     statResult = stat(dst, &dstStat);
452*9e94795aSAndroid Build Coastguard Worker     if (statResult == 0 && !S_ISDIR(dstStat.st_mode)) {
453*9e94795aSAndroid Build Coastguard Worker         fprintf(stderr,
454*9e94795aSAndroid Build Coastguard Worker             "acp: destination '%s' exists and is not a directory\n", dst);
455*9e94795aSAndroid Build Coastguard Worker         return -1;
456*9e94795aSAndroid Build Coastguard Worker     } else if (statResult != 0 && errno != ENOENT) {
457*9e94795aSAndroid Build Coastguard Worker         fprintf(stderr, "acp: unable to stat destination '%s'\n", dst);
458*9e94795aSAndroid Build Coastguard Worker         return -1;
459*9e94795aSAndroid Build Coastguard Worker     }
460*9e94795aSAndroid Build Coastguard Worker 
461*9e94795aSAndroid Build Coastguard Worker     if (statResult == 0) {
462*9e94795aSAndroid Build Coastguard Worker         if (isSameFile(pSrcStat, &dstStat)) {
463*9e94795aSAndroid Build Coastguard Worker             fprintf(stderr,
464*9e94795aSAndroid Build Coastguard Worker                 "acp: cannot copy directory into itself ('%s' and '%s')\n",
465*9e94795aSAndroid Build Coastguard Worker                 src, dst);
466*9e94795aSAndroid Build Coastguard Worker             return -1;
467*9e94795aSAndroid Build Coastguard Worker         }
468*9e94795aSAndroid Build Coastguard Worker     } else {
469*9e94795aSAndroid Build Coastguard Worker         DBUG(("---  creating dir '%s'\n", dst));
470*9e94795aSAndroid Build Coastguard Worker         cc = mkdir(dst, 0755);
471*9e94795aSAndroid Build Coastguard Worker         if (cc != 0) {
472*9e94795aSAndroid Build Coastguard Worker             fprintf(stderr, "acp: unable to create directory '%s': %s\n",
473*9e94795aSAndroid Build Coastguard Worker                 dst, strerror(errno));
474*9e94795aSAndroid Build Coastguard Worker             return -1;
475*9e94795aSAndroid Build Coastguard Worker         }
476*9e94795aSAndroid Build Coastguard Worker 
477*9e94795aSAndroid Build Coastguard Worker         /* only print on mkdir */
478*9e94795aSAndroid Build Coastguard Worker         printCopyMsg(src, dst, options);
479*9e94795aSAndroid Build Coastguard Worker     }
480*9e94795aSAndroid Build Coastguard Worker 
481*9e94795aSAndroid Build Coastguard Worker     /*
482*9e94795aSAndroid Build Coastguard Worker      * Open the directory, and plow through its contents.
483*9e94795aSAndroid Build Coastguard Worker      */
484*9e94795aSAndroid Build Coastguard Worker     dir = opendir(src);
485*9e94795aSAndroid Build Coastguard Worker     if (dir == NULL) {
486*9e94795aSAndroid Build Coastguard Worker         fprintf(stderr, "acp: unable to open directory '%s': %s\n",
487*9e94795aSAndroid Build Coastguard Worker             src, strerror(errno));
488*9e94795aSAndroid Build Coastguard Worker         return -1;
489*9e94795aSAndroid Build Coastguard Worker     }
490*9e94795aSAndroid Build Coastguard Worker 
491*9e94795aSAndroid Build Coastguard Worker     while (1) {
492*9e94795aSAndroid Build Coastguard Worker         struct dirent* ent;
493*9e94795aSAndroid Build Coastguard Worker         char* srcFile;
494*9e94795aSAndroid Build Coastguard Worker         char* dstFile;
495*9e94795aSAndroid Build Coastguard Worker         int srcLen, dstLen, nameLen;
496*9e94795aSAndroid Build Coastguard Worker 
497*9e94795aSAndroid Build Coastguard Worker         ent = readdir(dir);
498*9e94795aSAndroid Build Coastguard Worker         if (ent == NULL)
499*9e94795aSAndroid Build Coastguard Worker             break;
500*9e94795aSAndroid Build Coastguard Worker 
501*9e94795aSAndroid Build Coastguard Worker         if (strcmp(ent->d_name, ".") == 0 ||
502*9e94795aSAndroid Build Coastguard Worker             strcmp(ent->d_name, "..") == 0)
503*9e94795aSAndroid Build Coastguard Worker         {
504*9e94795aSAndroid Build Coastguard Worker             continue;
505*9e94795aSAndroid Build Coastguard Worker         }
506*9e94795aSAndroid Build Coastguard Worker 
507*9e94795aSAndroid Build Coastguard Worker         nameLen = strlen(ent->d_name);
508*9e94795aSAndroid Build Coastguard Worker         srcLen = strlen(src);
509*9e94795aSAndroid Build Coastguard Worker         dstLen = strlen(dst);
510*9e94795aSAndroid Build Coastguard Worker 
511*9e94795aSAndroid Build Coastguard Worker         srcFile = malloc(srcLen +1 + nameLen +1);
512*9e94795aSAndroid Build Coastguard Worker         memcpy(srcFile, src, srcLen);
513*9e94795aSAndroid Build Coastguard Worker         srcFile[srcLen] = FSSEP;
514*9e94795aSAndroid Build Coastguard Worker         memcpy(srcFile + srcLen+1, ent->d_name, nameLen +1);
515*9e94795aSAndroid Build Coastguard Worker 
516*9e94795aSAndroid Build Coastguard Worker         dstFile = malloc(dstLen +1 + nameLen +1);
517*9e94795aSAndroid Build Coastguard Worker         memcpy(dstFile, dst, dstLen);
518*9e94795aSAndroid Build Coastguard Worker         dstFile[dstLen] = FSSEP;
519*9e94795aSAndroid Build Coastguard Worker         memcpy(dstFile + dstLen+1, ent->d_name, nameLen +1);
520*9e94795aSAndroid Build Coastguard Worker 
521*9e94795aSAndroid Build Coastguard Worker         if (copyFileRecursive(srcFile, dstFile, false, options) != 0)
522*9e94795aSAndroid Build Coastguard Worker             retVal = -1;        /* note failure and keep going */
523*9e94795aSAndroid Build Coastguard Worker 
524*9e94795aSAndroid Build Coastguard Worker         free(srcFile);
525*9e94795aSAndroid Build Coastguard Worker         free(dstFile);
526*9e94795aSAndroid Build Coastguard Worker     }
527*9e94795aSAndroid Build Coastguard Worker     closedir(dir);
528*9e94795aSAndroid Build Coastguard Worker 
529*9e94795aSAndroid Build Coastguard Worker     setPermissions(dst, pSrcStat, options);
530*9e94795aSAndroid Build Coastguard Worker 
531*9e94795aSAndroid Build Coastguard Worker     return retVal;
532*9e94795aSAndroid Build Coastguard Worker }
533*9e94795aSAndroid Build Coastguard Worker 
534*9e94795aSAndroid Build Coastguard Worker /*
535*9e94795aSAndroid Build Coastguard Worker  * Do the actual copy.  This is called recursively from copyDirectory().
536*9e94795aSAndroid Build Coastguard Worker  *
537*9e94795aSAndroid Build Coastguard Worker  * "dst" should only be a directory if "src" is also a directory.
538*9e94795aSAndroid Build Coastguard Worker  *
539*9e94795aSAndroid Build Coastguard Worker  * Returns 0 on success.
540*9e94795aSAndroid Build Coastguard Worker  */
copyFileRecursive(const char * src,const char * dst,bool isCmdLine,unsigned int options)541*9e94795aSAndroid Build Coastguard Worker static int copyFileRecursive(const char* src, const char* dst, bool isCmdLine, unsigned int options)
542*9e94795aSAndroid Build Coastguard Worker {
543*9e94795aSAndroid Build Coastguard Worker     char* srcExe = NULL;
544*9e94795aSAndroid Build Coastguard Worker     char* dstExe = NULL;
545*9e94795aSAndroid Build Coastguard Worker     char* dstDir = NULL;
546*9e94795aSAndroid Build Coastguard Worker     struct stat srcStat;
547*9e94795aSAndroid Build Coastguard Worker     int retVal = 0;
548*9e94795aSAndroid Build Coastguard Worker     int statResult, statErrno;
549*9e94795aSAndroid Build Coastguard Worker     (void)isCmdLine;
550*9e94795aSAndroid Build Coastguard Worker 
551*9e94795aSAndroid Build Coastguard Worker     /*
552*9e94795aSAndroid Build Coastguard Worker      * Stat the source file.  If it doesn't exist, fail.
553*9e94795aSAndroid Build Coastguard Worker      */
554*9e94795aSAndroid Build Coastguard Worker     if (options & COPY_NO_DEREFERENCE)
555*9e94795aSAndroid Build Coastguard Worker         statResult = lstat(src, &srcStat);
556*9e94795aSAndroid Build Coastguard Worker     else
557*9e94795aSAndroid Build Coastguard Worker         statResult = stat(src, &srcStat);
558*9e94795aSAndroid Build Coastguard Worker     statErrno = errno;        /* preserve across .exe attempt */
559*9e94795aSAndroid Build Coastguard Worker 
560*9e94795aSAndroid Build Coastguard Worker     if (statResult < 0) {
561*9e94795aSAndroid Build Coastguard Worker         if (statErrno == ENOENT)
562*9e94795aSAndroid Build Coastguard Worker             fprintf(stderr, "acp: file '%s' does not exist\n", src);
563*9e94795aSAndroid Build Coastguard Worker         else
564*9e94795aSAndroid Build Coastguard Worker             fprintf(stderr, "acp: unable to stat '%s': %s\n",
565*9e94795aSAndroid Build Coastguard Worker                 src, strerror(statErrno));
566*9e94795aSAndroid Build Coastguard Worker         retVal = -1;
567*9e94795aSAndroid Build Coastguard Worker         goto bail;
568*9e94795aSAndroid Build Coastguard Worker     }
569*9e94795aSAndroid Build Coastguard Worker 
570*9e94795aSAndroid Build Coastguard Worker     /*
571*9e94795aSAndroid Build Coastguard Worker      * If "src" is a directory, ignore it if "recursive" isn't set.
572*9e94795aSAndroid Build Coastguard Worker      *
573*9e94795aSAndroid Build Coastguard Worker      * We want to create "dst" as a directory (or verify that it already
574*9e94795aSAndroid Build Coastguard Worker      * exists as a directory), and then copy its contents.
575*9e94795aSAndroid Build Coastguard Worker      */
576*9e94795aSAndroid Build Coastguard Worker     if (S_ISDIR(srcStat.st_mode)) {
577*9e94795aSAndroid Build Coastguard Worker         if (!(options & COPY_RECURSIVE)) {
578*9e94795aSAndroid Build Coastguard Worker             fprintf(stderr, "acp: omitting directory '%s'\n", src);
579*9e94795aSAndroid Build Coastguard Worker         } else {
580*9e94795aSAndroid Build Coastguard Worker             retVal = copyDirectory(src, dst, &srcStat, options);
581*9e94795aSAndroid Build Coastguard Worker         }
582*9e94795aSAndroid Build Coastguard Worker #if !defined(_WIN32)
583*9e94795aSAndroid Build Coastguard Worker     } else if (S_ISLNK(srcStat.st_mode)) {
584*9e94795aSAndroid Build Coastguard Worker         retVal = copySymlink(src, dst, &srcStat, options);
585*9e94795aSAndroid Build Coastguard Worker #endif
586*9e94795aSAndroid Build Coastguard Worker     } else if (S_ISREG(srcStat.st_mode)) {
587*9e94795aSAndroid Build Coastguard Worker         retVal = copyRegular(src, dst, &srcStat, options);
588*9e94795aSAndroid Build Coastguard Worker     } else {
589*9e94795aSAndroid Build Coastguard Worker         fprintf(stderr, "acp: skipping unusual file '%s' (mode=0%o)\n",
590*9e94795aSAndroid Build Coastguard Worker             src, srcStat.st_mode);
591*9e94795aSAndroid Build Coastguard Worker         retVal = -1;
592*9e94795aSAndroid Build Coastguard Worker     }
593*9e94795aSAndroid Build Coastguard Worker 
594*9e94795aSAndroid Build Coastguard Worker bail:
595*9e94795aSAndroid Build Coastguard Worker     free(srcExe);
596*9e94795aSAndroid Build Coastguard Worker     free(dstExe);
597*9e94795aSAndroid Build Coastguard Worker     free(dstDir);
598*9e94795aSAndroid Build Coastguard Worker     return retVal;
599*9e94795aSAndroid Build Coastguard Worker }
600*9e94795aSAndroid Build Coastguard Worker 
copyFile(const char * src,const char * dst,unsigned int options)601*9e94795aSAndroid Build Coastguard Worker int copyFile(const char* src, const char* dst, unsigned int options)
602*9e94795aSAndroid Build Coastguard Worker {
603*9e94795aSAndroid Build Coastguard Worker     return copyFileRecursive(src, dst, true, options);
604*9e94795aSAndroid Build Coastguard Worker }
605*9e94795aSAndroid Build Coastguard Worker 
606*9e94795aSAndroid Build Coastguard Worker 
607