1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1985-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* Phong Vo <kpv@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * original code
1N/A *
1N/A * James A. Woods, Informatics General Corporation,
1N/A * NASA Ames Research Center, 6/81.
1N/A * Usenix ;login:, February/March, 1983, p. 8.
1N/A *
1N/A * discipline/method interface
1N/A *
1N/A * Glenn Fowler
1N/A * AT&T Research
1N/A * modified from the original BSD source
1N/A *
1N/A * 'fastfind' scans a file list for the full pathname of a file
1N/A * given only a piece of the name. The list is processed with
1N/A * with "front-compression" and bigram coding. Front compression reduces
1N/A * space by a factor of 4-5, bigram coding by a further 20-25%.
1N/A *
1N/A * there are 4 methods:
1N/A *
1N/A * FF_old original with 7 bit bigram encoding (no magic)
1N/A * FF_gnu 8 bit clean front compression (FF_gnu_magic)
1N/A * FF_dir FF_gnu with sfgetl/sfputl and trailing / on dirs (FF_dir_magic)
1N/A * FF_typ FF_dir with (mime) types (FF_typ_magic)
1N/A *
1N/A * the bigram encoding steals the eighth bit (that's why its FF_old)
1N/A * maybe one day we'll limit it to readonly:
1N/A *
1N/A * 0-2*FF_OFF likeliest differential counts + offset to make nonnegative
1N/A * FF_ESC 4 byte big-endian out-of-range count+FF_OFF follows
1N/A * FF_MIN-FF_MAX ascii residue
1N/A * >=FF_MAX bigram codes
1N/A *
1N/A * a two-tiered string search technique is employed
1N/A *
1N/A * a metacharacter-free subpattern and partial pathname is matched
1N/A * backwards to avoid full expansion of the pathname list
1N/A *
1N/A * then the actual shell glob-style regular expression (if in this form)
1N/A * is matched against the candidate pathnames using the slower regexec()
1N/A *
1N/A * The original BSD code is covered by the BSD license:
1N/A *
1N/A * Copyright (c) 1985, 1993, 1999
1N/A * The Regents of the University of California. All rights reserved.
1N/A *
1N/A * Redistribution and use in source and binary forms, with or without
1N/A * modification, are permitted provided that the following conditions
1N/A * are met:
1N/A * 1. Redistributions of source code must retain the above copyright
1N/A * notice, this list of conditions and the following disclaimer.
1N/A * 2. Redistributions in binary form must reproduce the above copyright
1N/A * notice, this list of conditions and the following disclaimer in the
1N/A * documentation and/or other materials provided with the distribution.
1N/A * 3. Neither the name of the University nor the names of its contributors
1N/A * may be used to endorse or promote products derived from this software
1N/A * without specific prior written permission.
1N/A *
1N/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1N/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1N/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1N/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1N/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1N/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1N/A * SUCH DAMAGE.
1N/A */
1N/A
1N/Astatic const char id[] = "\n@(#)$Id: fastfind (AT&T Research) 2002-10-02 $\0\n";
1N/A
1N/Astatic const char lib[] = "libast:fastfind";
1N/A
1N/A#include "findlib.h"
1N/A
1N/A#define FIND_MATCH "*/(find|locate)/*"
1N/A
1N/A/*
1N/A * this db could be anywhere
1N/A * findcodes[] directories are checked for findnames[i]
1N/A */
1N/A
1N/Astatic char* findcodes[] =
1N/A{
1N/A 0,
1N/A 0,
1N/A FIND_CODES,
1N/A "/usr/local/share/lib",
1N/A "/usr/local/lib",
1N/A "/usr/share/lib",
1N/A "/usr/lib",
1N/A "/var/spool",
1N/A "/usr/local/var",
1N/A "/var/lib",
1N/A "/var/lib/slocate",
1N/A "/var/db",
1N/A};
1N/A
1N/Astatic char* findnames[] =
1N/A{
1N/A "find/codes",
1N/A "find/find.codes",
1N/A "locate/locatedb",
1N/A "locatedb",
1N/A "locate.database",
1N/A "slocate.db",
1N/A};
1N/A
1N/A/*
1N/A * convert t to lower case and drop leading x- and x- after /
1N/A * converted value copied to b of size n
1N/A */
1N/A
1N/Achar*
1N/Atypefix(char* buf, size_t n, register const char* t)
1N/A{
1N/A register int c;
1N/A register char* b = buf;
1N/A
1N/A if ((*t == 'x' || *t == 'X') && *(t + 1) == '-')
1N/A t += 2;
1N/A while (c = *t++)
1N/A {
1N/A if (isupper(c))
1N/A c = tolower(c);
1N/A if ((*b++ = c) == '/' && (*t == 'x' || *t == 'X') && *(t + 1) == '-')
1N/A t += 2;
1N/A }
1N/A *b = 0;
1N/A return buf;
1N/A}
1N/A
1N/A/*
1N/A * return a fastfind stream handle for pattern
1N/A */
1N/A
1N/AFind_t*
1N/Afindopen(const char* file, const char* pattern, const char* type, Finddisc_t* disc)
1N/A{
1N/A register Find_t* fp;
1N/A register char* p;
1N/A register char* s;
1N/A register char* b;
1N/A register int i;
1N/A register int j;
1N/A char* path;
1N/A int brace = 0;
1N/A int paren = 0;
1N/A int k;
1N/A int q;
1N/A int fd;
1N/A int uid;
1N/A Vmalloc_t* vm;
1N/A Type_t* tp;
1N/A struct stat st;
1N/A
1N/A
1N/A if (!(vm = vmopen(Vmdcheap, Vmbest, 0)))
1N/A goto nospace;
1N/A
1N/A /*
1N/A * NOTE: searching for FIND_CODES would be much simpler if we
1N/A * just stuck with our own, but we also support GNU
1N/A * locate codes and have to search for the one of a
1N/A * bazillion possible names for that file
1N/A */
1N/A
1N/A if (!findcodes[1])
1N/A findcodes[1] = getenv(FIND_CODES_ENV);
1N/A if (disc->flags & FIND_GENERATE)
1N/A {
1N/A if (!(fp = (Find_t*)vmnewof(vm, 0, Find_t, 1, sizeof(Encode_t) - sizeof(Code_t))))
1N/A goto nospace;
1N/A fp->vm = vm;
1N/A fp->id = lib;
1N/A fp->disc = disc;
1N/A fp->generate = 1;
1N/A if (file && (!*file || streq(file, "-")))
1N/A file = 0;
1N/A uid = geteuid();
1N/A j = (findcodes[0] = (char*)file) && *file == '/' ? 1 : elementsof(findcodes);
1N/A
1N/A /*
1N/A * look for the codes file, but since it may not exist yet,
1N/A * also look for the containing directory if i<2 or if
1N/A * it is sufficiently qualified (FIND_MATCH)
1N/A */
1N/A
1N/A for (i = 0; i < j; i++)
1N/A if (path = findcodes[i])
1N/A {
1N/A if (*path == '/')
1N/A {
1N/A if (!stat(path, &st))
1N/A {
1N/A if (S_ISDIR(st.st_mode))
1N/A {
1N/A for (k = 0; k < elementsof(findnames); k++)
1N/A {
1N/A sfsprintf(fp->encode.file, sizeof(fp->encode.file), "%s/%s", path, findnames[k]);
1N/A if (!eaccess(fp->encode.file, R_OK|W_OK))
1N/A {
1N/A path = fp->encode.file;
1N/A break;
1N/A }
1N/A if (strchr(findnames[k], '/') && (b = strrchr(fp->encode.file, '/')))
1N/A {
1N/A *b = 0;
1N/A if (!stat(fp->encode.file, &st) && st.st_uid == uid && (st.st_mode & S_IWUSR))
1N/A {
1N/A *b = '/';
1N/A path = fp->encode.file;
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A if (k < elementsof(findnames))
1N/A break;
1N/A }
1N/A else if (st.st_uid == uid && (st.st_mode & S_IWUSR))
1N/A {
1N/A sfsprintf(fp->encode.file, sizeof(fp->encode.file), "%s", path);
1N/A path = fp->encode.file;
1N/A break;
1N/A }
1N/A }
1N/A else if (i < 2 || strmatch(path, FIND_MATCH))
1N/A {
1N/A sfsprintf(fp->encode.file, sizeof(fp->encode.file), "%s", path);
1N/A if (b = strrchr(fp->encode.file, '/'))
1N/A {
1N/A *b = 0;
1N/A if (!stat(fp->encode.file, &st) && st.st_uid == uid && (st.st_mode & S_IWUSR))
1N/A {
1N/A *b = '/';
1N/A path = fp->encode.file;
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A }
1N/A else if (pathpath(path, "", PATH_REGULAR|PATH_READ|PATH_WRITE, fp->encode.file, sizeof(fp->encode.file)))
1N/A {
1N/A path = fp->encode.file;
1N/A break;
1N/A }
1N/A else if (b = strrchr(path, '/'))
1N/A {
1N/A sfsprintf(fp->encode.file, sizeof(fp->encode.file), "%-.*s", b - path, path);
1N/A if (pathpath(fp->encode.file, "", PATH_EXECUTE|PATH_READ|PATH_WRITE, fp->encode.temp, sizeof(fp->encode.temp)) &&
1N/A !stat(fp->encode.temp, &st) && st.st_uid == uid && (st.st_mode & S_IWUSR))
1N/A {
1N/A sfsprintf(fp->encode.file, sizeof(fp->encode.file), "%s%s", fp->encode.temp, b);
1N/A path = fp->encode.file;
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A if (i >= j)
1N/A {
1N/A if (fp->disc->errorf)
1N/A (*fp->disc->errorf)(fp, fp->disc, 2, "%s: cannot locate codes", file ? file : findcodes[2]);
1N/A goto drop;
1N/A }
1N/A if (fp->disc->flags & FIND_OLD)
1N/A {
1N/A /*
1N/A * FF_old generates temp data that is read
1N/A * in a second pass to generate the real codes
1N/A */
1N/A
1N/A fp->method = FF_old;
1N/A if (!(fp->fp = sftmp(32 * PATH_MAX)))
1N/A {
1N/A if (fp->disc->errorf)
1N/A (*fp->disc->errorf)(fp, fp->disc, ERROR_SYSTEM|2, "cannot create tmp file");
1N/A goto drop;
1N/A }
1N/A }
1N/A else
1N/A {
1N/A /*
1N/A * the rest generate into a temp file that
1N/A * is simply renamed on completion
1N/A */
1N/A
1N/A if (s = strrchr(path, '/'))
1N/A {
1N/A *s = 0;
1N/A p = path;
1N/A }
1N/A else
1N/A p = ".";
1N/A if (!pathtemp(fp->encode.temp, sizeof(fp->encode.temp), p, "ff", &fd))
1N/A {
1N/A if (fp->disc->errorf)
1N/A (*fp->disc->errorf)(fp, fp->disc, ERROR_SYSTEM|2, "%s: cannot create tmp file in this directory", p ? p : ".");
1N/A goto drop;
1N/A }
1N/A if (s)
1N/A *s = '/';
1N/A if (!(fp->fp = sfnew(NiL, NiL, (size_t)SF_UNBOUND, fd, SF_WRITE)))
1N/A {
1N/A if (fp->disc->errorf)
1N/A (*fp->disc->errorf)(fp, fp->disc, ERROR_SYSTEM|2, "%s: cannot open tmp file", fp->encode.temp);
1N/A close(fd);
1N/A goto drop;
1N/A }
1N/A if (fp->disc->flags & FIND_TYPE)
1N/A {
1N/A fp->method = FF_typ;
1N/A fp->encode.namedisc.key = offsetof(Type_t, name);
1N/A fp->encode.namedisc.link = offsetof(Type_t, byname);
1N/A fp->encode.indexdisc.key = offsetof(Type_t, index);
1N/A fp->encode.indexdisc.size = sizeof(unsigned long);
1N/A fp->encode.indexdisc.link = offsetof(Type_t, byindex);
1N/A s = "system/dir";
1N/A if (!(fp->encode.namedict = dtopen(&fp->encode.namedisc, Dttree)) || !(fp->encode.indexdict = dtopen(&fp->encode.indexdisc, Dttree)) || !(tp = newof(0, Type_t, 1, strlen(s) + 1)))
1N/A {
1N/A if (fp->encode.namedict)
1N/A dtclose(fp->encode.namedict);
1N/A if (fp->disc->errorf)
1N/A (*fp->disc->errorf)(fp, fp->disc, 2, "cannot allocate type table");
1N/A goto drop;
1N/A }
1N/A
1N/A /*
1N/A * type index 1 is always system/dir
1N/A */
1N/A
1N/A tp->index = ++fp->types;
1N/A strcpy(tp->name, s);
1N/A dtinsert(fp->encode.namedict, tp);
1N/A dtinsert(fp->encode.indexdict, tp);
1N/A }
1N/A else if (fp->disc->flags & FIND_GNU)
1N/A {
1N/A fp->method = FF_gnu;
1N/A sfputc(fp->fp, 0);
1N/A sfputr(fp->fp, FF_gnu_magic, 0);
1N/A }
1N/A else
1N/A {
1N/A fp->method = FF_dir;
1N/A sfputc(fp->fp, 0);
1N/A sfputr(fp->fp, FF_dir_magic, 0);
1N/A }
1N/A }
1N/A }
1N/A else
1N/A {
1N/A i = sizeof(Decode_t) + sizeof(Code_t);
1N/A if (!pattern || !*pattern)
1N/A pattern = "*";
1N/A i += (j = 2 * (strlen(pattern) + 1));
1N/A if (!(fp = (Find_t*)vmnewof(vm, 0, Find_t, 1, i)))
1N/A {
1N/A vmclose(vm);
1N/A return 0;
1N/A }
1N/A fp->vm = vm;
1N/A fp->id = lib;
1N/A fp->disc = disc;
1N/A if (disc->flags & FIND_ICASE)
1N/A fp->decode.ignorecase = 1;
1N/A j = (findcodes[0] = (char*)file) && *file == '/' ? 1 : elementsof(findcodes);
1N/A for (i = 0; i < j; i++)
1N/A if (path = findcodes[i])
1N/A {
1N/A if (*path == '/')
1N/A {
1N/A if (!stat(path, &st))
1N/A {
1N/A if (S_ISDIR(st.st_mode))
1N/A {
1N/A for (k = 0; k < elementsof(findnames); k++)
1N/A {
1N/A sfsprintf(fp->decode.path, sizeof(fp->decode.path), "%s/%s", path, findnames[k]);
1N/A if (fp->fp = sfopen(NiL, fp->decode.path, "r"))
1N/A {
1N/A path = fp->decode.path;
1N/A break;
1N/A }
1N/A }
1N/A if (fp->fp)
1N/A break;
1N/A }
1N/A else if (fp->fp = sfopen(NiL, path, "r"))
1N/A break;
1N/A }
1N/A }
1N/A else if ((path = pathpath(path, "", PATH_REGULAR|PATH_READ, fp->decode.path, sizeof(fp->decode.path))) && (fp->fp = sfopen(NiL, path, "r")))
1N/A break;
1N/A }
1N/A if (!fp->fp)
1N/A {
1N/A if (fp->disc->errorf)
1N/A (*fp->disc->errorf)(fp, fp->disc, 2, "%s: cannot locate codes", file ? file : findcodes[2]);
1N/A goto drop;
1N/A }
1N/A if (fstat(sffileno(fp->fp), &st))
1N/A {
1N/A if (fp->disc->errorf)
1N/A (*fp->disc->errorf)(fp, fp->disc, 2, "%s: cannot stat codes", path);
1N/A goto drop;
1N/A }
1N/A if (fp->secure = ((st.st_mode & (S_IRGRP|S_IROTH)) == S_IRGRP) && st.st_gid == getegid() && getegid() != getgid())
1N/A setgid(getgid());
1N/A fp->stamp = st.st_mtime;
1N/A b = (s = fp->decode.temp) + 1;
1N/A for (i = 0; i < elementsof(fp->decode.bigram1); i++)
1N/A {
1N/A if ((j = sfgetc(fp->fp)) == EOF)
1N/A goto invalid;
1N/A if (!(*s++ = fp->decode.bigram1[i] = j) && i)
1N/A {
1N/A i = -i;
1N/A break;
1N/A }
1N/A if ((j = sfgetc(fp->fp)) == EOF)
1N/A goto invalid;
1N/A if (!(*s++ = fp->decode.bigram2[i] = j) && (i || fp->decode.bigram1[0] >= '0' && fp->decode.bigram1[0] <= '1'))
1N/A break;
1N/A }
1N/A if (streq(b, FF_typ_magic))
1N/A {
1N/A if (type)
1N/A {
1N/A type = (const char*)typefix(fp->decode.bigram2, sizeof(fp->decode.bigram2), type);
1N/A memset(fp->decode.bigram1, 0, sizeof(fp->decode.bigram1));
1N/A }
1N/A fp->method = FF_typ;
1N/A for (j = 0, i = 1;; i++)
1N/A {
1N/A if (!(s = sfgetr(fp->fp, 0, 0)))
1N/A goto invalid;
1N/A if (!*s)
1N/A break;
1N/A if (type && strmatch(s, type))
1N/A {
1N/A FF_SET_TYPE(fp, i);
1N/A j++;
1N/A }
1N/A }
1N/A if (type && !j)
1N/A goto drop;
1N/A fp->types = j;
1N/A }
1N/A else if (streq(b, FF_dir_magic))
1N/A fp->method = FF_dir;
1N/A else if (streq(b, FF_gnu_magic))
1N/A fp->method = FF_gnu;
1N/A else if (!*b && *--b >= '0' && *b <= '1')
1N/A {
1N/A fp->method = FF_gnu;
1N/A while (j = sfgetc(fp->fp))
1N/A {
1N/A if (j == EOF || fp->decode.count >= sizeof(fp->decode.path))
1N/A goto invalid;
1N/A fp->decode.path[fp->decode.count++] = j;
1N/A }
1N/A }
1N/A else
1N/A {
1N/A fp->method = FF_old;
1N/A if (i < 0)
1N/A {
1N/A if ((j = sfgetc(fp->fp)) == EOF)
1N/A goto invalid;
1N/A fp->decode.bigram2[i = -i] = j;
1N/A }
1N/A while (++i < elementsof(fp->decode.bigram1))
1N/A {
1N/A if ((j = sfgetc(fp->fp)) == EOF)
1N/A goto invalid;
1N/A fp->decode.bigram1[i] = j;
1N/A if ((j = sfgetc(fp->fp)) == EOF)
1N/A goto invalid;
1N/A fp->decode.bigram2[i] = j;
1N/A }
1N/A if ((fp->decode.peek = sfgetc(fp->fp)) != FF_OFF)
1N/A goto invalid;
1N/A }
1N/A
1N/A /*
1N/A * set up the physical dir table
1N/A */
1N/A
1N/A if (disc->version >= 19980301L)
1N/A {
1N/A fp->verifyf = disc->verifyf;
1N/A if (disc->dirs && *disc->dirs)
1N/A {
1N/A for (k = 0; disc->dirs[k]; k++);
1N/A if (k == 1 && streq(disc->dirs[0], "/"))
1N/A k = 0;
1N/A if (k)
1N/A {
1N/A if (!(fp->dirs = vmnewof(fp->vm, 0, char*, 2 * k + 1, 0)))
1N/A goto drop;
1N/A if (!(fp->lens = vmnewof(fp->vm, 0, int, 2 * k, 0)))
1N/A goto drop;
1N/A p = 0;
1N/A b = fp->decode.temp;
1N/A j = fp->method == FF_old || fp->method == FF_gnu;
1N/A
1N/A /*
1N/A * fill the dir list with logical and
1N/A * physical names since we don't know
1N/A * which way the db was encoded (it
1N/A * could be *both* ways)
1N/A */
1N/A
1N/A for (i = q = 0; i < k; i++)
1N/A {
1N/A if (*(s = disc->dirs[i]) == '/')
1N/A sfsprintf(b, sizeof(fp->decode.temp) - 1, "%s", s);
1N/A else if (!p && !(p = getcwd(fp->decode.path, sizeof(fp->decode.path))))
1N/A goto nospace;
1N/A else
1N/A sfsprintf(b, sizeof(fp->decode.temp) - 1, "%s/%s", p, s);
1N/A s = pathcanon(b, sizeof(fp->decode.temp), 0);
1N/A *s = '/';
1N/A *(s + 1) = 0;
1N/A if (!(fp->dirs[q] = vmstrdup(fp->vm, b)))
1N/A goto nospace;
1N/A if (j)
1N/A (fp->dirs[q])[s - b] = 0;
1N/A q++;
1N/A *s = 0;
1N/A s = pathcanon(b, sizeof(fp->decode.temp), PATH_PHYSICAL);
1N/A *s = '/';
1N/A *(s + 1) = 0;
1N/A if (!strneq(b, fp->dirs[q - 1], s - b))
1N/A {
1N/A if (!(fp->dirs[q] = vmstrdup(fp->vm, b)))
1N/A goto nospace;
1N/A if (j)
1N/A (fp->dirs[q])[s - b] = 0;
1N/A q++;
1N/A }
1N/A }
1N/A strsort(fp->dirs, q, strcasecmp);
1N/A for (i = 0; i < q; i++)
1N/A fp->lens[i] = strlen(fp->dirs[i]);
1N/A }
1N/A }
1N/A }
1N/A if (fp->verifyf || (disc->flags & FIND_VERIFY))
1N/A {
1N/A if (fp->method != FF_dir && fp->method != FF_typ)
1N/A {
1N/A if (fp->disc->errorf)
1N/A (*fp->disc->errorf)(fp, fp->disc, 2, "%s: %s code format does not support directory verification", path, fp->method == FF_gnu ? FF_gnu_magic : "OLD-BIGRAM");
1N/A goto drop;
1N/A }
1N/A fp->verify = 1;
1N/A }
1N/A
1N/A /*
1N/A * extract last glob-free subpattern in name for fast pre-match
1N/A * prepend 0 for backwards match
1N/A */
1N/A
1N/A if (p = s = (char*)pattern)
1N/A {
1N/A b = fp->decode.pattern;
1N/A for (;;)
1N/A {
1N/A switch (*b++ = *p++)
1N/A {
1N/A case 0:
1N/A break;
1N/A case '\\':
1N/A s = p;
1N/A if (!*p++)
1N/A break;
1N/A continue;
1N/A case '[':
1N/A if (!brace)
1N/A {
1N/A brace++;
1N/A if (*p == ']')
1N/A p++;
1N/A }
1N/A continue;
1N/A case ']':
1N/A if (brace)
1N/A {
1N/A brace--;
1N/A s = p;
1N/A }
1N/A continue;
1N/A case '(':
1N/A if (!brace)
1N/A paren++;
1N/A continue;
1N/A case ')':
1N/A if (!brace && paren > 0 && !--paren)
1N/A s = p;
1N/A continue;
1N/A case '|':
1N/A case '&':
1N/A if (!brace && !paren)
1N/A {
1N/A s = "";
1N/A break;
1N/A }
1N/A continue;
1N/A case '*':
1N/A case '?':
1N/A s = p;
1N/A continue;
1N/A default:
1N/A continue;
1N/A }
1N/A break;
1N/A }
1N/A if (s != pattern && !streq(pattern, "*"))
1N/A {
1N/A fp->decode.match = 1;
1N/A if (i = regcomp(&fp->decode.re, pattern, REG_SHELL|REG_AUGMENTED|(fp->decode.ignorecase?REG_ICASE:0)))
1N/A {
1N/A if (disc->errorf)
1N/A {
1N/A regerror(i, &fp->decode.re, fp->decode.temp, sizeof(fp->decode.temp));
1N/A (*fp->disc->errorf)(fp, fp->disc, 2, "%s: %s", pattern, fp->decode.temp);
1N/A }
1N/A goto drop;
1N/A }
1N/A }
1N/A if (*s)
1N/A {
1N/A *b++ = 0;
1N/A while (i = *s++)
1N/A *b++ = i;
1N/A *b-- = 0;
1N/A fp->decode.end = b;
1N/A if (fp->decode.ignorecase)
1N/A for (s = fp->decode.pattern; s <= b; s++)
1N/A if (isupper(*s))
1N/A *s = tolower(*s);
1N/A }
1N/A }
1N/A }
1N/A return fp;
1N/A nospace:
1N/A if (disc->errorf)
1N/A (*fp->disc->errorf)(fp, fp->disc, 2, "out of space");
1N/A if (!vm)
1N/A return 0;
1N/A if (!fp)
1N/A {
1N/A vmclose(vm);
1N/A return 0;
1N/A }
1N/A goto drop;
1N/A invalid:
1N/A if (fp->disc->errorf)
1N/A (*fp->disc->errorf)(fp, fp->disc, 2, "%s: invalid codes", path);
1N/A drop:
1N/A if (!fp->generate && fp->decode.match)
1N/A regfree(&fp->decode.re);
1N/A if (fp->fp)
1N/A sfclose(fp->fp);
1N/A vmclose(fp->vm);
1N/A return 0;
1N/A}
1N/A
1N/A/*
1N/A * return the next fastfind path
1N/A * 0 returned when list exhausted
1N/A */
1N/A
1N/Achar*
1N/Afindread(register Find_t* fp)
1N/A{
1N/A register char* p;
1N/A register char* q;
1N/A register char* s;
1N/A register char* b;
1N/A register char* e;
1N/A register int c;
1N/A register int n;
1N/A register int m;
1N/A int ignorecase;
1N/A int t;
1N/A unsigned char w[4];
1N/A struct stat st;
1N/A
1N/A if (fp->generate)
1N/A return 0;
1N/A if (fp->decode.restore)
1N/A {
1N/A *fp->decode.restore = '/';
1N/A fp->decode.restore = 0;
1N/A }
1N/A ignorecase = fp->decode.ignorecase ? STR_ICASE : 0;
1N/A c = fp->decode.peek;
1N/A next:
1N/A for (;;)
1N/A {
1N/A switch (fp->method)
1N/A {
1N/A case FF_dir:
1N/A t = 0;
1N/A n = sfgetl(fp->fp);
1N/A goto grab;
1N/A case FF_gnu:
1N/A if ((c = sfgetc(fp->fp)) == EOF)
1N/A return 0;
1N/A if (c == 0x80)
1N/A {
1N/A if ((c = sfgetc(fp->fp)) == EOF)
1N/A return 0;
1N/A n = c << 8;
1N/A if ((c = sfgetc(fp->fp)) == EOF)
1N/A return 0;
1N/A n |= c;
1N/A if (n & 0x8000)
1N/A n = (n - 0xffff) - 1;
1N/A }
1N/A else if ((n = c) & 0x80)
1N/A n = (n - 0xff) - 1;
1N/A t = 0;
1N/A goto grab;
1N/A case FF_typ:
1N/A t = sfgetu(fp->fp);
1N/A n = sfgetl(fp->fp);
1N/A grab:
1N/A p = fp->decode.path + (fp->decode.count += n);
1N/A do
1N/A {
1N/A if ((c = sfgetc(fp->fp)) == EOF)
1N/A return 0;
1N/A } while (*p++ = c);
1N/A p -= 2;
1N/A break;
1N/A case FF_old:
1N/A if (c == EOF)
1N/A {
1N/A fp->decode.peek = c;
1N/A return 0;
1N/A }
1N/A if (c == FF_ESC)
1N/A {
1N/A if (sfread(fp->fp, w, sizeof(w)) != sizeof(w))
1N/A return 0;
1N/A if (fp->decode.swap >= 0)
1N/A {
1N/A c = (int32_t)((w[0] << 24) | (w[1] << 16) | (w[2] << 8) | w[3]);
1N/A if (!fp->decode.swap)
1N/A {
1N/A /*
1N/A * the old format uses machine
1N/A * byte order; this test uses
1N/A * the smallest magnitude of
1N/A * both byte orders on the
1N/A * first encoded path motion
1N/A * to determine the original
1N/A * byte order
1N/A */
1N/A
1N/A m = c;
1N/A if (m < 0)
1N/A m = -m;
1N/A n = (int32_t)((w[3] << 24) | (w[2] << 16) | (w[1] << 8) | w[0]);
1N/A if (n < 0)
1N/A n = -n;
1N/A if (m < n)
1N/A fp->decode.swap = 1;
1N/A else
1N/A {
1N/A fp->decode.swap = -1;
1N/A c = (int32_t)((w[3] << 24) | (w[2] << 16) | (w[1] << 8) | w[0]);
1N/A }
1N/A }
1N/A }
1N/A else
1N/A c = (int32_t)((w[3] << 24) | (w[2] << 16) | (w[1] << 8) | w[0]);
1N/A }
1N/A fp->decode.count += c - FF_OFF;
1N/A for (p = fp->decode.path + fp->decode.count; (c = sfgetc(fp->fp)) > FF_ESC;)
1N/A if (c & (1<<(CHAR_BIT-1)))
1N/A {
1N/A *p++ = fp->decode.bigram1[c & ((1<<(CHAR_BIT-1))-1)];
1N/A *p++ = fp->decode.bigram2[c & ((1<<(CHAR_BIT-1))-1)];
1N/A }
1N/A else
1N/A *p++ = c;
1N/A *p-- = 0;
1N/A t = 0;
1N/A break;
1N/A }
1N/A b = fp->decode.path;
1N/A if (fp->decode.found)
1N/A fp->decode.found = 0;
1N/A else
1N/A b += fp->decode.count;
1N/A if (fp->dirs)
1N/A for (;;)
1N/A {
1N/A if (!*fp->dirs)
1N/A return 0;
1N/A
1N/A /*
1N/A * use the ordering and lengths to prune
1N/A * comparison function calls
1N/A * (*fp->dirs)[*fp->lens]=='/' if its
1N/A * already been matched
1N/A */
1N/A
1N/A if ((n = p - fp->decode.path + 1) > (m = *fp->lens))
1N/A {
1N/A if (!(*fp->dirs)[m])
1N/A goto next;
1N/A if (!strncasecmp(*fp->dirs, fp->decode.path, m))
1N/A break;
1N/A }
1N/A else if (n == m)
1N/A {
1N/A if (!(*fp->dirs)[m])
1N/A {
1N/A if (!(n = strcasecmp(*fp->dirs, fp->decode.path)) && (ignorecase || !strcmp(*fp->dirs, fp->decode.path)))
1N/A {
1N/A if (m > 0)
1N/A {
1N/A (*fp->dirs)[m] = '/';
1N/A if ((*fp->dirs)[m - 1] != '/')
1N/A (*fp->dirs)[++(*fp->lens)] = '/';
1N/A }
1N/A break;
1N/A }
1N/A if (n >= 0)
1N/A goto next;
1N/A }
1N/A }
1N/A else if (!(*fp->dirs)[m])
1N/A goto next;
1N/A fp->dirs++;
1N/A fp->lens++;
1N/A }
1N/A if (fp->verify && (*p == '/' || t == 1))
1N/A {
1N/A if ((n = p - fp->decode.path))
1N/A *p = 0;
1N/A else
1N/A n = 1;
1N/A if (fp->verifyf)
1N/A n = (*fp->verifyf)(fp, fp->decode.path, n, fp->disc);
1N/A else if (stat(fp->decode.path, &st))
1N/A n = -1;
1N/A else if ((unsigned long)st.st_mtime > fp->stamp)
1N/A n = 1;
1N/A else
1N/A n = 0;
1N/A *p = '/';
1N/A
1N/A /*
1N/A * n<0 skip this subtree
1N/A * n==0 keep as is
1N/A * n>0 read this dir now
1N/A */
1N/A
1N/A /* NOT IMPLEMENTED YET */
1N/A }
1N/A if (FF_OK_TYPE(fp, t))
1N/A {
1N/A if (fp->decode.end)
1N/A {
1N/A if (*(s = p) == '/')
1N/A s--;
1N/A if (*fp->decode.pattern == '/' && b > fp->decode.path)
1N/A b--;
1N/A for (; s >= b; s--)
1N/A if (*s == *fp->decode.end || ignorecase && tolower(*s) == *fp->decode.end)
1N/A {
1N/A if (ignorecase)
1N/A for (e = fp->decode.end - 1, q = s - 1; *e && (*q == *e || tolower(*q) == *e); e--, q--);
1N/A else
1N/A for (e = fp->decode.end - 1, q = s - 1; *e && *q == *e; e--, q--);
1N/A if (!*e)
1N/A {
1N/A fp->decode.found = 1;
1N/A if (!fp->decode.match || strgrpmatch(fp->decode.path, fp->decode.pattern, NiL, 0, STR_MAXIMAL|STR_LEFT|STR_RIGHT|ignorecase))
1N/A {
1N/A fp->decode.peek = c;
1N/A if (*p == '/')
1N/A *(fp->decode.restore = p) = 0;
1N/A if (!fp->secure || !access(fp->decode.path, F_OK))
1N/A return fp->decode.path;
1N/A }
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A else if (!fp->decode.match || !(n = regexec(&fp->decode.re, fp->decode.path, 0, NiL, 0)))
1N/A {
1N/A fp->decode.peek = c;
1N/A if (*p == '/' && p > fp->decode.path)
1N/A *(fp->decode.restore = p) = 0;
1N/A if (!fp->secure || !access(fp->decode.path, F_OK))
1N/A return fp->decode.path;
1N/A }
1N/A else if (n != REG_NOMATCH)
1N/A {
1N/A if (fp->disc->errorf)
1N/A {
1N/A regerror(n, &fp->decode.re, fp->decode.temp, sizeof(fp->decode.temp));
1N/A (*fp->disc->errorf)(fp, fp->disc, 2, "%s: %s", fp->decode.pattern, fp->decode.temp);
1N/A }
1N/A return 0;
1N/A }
1N/A }
1N/A }
1N/A}
1N/A
1N/A/*
1N/A * add path to the code table
1N/A * paths are assumed to be in sort order
1N/A */
1N/A
1N/Aint
1N/Afindwrite(register Find_t* fp, const char* path, size_t len, const char* type)
1N/A{
1N/A register unsigned char* s;
1N/A register unsigned char* e;
1N/A register unsigned char* p;
1N/A register int n;
1N/A register int d;
1N/A register Type_t* x;
1N/A register unsigned long u;
1N/A
1N/A if (!fp->generate)
1N/A return -1;
1N/A if (type && fp->method == FF_dir)
1N/A {
1N/A len = sfsprintf(fp->encode.mark, sizeof(fp->encode.mark), "%-.*s/", len, path);
1N/A path = fp->encode.mark;
1N/A }
1N/A s = (unsigned char*)path;
1N/A if (len <= 0)
1N/A len = strlen(path);
1N/A if (len < sizeof(fp->encode.path))
1N/A e = s + len++;
1N/A else
1N/A {
1N/A len = sizeof(fp->encode.path) - 1;
1N/A e = s + len;
1N/A }
1N/A p = (unsigned char*)fp->encode.path;
1N/A while (s < e)
1N/A {
1N/A if (*s != *p++)
1N/A break;
1N/A s++;
1N/A }
1N/A n = s - (unsigned char*)path;
1N/A switch (fp->method)
1N/A {
1N/A case FF_gnu:
1N/A d = n - fp->encode.prefix;
1N/A if (d >= -127 && d <= 127)
1N/A sfputc(fp->fp, d & 0xff);
1N/A else
1N/A {
1N/A sfputc(fp->fp, 0x80);
1N/A sfputc(fp->fp, (d >> 8) & 0xff);
1N/A sfputc(fp->fp, d & 0xff);
1N/A }
1N/A fp->encode.prefix = n;
1N/A sfputr(fp->fp, (char*)s, 0);
1N/A break;
1N/A case FF_old:
1N/A sfprintf(fp->fp, "%ld", n - fp->encode.prefix + FF_OFF);
1N/A fp->encode.prefix = n;
1N/A sfputc(fp->fp, ' ');
1N/A p = s;
1N/A while (s < e)
1N/A {
1N/A n = *s++;
1N/A if (s >= e)
1N/A break;
1N/A fp->encode.code[n][*s++]++;
1N/A }
1N/A while (p < e)
1N/A {
1N/A if ((n = *p++) < FF_MIN || n >= FF_MAX)
1N/A n = '?';
1N/A sfputc(fp->fp, n);
1N/A }
1N/A sfputc(fp->fp, 0);
1N/A break;
1N/A case FF_typ:
1N/A if (type)
1N/A {
1N/A type = (const char*)typefix((char*)fp->encode.bigram, sizeof(fp->encode.bigram), type);
1N/A if (x = (Type_t*)dtmatch(fp->encode.namedict, type))
1N/A u = x->index;
1N/A else if (!(x = newof(0, Type_t, 1, strlen(type) + 1)))
1N/A u = 0;
1N/A else
1N/A {
1N/A u = x->index = ++fp->types;
1N/A strcpy(x->name, type);
1N/A dtinsert(fp->encode.namedict, x);
1N/A dtinsert(fp->encode.indexdict, x);
1N/A }
1N/A }
1N/A else
1N/A u = 0;
1N/A sfputu(fp->fp, u);
1N/A /*FALLTHROUGH...*/
1N/A case FF_dir:
1N/A d = n - fp->encode.prefix;
1N/A sfputl(fp->fp, d);
1N/A fp->encode.prefix = n;
1N/A sfputr(fp->fp, (char*)s, 0);
1N/A break;
1N/A }
1N/A memcpy(fp->encode.path, path, len);
1N/A return 0;
1N/A}
1N/A
1N/A/*
1N/A * findsync() helper
1N/A */
1N/A
1N/Astatic int
1N/Afinddone(register Find_t* fp)
1N/A{
1N/A int r;
1N/A
1N/A if (sfsync(fp->fp))
1N/A {
1N/A if (fp->disc->errorf)
1N/A (*fp->disc->errorf)(fp, fp->disc, 2, "%s: write error [sfsync]", fp->encode.file);
1N/A return -1;
1N/A }
1N/A if (sferror(fp->fp))
1N/A {
1N/A if (fp->disc->errorf)
1N/A (*fp->disc->errorf)(fp, fp->disc, 2, "%s: write error [sferror]", fp->encode.file);
1N/A return -1;
1N/A }
1N/A r = sfclose(fp->fp);
1N/A fp->fp = 0;
1N/A if (r)
1N/A {
1N/A if (fp->disc->errorf)
1N/A (*fp->disc->errorf)(fp, fp->disc, 2, "%s: write error [sfclose]", fp->encode.file);
1N/A return -1;
1N/A }
1N/A return 0;
1N/A}
1N/A
1N/A/*
1N/A * finish the code table
1N/A */
1N/A
1N/Astatic int
1N/Afindsync(register Find_t* fp)
1N/A{
1N/A register char* s;
1N/A register int n;
1N/A register int m;
1N/A register int d;
1N/A register Type_t* x;
1N/A char* t;
1N/A int b;
1N/A long z;
1N/A Sfio_t* sp;
1N/A
1N/A switch (fp->method)
1N/A {
1N/A case FF_dir:
1N/A case FF_gnu:
1N/A /*
1N/A * replace the real file with the temp file
1N/A */
1N/A
1N/A if (finddone(fp))
1N/A goto bad;
1N/A remove(fp->encode.file);
1N/A if (rename(fp->encode.temp, fp->encode.file))
1N/A {
1N/A if (fp->disc->errorf)
1N/A (*fp->disc->errorf)(fp, fp->disc, ERROR_SYSTEM|2, "%s: cannot rename from tmp file %s", fp->encode.file, fp->encode.temp);
1N/A remove(fp->encode.temp);
1N/A return -1;
1N/A }
1N/A break;
1N/A case FF_old:
1N/A /*
1N/A * determine the top FF_MAX bigrams
1N/A */
1N/A
1N/A for (n = 0; n < FF_MAX; n++)
1N/A for (m = 0; m < FF_MAX; m++)
1N/A fp->encode.hits[fp->encode.code[n][m]]++;
1N/A fp->encode.hits[0] = 0;
1N/A m = 1;
1N/A for (n = USHRT_MAX; n >= 0; n--)
1N/A if (d = fp->encode.hits[n])
1N/A {
1N/A fp->encode.hits[n] = m;
1N/A if ((m += d) > FF_MAX)
1N/A break;
1N/A }
1N/A while (--n >= 0)
1N/A fp->encode.hits[n] = 0;
1N/A for (n = FF_MAX - 1; n >= 0; n--)
1N/A for (m = FF_MAX - 1; m >= 0; m--)
1N/A if (fp->encode.hits[fp->encode.code[n][m]])
1N/A {
1N/A d = fp->encode.code[n][m];
1N/A b = fp->encode.hits[d] - 1;
1N/A fp->encode.code[n][m] = b + FF_MAX;
1N/A if (fp->encode.hits[d]++ >= FF_MAX)
1N/A fp->encode.hits[d] = 0;
1N/A fp->encode.bigram[b *= 2] = n;
1N/A fp->encode.bigram[b + 1] = m;
1N/A }
1N/A else
1N/A fp->encode.code[n][m] = 0;
1N/A
1N/A /*
1N/A * commit the real file
1N/A */
1N/A
1N/A if (sfseek(fp->fp, (Sfoff_t)0, SEEK_SET))
1N/A {
1N/A if (fp->disc->errorf)
1N/A (*fp->disc->errorf)(fp, fp->disc, ERROR_SYSTEM|2, "cannot rewind tmp file");
1N/A return -1;
1N/A }
1N/A if (!(sp = sfopen(NiL, fp->encode.file, "w")))
1N/A goto badcreate;
1N/A
1N/A /*
1N/A * dump the bigrams
1N/A */
1N/A
1N/A sfwrite(sp, fp->encode.bigram, sizeof(fp->encode.bigram));
1N/A
1N/A /*
1N/A * encode the massaged paths
1N/A */
1N/A
1N/A while (s = sfgetr(fp->fp, 0, 0))
1N/A {
1N/A z = strtol(s, &t, 0);
1N/A s = t;
1N/A if (z < 0 || z > 2 * FF_OFF)
1N/A {
1N/A sfputc(sp, FF_ESC);
1N/A sfputc(sp, (z >> 24));
1N/A sfputc(sp, (z >> 16));
1N/A sfputc(sp, (z >> 8));
1N/A sfputc(sp, z);
1N/A }
1N/A else
1N/A sfputc(sp, z);
1N/A while (n = *s++)
1N/A {
1N/A if (!(m = *s++))
1N/A {
1N/A sfputc(sp, n);
1N/A break;
1N/A }
1N/A if (d = fp->encode.code[n][m])
1N/A sfputc(sp, d);
1N/A else
1N/A {
1N/A sfputc(sp, n);
1N/A sfputc(sp, m);
1N/A }
1N/A }
1N/A }
1N/A sfclose(fp->fp);
1N/A fp->fp = sp;
1N/A if (finddone(fp))
1N/A goto bad;
1N/A break;
1N/A case FF_typ:
1N/A if (finddone(fp))
1N/A goto bad;
1N/A if (!(fp->fp = sfopen(NiL, fp->encode.temp, "r")))
1N/A {
1N/A if (fp->disc->errorf)
1N/A (*fp->disc->errorf)(fp, fp->disc, ERROR_SYSTEM|2, "%s: cannot read tmp file", fp->encode.temp);
1N/A remove(fp->encode.temp);
1N/A return -1;
1N/A }
1N/A
1N/A /*
1N/A * commit the output file
1N/A */
1N/A
1N/A if (!(sp = sfopen(NiL, fp->encode.file, "w")))
1N/A goto badcreate;
1N/A
1N/A /*
1N/A * write the header magic
1N/A */
1N/A
1N/A sfputc(sp, 0);
1N/A sfputr(sp, FF_typ_magic, 0);
1N/A
1N/A /*
1N/A * write the type table in index order starting with 1
1N/A */
1N/A
1N/A for (x = (Type_t*)dtfirst(fp->encode.indexdict); x; x = (Type_t*)dtnext(fp->encode.indexdict, x))
1N/A sfputr(sp, x->name, 0);
1N/A sfputc(sp, 0);
1N/A
1N/A /*
1N/A * append the front compressed strings
1N/A */
1N/A
1N/A if (sfmove(fp->fp, sp, SF_UNBOUND, -1) < 0 || !sfeof(fp->fp))
1N/A {
1N/A sfclose(sp);
1N/A if (fp->disc->errorf)
1N/A (*fp->disc->errorf)(fp, fp->disc, 2, "%s: cannot append codes", fp->encode.file);
1N/A goto bad;
1N/A }
1N/A sfclose(fp->fp);
1N/A fp->fp = sp;
1N/A if (finddone(fp))
1N/A goto bad;
1N/A remove(fp->encode.temp);
1N/A break;
1N/A }
1N/A return 0;
1N/A badcreate:
1N/A if (fp->disc->errorf)
1N/A (*fp->disc->errorf)(fp, fp->disc, 2, "%s: cannot write codes", fp->encode.file);
1N/A bad:
1N/A if (fp->fp)
1N/A {
1N/A sfclose(fp->fp);
1N/A fp->fp = 0;
1N/A }
1N/A remove(fp->encode.temp);
1N/A return -1;
1N/A}
1N/A
1N/A/*
1N/A * close an open fastfind stream
1N/A */
1N/A
1N/Aint
1N/Afindclose(register Find_t* fp)
1N/A{
1N/A int n = 0;
1N/A
1N/A if (!fp)
1N/A return -1;
1N/A if (fp->generate)
1N/A {
1N/A n = findsync(fp);
1N/A if (fp->encode.indexdict)
1N/A dtclose(fp->encode.indexdict);
1N/A if (fp->encode.namedict)
1N/A dtclose(fp->encode.namedict);
1N/A }
1N/A else
1N/A {
1N/A if (fp->decode.match)
1N/A regfree(&fp->decode.re);
1N/A n = 0;
1N/A }
1N/A if (fp->fp)
1N/A sfclose(fp->fp);
1N/A vmclose(fp->vm);
1N/A return n;
1N/A}