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)) >= 0)
457 if (state->force)
459 else if (state->interactive)
461 if (astquery(-1, "%s %s? ", state->opname, state->path) < 0 || sh_checksig(state->context))
464 else if (state->op == LN)
466 error(2, "%s: cannot %s existing file", state->path, state->opname);
470 else if (state->force)
478 st.st_uid != state->uid ? "``not owner''" :
480 if (state->interactive)
482 if (astquery(-1, "override protection %s for %s? ", protection, state->path) < 0 || sh_checksig(state->context))
488 error(2, "%s: cannot %s %s protection", state->path, state->opname, protection);
493 switch (state->backup)
498 if (s = strrchr(state->path, '/'))
500 e = state->path;
506 s = state->path;
513 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)
520 if (s != state->path)
522 if (v || state->backup == BAK_number)
524 sfprintf(state->tmp, "%s.%s%d%s", state->path, state->suffix, v + 1, state->suffix);
529 sfprintf(state->tmp, "%s%s", state->path, state->suffix);
531 if (!(s = sfstruse(state->tmp)))
532 error(ERROR_SYSTEM|3, "%s: out of space", state->path);
533 if (rename(state->path, s))
535 error(ERROR_SYSTEM|2, "%s: cannot backup to %s", state->path, s);
540 if (rm && remove(state->path))
542 error(ERROR_SYSTEM|2, "%s: cannot remove", state->path);
549 switch (state->op)
554 if (!rename(ent->fts_path, state->path))
558 else if (!rm && st.st_mode && !remove(state->path))
565 error(ERROR_SYSTEM|2, "%s: cannot rename to %s", ent->fts_path, state->path);
575 if ((n = pathgetlink(ent->fts_path, state->text, sizeof(state->text) - 1)) < 0)
580 state->text[n] = 0;
581 if (pathsetlink(state->text, state->path))
583 error(ERROR_SYSTEM|2, "%s: cannot copy symbolic link to %s", ent->fts_path, state->path);
587 else if (state->op == CP || S_ISREG(ent->fts_statp->st_mode) || S_ISDIR(ent->fts_statp->st_mode))
594 else if ((wfd = open(state->path, st.st_mode ? (state->wflags & ~O_EXCL) : state->wflags, ent->fts_statp->st_mode & state->perm)) < 0)
596 error(ERROR_SYSTEM|2, "%s: cannot write", state->path);
605 error(ERROR_SYSTEM|2, "%s: %s read stream error", ent->fts_path, state->path);
614 error(ERROR_SYSTEM|2, "%s: %s write stream error", ent->fts_path, state->path);
624 if (sfsync(op) || state->sync && fsync(wfd) || sfclose(op))
629 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"));
638 if (mknod(state->path, ent->fts_statp->st_mode, idevice(ent->fts_statp)))
640 error(ERROR_SYSTEM|2, "%s: cannot copy special file to %s", ent->fts_path, state->path);
649 if (state->preserve)
653 if (stat(state->path, &st))
654 error(ERROR_SYSTEM|2, "%s: cannot stat", state->path);
657 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))
658 error(ERROR_SYSTEM|2, "%s: cannot reset mode to %s", state->path, fmtmode(st.st_mode & state->perm, 0) + 1);
659 if (state->preserve & (PRESERVE_IDS|PRESERVE_TIME))
660 preserve(state, state->path, &st, ent->fts_statp);
663 if (state->op == MV && remove(ent->fts_path))
668 if ((*state->link)(ent->fts_path, state->path))
669 error(ERROR_SYSTEM|2, "%s: cannot link to %s", ent->fts_path, state->path);
688 State_t* state;
693 if (!(sh = CMD_CONTEXT(context)) || !(state = (State_t*)sh->ptr))
695 if (!(state = newof(0, State_t, 1, 0)))
698 sh->ptr = state;
701 memset(state, 0, offsetof(State_t, INITSTATE));
702 state->context = context;
703 state->presiz = -1;
705 state->flags = FTS_NOCHDIR|FTS_NOSEEDOTDIR;
706 state->uid = geteuid();
707 state->wflags = O_WRONLY|O_CREAT|O_TRUNC|O_BINARY;
708 if (!state->tmp && !(state->tmp = sfstropen()))
710 sfputr(state->tmp, usage_head, -1);
716 sfputr(state->tmp, usage_cp, -1);
717 state->op = CP;
718 state->stat = stat;
723 sfputr(state->tmp, usage_ln, -1);
724 state->op = LN;
725 state->flags |= FTS_PHYSICAL;
726 state->link = link;
727 state->remove = 1;
728 state->stat = lstat;
733 sfputr(state->tmp, usage_mv, -1);
734 state->op = MV;
735 state->flags |= FTS_PHYSICAL;
736 state->preserve = PRESERVE_IDS|PRESERVE_PERM|PRESERVE_TIME;
737 state->stat = lstat;
744 sfputr(state->tmp, usage_tail, -1);
745 if (!(usage = sfstruse(state->tmp)))
746 error(ERROR_SYSTEM|3, "%s: out of space", state->path);
747 state->opname = state->op == CP ? ERROR_translate(0, 0, 0, "overwrite") : ERROR_translate(0, 0, 0, "replace");
753 state->flags |= FTS_PHYSICAL;
754 state->preserve = PRESERVE_IDS|PRESERVE_PERM|PRESERVE_TIME;
755 state->recursive = 1;
767 state->preserve |= PRESERVE_IDS|PRESERVE_PERM|PRESERVE_TIME;
770 state->preserve |= PRESERVE_IDS;
773 state->preserve |= PRESERVE_PERM;
776 state->preserve |= PRESERVE_TIME;
786 state->backup = 1;
789 state->force = 1;
790 if (state->op != CP || !standard)
791 state->interactive = 0;
794 state->hierarchy = 1;
797 state->interactive = 1;
798 if (state->op != CP || !standard)
799 state->force = 0;
802 state->op = LN;
803 state->link = link;
804 state->stat = lstat;
807 state->preserve = PRESERVE_IDS|PRESERVE_PERM|PRESERVE_TIME;
810 state->recursive = 1;
815 state->op = LN;
816 state->link = pathsetlink;
817 state->stat = lstat;
820 state->update = 1;
823 state->verbose = 1;
826 state->flags |= FTS_XDEV;
830 state->backup = 1;
834 state->sync = 1;
840 state->flags |= FTS_META|FTS_PHYSICAL;
844 state->flags &= ~FTS_PHYSICAL;
848 state->flags &= ~FTS_META;
849 state->flags |= FTS_PHYSICAL;
853 state->recursive = 1;
854 state->flags &= ~FTS_META;
855 state->flags |= FTS_PHYSICAL;
859 state->suffix = opt_info.arg;
862 state->remove = 1;
886 state->wflags |= O_EXCL;
893 if (state->backup)
896 state->backup = 0;
908 state->backup = BAK_existing;
920 state->backup = BAK_simple;
928 state->backup = 0;
936 state->backup = BAK_number;
943 if (!state->suffix && !(state->suffix = getenv("SIMPLE_BACKUP_SUFFIX")))
944 state->suffix = "~";
945 state->suflen = strlen(state->suffix);
950 state->flags |= fts_flags();
962 if (!(state->directory = !stat(file, &st) && S_ISDIR(st.st_mode)) && argc > 1)
964 if (s && !state->directory)
966 if ((state->fs3d = fs3d(FS3D_TEST)) && strmatch(file, "...|*/...|.../*"))
967 state->official = 1;
968 state->postsiz = strlen(file);
969 if (state->pathsiz < roundof(state->postsiz + 2, PATH_CHUNK) && !(state->path = newof(state->path, char, state->pathsiz = roundof(state->postsiz + 2, PATH_CHUNK), 0)))
971 memcpy(state->path, file, state->postsiz + 1);
972 if (state->directory && state->path[state->postsiz - 1] != '/')
973 state->path[state->postsiz++] = '/';
974 if (state->hierarchy)
976 if (!state->directory)
978 state->missmode = st.st_mode;
980 state->perm = state->uid ? S_IPERM : (S_IPERM & ~S_ISVTX);
981 if (!state->recursive)
982 state->flags |= FTS_TOP;
983 if (fts = fts_open(argv, state->flags, NiL))
985 while (!sh_checksig(context) && (ent = fts_read(fts)) && !visit(state, ent));
988 else if (state->link != pathsetlink)
989 switch (state->op)
1001 else if ((*state->link)(*argv, state->path))
1002 error(ERROR_SYSTEM|2, "%s: cannot link to %s", *argv, state->path);
1005 if (state->path)
1006 free(state->path);
1007 free(state);