Lines Matching refs:state

157 typedef struct State_s			/* program state		*/
171 int postsiz; /* state.path post index */
172 int presiz; /* state.path pre index */
176 int suflen; /* strlen(state.suffix) */
186 #define INITSTATE pathsiz /* (re)init state before this */
187 int pathsiz; /* state.path buffer size */
191 char* opname; /* state.op message string */
206 preserve(State_t* state, const char* path, struct stat* ns, struct stat* os)
210 if ((state->preserve & PRESERVE_TIME) && tmxtouch(path, tmxgetatime(os), tmxgetmtime(os), TMX_NOTIME, 0))
212 if (state->preserve & PRESERVE_IDS)
215 if (n && chown(state->path, os->st_uid, os->st_gid))
232 * visit a single file and state.op to the destination
236 visit(State_t* state, register FTSENT* ent)
265 if (state->hierarchy)
266 state->presiz = -1;
269 state->presiz = ent->fts_pathlen;
273 state->presiz--;
275 state->presiz -= base - ent->fts_name;
282 if (state->directory)
283 state->presiz -= len + 1;
288 base = ent->fts_path + state->presiz + 1;
289 len = ent->fts_pathlen - state->presiz - 1;
292 if (state->directory)
294 if ((state->postsiz + len) > state->pathsiz && !(state->path = newof(state->path, char, state->pathsiz = roundof(state->postsiz + len, PATH_CHUNK), 0)))
296 if (state->hierarchy && ent->fts_level == 0 && strchr(base, '/'))
298 s = state->path + state->postsiz;
303 if (access(state->path, F_OK))
305 st.st_mode = state->missmode;
309 stat(state->path, &st);
312 if (mkdir(state->path, st.st_mode & S_IPERM))
314 error(ERROR_SYSTEM|2, "%s: cannot create directory -- %s ignored", state->path, ent->fts_path);
327 if (state->preserve && state->op != LN || ent->fts_level > 0 && (ent->fts_statp->st_mode & S_IRWXU) != S_IRWXU)
330 memcpy(state->path + state->postsiz, base, len);
332 state->path[state->postsiz] = 0;
333 if (stat(state->path, &st))
334 error(ERROR_SYSTEM|2, "%s: cannot stat", state->path);
337 if ((ent->fts_statp->st_mode & S_IPERM) != (st.st_mode & S_IPERM) && chmod(state->path, ent->fts_statp->st_mode & S_IPERM))
338 error(ERROR_SYSTEM|2, "%s: cannot reset directory mode to %s", state->path, fmtmode(st.st_mode & S_IPERM, 0) + 1);
339 if (state->preserve & (PRESERVE_IDS|PRESERVE_TIME))
340 preserve(state, state->path, &st, ent->fts_statp);
347 if (!state->recursive)
350 if (state->op == CP)
352 else if (state->link == link && !state->force)
369 if (state->directory)
370 memcpy(state->path + state->postsiz, base, len);
371 if (!(*state->stat)(state->path, &st))
375 error(2, "%s: not a directory -- %s ignored", state->path, ent->fts_path);
379 else if (mkdir(state->path, (ent->fts_statp->st_mode & S_IPERM)|(ent->fts_info == FTS_D ? S_IRWXU : 0)))
381 error(ERROR_SYSTEM|2, "%s: cannot create directory -- %s ignored", state->path, ent->fts_path);
384 if (!state->directory)
386 state->directory = 1;
387 state->path[state->postsiz++] = '/';
388 state->presiz--;
396 if (state->link != pathsetlink)
404 if (state->op == CP)
412 if (state->directory)
413 memcpy(state->path + state->postsiz, base, len);
414 if ((*state->stat)(state->path, &st))
416 else if (state->update && !S_ISDIR(st.st_mode) && (unsigned long)ent->fts_statp->st_mtime < (unsigned long)st.st_mtime)
421 else if (!state->fs3d || !iview(&st))
427 if (state->op != LN && st.st_dev == ent->fts_statp->st_dev && st.st_ino == ent->fts_statp->st_ino)
429 if (state->op == MV)
435 if (state->verbose)
436 sfputr(sfstdout, state->path, '\n');
439 if (!state->official)
440 error(2, "%s: identical to %s", state->path, ent->fts_path);
445 error(2, "%s: cannot %s existing directory", state->path, state->opname);
448 if (state->verbose)
449 sfputr(sfstdout, state->path, '\n');
450 rm = state->remove || ent->fts_info == FTS_SL;
451 if (!rm || !state->force)
453 if (S_ISLNK(st.st_mode) && (n = -1) || (n = open(state->path, O_RDWR|O_BINARY|O_cloexec)) >= 0)
457 if (state->force)
459 else if (state->interactive)
461 if ((n = astquery(-1, "%s %s? ", state->opname, state->path)) < 0 || sh_checksig(state->context))
466 else if (state->op == LN)
468 error(2, "%s: cannot %s existing file", state->path, state->opname);
472 else if (state->force)
480 st.st_uid != state->uid ? "``not owner''" :
482 if (state->interactive)
484 if ((n = astquery(-1, "override protection %s for %s? ", protection, state->path)) < 0 || sh_checksig(state->context))
492 error(2, "%s: cannot %s %s protection", state->path, state->opname, protection);
497 switch (state->backup)
502 if (s = strrchr(state->path, '/'))
504 e = state->path;
510 s = state->path;
517 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)
524 if (s != state->path)
526 if (v || state->backup == BAK_number)
528 sfprintf(state->tmp, "%s.%s%d%s", state->path, state->suffix, v + 1, state->suffix);
533 sfprintf(state->tmp, "%s%s", state->path, state->suffix);
535 if (!(s = sfstruse(state->tmp)))
536 error(ERROR_SYSTEM|3, "%s: out of space", state->path);
537 if (rename(state->path, s))
539 error(ERROR_SYSTEM|2, "%s: cannot backup to %s", state->path, s);
544 if (rm && remove(state->path))
546 error(ERROR_SYSTEM|2, "%s: cannot remove", state->path);
553 switch (state->op)
558 if (!rename(ent->fts_path, state->path))
562 else if (!rm && st.st_mode && !remove(state->path))
569 error(ERROR_SYSTEM|2, "%s: cannot rename to %s", ent->fts_path, state->path);
579 if ((n = pathgetlink(ent->fts_path, state->text, sizeof(state->text) - 1)) < 0)
584 state->text[n] = 0;
585 if (pathsetlink(state->text, state->path))
587 error(ERROR_SYSTEM|2, "%s: cannot copy symbolic link to %s", ent->fts_path, state->path);
591 else if (state->op == CP || S_ISREG(ent->fts_statp->st_mode) || S_ISDIR(ent->fts_statp->st_mode))
598 else if ((wfd = open(state->path, (st.st_mode ? (state->wflags & ~O_EXCL) : state->wflags)|O_cloexec, ent->fts_statp->st_mode & state->perm)) < 0)
600 error(ERROR_SYSTEM|2, "%s: cannot write", state->path);
609 error(ERROR_SYSTEM|2, "%s: %s read stream error", ent->fts_path, state->path);
616 error(ERROR_SYSTEM|2, "%s: %s write stream error", ent->fts_path, state->path);
626 if (sfsync(op) || state->sync && fsync(wfd) || sfclose(op))
632 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"));
641 if (mknod(state->path, ent->fts_statp->st_mode, idevice(ent->fts_statp)))
643 error(ERROR_SYSTEM|2, "%s: cannot copy special file to %s", ent->fts_path, state->path);
652 if (state->preserve)
656 if (stat(state->path, &st))
657 error(ERROR_SYSTEM|2, "%s: cannot stat", state->path);
660 if ((state->preserve & PRESERVE_PERM) && (ent->fts_statp->st_mode & state->perm) != (st.st_mode & state->perm) && chmod(state->path, ent->fts_statp->st_mode & state->perm))
661 error(ERROR_SYSTEM|2, "%s: cannot reset mode to %s", state->path, fmtmode(st.st_mode & state->perm, 0) + 1);
662 if (state->preserve & (PRESERVE_IDS|PRESERVE_TIME))
663 preserve(state, state->path, &st, ent->fts_statp);
666 if (state->op == MV && remove(ent->fts_path))
671 if ((*state->link)(ent->fts_path, state->path))
672 error(ERROR_SYSTEM|2, "%s: cannot link to %s", ent->fts_path, state->path);
691 State_t* state;
696 if (!(sh = CMD_CONTEXT(context)) || !(state = (State_t*)sh->ptr))
698 if (!(state = newof(0, State_t, 1, 0)))
701 sh->ptr = state;
704 memset(state, 0, offsetof(State_t, INITSTATE));
705 state->context = context;
706 state->presiz = -1;
708 state->flags = FTS_NOCHDIR|FTS_NOSEEDOTDIR;
709 state->uid = geteuid();
710 state->wflags = O_WRONLY|O_CREAT|O_TRUNC|O_BINARY;
711 if (!state->tmp && !(state->tmp = sfstropen()))
713 sfputr(state->tmp, usage_head, -1);
719 sfputr(state->tmp, usage_cp, -1);
720 state->op = CP;
721 state->stat = stat;
726 sfputr(state->tmp, usage_ln, -1);
727 state->op = LN;
728 state->flags |= FTS_PHYSICAL;
729 state->link = link;
730 state->remove = 1;
731 state->stat = lstat;
736 sfputr(state->tmp, usage_mv, -1);
737 state->op = MV;
738 state->flags |= FTS_PHYSICAL;
739 state->preserve = PRESERVE_IDS|PRESERVE_PERM|PRESERVE_TIME;
740 state->stat = lstat;
747 sfputr(state->tmp, usage_tail, -1);
748 if (!(usage = sfstruse(state->tmp)))
749 error(ERROR_SYSTEM|3, "%s: out of space", state->path);
750 state->opname = state->op == CP ? ERROR_translate(0, 0, 0, "overwrite") : ERROR_translate(0, 0, 0, "replace");
756 state->flags |= FTS_PHYSICAL;
757 state->preserve = PRESERVE_IDS|PRESERVE_PERM|PRESERVE_TIME;
758 state->recursive = 1;
770 state->preserve |= PRESERVE_IDS|PRESERVE_PERM|PRESERVE_TIME;
773 state->preserve |= PRESERVE_IDS;
776 state->preserve |= PRESERVE_PERM;
779 state->preserve |= PRESERVE_TIME;
789 state->backup = 1;
792 state->force = 1;
793 if (state->op != CP || !standard)
794 state->interactive = 0;
797 state->hierarchy = 1;
800 state->interactive = 1;
801 if (state->op != CP || !standard)
802 state->force = 0;
805 state->op = LN;
806 state->link = link;
807 state->stat = lstat;
810 state->preserve = PRESERVE_IDS|PRESERVE_PERM|PRESERVE_TIME;
813 state->recursive = 1;
818 state->op = LN;
819 state->link = pathsetlink;
820 state->stat = lstat;
823 state->update = 1;
826 state->verbose = 1;
829 state->flags |= FTS_XDEV;
833 state->backup = 1;
837 state->sync = 1;
843 state->flags |= FTS_META|FTS_PHYSICAL;
847 state->flags &= ~FTS_PHYSICAL;
851 state->flags &= ~FTS_META;
852 state->flags |= FTS_PHYSICAL;
856 state->recursive = 1;
857 state->flags &= ~FTS_META;
858 state->flags |= FTS_PHYSICAL;
862 state->suffix = opt_info.arg;
865 state->remove = 1;
889 state->wflags |= O_EXCL;
896 if (state->backup)
899 state->backup = 0;
911 state->backup = BAK_existing;
923 state->backup = BAK_simple;
931 state->backup = 0;
939 state->backup = BAK_number;
946 if (!state->suffix && !(state->suffix = getenv("SIMPLE_BACKUP_SUFFIX")))
947 state->suffix = "~";
948 state->suflen = strlen(state->suffix);
953 state->flags |= fts_flags() | FTS_META;
965 if (!(state->directory = !stat(file, &st) && S_ISDIR(st.st_mode)) && argc > 1)
967 if (s && !state->directory)
969 if ((state->fs3d = fs3d(FS3D_TEST)) && strmatch(file, "...|*/...|.../*"))
970 state->official = 1;
971 state->postsiz = strlen(file);
972 if (state->pathsiz < roundof(state->postsiz + 2, PATH_CHUNK) && !(state->path = newof(state->path, char, state->pathsiz = roundof(state->postsiz + 2, PATH_CHUNK), 0)))
974 memcpy(state->path, file, state->postsiz + 1);
975 if (state->directory && state->path[state->postsiz - 1] != '/')
976 state->path[state->postsiz++] = '/';
977 if (state->hierarchy)
979 if (!state->directory)
981 state->missmode = st.st_mode;
983 state->perm = state->uid ? S_IPERM : (S_IPERM & ~S_ISVTX);
984 if (!state->recursive)
985 state->flags |= FTS_TOP;
986 if (fts = fts_open(argv, state->flags, NiL))
988 while (!sh_checksig(context) && (ent = fts_read(fts)) && !visit(state, ent));
991 else if (state->link != pathsetlink)
992 switch (state->op)
1004 else if ((*state->link)(*argv, state->path))
1005 error(ERROR_SYSTEM|2, "%s: cannot link to %s", *argv, state->path);
1008 if (state->path)
1009 free(state->path);
1010 free(state);