1 /* $Cambridge: exim/src/src/lookups/dsearch.c,v 1.1 2004/10/07 13:10:01 ph10 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2004 */
8 /* See the file NOTICE for conditions of use and distribution. */
10 /* The idea for this code came from Matthew Byng-Maddick, but his original has
11 been heavily reworked a lot for Exim 4 (and it now uses stat() rather than a
16 #include "lf_functions.h"
21 /*************************************************
23 *************************************************/
25 /* See local README for interface description. We open the directory to test
26 whether it exists and whether it is searchable. However, we don't need to keep
27 it open, because the "search" can be done by a call to stat() rather than
28 actually scanning through the list of files. */
31 dsearch_open(uschar *dirname, uschar **errmsg)
33 DIR *dp = opendir(CS dirname);
36 int save_errno = errno;
37 *errmsg = string_open_failed(errno, "%s for directory search", dirname);
46 /*************************************************
48 *************************************************/
50 /* The handle will always be (void *)(-1), but don't try casting it to an
51 integer as this gives warnings on 64-bit systems. */
54 dsearch_check(void *handle, uschar *filename, int modemask, uid_t *owners,
55 gid_t *owngroups, uschar **errmsg)
58 return lf_check_file(-1, filename, S_IFDIR, modemask, owners, owngroups,
59 "dsearch", errmsg) == 0;
63 /*************************************************
65 *************************************************/
67 /* See local README for interface description. We use stat() instead of
68 scanning the directory, as it is hopefully faster to let the OS do the scanning
72 dsearch_find(void *handle, uschar *dirname, uschar *keystring, int length,
73 uschar **result, uschar **errmsg, BOOL *do_cache)
77 uschar filename[PATH_MAX];
79 handle = handle; /* Keep picky compilers happy */
83 if (Ustrchr(keystring, '/') != 0)
85 *errmsg = string_sprintf("key for dsearch lookup contains a slash: %s",
90 if (!string_format(filename, sizeof(filename), "%s/%s", dirname, keystring))
92 *errmsg = US"path name too long";
96 if (Ustat(filename, &statbuf) >= 0)
98 *result = string_copy(keystring);
102 if (errno == ENOENT) return FAIL;
105 *errmsg = string_sprintf("%s: stat failed", filename);
111 /*************************************************
112 * Close entry point *
113 *************************************************/
115 /* See local README for interface description */
118 dsearch_close(void *handle)
120 handle = handle; /* Avoid compiler warning */
123 /* End of lookups/dsearch.c */