17
17
/* Convert MSDOS-TIME to standar time_t (still needed?) */
19
19
#include "mysys_priv.h"
20
#include <mystrings/m_string.h>
21
21
#include <my_dir.h> /* Structs used by my_dir,includes sys/types */
22
22
#include "mysys_err.h"
23
23
#if defined(HAVE_DIRENT_H)
24
24
# include <dirent.h>
25
# define NAMLEN(dirent) strlen((dirent)->d_name)
26
27
# define dirent direct
28
# define NAMLEN(dirent) (dirent)->d_namlen
27
29
# if defined(HAVE_SYS_NDIR_H)
28
30
# include <sys/ndir.h>
51
53
#define NAMES_START_SIZE 32768
54
static int comp_names(const struct fileinfo *a, const struct fileinfo *b);
56
static int comp_names(struct fileinfo *a,struct fileinfo *b);
57
59
/* We need this because program don't know with malloc we used */
59
61
void my_dirend(MY_DIR *buffer)
63
DBUG_ENTER("my_dirend");
63
delete_dynamic((DYNAMIC_ARRAY*)((char*)buffer +
66
delete_dynamic((DYNAMIC_ARRAY*)((char*)buffer +
64
67
ALIGN_SIZE(sizeof(MY_DIR))));
65
free_root((MEM_ROOT*)((char*)buffer + ALIGN_SIZE(sizeof(MY_DIR)) +
68
free_root((MEM_ROOT*)((char*)buffer + ALIGN_SIZE(sizeof(MY_DIR)) +
66
69
ALIGN_SIZE(sizeof(DYNAMIC_ARRAY))), MYF(0));
67
free((unsigned char*) buffer);
70
my_free((uchar*) buffer,MYF(0));
73
76
/* Compare in sort of filenames */
75
static int comp_names(const struct fileinfo *a, const struct fileinfo *b)
78
static int comp_names(struct fileinfo *a, struct fileinfo *b)
77
80
return (strcmp(a->name,b->name));
90
93
char tmp_path[FN_REFLEN+1],*tmp_file;
91
94
char dirent_tmp[sizeof(struct dirent)+_POSIX_PATH_MAX+1];
96
DBUG_PRINT("my",("path: '%s' MyFlags: %d",path,MyFlags));
93
98
#if !defined(HAVE_READDIR_R)
94
99
pthread_mutex_lock(&THR_LOCK_open);
97
102
dirp = opendir(directory_file_name(tmp_path,(char *) path));
99
! (buffer= (char *) malloc(ALIGN_SIZE(sizeof(MY_DIR)) +
100
ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)) +
104
! (buffer= my_malloc(ALIGN_SIZE(sizeof(MY_DIR)) +
105
ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)) +
106
sizeof(MEM_ROOT), MyFlags)))
104
dir_entries_storage= (DYNAMIC_ARRAY*)(buffer + ALIGN_SIZE(sizeof(MY_DIR)));
109
dir_entries_storage= (DYNAMIC_ARRAY*)(buffer + ALIGN_SIZE(sizeof(MY_DIR)));
105
110
names_storage= (MEM_ROOT*)(buffer + ALIGN_SIZE(sizeof(MY_DIR)) +
106
111
ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)));
108
113
if (my_init_dynamic_array(dir_entries_storage, sizeof(FILEINFO),
109
114
ENTRIES_START_SIZE, ENTRIES_INCREMENT))
111
free((unsigned char*) buffer);
116
my_free((uchar*) buffer,MYF(0));
114
119
init_alloc_root(names_storage, NAMES_START_SIZE, NAMES_START_SIZE);
116
121
/* MY_DIR structure is allocated and completly initialized at this point */
117
122
result= (MY_DIR*)buffer;
119
tmp_file= strchr(tmp_path, '\0');
124
tmp_file=strend(tmp_path);
121
126
dp= (struct dirent*) dirent_tmp;
123
128
while (!(READDIR(dirp,(struct dirent*) dirent_tmp,dp)))
125
130
if (!(finfo.name= strdup_root(names_storage, dp->d_name)))
128
133
if (MyFlags & MY_WANT_STAT)
130
if (!(finfo.mystat= (struct stat*)alloc_root(names_storage,
135
if (!(finfo.mystat= (struct stat*)alloc_root(names_storage,
131
136
sizeof(struct stat))))
134
memset(finfo.mystat, 0, sizeof(struct stat));
135
strcpy(tmp_file,dp->d_name);
136
stat(tmp_path, finfo.mystat);
137
if (!(finfo.mystat->st_mode & S_IREAD))
139
bzero(finfo.mystat, sizeof(struct stat));
140
VOID(strmov(tmp_file,dp->d_name));
141
VOID(stat(tmp_path, finfo.mystat));
142
if (!(finfo.mystat->st_mode & MY_S_IREAD))
141
146
finfo.mystat= NULL;
143
if (push_dynamic(dir_entries_storage, (unsigned char*)&finfo))
148
if (push_dynamic(dir_entries_storage, (uchar*)&finfo))
151
156
result->dir_entry= (FILEINFO *)dir_entries_storage->buffer;
152
157
result->number_off_files= dir_entries_storage->elements;
154
159
if (!(MyFlags & MY_DONT_SORT))
155
160
my_qsort((void *) result->dir_entry, result->number_off_files,
156
161
sizeof(FILEINFO), (qsort_cmp) comp_names);
160
165
#if !defined(HAVE_READDIR_R)