Changeset 3221 for trunk/src/kmk/kmkbuiltin/cp_utils.c
- Timestamp:
- Mar 31, 2018, 1:27:52 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/kmkbuiltin/cp_utils.c
r3219 r3221 36 36 #endif /* not lint */ 37 37 38 /********************************************************************************************************************************* 39 * Header Files * 40 *********************************************************************************************************************************/ 38 41 #define MSC_DO_64_BIT_IO 39 42 #include "config.h" … … 72 75 #include "cmp_extern.h" 73 76 77 78 /********************************************************************************************************************************* 79 * Defined Constants And Macros * 80 *********************************************************************************************************************************/ 74 81 #define cp_pct(x,y) (int)(100.0 * (double)(x) / (double)(y)) 75 82 76 83 #ifndef MAXBSIZE 77 # define MAXBSIZE 0x 2000084 # define MAXBSIZE 0x10000 78 85 #endif 79 86 #ifndef O_BINARY … … 87 94 88 95 int 89 copy_file( PKMKBUILTINCTX pCtx, const FTSENT *entp, int dne, int changed_only, int *pcopied)90 { 91 staticchar buf[MAXBSIZE];96 copy_file(CPUTILSINSTANCE *pThis, const FTSENT *entp, int dne, int changed_only, int *pcopied) 97 { 98 /*static*/ char buf[MAXBSIZE]; 92 99 struct stat *fs; 93 100 int ch, checkch, from_fd, rcount, rval, to_fd; … … 103 110 104 111 if ((from_fd = open(entp->fts_path, O_RDONLY | O_BINARY | KMK_OPEN_NO_INHERIT, 0)) == -1) { 105 warn(p Ctx, "open: %s", entp->fts_path);112 warn(pThis->pCtx, "open: %s", entp->fts_path); 106 113 return (1); 107 114 } … … 120 127 /* compare the files first if requested */ 121 128 if (changed_only) { 122 if (cmp_fd_and_file(p Ctx, from_fd, entp->fts_path,to.p_path,129 if (cmp_fd_and_file(pThis->pCtx, from_fd, entp->fts_path, pThis->to.p_path, 123 130 1 /* silent */, 0 /* lflag */, 124 131 0 /* special */) == OK_EXIT) { … … 129 136 close(from_fd); 130 137 if ((from_fd = open(entp->fts_path, O_RDONLY | O_BINARY | KMK_OPEN_NO_INHERIT, 0)) == -1) { 131 warn(p Ctx, "open: %s", entp->fts_path);138 warn(pThis->pCtx, "open: %s", entp->fts_path); 132 139 return (1); 133 140 } … … 136 143 137 144 #define YESNO "(y/n [n]) " 138 if ( nflag) {139 if ( vflag)140 kmk_builtin_ctx_printf(p Ctx, 0, "%s not overwritten\n",to.p_path);145 if (pThis->nflag) { 146 if (pThis->vflag) 147 kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s not overwritten\n", pThis->to.p_path); 141 148 return (0); 142 } else if ( iflag) {149 } else if (pThis->iflag) { 143 150 (void)fprintf(stderr, "overwrite %s? %s", 144 to.p_path, YESNO);151 pThis->to.p_path, YESNO); 145 152 checkch = ch = getchar(); 146 153 while (ch != '\n' && ch != EOF) … … 148 155 if (checkch != 'y' && checkch != 'Y') { 149 156 (void)close(from_fd); 150 kmk_builtin_ctx_printf(p Ctx, 1, "not overwritten\n");157 kmk_builtin_ctx_printf(pThis->pCtx, 1, "not overwritten\n"); 151 158 return (1); 152 159 } 153 160 } 154 161 155 if ( fflag) {162 if (pThis->fflag) { 156 163 /* remove existing destination file name, 157 164 * create a new file */ 158 (void)unlink( to.p_path);159 to_fd = open( to.p_path, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY | KMK_OPEN_NO_INHERIT,165 (void)unlink(pThis->to.p_path); 166 to_fd = open(pThis->to.p_path, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY | KMK_OPEN_NO_INHERIT, 160 167 fs->st_mode & ~(S_ISUID | S_ISGID)); 161 168 } else 162 169 /* overwrite existing destination file name */ 163 to_fd = open( to.p_path, O_WRONLY | O_TRUNC | O_BINARY | KMK_OPEN_NO_INHERIT, 0);170 to_fd = open(pThis->to.p_path, O_WRONLY | O_TRUNC | O_BINARY | KMK_OPEN_NO_INHERIT, 0); 164 171 } else 165 to_fd = open( to.p_path, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY | KMK_OPEN_NO_INHERIT,172 to_fd = open(pThis->to.p_path, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY | KMK_OPEN_NO_INHERIT, 166 173 fs->st_mode & ~(S_ISUID | S_ISGID)); 167 174 168 175 if (to_fd == -1) { 169 warn(p Ctx, "open: %s",to.p_path);176 warn(pThis->pCtx, "open: %s", pThis->to.p_path); 170 177 (void)close(from_fd); 171 178 return (1); … … 185 192 if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ, 186 193 MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) { 187 warn(p Ctx, "mmap: %s", entp->fts_path);194 warn(pThis->pCtx, "mmap: %s", entp->fts_path); 188 195 rval = 1; 189 196 } else { … … 193 200 wcount = write(to_fd, bufp, wresid); 194 201 wtotal += wcount; 195 if (info) { 196 info = 0; 197 kmk_builtin_ctx_printf(pCtx, 1, 202 # if defined(SIGINFO) && defined(KMK_BUILTIN_STANDALONE) 203 if (g_cp_info) { 204 g_cp_info = 0; 205 kmk_builtin_ctx_printf(pThis->pCtx, 1, 198 206 "%s -> %s %3d%%\n", 199 entp->fts_path, to.p_path,207 entp->fts_path, pThis->to.p_path, 200 208 cp_pct(wtotal, fs->st_size)); 201 209 202 210 } 211 #endif 203 212 if (wcount >= (ssize_t)wresid || wcount <= 0) 204 213 break; 205 214 } 206 215 if (wcount != (ssize_t)wresid) { 207 warn(p Ctx, "write[%zd != %zu]: %s", wcount, wresid,to.p_path);216 warn(pThis->pCtx, "write[%zd != %zu]: %s", wcount, wresid, pThis->to.p_path); 208 217 rval = 1; 209 218 } 210 219 /* Some systems don't unmap on close(2). */ 211 220 if (munmap(p, fs->st_size) < 0) { 212 warn(p Ctx, "munmap: %s", entp->fts_path);221 warn(pThis->pCtx, "munmap: %s", entp->fts_path); 213 222 rval = 1; 214 223 } … … 223 232 wcount = write(to_fd, bufp, wresid); 224 233 wtotal += wcount; 225 if (info) { 226 info = 0; 227 kmk_builtin_ctx_printf(pCtx, 1, 234 #if defined(SIGINFO) && defined(KMK_BUILTIN_STANDALONE) 235 if (g_cp_info) { 236 g_cp_info = 0; 237 kmk_builtin_ctx_printf(pThis->pCtx, 1, 228 238 "%s -> %s %3d%%\n", 229 entp->fts_path, to.p_path,239 entp->fts_path, pThis->to.p_path, 230 240 cp_pct(wtotal, fs->st_size)); 231 241 232 242 } 243 #endif 233 244 if (wcount >= (ssize_t)wresid || wcount <= 0) 234 245 break; 235 246 } 236 247 if (wcount != (ssize_t)wresid) { 237 warn(p Ctx, "write[%zd != %zu]: %s", wcount, wresid,to.p_path);248 warn(pThis->pCtx, "write[%zd != %zu]: %s", wcount, wresid, pThis->to.p_path); 238 249 rval = 1; 239 250 break; … … 241 252 } 242 253 if (rcount < 0) { 243 warn(p Ctx, "read: %s", entp->fts_path);254 warn(pThis->pCtx, "read: %s", entp->fts_path); 244 255 rval = 1; 245 256 } … … 253 264 */ 254 265 255 if (p flag && setfile(pCtx, fs, to_fd))266 if (pThis->pflag && copy_file_attribs(pThis, fs, to_fd)) 256 267 rval = 1; 257 268 (void)close(from_fd); 258 269 if (close(to_fd)) { 259 warn(p Ctx, "close: %s",to.p_path);270 warn(pThis->pCtx, "close: %s", pThis->to.p_path); 260 271 rval = 1; 261 272 } … … 264 275 265 276 int 266 copy_link( PKMKBUILTINCTX pCtx, const FTSENT *p, int exists)277 copy_link(CPUTILSINSTANCE *pThis, const FTSENT *p, int exists) 267 278 { 268 279 int len; … … 270 281 271 282 if ((len = readlink(p->fts_path, llink, sizeof(llink) - 1)) == -1) { 272 warn(p Ctx, "readlink: %s", p->fts_path);283 warn(pThis->pCtx, "readlink: %s", p->fts_path); 273 284 return (1); 274 285 } 275 286 llink[len] = '\0'; 276 if (exists && unlink( to.p_path)) {277 warn(p Ctx, "unlink: %s",to.p_path);278 return (1); 279 } 280 if (symlink(llink, to.p_path)) {281 warn(p Ctx, "symlink: %s", llink);282 return (1); 283 } 284 return (p flag ? setfile(pCtx, p->fts_statp, -1) : 0);285 } 286 287 int 288 copy_fifo( PKMKBUILTINCTX pCtx, struct stat *from_stat, int exists)289 { 290 if (exists && unlink( to.p_path)) {291 warn(p Ctx, "unlink: %s",to.p_path);292 return (1); 293 } 294 if (mkfifo( to.p_path, from_stat->st_mode)) {295 warn(p Ctx, "mkfifo: %s",to.p_path);296 return (1); 297 } 298 return (p flag ? setfile(pCtx, from_stat, -1) : 0);299 } 300 301 int 302 copy_special( PKMKBUILTINCTX pCtx, struct stat *from_stat, int exists)303 { 304 if (exists && unlink( to.p_path)) {305 warn(p Ctx, "unlink: %s",to.p_path);306 return (1); 307 } 308 if (mknod( to.p_path, from_stat->st_mode, from_stat->st_rdev)) {309 warn(p Ctx, "mknod: %s",to.p_path);310 return (1); 311 } 312 return (p flag ? setfile(pCtx, from_stat, -1) : 0);313 } 314 315 int 316 setfile(PKMKBUILTINCTX pCtx, struct stat *fs, int fd)317 { 318 staticstruct timeval tv[2];287 if (exists && unlink(pThis->to.p_path)) { 288 warn(pThis->pCtx, "unlink: %s", pThis->to.p_path); 289 return (1); 290 } 291 if (symlink(llink, pThis->to.p_path)) { 292 warn(pThis->pCtx, "symlink: %s", llink); 293 return (1); 294 } 295 return (pThis->pflag ? copy_file_attribs(pThis, p->fts_statp, -1) : 0); 296 } 297 298 int 299 copy_fifo(CPUTILSINSTANCE *pThis, struct stat *from_stat, int exists) 300 { 301 if (exists && unlink(pThis->to.p_path)) { 302 warn(pThis->pCtx, "unlink: %s", pThis->to.p_path); 303 return (1); 304 } 305 if (mkfifo(pThis->to.p_path, from_stat->st_mode)) { 306 warn(pThis->pCtx, "mkfifo: %s", pThis->to.p_path); 307 return (1); 308 } 309 return (pThis->pflag ? copy_file_attribs(pThis, from_stat, -1) : 0); 310 } 311 312 int 313 copy_special(CPUTILSINSTANCE *pThis, struct stat *from_stat, int exists) 314 { 315 if (exists && unlink(pThis->to.p_path)) { 316 warn(pThis->pCtx, "unlink: %s", pThis->to.p_path); 317 return (1); 318 } 319 if (mknod(pThis->to.p_path, from_stat->st_mode, from_stat->st_rdev)) { 320 warn(pThis->pCtx, "mknod: %s", pThis->to.p_path); 321 return (1); 322 } 323 return (pThis->pflag ? copy_file_attribs(pThis, from_stat, -1) : 0); 324 } 325 326 int 327 copy_file_attribs(CPUTILSINSTANCE *pThis, struct stat *fs, int fd) 328 { 329 /*static*/ struct timeval tv[2]; 319 330 struct stat ts; 320 331 int rval, gotstat, islink, fdval; … … 334 345 tv[0].tv_usec = tv[1].tv_usec = 0; 335 346 #endif 336 if (islink ? lutimes( to.p_path, tv) : utimes(to.p_path, tv)) {337 warn(p Ctx, "%sutimes: %s", islink ? "l" : "",to.p_path);347 if (islink ? lutimes(pThis->to.p_path, tv) : utimes(pThis->to.p_path, tv)) { 348 warn(pThis->pCtx, "%sutimes: %s", islink ? "l" : "", pThis->to.p_path); 338 349 rval = 1; 339 350 } 340 351 if (fdval ? fstat(fd, &ts) : 341 (islink ? lstat( to.p_path, &ts) : stat(to.p_path, &ts)))352 (islink ? lstat(pThis->to.p_path, &ts) : stat(pThis->to.p_path, &ts))) 342 353 gotstat = 0; 343 354 else { … … 354 365 if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid) 355 366 if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) : 356 (islink ? lchown( to.p_path, fs->st_uid, fs->st_gid) :357 chown( to.p_path, fs->st_uid, fs->st_gid))) {367 (islink ? lchown(pThis->to.p_path, fs->st_uid, fs->st_gid) : 368 chown(pThis->to.p_path, fs->st_uid, fs->st_gid))) { 358 369 if (errno != EPERM) { 359 warn(p Ctx, "chown: %s",to.p_path);370 warn(pThis->pCtx, "chown: %s", pThis->to.p_path); 360 371 rval = 1; 361 372 } … … 365 376 if (!gotstat || fs->st_mode != ts.st_mode) 366 377 if (fdval ? fchmod(fd, fs->st_mode) : 367 (islink ? lchmod( to.p_path, fs->st_mode) :368 chmod( to.p_path, fs->st_mode))) {369 warn(p Ctx, "chmod: %s",to.p_path);378 (islink ? lchmod(pThis->to.p_path, fs->st_mode) : 379 chmod(pThis->to.p_path, fs->st_mode))) { 380 warn(pThis->pCtx, "chmod: %s", pThis->to.p_path); 370 381 rval = 1; 371 382 } … … 376 387 fchflags(fd, fs->st_flags) : 377 388 (islink ? (errno = ENOSYS) : 378 chflags( to.p_path, fs->st_flags))) {379 warn(p Ctx, "chflags: %s",to.p_path);389 chflags(pThis->to.p_path, fs->st_flags))) { 390 warn(pThis->pCtx, "chflags: %s", pThis->to.p_path); 380 391 rval = 1; 381 392 }
Note:
See TracChangeset
for help on using the changeset viewer.