English Language flag
// Log In
// CVSweb
Project: FreeWRT
// Summary // Activity // Search // Tracker // Lists // News // SCM // Wiki

SCM Repository

ViewVC logotype

Contents of /branches/freewrt_1_0/tools/paxmirabilis/src/sel_subs.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3816 - (show annotations) (download)
Wed Oct 29 17:58:40 2008 UTC (5 years, 3 months ago) by tg
File MIME type: text/plain
File size: 14093 byte(s)
FreeWRT trunk:

• merge new MirCPIO upstream, changes:
  – allow multiple -v options not only for tar but also for cpio, pax
  – add code to work around broken archives such as the CPIO archive inside
    Fedora Core 4 glibc-common-2.3.6-3.i386.rpm which carry the actual data
    of hardlinks not in the first but a later (here, the last) occurence of
    the file in question: iff hardlinking succeeds (no cross-device!), size
    of the linked files is 0, size of the archive member is greater than 0,
    we are extracting, but not to stdout, proceed writing out the data.
• add mircpio, mirpax, mirtar links in order to prevent interfering with
  native tools when both are in $PATH (this has worked since the last
  update, can be used now)

FreeWRT 1.0-stable:

• merge paxmirabilis complete from trunk

note: untested

1 /* $OpenBSD: sel_subs.c,v 1.18 2004/04/16 22:50:23 deraadt Exp $ */
2 /* $NetBSD: sel_subs.c,v 1.5 1995/03/21 09:07:42 cgd Exp $ */
3
4 /*-
5 * Copyright (c) 1992 Keith Muller.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Keith Muller of the University of California, San Diego.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/param.h>
38 #include <sys/time.h>
39 #include <sys/stat.h>
40 #include <ctype.h>
41 #include <grp.h>
42 #include <pwd.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include "pax.h"
48 #include "sel_subs.h"
49 #include "extern.h"
50
51 __SCCSID("@(#)sel_subs.c 8.1 (Berkeley) 5/31/93");
52 __RCSID("$MirOS: src/bin/pax/sel_subs.c,v 1.3 2008/03/14 15:55:21 tg Exp $");
53
54 static int str_sec(const char *, time_t *);
55 static int usr_match(ARCHD *);
56 static int grp_match(ARCHD *);
57 static int trng_match(ARCHD *);
58
59 static TIME_RNG *trhead = NULL; /* time range list head */
60 static TIME_RNG *trtail = NULL; /* time range list tail */
61 static USRT **usrtb = NULL; /* user selection table */
62 static GRPT **grptb = NULL; /* group selection table */
63
64 /*
65 * Routines for selection of archive members
66 */
67
68 /*
69 * sel_chk()
70 * check if this file matches a specified uid, gid or time range
71 * Return:
72 * 0 if this archive member should be processed, 1 if it should be skipped
73 */
74
75 int
76 sel_chk(ARCHD *arcn)
77 {
78 if (((usrtb != NULL) && usr_match(arcn)) ||
79 ((grptb != NULL) && grp_match(arcn)) ||
80 ((trhead != NULL) && trng_match(arcn)))
81 return(1);
82 return(0);
83 }
84
85 /*
86 * User/group selection routines
87 *
88 * Routines to handle user selection of files based on the file uid/gid. To
89 * add an entry, the user supplies either the name or the uid/gid starting with
90 * a # on the command line. A \# will escape the #.
91 */
92
93 /*
94 * usr_add()
95 * add a user match to the user match hash table
96 * Return:
97 * 0 if added ok, -1 otherwise;
98 */
99
100 int
101 usr_add(char *str)
102 {
103 u_int indx;
104 USRT *pt;
105 struct passwd *pw;
106 uid_t uid;
107
108 /*
109 * create the table if it doesn't exist
110 */
111 if ((str == NULL) || (*str == '\0'))
112 return(-1);
113 if ((usrtb == NULL) &&
114 ((usrtb = (USRT **)calloc(USR_TB_SZ, sizeof(USRT *))) == NULL)) {
115 paxwarn(1, "Unable to allocate memory for user selection table");
116 return(-1);
117 }
118
119 /*
120 * figure out user spec
121 */
122 if (str[0] != '#') {
123 /*
124 * it is a user name, \# escapes # as first char in user name
125 */
126 if ((str[0] == '\\') && (str[1] == '#'))
127 ++str;
128 if ((pw = getpwnam(str)) == NULL) {
129 paxwarn(1, "Unable to find uid for user: %s", str);
130 return(-1);
131 }
132 uid = (uid_t)pw->pw_uid;
133 } else
134 uid = (uid_t)strtoul(str+1, NULL, 10);
135 endpwent();
136
137 /*
138 * hash it and go down the hash chain (if any) looking for it
139 */
140 indx = ((unsigned)uid) % USR_TB_SZ;
141 if ((pt = usrtb[indx]) != NULL) {
142 while (pt != NULL) {
143 if (pt->uid == uid)
144 return(0);
145 pt = pt->fow;
146 }
147 }
148
149 /*
150 * uid is not yet in the table, add it to the front of the chain
151 */
152 if ((pt = (USRT *)malloc(sizeof(USRT))) != NULL) {
153 pt->uid = uid;
154 pt->fow = usrtb[indx];
155 usrtb[indx] = pt;
156 return(0);
157 }
158 paxwarn(1, "User selection table out of memory");
159 return(-1);
160 }
161
162 /*
163 * usr_match()
164 * check if this files uid matches a selected uid.
165 * Return:
166 * 0 if this archive member should be processed, 1 if it should be skipped
167 */
168
169 static int
170 usr_match(ARCHD *arcn)
171 {
172 USRT *pt;
173
174 /*
175 * hash and look for it in the table
176 */
177 pt = usrtb[((unsigned)arcn->sb.st_uid) % USR_TB_SZ];
178 while (pt != NULL) {
179 if (pt->uid == arcn->sb.st_uid)
180 return(0);
181 pt = pt->fow;
182 }
183
184 /*
185 * not found
186 */
187 return(1);
188 }
189
190 /*
191 * grp_add()
192 * add a group match to the group match hash table
193 * Return:
194 * 0 if added ok, -1 otherwise;
195 */
196
197 int
198 grp_add(char *str)
199 {
200 u_int indx;
201 GRPT *pt;
202 struct group *gr;
203 gid_t gid;
204
205 /*
206 * create the table if it doesn't exist
207 */
208 if ((str == NULL) || (*str == '\0'))
209 return(-1);
210 if ((grptb == NULL) &&
211 ((grptb = (GRPT **)calloc(GRP_TB_SZ, sizeof(GRPT *))) == NULL)) {
212 paxwarn(1, "Unable to allocate memory fo group selection table");
213 return(-1);
214 }
215
216 /*
217 * figure out user spec
218 */
219 if (str[0] != '#') {
220 /*
221 * it is a group name, \# escapes # as first char in group name
222 */
223 if ((str[0] == '\\') && (str[1] == '#'))
224 ++str;
225 if ((gr = getgrnam(str)) == NULL) {
226 paxwarn(1,"Cannot determine gid for group name: %s", str);
227 return(-1);
228 }
229 gid = (gid_t)gr->gr_gid;
230 } else
231 gid = (gid_t)strtoul(str+1, NULL, 10);
232 endgrent();
233
234 /*
235 * hash it and go down the hash chain (if any) looking for it
236 */
237 indx = ((unsigned)gid) % GRP_TB_SZ;
238 if ((pt = grptb[indx]) != NULL) {
239 while (pt != NULL) {
240 if (pt->gid == gid)
241 return(0);
242 pt = pt->fow;
243 }
244 }
245
246 /*
247 * gid not in the table, add it to the front of the chain
248 */
249 if ((pt = (GRPT *)malloc(sizeof(GRPT))) != NULL) {
250 pt->gid = gid;
251 pt->fow = grptb[indx];
252 grptb[indx] = pt;
253 return(0);
254 }
255 paxwarn(1, "Group selection table out of memory");
256 return(-1);
257 }
258
259 /*
260 * grp_match()
261 * check if this files gid matches a selected gid.
262 * Return:
263 * 0 if this archive member should be processed, 1 if it should be skipped
264 */
265
266 static int
267 grp_match(ARCHD *arcn)
268 {
269 GRPT *pt;
270
271 /*
272 * hash and look for it in the table
273 */
274 pt = grptb[((unsigned)arcn->sb.st_gid) % GRP_TB_SZ];
275 while (pt != NULL) {
276 if (pt->gid == arcn->sb.st_gid)
277 return(0);
278 pt = pt->fow;
279 }
280
281 /*
282 * not found
283 */
284 return(1);
285 }
286
287 /*
288 * Time range selection routines
289 *
290 * Routines to handle user selection of files based on the modification and/or
291 * inode change time falling within a specified time range (the non-standard
292 * -T flag). The user may specify any number of different file time ranges.
293 * Time ranges are checked one at a time until a match is found (if at all).
294 * If the file has a mtime (and/or ctime) which lies within one of the time
295 * ranges, the file is selected. Time ranges may have a lower and/or a upper
296 * value. These ranges are inclusive. When no time ranges are supplied to pax
297 * with the -T option, all members in the archive will be selected by the time
298 * range routines. When only a lower range is supplied, only files with a
299 * mtime (and/or ctime) equal to or younger are selected. When only a upper
300 * range is supplied, only files with a mtime (and/or ctime) equal to or older
301 * are selected. When the lower time range is equal to the upper time range,
302 * only files with a mtime (or ctime) of exactly that time are selected.
303 */
304
305 /*
306 * trng_add()
307 * add a time range match to the time range list.
308 * This is a non-standard pax option. Lower and upper ranges are in the
309 * format: [[[[[cc]yy]mm]dd]HH]MM[.SS] and are comma separated.
310 * Time ranges are based on current time, so 1234 would specify a time of
311 * 12:34 today.
312 * Return:
313 * 0 if the time range was added to the list, -1 otherwise
314 */
315
316 int
317 trng_add(char *str)
318 {
319 TIME_RNG *pt;
320 char *up_pt = NULL;
321 char *stpt;
322 char *flgpt;
323 int dot = 0;
324
325 /*
326 * throw out the badly formed time ranges
327 */
328 if ((str == NULL) || (*str == '\0')) {
329 paxwarn(1, "Empty time range string");
330 return(-1);
331 }
332
333 /*
334 * locate optional flags suffix /{cm}.
335 */
336 if ((flgpt = strrchr(str, '/')) != NULL)
337 *flgpt++ = '\0';
338
339 for (stpt = str; *stpt != '\0'; ++stpt) {
340 if ((*stpt >= '0') && (*stpt <= '9'))
341 continue;
342 if ((*stpt == ',') && (up_pt == NULL)) {
343 *stpt = '\0';
344 up_pt = stpt + 1;
345 dot = 0;
346 continue;
347 }
348
349 /*
350 * allow only one dot per range (secs)
351 */
352 if ((*stpt == '.') && (!dot)) {
353 ++dot;
354 continue;
355 }
356 paxwarn(1, "Improperly specified time range: %s", str);
357 goto out;
358 }
359
360 /*
361 * allocate space for the time range and store the limits
362 */
363 if ((pt = (TIME_RNG *)malloc(sizeof(TIME_RNG))) == NULL) {
364 paxwarn(1, "Unable to allocate memory for time range");
365 return(-1);
366 }
367
368 /*
369 * by default we only will check file mtime, but user can specify
370 * mtime, ctime (inode change time) or both.
371 */
372 if ((flgpt == NULL) || (*flgpt == '\0'))
373 pt->flgs = CMPMTME;
374 else {
375 pt->flgs = 0;
376 while (*flgpt != '\0') {
377 switch (*flgpt) {
378 case 'M':
379 case 'm':
380 pt->flgs |= CMPMTME;
381 break;
382 case 'C':
383 case 'c':
384 pt->flgs |= CMPCTME;
385 break;
386 default:
387 paxwarn(1, "Bad option %c with time range %s",
388 *flgpt, str);
389 goto out;
390 }
391 ++flgpt;
392 }
393 }
394
395 /*
396 * start off with the current time
397 */
398 pt->low_time = pt->high_time = time(NULL);
399 if (*str != '\0') {
400 /*
401 * add lower limit
402 */
403 if (str_sec(str, &(pt->low_time)) < 0) {
404 paxwarn(1, "Illegal lower time range %s", str);
405 (void)free((char *)pt);
406 goto out;
407 }
408 pt->flgs |= HASLOW;
409 }
410
411 if ((up_pt != NULL) && (*up_pt != '\0')) {
412 /*
413 * add upper limit
414 */
415 if (str_sec(up_pt, &(pt->high_time)) < 0) {
416 paxwarn(1, "Illegal upper time range %s", up_pt);
417 (void)free((char *)pt);
418 goto out;
419 }
420 pt->flgs |= HASHIGH;
421
422 /*
423 * check that the upper and lower do not overlap
424 */
425 if (pt->flgs & HASLOW) {
426 if (pt->low_time > pt->high_time) {
427 paxwarn(1, "Upper %s and lower %s time overlap",
428 up_pt, str);
429 (void)free((char *)pt);
430 return(-1);
431 }
432 }
433 }
434
435 pt->fow = NULL;
436 if (trhead == NULL) {
437 trtail = trhead = pt;
438 return(0);
439 }
440 trtail->fow = pt;
441 trtail = pt;
442 return(0);
443
444 out:
445 paxwarn(1, "Time range format is: [[[[[cc]yy]mm]dd]HH]MM[.SS][/[c][m]]");
446 return(-1);
447 }
448
449 /*
450 * trng_match()
451 * check if this files mtime/ctime falls within any supplied time range.
452 * Return:
453 * 0 if this archive member should be processed, 1 if it should be skipped
454 */
455
456 static int
457 trng_match(ARCHD *arcn)
458 {
459 TIME_RNG *pt;
460
461 /*
462 * have to search down the list one at a time looking for a match.
463 * remember time range limits are inclusive.
464 */
465 pt = trhead;
466 while (pt != NULL) {
467 switch (pt->flgs & CMPBOTH) {
468 case CMPBOTH:
469 /*
470 * user wants both mtime and ctime checked for this
471 * time range
472 */
473 if (((pt->flgs & HASLOW) &&
474 (arcn->sb.st_mtime < pt->low_time) &&
475 (arcn->sb.st_ctime < pt->low_time)) ||
476 ((pt->flgs & HASHIGH) &&
477 (arcn->sb.st_mtime > pt->high_time) &&
478 (arcn->sb.st_ctime > pt->high_time))) {
479 pt = pt->fow;
480 continue;
481 }
482 break;
483 case CMPCTME:
484 /*
485 * user wants only ctime checked for this time range
486 */
487 if (((pt->flgs & HASLOW) &&
488 (arcn->sb.st_ctime < pt->low_time)) ||
489 ((pt->flgs & HASHIGH) &&
490 (arcn->sb.st_ctime > pt->high_time))) {
491 pt = pt->fow;
492 continue;
493 }
494 break;
495 case CMPMTME:
496 default:
497 /*
498 * user wants only mtime checked for this time range
499 */
500 if (((pt->flgs & HASLOW) &&
501 (arcn->sb.st_mtime < pt->low_time)) ||
502 ((pt->flgs & HASHIGH) &&
503 (arcn->sb.st_mtime > pt->high_time))) {
504 pt = pt->fow;
505 continue;
506 }
507 break;
508 }
509 break;
510 }
511
512 if (pt == NULL)
513 return(1);
514 return(0);
515 }
516
517 /*
518 * str_sec()
519 * Convert a time string in the format of [[[[[cc]yy]mm]dd]HH]MM[.SS] to
520 * seconds UTC. Tval already has current time loaded into it at entry.
521 * Return:
522 * 0 if converted ok, -1 otherwise
523 */
524
525 static int
526 str_sec(const char *p, time_t *tval)
527 {
528 struct tm *lt;
529 const char *dot, *t;
530 size_t len;
531 int bigyear;
532 int yearset;
533
534 yearset = 0;
535 len = strlen(p);
536
537 for (t = p, dot = NULL; *t; ++t) {
538 if (isdigit(*t))
539 continue;
540 if (*t == '.' && dot == NULL) {
541 dot = t;
542 continue;
543 }
544 return(-1);
545 }
546
547 lt = localtime(tval);
548
549 if (dot != NULL) { /* .SS */
550 if (strlen(++dot) != 2)
551 return(-1);
552 lt->tm_sec = ATOI2(dot);
553 if (lt->tm_sec > 61)
554 return(-1);
555 len -= 3;
556 } else
557 lt->tm_sec = 0;
558
559 switch (len) {
560 case 12: /* cc */
561 bigyear = ATOI2(p);
562 lt->tm_year = (bigyear * 100) - TM_YEAR_BASE;
563 yearset = 1;
564 /* FALLTHROUGH */
565 case 10: /* yy */
566 if (yearset) {
567 lt->tm_year += ATOI2(p);
568 } else {
569 lt->tm_year = ATOI2(p);
570 if (lt->tm_year < 69) /* hack for 2000 ;-} */
571 lt->tm_year += (2000 - TM_YEAR_BASE);
572 else
573 lt->tm_year += (1900 - TM_YEAR_BASE);
574 }
575 /* FALLTHROUGH */
576 case 8: /* mm */
577 lt->tm_mon = ATOI2(p);
578 if ((lt->tm_mon > 12) || !lt->tm_mon)
579 return(-1);
580 --lt->tm_mon; /* time struct is 0 - 11 */
581 /* FALLTHROUGH */
582 case 6: /* dd */
583 lt->tm_mday = ATOI2(p);
584 if ((lt->tm_mday > 31) || !lt->tm_mday)
585 return(-1);
586 /* FALLTHROUGH */
587 case 4: /* HH */
588 lt->tm_hour = ATOI2(p);
589 if (lt->tm_hour > 23)
590 return(-1);
591 /* FALLTHROUGH */
592 case 2: /* MM */
593 lt->tm_min = ATOI2(p);
594 if (lt->tm_min > 59)
595 return(-1);
596 break;
597 default:
598 return(-1);
599 }
600
601 /* convert broken-down time to UTC clock time seconds */
602 if ((*tval = mktime(lt)) == -1)
603 return(-1);
604 return(0);
605 }

root@freewrt.org:443
ViewVC Help
Powered by ViewVC 1.1.20