"Fossies" - the Fresh Open Source Software Archive

Member "schily-2021-09-18/star_sym/star_sym.c" (20 Aug 2021, 10394 Bytes) of package /linux/privat/schily-2021-09-18.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "star_sym.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes reports: 2021-08-14_vs_2021-09-18 or 2021-07-29_vs_2021-09-18.

    1 /* @(#)star_sym.c   1.22 21/08/20 Copyright 2005-2021 J. Schilling */
    2 #include <schily/mconfig.h>
    3 #ifndef lint
    4 static  UConst char sccsid[] =
    5     "@(#)star_sym.c 1.22 21/08/20 Copyright 2005-2021 J. Schilling";
    6 #endif
    7 /*
    8  *  Read in the star inode data base and write a human
    9  *  readable version.
   10  *
   11  *  Copyright (c) 2005-2021 J. Schilling
   12  */
   13 /*
   14  * The contents of this file are subject to the terms of the
   15  * Common Development and Distribution License, Version 1.0 only
   16  * (the "License").  You may not use this file except in compliance
   17  * with the License.
   18  *
   19  * See the file CDDL.Schily.txt in this distribution for details.
   20  * A copy of the CDDL is also available via the Internet at
   21  * http://www.opensource.org/licenses/cddl1.txt
   22  *
   23  * When distributing Covered Code, include this CDDL HEADER in each
   24  * file and include the License file CDDL.Schily.txt from this distribution.
   25  */
   26 
   27 #include <schily/stdio.h>
   28 #include <schily/stdlib.h>
   29 #include <schily/unistd.h>
   30 #include <schily/standard.h>
   31 #include "star.h"
   32 #include "restore.h"
   33 #include "dumpdate.h"
   34 #include <schily/jmpdefs.h> /* To include __jmalloc() */
   35 #include <schily/fcntl.h>
   36 #define GT_COMERR       /* #define comerr gtcomerr */
   37 #define GT_ERROR        /* #define error gterror   */
   38 #include <schily/schily.h>
   39 #include <schily/maxpath.h>
   40 #include <schily/nlsdefs.h>
   41 #include "starsubs.h"
   42 
   43 #ifndef HAVE_LSTAT
   44 #define lstat   stat
   45 #endif
   46 
   47 #if S_ISUID == TSUID && S_ISGID == TSGID && S_ISVTX == TSVTX && \
   48     S_IRUSR == TUREAD && S_IWUSR == TUWRITE && S_IXUSR == TUEXEC && \
   49     S_IRGRP == TGREAD && S_IWGRP == TGWRITE && S_IXGRP == TGEXEC && \
   50     S_IROTH == TOREAD && S_IWOTH == TOWRITE && S_IXOTH == TOEXEC
   51 
   52 #define HAVE_POSIX_MODE_BITS    /* st_mode bits are equal to TAR mode bits */
   53 #endif
   54 
   55 struct star_stats   xstats;     /* for printing statistics  */
   56 
   57 dev_t   curfs = NODEV;          /* Current st_dev for -M option */
   58 char    *vers;              /* the full version string  */
   59 BOOL    force_remove = FALSE;       /* -force-remove on extraction  */
   60 BOOL    remove_recursive = FALSE;   /* -remove-recursive on extract */
   61 BOOL    dopartial = FALSE;      /* -partial in incremental mode */
   62 BOOL    forcerestore = FALSE;       /* -force-restore in incremental mode   */
   63 BOOL    uncond    = FALSE;      /* -U unconditional extract */
   64 BOOL    nowarn    = FALSE;      /* -nowarn has been specified   */
   65 int xdebug    = 0;          /* eXtended debug level     */
   66 GINFO   _grinfo;            /* Global read information  */
   67 GINFO   *grip = &_grinfo;       /* Global read info pointer */
   68 
   69 LOCAL   void    star_mkvers __PR((void));
   70 LOCAL   void    usage       __PR((int ret));
   71 EXPORT  int main        __PR((int ac, char *av[]));
   72 LOCAL   void    make_symtab __PR((int ac, char *av[]));
   73 
   74 
   75 EXPORT int
   76 hdr_type(name)
   77     char    *name;
   78 {
   79     return (-1);
   80 }
   81 
   82 EXPORT BOOL
   83 remove_file(name, isfirst)
   84     register char   *name;
   85         BOOL    isfirst;
   86 {
   87     return (FALSE);
   88 }
   89 
   90 EXPORT BOOL
   91 make_adir(info)
   92     register FINFO  *info;
   93 {
   94     return (FALSE);
   95 }
   96 
   97 #include <schily/stat.h>
   98 EXPORT BOOL
   99 _getinfo(name, info)
  100     char    *name;
  101     register FINFO  *info;
  102 {
  103     struct stat sb;
  104 
  105     if (lstatat(name, &sb, AT_SYMLINK_NOFOLLOW) < 0)
  106         return (FALSE);
  107 
  108     info->f_name = name;
  109     info->f_ino = sb.st_ino;
  110     switch ((int)(sb.st_mode & S_IFMT)) {
  111 
  112     case    S_IFREG:    info->f_filetype = F_FILE; break;
  113     case    S_IFDIR:    info->f_filetype = F_DIR; break;
  114 #ifdef  S_IFLNK
  115     case    S_IFLNK:    info->f_filetype = F_SLINK; break;
  116 #endif
  117 
  118     default:        info->f_filetype = F_SPEC;
  119     }
  120     return (TRUE);
  121 }
  122 
  123 #ifdef  HAVE_POSIX_MODE_BITS    /* st_mode bits are equal to TAR mode bits */
  124 #define OSMODE(tarmode)     (tarmode)
  125 #else
  126 #define OSMODE(tarmode)     ((tarmode & TSUID   ? S_ISUID : 0)  \
  127                 | (tarmode & TSGID   ? S_ISGID : 0) \
  128                 | (tarmode & TSVTX   ? S_ISVTX : 0) \
  129                 | (tarmode & TUREAD  ? S_IRUSR : 0) \
  130                 | (tarmode & TUWRITE ? S_IWUSR : 0) \
  131                 | (tarmode & TUEXEC  ? S_IXUSR : 0) \
  132                 | (tarmode & TGREAD  ? S_IRGRP : 0) \
  133                 | (tarmode & TGWRITE ? S_IWGRP : 0) \
  134                 | (tarmode & TGEXEC  ? S_IXGRP : 0) \
  135                 | (tarmode & TOREAD  ? S_IROTH : 0) \
  136                 | (tarmode & TOWRITE ? S_IWOTH : 0) \
  137                 | (tarmode & TOEXEC  ? S_IXOTH : 0))
  138 #endif
  139 
  140 EXPORT mode_t
  141 #ifdef  PROTOTYPES
  142 osmode(register mode_t tarmode)
  143 #else
  144 osmode(tarmode)
  145     register mode_t     tarmode;
  146 #endif
  147 {
  148     register mode_t     _osmode;
  149 
  150     _osmode = OSMODE(tarmode);
  151     return (_osmode);
  152 }
  153 
  154 
  155 EXPORT char *
  156 hdr_name(type)
  157     int type;
  158 {
  159     return ("XXX");
  160 }
  161 
  162 EXPORT BOOL
  163 create_dirs(name)
  164     register char   *name;
  165 {
  166     return (TRUE);
  167 }
  168 
  169 EXPORT char *
  170 dt_name(type)
  171     int type;
  172 {
  173     return ("unknown");
  174 }
  175 
  176 EXPORT int
  177 dt_type(name)
  178     char    *name;
  179 {
  180     return (DT_UNKN);
  181 }
  182 
  183 EXPORT BOOL
  184 same_symlink(info)
  185     FINFO   *info;
  186 {
  187     return (FALSE);
  188 }
  189 
  190 EXPORT BOOL
  191 same_special(info)
  192     FINFO   *info;
  193 {
  194     return (FALSE);
  195 }
  196 
  197 LOCAL char  strvers[] = "1.5";
  198 LOCAL void
  199 star_mkvers()
  200 {
  201     char    buf[512];
  202 
  203     if (vers != NULL)
  204         return;
  205 
  206     js_snprintf(buf, sizeof (buf),
  207         "%s %s (%s-%s-%s)", "star", strvers, HOST_CPU, HOST_VENDOR, HOST_OS);
  208 
  209     vers = ___savestr(buf);
  210 }
  211 
  212 LOCAL void
  213 usage(ret)
  214     int ret;
  215 {
  216     error("Usage:\t%s [options] < file\n", get_progname());
  217     error("Options:\n");
  218     error("\t-help\t\tprint this help\n");
  219     error("\t-version\tPrint version number.\n");
  220     error("\n%s extracts star incremental restore database\n", get_progname());
  221     exit(ret);
  222     /* NOTREACHED */
  223 }
  224 
  225 EXPORT int
  226 main(ac, av)
  227     int ac;
  228     char    *av[];
  229 {
  230 extern  BOOL        is_star;
  231     int     cac = ac;
  232     char    *const *cav = av;
  233     char        *opt = "help,h,version";
  234     BOOL        help = FALSE;
  235     BOOL        prvers = FALSE;
  236 
  237     is_star = FALSE;
  238     save_args(ac, av);
  239 
  240     (void) setlocale(LC_ALL, "");
  241 
  242 #ifdef  USE_NLS
  243 #if !defined(TEXT_DOMAIN)   /* Should be defined by cc -D */
  244 #define TEXT_DOMAIN "p"     /* Use this only if it weren't */
  245 #endif
  246     { char  *dir;
  247     dir = searchfileinpath("share/locale", F_OK,
  248                     SIP_ANY_FILE|SIP_NO_PATH, NULL);
  249     if (dir)
  250         (void) bindtextdomain(TEXT_DOMAIN, dir);
  251     else
  252 #if defined(PROTOTYPES) && defined(INS_BASE)
  253     (void) bindtextdomain(TEXT_DOMAIN, INS_BASE "/share/locale");
  254 #else
  255     (void) bindtextdomain(TEXT_DOMAIN, "/usr/share/locale");
  256 #endif
  257     (void) textdomain(TEXT_DOMAIN);
  258     }
  259 #endif  /* USE_NLS */
  260 
  261     star_mkvers();
  262 
  263     cac--;
  264     cav++;
  265     if (getallargs(&cac, &cav, opt, &help, &help, &prvers) < 0) {
  266         errmsgno(EX_BAD, "Bad Option: '%s'.\n", cav[0]);
  267         usage(EX_BAD);
  268     }
  269     if (help)
  270         usage(0);
  271     if (prvers) {
  272         printf("%s: %s\n\n", get_progname(), vers);
  273         gtprintf("Copyright (C) 2005-2021 %s\n", _("Jörg Schilling"));
  274         gtprintf("This is free software; see the source for copying conditions.  There is NO\n");
  275         gtprintf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
  276         exit(0);
  277     }
  278 
  279     /*
  280      * As long as all options cause star_sym to exit, we may continue
  281      * with this lazy arg handling.
  282      */
  283     if (ac > 2) {
  284         /*
  285          * Reconstruct star-symtable
  286          * Call:
  287          * star_sym <restore-dir "."> <backup-dir>
  288          */
  289         make_symtab(ac, av);
  290         return (0);
  291     }
  292     if (ac < 2)
  293         sym_open(NULL);     /* dump ./star-symtable */
  294     else
  295         sym_open(av[1]);    /* sump named symtable */
  296     printLsym(stderr);
  297     return (0);
  298 }
  299 
  300 /* ------------------------------------------------------------------------- */
  301 #include <schily/nlsdefs.h>
  302 
  303 #include <schily/walk.h>
  304 #include <schily/find.h>
  305 
  306 
  307 LOCAL   int walkfunc    __PR((char *nm, struct stat *fs, int type, struct WALK *state));
  308 EXPORT  int doscan      __PR((int ac, char **av));
  309 
  310 LOCAL void
  311 make_symtab(ac, av)
  312     int ac;
  313     char    *av[];
  314 {
  315 extern  BOOL    restore_valid;
  316 
  317     sym_initmaps();
  318     restore_valid = TRUE;
  319     doscan(ac, av);
  320     sym_close();
  321 }
  322 
  323 
  324 LOCAL int
  325 walkfunc(nm, fs, type, state)
  326     char        *nm;
  327     struct stat *fs;
  328     int     type;
  329     struct WALK *state;
  330 {
  331     if (state->quitfun) {
  332         /*
  333          * Check for shell builtin signal abort condition.
  334          */
  335         if ((*state->quitfun)(state->qfarg)) {
  336             state->flags |= WALK_WF_QUIT;
  337             return (0);
  338         }
  339     }
  340     if (type == WALK_NS) {
  341         ferrmsg(state->std[2],
  342                 gettext("Cannot stat '%s'.\n"), nm);
  343         state->err = 1;
  344         return (0);
  345     } else if (type == WALK_SLN && (state->walkflags & WALK_PHYS) == 0) {
  346         ferrmsg(state->std[2],
  347                 gettext("Cannot follow symlink '%s'.\n"), nm);
  348         state->err = 1;
  349         return (0);
  350     } else if (type == WALK_DNR) {
  351         if (state->flags & WALK_WF_NOCHDIR) {
  352             ferrmsg(state->std[2],
  353                 gettext("Cannot chdir to '%s'.\n"), nm);
  354         } else {
  355             ferrmsg(state->std[2],
  356                 gettext("Cannot read '%s'.\n"), nm);
  357         }
  358         state->err = 1;
  359         return (0);
  360     }
  361 
  362     if (state->maxdepth >= 0 && state->level >= state->maxdepth)
  363         state->flags |= WALK_WF_PRUNE;
  364     if (state->mindepth >= 0 && state->level < state->mindepth)
  365         return (0);
  366 
  367     {
  368 #ifndef HAVE_FCHDIR
  369         char        cwd[MAXPATHNAME+1];
  370 #else
  371         int     f;
  372 #endif
  373         struct stat sb;
  374         char        name[4096];
  375 
  376         sprintf(name, "%s/%s", (char *)state->auxp, &nm[1]);
  377         sb.st_ino = 0;
  378 
  379 #ifndef HAVE_FCHDIR
  380         cwd[0] = '\0';
  381         getcwd(cwd, sizeof (cwd));
  382 #else
  383         /*
  384          * Note that we do not need O_RDONLY as we do not like to
  385          * run readdir() on that directory but just fchdir().
  386          */
  387         f = open(".", O_SEARCH|O_DIRECTORY);
  388 #endif
  389         walkhome(state);
  390         lstatat(name, &sb, AT_SYMLINK_NOFOLLOW);
  391 #ifndef HAVE_FCHDIR
  392         chdir(cwd);
  393 #else
  394         fchdir(f);
  395         close(f);
  396 #endif
  397         padd_node(nm, sb.st_ino, fs->st_ino, (type ==  WALK_D) ?  I_DIR:0);
  398     }
  399     return (0);
  400 }
  401 
  402 
  403 EXPORT int
  404 doscan(ac, av)
  405     int ac;
  406     char    **av;
  407 {
  408     finda_t fa;
  409     findn_t *Tree;
  410     struct WALK walkstate;
  411     int oraise[3];
  412     int ret = 0;
  413     int i;
  414     FILE    *std[3];
  415 
  416     std[0] = stdin;
  417     std[1] = stdout;
  418     std[2] = stderr;
  419 
  420     find_argsinit(&fa);
  421     for (i = 0; i < 3; i++) {
  422         oraise[i] = file_getraise(std[i]);
  423         file_raise(std[i], FALSE);
  424         fa.std[i] = std[i];
  425     }
  426     fa.walkflags = WALK_CHDIR | WALK_PHYS;
  427     fa.walkflags |= WALK_NOEXIT;
  428 
  429     Tree = 0;
  430     if (Tree == 0) {
  431         Tree = find_printnode();
  432     } else if (!fa.found_action) {
  433         Tree = find_addprint(Tree, &fa);
  434         if (Tree == (findn_t *)NULL) {
  435             ret = geterrno();
  436             goto out;
  437         }
  438     }
  439     walkinitstate(&walkstate);
  440     for (i = 0; i < 3; i++)
  441         walkstate.std[i] = std[i];
  442     if (fa.patlen > 0) {
  443         walkstate.patstate = __fjmalloc(std[2],
  444                     sizeof (int) * fa.patlen,
  445                     "space for pattern state", JM_RETURN);
  446         if (walkstate.patstate == NULL) {
  447             ret = geterrno();
  448             goto out;
  449         }
  450     }
  451 
  452     find_timeinit(time(0));
  453 
  454     walkstate.walkflags = fa.walkflags;
  455     walkstate.maxdepth  = fa.maxdepth;
  456     walkstate.mindepth  = fa.mindepth;
  457     walkstate.lname     = NULL;
  458     walkstate.tree      = Tree;
  459     walkstate.err       = 0;
  460     walkstate.pflags    = 0;
  461     walkstate.auxp      = av[2];
  462 
  463     treewalk(av[1], walkfunc, &walkstate);
  464 
  465     find_free(Tree, &fa);
  466     ret = walkstate.err;
  467 
  468 out:
  469     for (i = 0; i < 3; i++) {
  470         fflush(std[i]);
  471         if (ferror(std[i]))
  472             clearerr(std[i]);
  473         file_raise(std[i], oraise[i]);
  474     }
  475     return (ret);
  476 }