/[gentoo-projects]/portage-utils/qsize.c
Gentoo

Contents of /portage-utils/qsize.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.39 - (show annotations) (download) (as text)
Sun Dec 18 01:17:14 2011 UTC (2 years, 7 months ago) by vapier
Branch: MAIN
Changes since 1.38: +20 -26 lines
File MIME type: text/x-csrc
overhaul vdb walking

1 /*
2 * Copyright 2005-2010 Gentoo Foundation
3 * Distributed under the terms of the GNU General Public License v2
4 * $Header: /var/cvsroot/gentoo-projects/portage-utils/qsize.c,v 1.38 2011/03/17 03:01:19 vapier Exp $
5 *
6 * Copyright 2005-2010 Ned Ludd - <solar@gentoo.org>
7 * Copyright 2005-2010 Mike Frysinger - <vapier@gentoo.org>
8 */
9
10 #ifdef APPLET_qsize
11
12 #define QSIZE_FLAGS "fasSmkbi:" COMMON_FLAGS
13 static struct option const qsize_long_opts[] = {
14 {"filesystem", no_argument, NULL, 'f'},
15 {"all", no_argument, NULL, 'a'},
16 {"sum", no_argument, NULL, 's'},
17 {"sum-only", no_argument, NULL, 'S'},
18 {"megabytes", no_argument, NULL, 'm'},
19 {"kilobytes", no_argument, NULL, 'k'},
20 {"bytes", no_argument, NULL, 'b'},
21 {"ignore", a_argument, NULL, 'i'},
22 COMMON_LONG_OPTS
23 };
24 static const char * const qsize_opts_help[] = {
25 "Show size used on disk",
26 "Size all installed packages",
27 "Include a summary",
28 "Show just the summary",
29 "Display size in megabytes",
30 "Display size in kilobytes",
31 "Display size in bytes",
32 "Ignore regexp string",
33 COMMON_OPTS_HELP
34 };
35 static const char qsize_rcsid[] = "$Id: qsize.c,v 1.38 2011/03/17 03:01:19 vapier Exp $";
36 #define qsize_usage(ret) usage(ret, QSIZE_FLAGS, qsize_long_opts, qsize_opts_help, lookup_applet_idx("qsize"))
37
38 int qsize_main(int argc, char **argv)
39 {
40 q_vdb_ctx *ctx;
41 q_vdb_cat_ctx *cat_ctx;
42 q_vdb_pkg_ctx *pkg_ctx;
43 int i;
44 char search_all = 0;
45 struct stat st;
46 char fs_size = 0, summary = 0, summary_only = 0;
47 size_t num_all_files, num_all_nonfiles, num_all_ignored;
48 size_t num_files, num_nonfiles, num_ignored;
49 uint64_t num_all_bytes, num_bytes;
50 size_t disp_units = 0;
51 const char *str_disp_units = NULL;
52 size_t buflen;
53 char *buf;
54 char filename[_Q_PATH_MAX], *filename_root;
55 queue *ignore_regexp = NULL;
56
57 DBG("argc=%d argv[0]=%s argv[1]=%s",
58 argc, argv[0], argc > 1 ? argv[1] : "NULL?");
59
60 while ((i = GETOPT_LONG(QSIZE, qsize, "")) != -1) {
61 switch (i) {
62 COMMON_GETOPTS_CASES(qsize)
63 case 'f': fs_size = 1; break;
64 case 'a': search_all = 1; break;
65 case 's': summary = 1; break;
66 case 'S': summary = summary_only = 1; break;
67 case 'm': disp_units = MEGABYTE; str_disp_units = "MB"; break;
68 case 'k': disp_units = KILOBYTE; str_disp_units = "KB"; break;
69 case 'b': disp_units = 1; str_disp_units = "bytes"; break;
70 case 'i': ignore_regexp = add_set(optarg, optarg, ignore_regexp); break;
71 }
72 }
73 if ((argc == optind) && !search_all)
74 qsize_usage(EXIT_FAILURE);
75
76 num_all_bytes = num_all_files = num_all_nonfiles = num_all_ignored = 0;
77
78 strcpy(filename, portroot);
79 filename_root = filename + strlen(filename);
80 buflen = _Q_PATH_MAX;
81 buf = xmalloc(buflen);
82
83 ctx = q_vdb_open();
84 if (!ctx)
85 return EXIT_FAILURE;
86
87 /* open /var/db/pkg */
88 while ((cat_ctx = q_vdb_next_cat(ctx))) {
89 /* open the cateogry */
90 const char *catname = cat_ctx->name;
91 while ((pkg_ctx = q_vdb_next_pkg(cat_ctx))) {
92 const char *pkgname = pkg_ctx->name;
93 FILE *fp;
94 /* see if this cat/pkg is requested */
95 if (!search_all) {
96 for (i = optind; i < argc; ++i) {
97 snprintf(buf, buflen, "%s/%s", catname, pkgname);
98 if (rematch(argv[i], buf, REG_EXTENDED) == 0)
99 break;
100 if (rematch(argv[i], pkgname, REG_EXTENDED) == 0)
101 break;
102 }
103 if (i == argc)
104 goto next_pkg;
105 }
106
107 if ((fp = q_vdb_pkg_fopenat_ro(pkg_ctx, "CONTENTS")) == NULL)
108 goto next_pkg;
109
110 num_ignored = num_files = num_nonfiles = num_bytes = 0;
111 while (getline(&buf, &buflen, fp) != -1) {
112 contents_entry *e;
113 queue *ll;
114 int ok = 0;
115
116 e = contents_parse_line(buf);
117 if (!e)
118 continue;
119
120 for (ll = ignore_regexp; ll != NULL; ll = ll->next) {
121 if (rematch(ll->name, e->name, REG_EXTENDED) == 0) {
122 num_ignored += 1;
123 ok = 1;
124 }
125 }
126 if (ok)
127 continue;
128
129 if (e->type == CONTENTS_OBJ || e->type == CONTENTS_SYM) {
130 strcpy(filename_root, e->name);
131 ++num_files;
132 if (!lstat(filename, &st))
133 num_bytes += (fs_size ? st.st_blocks * S_BLKSIZE : st.st_size);
134 } else
135 ++num_nonfiles;
136 }
137 fclose(fp);
138 num_all_bytes += num_bytes;
139 num_all_files += num_files;
140 num_all_nonfiles += num_nonfiles;
141 num_all_ignored += num_ignored;
142
143 if (!summary_only) {
144 printf("%s%s/%s%s%s: %lu files, %lu non-files, ", BOLD,
145 catname, BLUE, pkgname, NORM,
146 (unsigned long)num_files,
147 (unsigned long)num_nonfiles);
148 if (num_ignored)
149 printf("%lu names-ignored, ", (unsigned long)num_ignored);
150 if (disp_units)
151 printf("%s %s\n",
152 make_human_readable_str(num_bytes, 1, disp_units),
153 str_disp_units);
154 else
155 printf("%lu.%lu KB\n",
156 (unsigned long)(num_bytes / KILOBYTE),
157 (unsigned long)(((num_bytes%KILOBYTE)*1000)/KILOBYTE));
158 }
159
160 next_pkg:
161 q_vdb_close_pkg(pkg_ctx);
162 }
163 }
164
165 if (summary) {
166 printf(" %sTotals%s: %lu files, %lu non-files, ", BOLD, NORM,
167 (unsigned long)num_all_files,
168 (unsigned long)num_all_nonfiles);
169 if (num_all_ignored)
170 printf("%lu names-ignored, ", (unsigned long)num_all_ignored);
171 if (disp_units)
172 printf("%s %s\n",
173 make_human_readable_str(num_all_bytes, 1, disp_units),
174 str_disp_units);
175 else
176 printf("%lu.%lu MB\n",
177 (unsigned long)(num_all_bytes / MEGABYTE),
178 (unsigned long)(((num_all_bytes%MEGABYTE)*1000)/MEGABYTE));
179 }
180 free_sets(ignore_regexp);
181 return EXIT_SUCCESS;
182 }
183
184 #else
185 DEFINE_APPLET_STUB(qsize)
186 #endif

  ViewVC Help
Powered by ViewVC 1.1.20