dir.c revision d9edfd56422af0e709d4548ce838f2167d1ea115
5fa46bc91672ef5737aee6f99763161511566c24Tinderbox User * Copyright (C) 2004, 2007, 2008 Internet Systems Consortium, Inc. ("ISC")
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * Copyright (C) 1999-2001 Internet Software Consortium.
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * Permission to use, copy, modify, and/or distribute this software for any
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * purpose with or without fee is hereby granted, provided that the above
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * copyright notice and this permission notice appear in all copies.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PERFORMANCE OF THIS SOFTWARE.
28a8f5b0de57d269cf2845c69cb6abe18cbd3b3aMark Andrews/* $Id: dir.c,v 1.16 2008/11/02 23:47:01 tbox Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Principal Authors: DCL */
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington#define ISC_DIR_MAGIC ISC_MAGIC('D', 'I', 'R', '*')
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington#define VALID_DIR(dir) ISC_MAGIC_VALID(dir, ISC_DIR_MAGIC)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley memset(&(dir->entry.find_data), 0, sizeof(dir->entry.find_data));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Allocate workspace and open directory stream. If either one fails,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * NULL will be returned.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyisc_dir_open(isc_dir_t *dir, const char *dirname) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(VALID_DIR(dir) && dir->search_handle == INVALID_HANDLE_VALUE);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Copy directory name. Need to have enough space for the name,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * a possible path separator, the wildcard, and the final NUL.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /* XXXDCL ? */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Append path separator, if needed, and "*".
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (dir->dirname < p && *(p - 1) != '\\' && *(p - 1) != ':')
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *p++ = '\\';
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *p++ = '\0';
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Open stream.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Return previously retrieved file or get next one. Unix's dirent has
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * separate open and read functions, but the Win32 and DOS interfaces open
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * the dir stream and reads the first file in one operation.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(VALID_DIR(dir) && dir->search_handle != INVALID_HANDLE_VALUE);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * start_directory() already filled in the first entry.
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley * Fetch next file in directory.
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * Either the last file has been processed or
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * an error has occured. The former is not
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * really an error, but the latter is.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Make sure that the space for the name is long enough.
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley strcpy(dir->entry.name, dir->entry.find_data.cFileName);
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley * Close directory stream.
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley REQUIRE(VALID_DIR(dir) && dir->search_handle != INVALID_HANDLE_VALUE);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Reposition directory stream at start.
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson REQUIRE(VALID_DIR(dir) && dir->search_handle != INVALID_HANDLE_VALUE);
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * NT cannot reposition the seek pointer to the beginning of the
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * the directory stream, but rather the directory needs to be
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * closed and reopened. The latter might fail.
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * Initialize isc_dir_t structure with new directory. The function
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * returns 0 on failure and nonzero on success.
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * - Be sure to close previous stream before opening new one
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley REQUIRE(dir->search_handle == INVALID_HANDLE_VALUE);
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews * Open stream and retrieve first file.
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews dir->search_handle = FindFirstFile(dir->dirname,
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews if (dir->search_handle == INVALID_HANDLE_VALUE) {
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews * Something went wrong but we don't know what. GetLastError()
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews * could give us more information about the error, but the
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews * MSDN documentation is frustratingly thin about what
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews * possible errors could have resulted. (Score one for
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews * the Unix manual pages.) So there is just this lame error
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews * instead of being able to differentiate ISC_R_NOTFOUND
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews * from ISC_R_UNEXPECTED.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Make sure that the space for the name is long enough.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Fill in the data for the first entry of the directory.
da527e4ff6a013364826637963e7ac372e024f33David Lawrence strcpy(dir->entry.name, dir->entry.find_data.cFileName);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Change the current directory to 'dirname'.
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * mkdtemp is not portable, so this emulates it.
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * Replace trailing Xs with the process-id, zero-filled.
ce3761f64d3d734cc94605026985898900ecc474Bob Halley for (x = templet + strlen(templet) - 1; *x == 'X' && x >= templet;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley x++; /* Set x to start of ex-Xs. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * The BSD algorithm.
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley while (*p != '\0') {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley else if (*p != 'z')
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * Reset character and move to next.
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews if (*p == '\0') {
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * Tried all combinations. errno should already
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * be EEXIST, but ensure it is anyway for
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * isc__errno2result().
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews } while (1);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews if (i == -1)