Splint fiddles.
[tools/librpm-tizen.git] / rpmio / tfts.c
1 #include "system.h"
2 #include <fts.h>
3
4 #include <rpmio_internal.h>
5 #include <rpmmacro.h>
6 #include <rpmmessages.h>
7 #include <popt.h>
8
9 #include "debug.h"
10
11 /*@unchecked@*/
12 static int _fts_debug = 0;
13
14 #define FTPPATH         "ftp://porkchop/mnt/redhat/beehive/comps/dist/7.2-rpm"
15 #define DIRPATH         "/mnt/redhat/beehive/comps/dist/7.2-rpm"
16 static char * dirpath = DIRPATH;
17 static char * ftppath = FTPPATH;
18
19 static int ndirs = 0;
20 static int nfiles = 0;
21
22 static int indent = 2;
23
24 static const char * ftsInfoStrings[] = {
25     "UNKNOWN",
26     "D",
27     "DC",
28     "DEFAULT",
29     "DNR",
30     "DOT",
31     "DP",
32     "ERR",
33     "F",
34     "INIT",
35     "NS",
36     "NSOK",
37     "SL",
38     "SLNONE",
39     "W",
40 };
41
42 static const char * ftsInfoStr(int fts_info) {
43     if (!(fts_info >= 1 && fts_info <= 14))
44         fts_info = 0;
45     return ftsInfoStrings[ fts_info ];
46 }
47
48 static int ftsPrint(FTS * ftsp, FTSENT * fts)
49 {
50
51     if (_fts_debug)
52         fprintf(stderr, "FTS_%s\t%*s %s\n", ftsInfoStr(fts->fts_info),
53                 indent * (fts->fts_level < 0 ? 0 : fts->fts_level), "",
54                 fts->fts_name);
55
56     switch (fts->fts_info) {
57     case FTS_D:         /* preorder directory */
58         ndirs++;
59         break;
60     case FTS_DP:        /* postorder directory */
61         break;
62     case FTS_F:         /* regular file */
63         nfiles++;
64         break;
65     case FTS_NS:        /* stat(2) failed */
66     case FTS_DNR:       /* unreadable directory */
67     case FTS_ERR:       /* error; errno is set */
68         break;
69     case FTS_DC:        /* directory that causes cycles */
70     case FTS_DEFAULT:   /* none of the above */
71     case FTS_DOT:       /* dot or dot-dot */
72     case FTS_INIT:      /* initialized only */
73     case FTS_NSOK:      /* no stat(2) requested */
74     case FTS_SL:        /* symbolic link */
75     case FTS_SLNONE:    /* symbolic link without target */
76     case FTS_W:         /* whiteout object */
77     default:
78         break;
79     }
80
81     return 0;
82 }
83
84
85 static int ftsOpts = 0;
86
87 static void ftsWalk(const char * path)
88 {
89     const char * ftsSet[2];
90     FTS * ftsp;
91     FTSENT * fts;
92     int xx;
93
94
95     ftsSet[0] = path;
96     ftsSet[1] = NULL;
97
98     ndirs = nfiles = 0;
99     ftsp = Fts_open((char *const *)ftsSet, ftsOpts, NULL);
100     while((fts = Fts_read(ftsp)) != NULL)
101         xx = ftsPrint(ftsp, fts);
102     xx = Fts_close(ftsp);
103 fprintf(stderr, "===== (%d/%d) dirs/files in %s\n", ndirs, nfiles, path);
104
105 }
106
107 static struct poptOption optionsTable[] = {
108  { "ftsdebug", 'd', POPT_ARG_VAL,       &_fts_debug, -1,        NULL, NULL },
109
110  { "comfollow", '\0', POPT_BIT_SET,     &ftsOpts, FTS_COMFOLLOW,
111         N_("follow command line symlinks"), NULL },
112  { "logical", '\0', POPT_BIT_SET,       &ftsOpts, FTS_LOGICAL,
113         N_("logical walk"), NULL },
114  { "nochdir", '\0', POPT_BIT_SET,       &ftsOpts, FTS_NOCHDIR,
115         N_("don't change directories"), NULL },
116  { "nostat", '\0', POPT_BIT_SET,        &ftsOpts, FTS_NOSTAT,
117         N_("don't get stat info"), NULL },
118  { "physical", '\0', POPT_BIT_SET,      &ftsOpts, FTS_PHYSICAL,
119         N_("physical walk"), NULL },
120  { "seedot", '\0', POPT_BIT_SET,        &ftsOpts, FTS_SEEDOT,
121         N_("return dot and dot-dot"), NULL },
122  { "xdev", '\0', POPT_BIT_SET,          &ftsOpts, FTS_XDEV,
123         N_("don't cross devices"), NULL },
124  { "whiteout", '\0', POPT_BIT_SET,      &ftsOpts, FTS_WHITEOUT,
125         N_("return whiteout information"), NULL },
126
127  { "ftpdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_ftp_debug, -1,
128         N_("debug protocol data stream"), NULL},
129  { "rpmiodebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmio_debug, -1,
130         N_("debug rpmio I/O"), NULL},
131  { "urldebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_url_debug, -1,
132         N_("debug URL cache handling"), NULL},
133  { "verbose", 'v', 0, 0, 'v',                           NULL, NULL },
134   POPT_AUTOHELP
135   POPT_TABLEEND
136 };
137
138 int
139 main(int argc, const char *argv[])
140 {
141     poptContext optCon = poptGetContext(argv[0], argc, argv, optionsTable, 0);
142     int rc;
143
144     while ((rc = poptGetNextOpt(optCon)) > 0) {
145         switch (rc) {
146         case 'v':
147             rpmIncreaseVerbosity();
148             /*@switchbreak@*/ break;
149         default:
150             /*@switchbreak@*/ break;
151         }
152     }
153
154     if (ftsOpts == 0)
155         ftsOpts = (FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOSTAT);
156
157     ftsWalk(dirpath);
158     ftsWalk(ftppath);
159
160     return 0;
161 }