Lines Matching refs:state
191 preserve(State_t* state, const char* path, struct stat* ns, struct stat* os) in preserve() argument
198 if (n && chown(state->path, os->st_uid, os->st_gid)) in preserve()
218 visit(State_t* state, register FTSENT* ent) in visit() argument
247 if (state->hierarchy) in visit()
248 state->presiz = -1; in visit()
251 state->presiz = ent->fts_pathlen; in visit()
255 state->presiz--; in visit()
257 state->presiz -= base - ent->fts_name; in visit()
264 if (state->directory) in visit()
265 state->presiz -= len + 1; in visit()
270 base = ent->fts_path + state->presiz + 1; in visit()
271 len = ent->fts_pathlen - state->presiz - 1; in visit()
274 if (state->directory) in visit()
276 …if ((state->postsiz + len) > state->pathsiz && !(state->path = newof(state->path, char, state->pat… in visit()
278 if (state->hierarchy && ent->fts_level == 0 && strchr(base, '/')) in visit()
280 s = state->path + state->postsiz; in visit()
285 if (access(state->path, F_OK)) in visit()
287 st.st_mode = state->missmode; in visit()
291 stat(state->path, &st); in visit()
294 if (mkdir(state->path, st.st_mode & S_IPERM)) in visit()
296 … error(ERROR_SYSTEM|2, "%s: cannot create directory -- %s ignored", state->path, ent->fts_path); in visit()
309 …if (state->preserve && state->op != LN || ent->fts_level > 0 && (ent->fts_statp->st_mode & S_IRWXU… in visit()
312 memcpy(state->path + state->postsiz, base, len); in visit()
314 state->path[state->postsiz] = 0; in visit()
315 if (stat(state->path, &st)) in visit()
316 error(ERROR_SYSTEM|2, "%s: cannot stat", state->path); in visit()
319 …if ((ent->fts_statp->st_mode & S_IPERM) != (st.st_mode & S_IPERM) && chmod(state->path, ent->fts_s… in visit()
320 …error(ERROR_SYSTEM|2, "%s: cannot reset directory mode to %s", state->path, fmtmode(st.st_mode & S… in visit()
321 if (state->preserve) in visit()
322 preserve(state, state->path, &st, ent->fts_statp); in visit()
329 if (!state->recursive) in visit()
332 if (state->op == CP) in visit()
334 else if (state->link == link && !state->force) in visit()
351 if (state->directory) in visit()
352 memcpy(state->path + state->postsiz, base, len); in visit()
353 if (!(*state->stat)(state->path, &st)) in visit()
357 error(2, "%s: not a directory -- %s ignored", state->path, ent->fts_path); in visit()
361 …else if (mkdir(state->path, (ent->fts_statp->st_mode & S_IPERM)|(ent->fts_info == FTS_D ? S_IRWXU … in visit()
363 error(ERROR_SYSTEM|2, "%s: cannot create directory -- %s ignored", state->path, ent->fts_path); in visit()
366 if (!state->directory) in visit()
368 state->directory = 1; in visit()
369 state->path[state->postsiz++] = '/'; in visit()
370 state->presiz--; in visit()
378 if (state->link != pathsetlink) in visit()
386 if (state->op == CP) in visit()
394 if (state->directory) in visit()
395 memcpy(state->path + state->postsiz, base, len); in visit()
396 if ((*state->stat)(state->path, &st)) in visit()
398 …else if (state->update && !S_ISDIR(st.st_mode) && (unsigned long)ent->fts_statp->st_mtime < (unsig… in visit()
403 else if (!state->fs3d || !iview(&st)) in visit()
411 if (state->op == MV) in visit()
417 if (state->verbose) in visit()
418 sfputr(sfstdout, state->path, '\n'); in visit()
421 if (!state->official) in visit()
422 error(2, "%s: identical to %s", state->path, ent->fts_path); in visit()
427 error(2, "%s: cannot %s existing directory", state->path, state->opname); in visit()
430 if (state->verbose) in visit()
431 sfputr(sfstdout, state->path, '\n'); in visit()
432 rm = state->op == LN || ent->fts_info == FTS_SL; in visit()
433 if (!rm || !state->force) in visit()
435 if ((n = open(state->path, O_RDWR|O_BINARY)) >= 0) in visit()
438 if (state->force) in visit()
440 else if (state->interactive) in visit()
442 if (astquery(-1, "%s %s? ", state->opname, state->path) < 0 || sh_checksig(state->context)) in visit()
445 else if (state->op == LN) in visit()
447 error(2, "%s: cannot %s existing file", state->path, state->opname); in visit()
451 else if (state->force) in visit()
459 st.st_uid != state->uid ? "``not owner''" : in visit()
461 if (state->interactive) in visit()
463 …tquery(-1, "override protection %s for %s? ", protection, state->path) < 0 || sh_checksig(state->c… in visit()
469 error(2, "%s: cannot %s %s protection", state->path, state->opname, protection); in visit()
474 switch (state->backup) in visit()
479 if (s = strrchr(state->path, '/')) in visit()
481 e = state->path; in visit()
487 s = state->path; in visit()
494 …b->fts_name + n + 1, state->suffix, state->suflen) && (m = strtol(sub->fts_name + n + state->sufle… in visit()
501 if (s != state->path) in visit()
503 if (v || state->backup == BAK_number) in visit()
505 sfprintf(state->tmp, "%s.%s%d%s", state->path, state->suffix, v + 1, state->suffix); in visit()
510 sfprintf(state->tmp, "%s%s", state->path, state->suffix); in visit()
512 if (!(s = sfstruse(state->tmp))) in visit()
513 error(ERROR_SYSTEM|3, "%s: out of space", state->path); in visit()
514 if (rename(state->path, s)) in visit()
516 error(ERROR_SYSTEM|2, "%s: cannot backup to %s", state->path, s); in visit()
521 if (rm && remove(state->path)) in visit()
523 error(ERROR_SYSTEM|2, "%s: cannot remove", state->path); in visit()
530 switch (state->op) in visit()
535 if (!rename(ent->fts_path, state->path)) in visit()
539 else if (!rm && st.st_mode && !remove(state->path)) in visit()
546 error(ERROR_SYSTEM|2, "%s: cannot rename to %s", ent->fts_path, state->path); in visit()
556 if ((n = pathgetlink(ent->fts_path, state->text, sizeof(state->text) - 1)) < 0) in visit()
561 state->text[n] = 0; in visit()
562 if (pathsetlink(state->text, state->path)) in visit()
564 error(ERROR_SYSTEM|2, "%s: cannot copy symbolic link to %s", ent->fts_path, state->path); in visit()
568 else if (state->op == CP || S_ISREG(ent->fts_statp->st_mode) || S_ISDIR(ent->fts_statp->st_mode)) in visit()
575 …else if ((wfd = open(state->path, st.st_mode ? (state->wflags & ~O_EXCL) : state->wflags, ent->fts… in visit()
577 error(ERROR_SYSTEM|2, "%s: cannot write", state->path); in visit()
586 error(ERROR_SYSTEM|2, "%s: %s read stream error", ent->fts_path, state->path); in visit()
595 error(ERROR_SYSTEM|2, "%s: %s write stream error", ent->fts_path, state->path); in visit()
605 if (sfsync(op) || state->sync && fsync(wfd) || sfclose(op)) in visit()
610 …error(ERROR_SYSTEM|2, "%s: %s %s error", ent->fts_path, state->path, n == 1 ? ERROR_translate(0, 0… in visit()
619 if (mknod(state->path, ent->fts_statp->st_mode, idevice(ent->fts_statp))) in visit()
621 error(ERROR_SYSTEM|2, "%s: cannot copy special file to %s", ent->fts_path, state->path); in visit()
630 if (state->preserve) in visit()
634 if (stat(state->path, &st)) in visit()
635 error(ERROR_SYSTEM|2, "%s: cannot stat", state->path); in visit()
638 …((ent->fts_statp->st_mode & state->perm) != (st.st_mode & state->perm) && chmod(state->path, ent->… in visit()
639 …error(ERROR_SYSTEM|2, "%s: cannot reset mode to %s", state->path, fmtmode(st.st_mode & state->perm… in visit()
640 preserve(state, state->path, &st, ent->fts_statp); in visit()
643 if (state->op == MV && remove(ent->fts_path)) in visit()
648 if ((*state->link)(ent->fts_path, state->path)) in visit()
649 error(ERROR_SYSTEM|2, "%s: cannot link to %s", ent->fts_path, state->path); in visit()
668 State_t* state; in b_cp() local
673 if (!(sh = CMD_CONTEXT(context)) || !(state = (State_t*)sh->ptr)) in b_cp()
675 if (!(state = newof(0, State_t, 1, 0))) in b_cp()
678 sh->ptr = state; in b_cp()
681 memset(state, 0, offsetof(State_t, INITSTATE)); in b_cp()
682 state->context = context; in b_cp()
683 state->presiz = -1; in b_cp()
685 state->flags = FTS_NOCHDIR|FTS_NOSEEDOTDIR; in b_cp()
686 state->uid = geteuid(); in b_cp()
687 state->wflags = O_WRONLY|O_CREAT|O_TRUNC|O_BINARY; in b_cp()
688 if (!state->tmp && !(state->tmp = sfstropen())) in b_cp()
690 sfputr(state->tmp, usage_head, -1); in b_cp()
696 sfputr(state->tmp, usage_cp, -1); in b_cp()
697 state->op = CP; in b_cp()
698 state->stat = stat; in b_cp()
703 sfputr(state->tmp, usage_ln, -1); in b_cp()
704 state->op = LN; in b_cp()
705 state->flags |= FTS_PHYSICAL; in b_cp()
706 state->link = link; in b_cp()
707 state->stat = lstat; in b_cp()
712 sfputr(state->tmp, usage_mv, -1); in b_cp()
713 state->op = MV; in b_cp()
714 state->flags |= FTS_PHYSICAL; in b_cp()
715 state->preserve = 1; in b_cp()
716 state->stat = lstat; in b_cp()
723 sfputr(state->tmp, usage_tail, -1); in b_cp()
724 if (!(usage = sfstruse(state->tmp))) in b_cp()
725 error(ERROR_SYSTEM|3, "%s: out of space", state->path); in b_cp()
726 …state->opname = state->op == CP ? ERROR_translate(0, 0, 0, "overwrite") : ERROR_translate(0, 0, 0,… in b_cp()
732 state->flags |= FTS_PHYSICAL; in b_cp()
733 state->preserve = 1; in b_cp()
734 state->recursive = 1; in b_cp()
738 state->backup = 1; in b_cp()
741 state->force = 1; in b_cp()
742 if (state->op != CP || !standard) in b_cp()
743 state->interactive = 0; in b_cp()
746 state->hierarchy = 1; in b_cp()
749 state->interactive = 1; in b_cp()
750 if (state->op != CP || !standard) in b_cp()
751 state->force = 0; in b_cp()
754 state->op = LN; in b_cp()
755 state->link = link; in b_cp()
756 state->stat = lstat; in b_cp()
759 state->preserve = 1; in b_cp()
762 state->recursive = 1; in b_cp()
767 state->op = LN; in b_cp()
768 state->link = pathsetlink; in b_cp()
769 state->stat = lstat; in b_cp()
772 state->update = 1; in b_cp()
775 state->verbose = 1; in b_cp()
778 state->flags |= FTS_XDEV; in b_cp()
782 state->sync = 1; in b_cp()
788 state->flags |= FTS_META|FTS_PHYSICAL; in b_cp()
792 state->flags &= ~FTS_PHYSICAL; in b_cp()
796 state->flags &= ~FTS_META; in b_cp()
797 state->flags |= FTS_PHYSICAL; in b_cp()
801 state->recursive = 1; in b_cp()
802 state->flags &= ~FTS_META; in b_cp()
803 state->flags |= FTS_PHYSICAL; in b_cp()
807 state->suffix = opt_info.arg; in b_cp()
834 state->wflags |= O_EXCL; in b_cp()
841 if (state->backup) in b_cp()
844 state->backup = BAK_existing; in b_cp()
856 state->backup = BAK_existing; in b_cp()
868 state->backup = BAK_simple; in b_cp()
876 state->backup = BAK_number; in b_cp()
883 if (!state->suffix && !(state->suffix = getenv("SIMPLE_BACKUP_SUFFIX"))) in b_cp()
884 state->suffix = "~"; in b_cp()
885 state->suflen = strlen(state->suffix); in b_cp()
890 state->flags |= fts_flags(); in b_cp()
902 if (!(state->directory = !stat(file, &st) && S_ISDIR(st.st_mode)) && argc > 1) in b_cp()
904 if (s && !state->directory) in b_cp()
906 if ((state->fs3d = fs3d(FS3D_TEST)) && strmatch(file, "...|*/...|.../*")) in b_cp()
907 state->official = 1; in b_cp()
908 state->postsiz = strlen(file); in b_cp()
909 …if (state->pathsiz < roundof(state->postsiz + 2, PATH_CHUNK) && !(state->path = newof(state->path,… in b_cp()
911 memcpy(state->path, file, state->postsiz + 1); in b_cp()
912 if (state->directory && state->path[state->postsiz - 1] != '/') in b_cp()
913 state->path[state->postsiz++] = '/'; in b_cp()
914 if (state->hierarchy) in b_cp()
916 if (!state->directory) in b_cp()
918 state->missmode = st.st_mode; in b_cp()
920 state->perm = state->uid ? S_IPERM : (S_IPERM & ~S_ISVTX); in b_cp()
921 if (!state->recursive) in b_cp()
922 state->flags |= FTS_TOP; in b_cp()
923 if (fts = fts_open(argv, state->flags, NiL)) in b_cp()
925 while (!sh_checksig(context) && (ent = fts_read(fts)) && !visit(state, ent)); in b_cp()
928 else if (state->link != pathsetlink) in b_cp()
929 switch (state->op) in b_cp()
941 else if ((*state->link)(*argv, state->path)) in b_cp()
942 error(ERROR_SYSTEM|2, "%s: cannot link to %s", *argv, state->path); in b_cp()
945 if (state->path) in b_cp()
946 free(state->path); in b_cp()
947 free(state); in b_cp()