mariadb/storage/bdb/os_win32/os_dir.c

110 lines
2.1 KiB
C
Raw Normal View History

2001-03-04 19:42:05 -05:00
/*-
* See the file LICENSE for redistribution information.
*
2005-12-05 10:27:46 -08:00
* Copyright (c) 1997-2005
2001-03-04 19:42:05 -05:00
* Sleepycat Software. All rights reserved.
2005-07-20 15:48:22 -07:00
*
2005-12-05 10:27:46 -08:00
* $Id: os_dir.c,v 12.2 2005/07/06 23:52:43 dda Exp $
2001-03-04 19:42:05 -05:00
*/
#include "db_config.h"
#include "db_int.h"
/*
* __os_dirlist --
* Return a list of the files in a directory.
*/
int
__os_dirlist(dbenv, dir, namesp, cntp)
DB_ENV *dbenv;
const char *dir;
char ***namesp;
int *cntp;
{
2005-07-20 15:48:22 -07:00
HANDLE dirhandle;
WIN32_FIND_DATA fdata;
int arraysz, cnt, ret;
char **names, *onename;
_TCHAR tfilespec[MAXPATHLEN + 1];
2005-12-05 10:27:46 -08:00
_TCHAR *tdir;
2001-03-04 19:42:05 -05:00
2002-10-30 15:57:05 +04:00
if (DB_GLOBAL(j_dirlist) != NULL)
return (DB_GLOBAL(j_dirlist)(dir, namesp, cntp));
2001-03-04 19:42:05 -05:00
2005-12-05 10:27:46 -08:00
TO_TSTRING(dbenv, dir, tdir, ret);
if (ret != 0)
return (ret);
2005-07-20 15:48:22 -07:00
(void)_sntprintf(tfilespec, MAXPATHLEN,
2005-12-05 10:27:46 -08:00
_T("%s%hc*"), tdir, PATH_SEPARATOR[0]);
2005-07-20 15:48:22 -07:00
if ((dirhandle = FindFirstFile(tfilespec, &fdata))
== INVALID_HANDLE_VALUE)
2001-03-04 19:42:05 -05:00
return (__os_get_errno());
names = NULL;
2005-07-20 15:48:22 -07:00
arraysz = cnt = ret = 0;
for (;;) {
2001-03-04 19:42:05 -05:00
if (cnt >= arraysz) {
arraysz += 100;
if ((ret = __os_realloc(dbenv,
2002-10-30 15:57:05 +04:00
arraysz * sizeof(names[0]), &names)) != 0)
2005-07-20 15:48:22 -07:00
goto err;
}
/*
* FROM_TSTRING doesn't necessarily allocate new memory, so we
* must do that explicitly. Unfortunately, when compiled with
* UNICODE, we'll copy twice.
*/
FROM_TSTRING(dbenv, fdata.cFileName, onename, ret);
if (ret != 0)
goto err;
ret = __os_strdup(dbenv, onename, &names[cnt]);
FREE_STRING(dbenv, onename);
if (ret != 0)
goto err;
cnt++;
if (!FindNextFile(dirhandle, &fdata)) {
if (GetLastError() == ERROR_NO_MORE_FILES)
break;
else {
ret = __os_get_errno();
goto err;
}
2001-03-04 19:42:05 -05:00
}
}
2005-07-20 15:48:22 -07:00
err: if (!FindClose(dirhandle) && ret == 0)
ret = __os_get_errno();
2001-03-04 19:42:05 -05:00
2005-07-20 15:48:22 -07:00
if (ret == 0) {
*namesp = names;
*cntp = cnt;
} else if (names != NULL)
2002-10-30 15:57:05 +04:00
__os_dirfree(dbenv, names, cnt);
2005-07-20 15:48:22 -07:00
2005-12-05 10:27:46 -08:00
FREE_STRING(dbenv, tdir);
2001-03-04 19:42:05 -05:00
return (ret);
}
/*
* __os_dirfree --
* Free the list of files.
*/
void
2002-10-30 15:57:05 +04:00
__os_dirfree(dbenv, names, cnt)
DB_ENV *dbenv;
2001-03-04 19:42:05 -05:00
char **names;
int cnt;
{
2002-10-30 15:57:05 +04:00
if (DB_GLOBAL(j_dirfree) != NULL) {
DB_GLOBAL(j_dirfree)(names, cnt);
2001-03-04 19:42:05 -05:00
return;
}
while (cnt > 0)
2002-10-30 15:57:05 +04:00
__os_free(dbenv, names[--cnt]);
__os_free(dbenv, names);
2001-03-04 19:42:05 -05:00
}