Lines Matching refs:state

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