From owner-p4-projects@FreeBSD.ORG Wed May 12 16:18:48 2010 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 2B6EF1065670; Wed, 12 May 2010 16:18:48 +0000 (UTC) Delivered-To: perforce@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id CAF39106566B for ; Wed, 12 May 2010 16:18:47 +0000 (UTC) (envelope-from jona@FreeBSD.org) Received: from repoman.freebsd.org (repoman.freebsd.org [69.147.83.41]) by mx1.freebsd.org (Postfix) with ESMTP id B0AC38FC1D for ; Wed, 12 May 2010 16:18:47 +0000 (UTC) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.3/8.14.3) with ESMTP id o4CGIlKH028260 for ; Wed, 12 May 2010 16:18:47 GMT (envelope-from jona@FreeBSD.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.3/8.14.3/Submit) id o4CGIlj6028258 for perforce@freebsd.org; Wed, 12 May 2010 16:18:47 GMT (envelope-from jona@FreeBSD.org) Date: Wed, 12 May 2010 16:18:47 GMT Message-Id: <201005121618.o4CGIlj6028258@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to jona@FreeBSD.org using -f From: Jonathan Anderson To: Perforce Change Reviews Precedence: bulk Cc: Subject: PERFORCE change 178149 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 12 May 2010 16:18:48 -0000 http://p4web.freebsd.org/@@178149?ac=10 Change 178149 by jona@jona-belle-freebsd8 on 2010/05/12 16:18:17 Added lc_fdlist_find(), changed whitespace in lc_fdlist_lookup() for clarity Affected files ... .. //depot/projects/trustedbsd/capabilities/src/lib/libcapsicum/libcapsicum.h#15 edit .. //depot/projects/trustedbsd/capabilities/src/lib/libcapsicum/libcapsicum_fdlist.c#13 edit Differences ... ==== //depot/projects/trustedbsd/capabilities/src/lib/libcapsicum/libcapsicum.h#15 (text+ko) ==== @@ -30,7 +30,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $P4: //depot/projects/trustedbsd/capabilities/src/lib/libcapsicum/libcapsicum.h#14 $ + * $P4: //depot/projects/trustedbsd/capabilities/src/lib/libcapsicum/libcapsicum.h#15 $ */ #ifndef _LIBCAPSICUM_H_ @@ -90,6 +90,19 @@ cap_rights_t rights); /* + * Open a stored file descriptor. + * + * Given a filename '/foo/bar/fubar', this function will attempt to find the file + * in the FD list. If that fails, it will attempt to find a parent directory in the + * FD list and supply a filename relative to that FD (which will be a pointer to a + * location within the supplied filename - do NOT free it!). + */ +int +lc_fdlist_find(struct lc_fdlist *lfp, const char *subsystem, + const char *classname, const char *filename, + const char **relative_name); + +/* * Look up a file descriptor. * * Multiple entries with the same classname are allowed, so iterating through ==== //depot/projects/trustedbsd/capabilities/src/lib/libcapsicum/libcapsicum_fdlist.c#13 (text+ko) ==== @@ -31,7 +31,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $P4: //depot/projects/trustedbsd/capabilities/src/lib/libcapsicum/libcapsicum_fdlist.c#12 $ + * $P4: //depot/projects/trustedbsd/capabilities/src/lib/libcapsicum/libcapsicum_fdlist.c#13 $ */ #include @@ -393,6 +393,60 @@ } int +lc_fdlist_find(struct lc_fdlist *lfp, const char *subsystem, + const char *classname, const char *filename, + const char **relative_name) +{ + int pos = 0; + int fd = -1; + + /* try to find the file itself in the FD list */ + size_t len = strlen(filename); + *relative_name = filename + len; + + while (fd == -1) + { + char *dirname; + + if (lc_fdlist_lookup(lfp, subsystem, classname, + &dirname, &fd, &pos) == -1) + break; + + if (strncmp(dirname, filename, len + 1)) fd = -1; + free(dirname); + } + + if (fd >= 0) return fd; + + + /* now try to find a parent directory and a relative filename */ + *relative_name = NULL; + pos = 0; + + while (fd == -1) + { + char *dirname; + + if (lc_fdlist_lookup(lfp, subsystem, classname, + &dirname, &fd, &pos) == -1) + return (-1); + + len = strlen(dirname); + if (strncmp(dirname, filename, len)) fd = -1; + else + { + *relative_name = filename + len; + if (**relative_name == '/') (*relative_name)++; + } + + free(dirname); + } + + return fd; +} + + +int lc_fdlist_lookup(struct lc_fdlist *lfp, const char *subsystem, const char *classname, char **name, int *fdp, int *pos) { @@ -412,28 +466,30 @@ for (u_int i = (pos ? *pos : 0); i < lfsp->count; i++) { struct lc_fdlist_entry *entry = lfsp->entries + i; - if ((!subsystem || - !strncmp(subsystem, names + entry->sysoff, - entry->syslen + 1)) - && (!classname || !strncmp(classname, names + - entry->classoff, entry->classnamelen + 1))) { + if ((!subsystem + || !strncmp(subsystem, names + entry->sysoff, + entry->syslen + 1)) + && (!classname + || !strncmp(classname, names + entry->classoff, + entry->classnamelen + 1))) + { + /* found a matching entry! */ + successful = 1; + *fdp = entry->fd; - /* found a matching entry! */ if (name) { *name = malloc(entry->namelen + 1); strncpy(*name, names + entry->nameoff, entry->namelen + 1); } - - *fdp = entry->fd; - if (pos != NULL) *pos = i + 1; - successful = 1; + if (pos) *pos = i + 1; break; } } UNLOCK(lfp); if (successful) return (0); + errno = ENOENT; return (-1); }