2 * Copyright (c) 1992 Keith Muller.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)sel_subs.c 8.1 (Berkeley) 5/31/93
38 * $FreeBSD: src/bin/pax/sel_subs.c,v 1.11.2.1 2001/08/01 05:03:11 obrien Exp $
39 * $DragonFly: src/bin/pax/sel_subs.c,v 1.6 2004/11/07 20:54:51 eirikn Exp $
42 #include <sys/types.h>
56 static int str_sec (char *, time_t *);
57 static int usr_match (ARCHD *);
58 static int grp_match (ARCHD *);
59 static int trng_match (ARCHD *);
61 static TIME_RNG *trhead = NULL; /* time range list head */
62 static TIME_RNG *trtail = NULL; /* time range list tail */
63 static USRT **usrtb = NULL; /* user selection table */
64 static GRPT **grptb = NULL; /* group selection table */
67 * Routines for selection of archive members
72 * check if this file matches a specified uid, gid or time range
74 * 0 if this archive member should be processed, 1 if it should be skipped
80 if (((usrtb != NULL) && usr_match(arcn)) ||
81 ((grptb != NULL) && grp_match(arcn)) ||
82 ((trhead != NULL) && trng_match(arcn)))
88 * User/group selection routines
90 * Routines to handle user selection of files based on the file uid/gid. To
91 * add an entry, the user supplies either then name or the uid/gid starting with
92 * a # on the command line. A \# will escape the #.
97 * add a user match to the user match hash table
99 * 0 if added ok, -1 otherwise;
111 * create the table if it doesn't exist
113 if ((str == NULL) || (*str == '\0'))
115 if ((usrtb == NULL) &&
116 ((usrtb = (USRT **)calloc(USR_TB_SZ, sizeof(USRT *))) == NULL)) {
117 paxwarn(1, "Unable to allocate memory for user selection table");
122 * figure out user spec
126 * it is a user name, \# escapes # as first char in user name
128 if ((str[0] == '\\') && (str[1] == '#'))
130 if ((pw = getpwnam(str)) == NULL) {
131 paxwarn(1, "Unable to find uid for user: %s", str);
134 uid = (uid_t)pw->pw_uid;
137 uid = (uid_t)atoi(str+1);
139 uid = (uid_t)strtoul(str+1, NULL, 10);
144 * hash it and go down the hash chain (if any) looking for it
146 indx = ((unsigned)uid) % USR_TB_SZ;
147 if ((pt = usrtb[indx]) != NULL) {
156 * uid is not yet in the table, add it to the front of the chain
158 if ((pt = (USRT *)malloc(sizeof(USRT))) != NULL) {
160 pt->fow = usrtb[indx];
164 paxwarn(1, "User selection table out of memory");
170 * check if this files uid matches a selected uid.
172 * 0 if this archive member should be processed, 1 if it should be skipped
176 usr_match(ARCHD *arcn)
181 * hash and look for it in the table
183 pt = usrtb[((unsigned)arcn->sb.st_uid) % USR_TB_SZ];
185 if (pt->uid == arcn->sb.st_uid)
198 * add a group match to the group match hash table
200 * 0 if added ok, -1 otherwise;
212 * create the table if it doesn't exist
214 if ((str == NULL) || (*str == '\0'))
216 if ((grptb == NULL) &&
217 ((grptb = (GRPT **)calloc(GRP_TB_SZ, sizeof(GRPT *))) == NULL)) {
218 paxwarn(1, "Unable to allocate memory fo group selection table");
223 * figure out user spec
227 * it is a group name, \# escapes # as first char in group name
229 if ((str[0] == '\\') && (str[1] == '#'))
231 if ((gr = getgrnam(str)) == NULL) {
232 paxwarn(1,"Cannot determine gid for group name: %s", str);
235 gid = (gid_t)gr->gr_gid;
238 gid = (gid_t)atoi(str+1);
240 gid = (gid_t)strtoul(str+1, NULL, 10);
245 * hash it and go down the hash chain (if any) looking for it
247 indx = ((unsigned)gid) % GRP_TB_SZ;
248 if ((pt = grptb[indx]) != NULL) {
257 * gid not in the table, add it to the front of the chain
259 if ((pt = (GRPT *)malloc(sizeof(GRPT))) != NULL) {
261 pt->fow = grptb[indx];
265 paxwarn(1, "Group selection table out of memory");
271 * check if this files gid matches a selected gid.
273 * 0 if this archive member should be processed, 1 if it should be skipped
277 grp_match(ARCHD *arcn)
282 * hash and look for it in the table
284 pt = grptb[((unsigned)arcn->sb.st_gid) % GRP_TB_SZ];
286 if (pt->gid == arcn->sb.st_gid)
298 * Time range selection routines
300 * Routines to handle user selection of files based on the modification and/or
301 * inode change time falling within a specified time range (the non-standard
302 * -T flag). The user may specify any number of different file time ranges.
303 * Time ranges are checked one at a time until a match is found (if at all).
304 * If the file has a mtime (and/or ctime) which lies within one of the time
305 * ranges, the file is selected. Time ranges may have a lower and/or a upper
306 * value. These ranges are inclusive. When no time ranges are supplied to pax
307 * with the -T option, all members in the archive will be selected by the time
308 * range routines. When only a lower range is supplied, only files with a
309 * mtime (and/or ctime) equal to or younger are selected. When only a upper
310 * range is supplied, only files with a mtime (and/or ctime) equal to or older
311 * are selected. When the lower time range is equal to the upper time range,
312 * only files with a mtime (or ctime) of exactly that time are selected.
317 * add a time range match to the time range list.
318 * This is a non-standard pax option. Lower and upper ranges are in the
319 * format: [yy[mm[dd[hh]]]]mm[.ss] and are comma separated.
320 * Time ranges are based on current time, so 1234 would specify a time of
323 * 0 if the time range was added to the list, -1 otherwise
336 * throw out the badly formed time ranges
338 if ((str == NULL) || (*str == '\0')) {
339 paxwarn(1, "Empty time range string");
344 * locate optional flags suffix /{cm}.
346 if ((flgpt = strrchr(str, '/')) != NULL)
349 for (stpt = str; *stpt != '\0'; ++stpt) {
350 if ((*stpt >= '0') && (*stpt <= '9'))
352 if ((*stpt == ',') && (up_pt == NULL)) {
360 * allow only one dot per range (secs)
362 if ((*stpt == '.') && (!dot)) {
366 paxwarn(1, "Improperly specified time range: %s", str);
371 * allocate space for the time range and store the limits
373 if ((pt = (TIME_RNG *)malloc(sizeof(TIME_RNG))) == NULL) {
374 paxwarn(1, "Unable to allocate memory for time range");
379 * by default we only will check file mtime, but usee can specify
380 * mtime, ctime (inode change time) or both.
382 if ((flgpt == NULL) || (*flgpt == '\0'))
386 while (*flgpt != '\0') {
397 paxwarn(1, "Bad option %c with time range %s",
406 * start off with the current time
408 pt->low_time = pt->high_time = time(NULL);
413 if (str_sec(str, &(pt->low_time)) < 0) {
414 paxwarn(1, "Illegal lower time range %s", str);
421 if ((up_pt != NULL) && (*up_pt != '\0')) {
425 if (str_sec(up_pt, &(pt->high_time)) < 0) {
426 paxwarn(1, "Illegal upper time range %s", up_pt);
433 * check that the upper and lower do not overlap
435 if (pt->flgs & HASLOW) {
436 if (pt->low_time > pt->high_time) {
437 paxwarn(1, "Upper %s and lower %s time overlap",
446 if (trhead == NULL) {
447 trtail = trhead = pt;
455 paxwarn(1, "Time range format is: [yy[mm[dd[hh]]]]mm[.ss][/[c][m]]");
461 * check if this files mtime/ctime falls within any supplied time range.
463 * 0 if this archive member should be processed, 1 if it should be skipped
467 trng_match(ARCHD *arcn)
472 * have to search down the list one at a time looking for a match.
473 * remember time range limits are inclusive.
477 switch(pt->flgs & CMPBOTH) {
480 * user wants both mtime and ctime checked for this
483 if (((pt->flgs & HASLOW) &&
484 (arcn->sb.st_mtime < pt->low_time) &&
485 (arcn->sb.st_ctime < pt->low_time)) ||
486 ((pt->flgs & HASHIGH) &&
487 (arcn->sb.st_mtime > pt->high_time) &&
488 (arcn->sb.st_ctime > pt->high_time))) {
495 * user wants only ctime checked for this time range
497 if (((pt->flgs & HASLOW) &&
498 (arcn->sb.st_ctime < pt->low_time)) ||
499 ((pt->flgs & HASHIGH) &&
500 (arcn->sb.st_ctime > pt->high_time))) {
508 * user wants only mtime checked for this time range
510 if (((pt->flgs & HASLOW) &&
511 (arcn->sb.st_mtime < pt->low_time)) ||
512 ((pt->flgs & HASHIGH) &&
513 (arcn->sb.st_mtime > pt->high_time))) {
529 * Convert a time string in the format of [yy[mm[dd[hh]]]]mm[.ss] to gmt
530 * seconds. Tval already has current time loaded into it at entry.
532 * 0 if converted ok, -1 otherwise
536 str_sec(char *str, time_t *tval)
541 lt = localtime(tval);
542 if ((dot = strchr(str, '.')) != NULL) {
547 if (strlen(dot) != 2)
549 if ((lt->tm_sec = ATOI2(dot)) > 61)
554 switch (strlen(str)) {
558 * watch out for year 2000
560 if ((lt->tm_year = ATOI2(str)) < 69)
567 * watch out months are from 0 - 11 internally
569 if ((lt->tm_mon = ATOI2(str)) > 12)
578 if ((lt->tm_mday = ATOI2(str)) > 31)
586 if ((lt->tm_hour = ATOI2(str)) > 23)
594 if ((lt->tm_min = ATOI2(str)) > 59)
601 * convert broken-down time to GMT clock time seconds
603 if ((*tval = mktime(lt)) == -1)