Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

os_dir.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1997-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: os_dir.c,v 12.2 2005/07/06 23:52:43 dda Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #include "db_int.h"
00013 
00014 /*
00015  * __os_dirlist --
00016  *      Return a list of the files in a directory.
00017  */
00018 int
00019 __os_dirlist(dbenv, dir, namesp, cntp)
00020         DB_ENV *dbenv;
00021         const char *dir;
00022         char ***namesp;
00023         int *cntp;
00024 {
00025         HANDLE dirhandle;
00026         WIN32_FIND_DATA fdata;
00027         int arraysz, cnt, ret;
00028         char **names, *onename;
00029         _TCHAR tfilespec[MAXPATHLEN + 1];
00030         _TCHAR *tdir;
00031 
00032         if (DB_GLOBAL(j_dirlist) != NULL)
00033                 return (DB_GLOBAL(j_dirlist)(dir, namesp, cntp));
00034 
00035         TO_TSTRING(dbenv, dir, tdir, ret);
00036         if (ret != 0)
00037                 return (ret);
00038 
00039         (void)_sntprintf(tfilespec, MAXPATHLEN,
00040             _T("%s%hc*"), tdir, PATH_SEPARATOR[0]);
00041         if ((dirhandle = FindFirstFile(tfilespec, &fdata))
00042             == INVALID_HANDLE_VALUE)
00043                 return (__os_get_errno());
00044 
00045         names = NULL;
00046         arraysz = cnt = ret = 0;
00047         for (;;) {
00048                 if (cnt >= arraysz) {
00049                         arraysz += 100;
00050                         if ((ret = __os_realloc(dbenv,
00051                             arraysz * sizeof(names[0]), &names)) != 0)
00052                                 goto err;
00053                 }
00054                 /*
00055                  * FROM_TSTRING doesn't necessarily allocate new memory, so we
00056                  * must do that explicitly.  Unfortunately, when compiled with
00057                  * UNICODE, we'll copy twice.
00058                  */
00059                 FROM_TSTRING(dbenv, fdata.cFileName, onename, ret);
00060                 if (ret != 0)
00061                         goto err;
00062                 ret = __os_strdup(dbenv, onename, &names[cnt]);
00063                 FREE_STRING(dbenv, onename);
00064                 if (ret != 0)
00065                         goto err;
00066                 cnt++;
00067                 if (!FindNextFile(dirhandle, &fdata)) {
00068                         if (GetLastError() == ERROR_NO_MORE_FILES)
00069                                 break;
00070                         else {
00071                                 ret = __os_get_errno();
00072                                 goto err;
00073                         }
00074                 }
00075         }
00076 
00077 err:    if (!FindClose(dirhandle) && ret == 0)
00078                 ret = __os_get_errno();
00079 
00080         if (ret == 0) {
00081                 *namesp = names;
00082                 *cntp = cnt;
00083         } else if (names != NULL)
00084                 __os_dirfree(dbenv, names, cnt);
00085 
00086         FREE_STRING(dbenv, tdir);
00087 
00088         return (ret);
00089 }
00090 
00091 /*
00092  * __os_dirfree --
00093  *      Free the list of files.
00094  */
00095 void
00096 __os_dirfree(dbenv, names, cnt)
00097         DB_ENV *dbenv;
00098         char **names;
00099         int cnt;
00100 {
00101         if (DB_GLOBAL(j_dirfree) != NULL) {
00102                 DB_GLOBAL(j_dirfree)(names, cnt);
00103                 return;
00104         }
00105 
00106         while (cnt > 0)
00107                 __os_free(dbenv, names[--cnt]);
00108         __os_free(dbenv, names);
00109 }

Generated on Sun Dec 25 12:14:42 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2