19 #include "mysys_priv.h"
22 #include "mysys_err.h"
23 #if defined(HAVE_DIRENT_H)
25 # define NAMLEN(dirent) strlen((dirent)->d_name)
27 # define dirent direct
28 # define NAMLEN(dirent) (dirent)->d_namlen
29 # if defined(HAVE_SYS_NDIR_H)
30 # include <sys/ndir.h>
32 # if defined(HAVE_SYS_DIR_H)
35 # if defined(HAVE_NDIR_H)
45 #if defined(HAVE_READDIR_R)
46 #define READDIR(A,B,C) ((errno=readdir_r(A,B,&C)) != 0 || !C)
48 #define READDIR(A,B,C) (!(C=readdir(A)))
56 #define ENTRIES_START_SIZE (8192/sizeof(FILEINFO))
57 #define ENTRIES_INCREMENT (65536/sizeof(FILEINFO))
58 #define NAMES_START_SIZE 32768
66 void my_dirend(
MY_DIR *buffer)
68 DBUG_ENTER(
"my_dirend");
72 ALIGN_SIZE(
sizeof(
MY_DIR))));
85 return (strcmp(a->name,b->name));
91 MY_DIR *my_dir(
const char *path, myf MyFlags)
100 char tmp_path[FN_REFLEN + 2], *tmp_file;
101 char dirent_tmp[
sizeof(
struct dirent)+_POSIX_PATH_MAX+1];
103 DBUG_ENTER(
"my_dir");
104 DBUG_PRINT(
"my",(
"path: '%s' MyFlags: %d",path,MyFlags));
106 #if !defined(HAVE_READDIR_R)
110 dirp = opendir(directory_file_name(tmp_path,(
char *) path));
111 #if defined(__amiga__)
112 if ((dirp->dd_fd) < 0)
116 ! (buffer= my_malloc(ALIGN_SIZE(
sizeof(
MY_DIR)) +
125 if (my_init_dynamic_array(dir_entries_storage,
sizeof(
FILEINFO),
126 ENTRIES_START_SIZE, ENTRIES_INCREMENT))
131 init_alloc_root(names_storage, NAMES_START_SIZE, NAMES_START_SIZE);
136 tmp_file=strend(tmp_path);
138 dp= (
struct dirent*) dirent_tmp;
140 while (!(READDIR(dirp,(
struct dirent*) dirent_tmp,dp)))
142 if (!(finfo.name= strdup_root(names_storage, dp->d_name)))
145 if (MyFlags & MY_WANT_STAT)
147 if (!(finfo.mystat= (MY_STAT*)alloc_root(names_storage,
151 memset(finfo.mystat, 0,
sizeof(MY_STAT));
152 (void) strmov(tmp_file,dp->d_name);
153 (void) my_stat(tmp_path, finfo.mystat, MyFlags);
154 if (!(finfo.mystat->st_mode & MY_S_IREAD))
160 if (push_dynamic(dir_entries_storage, (uchar*)&finfo))
164 (void) closedir(dirp);
165 #if !defined(HAVE_READDIR_R)
168 result->dir_entry= (
FILEINFO *)dir_entries_storage->buffer;
169 result->number_off_files= dir_entries_storage->elements;
171 if (!(MyFlags & MY_DONT_SORT))
172 my_qsort((
void *) result->dir_entry, result->number_off_files,
173 sizeof(
FILEINFO), (qsort_cmp) comp_names);
177 #if !defined(HAVE_READDIR_R)
182 (void) closedir(dirp);
184 if (MyFlags & (MY_FAE | MY_WME))
186 char errbuf[MYSYS_STRERROR_SIZE];
187 my_error(EE_DIR, MYF(ME_BELL+ME_WAITTANG), path,
188 my_errno, my_strerror(errbuf,
sizeof(errbuf), my_errno));
190 DBUG_RETURN((
MY_DIR *) NULL);
201 char * directory_file_name (
char * dst,
const char *src)
205 DBUG_ASSERT(strlen(src) < (FN_REFLEN + 1));
209 end= strnmov(dst, src, FN_REFLEN + 1);
210 if (end[-1] != FN_LIBCHAR)
226 MY_DIR *my_dir(
const char *path, myf MyFlags)
236 struct _finddata_t find;
239 char tmp_path[FN_REFLEN],*tmp_file,attrib;
245 DBUG_ENTER(
"my_dir");
246 DBUG_PRINT(
"my",(
"path: '%s' stat: %d MyFlags: %d",path,MyFlags));
252 tmp_file= strnmov(tmp_file, path, FN_REFLEN-5);
253 if (tmp_file[-1] == FN_DEVCHAR)
255 if (tmp_file[-1] != FN_LIBCHAR)
256 *tmp_file++ =FN_LIBCHAR;
262 if (!(buffer= my_malloc(ALIGN_SIZE(
sizeof(
MY_DIR)) +
271 if (my_init_dynamic_array(dir_entries_storage,
sizeof(
FILEINFO),
272 ENTRIES_START_SIZE, ENTRIES_INCREMENT))
277 init_alloc_root(names_storage, NAMES_START_SIZE, NAMES_START_SIZE);
283 if ((handle= findfirst(tmp_path,&find,0)) == -1L)
285 if ((handle=_findfirst(tmp_path,&find)) == -1L)
288 DBUG_PRINT(
"info", (
"findfirst returned error, errno: %d", errno));
303 attrib= find.ff_attrib;
311 if (attrib & (_A_HIDDEN | _A_SYSTEM))
315 if (!(finfo.name= strdup_root(names_storage, find.ff_name)))
318 if (!(finfo.name= strdup_root(names_storage, find.name)))
321 if (MyFlags & MY_WANT_STAT)
323 if (!(finfo.mystat= (MY_STAT*)alloc_root(names_storage,
327 memset(finfo.mystat, 0,
sizeof(MY_STAT));
329 finfo.mystat->st_size=find.ff_fsize;
331 finfo.mystat->st_size=find.size;
334 if (!(attrib & _A_RDONLY))
336 if (attrib & _A_SUBDIR)
338 finfo.mystat->st_mode=
mode;
340 finfo.mystat->st_mtime= ((uint32) find.ff_ftime);
342 finfo.mystat->st_mtime= ((uint32) find.time_write);
348 if (push_dynamic(dir_entries_storage, (uchar*)&finfo))
352 while (findnext(&find) == 0);
354 while (_findnext(handle,&find) == 0);
360 result->dir_entry= (
FILEINFO *)dir_entries_storage->buffer;
361 result->number_off_files= dir_entries_storage->elements;
363 if (!(MyFlags & MY_DONT_SORT))
364 my_qsort((
void *) result->dir_entry, result->number_off_files,
365 sizeof(
FILEINFO), (qsort_cmp) comp_names);
366 DBUG_PRINT(
"exit", (
"found %d files", result->number_off_files));
375 if (MyFlags & MY_FAE+MY_WME)
377 char errbuf[MYSYS_STRERROR_SIZE];
378 my_error(EE_DIR, MYF(ME_BELL+ME_WAITTANG), path,
379 errno, my_strerror(errbuf,
sizeof(errbuf), errno));
381 DBUG_RETURN((
MY_DIR *) NULL);
392 int my_fstat(File Filedes, MY_STAT *stat_area,
393 myf MyFlags __attribute__((unused)))
395 DBUG_ENTER(
"my_fstat");
396 DBUG_PRINT(
"my",(
"fd: %d MyFlags: %d", Filedes, MyFlags));
398 DBUG_RETURN(my_win_fstat(Filedes, stat_area));
400 DBUG_RETURN(fstat(Filedes, (
struct stat *) stat_area));
405 MY_STAT *my_stat(
const char *path, MY_STAT *stat_area, myf my_flags)
407 const int m_used= (stat_area == NULL);
408 DBUG_ENTER(
"my_stat");
409 DBUG_PRINT(
"my", (
"path: '%s' stat_area: 0x%lx MyFlags: %d", path,
410 (
long) stat_area, my_flags));
413 if (!(stat_area= (MY_STAT *) my_malloc(
sizeof(MY_STAT), my_flags)))
416 if (! stat((
char *) path, (
struct stat *) stat_area) )
417 DBUG_RETURN(stat_area);
419 if (! my_win_stat(path, stat_area) )
420 DBUG_RETURN(stat_area);
422 DBUG_PRINT(
"error",(
"Got errno: %d from stat", errno));
428 if (my_flags & (MY_FAE+MY_WME))
430 char errbuf[MYSYS_STRERROR_SIZE];
431 my_error(EE_STAT, MYF(ME_BELL+ME_WAITTANG), path,
432 my_errno, my_strerror(errbuf,
sizeof(errbuf), my_errno));
433 DBUG_RETURN((MY_STAT *) NULL);
435 DBUG_RETURN((MY_STAT *) NULL);