~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
/* TODO: check for overun of memory for names. */
17
994.2.4 by Monty Taylor
Blast. Fixed some make distcheck issues.
18
#include	"mysys/mysys_priv.h"
212.5.18 by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype.
19
#include	<mystrings/m_string.h>
994.2.4 by Monty Taylor
Blast. Fixed some make distcheck issues.
20
#include	"mysys/mysys_err.h"
685.1.3 by Monty Taylor
Turned off stdinc - and then fixed the carnage.
21
#include	"my_dir.h"	/* Structs used by my_dir,includes sys/types */
1192.3.44 by Monty Taylor
Removed checks for things that are either defined ISO/POSIX standard, or that
22
#include <dirent.h>
1 by brian
clean slate
23
28.1.35 by Monty Taylor
Removed all references to THREAD.
24
#if defined(HAVE_READDIR_R)
1 by brian
clean slate
25
#define READDIR(A,B,C) ((errno=readdir_r(A,B,&C)) != 0 || !C)
1034.1.8 by Brian Aker
Remove locks around my_open(). Open file counts are now "best effort" (not
26
#else 
27
#error You must have a thread safe readdir() 
1 by brian
clean slate
28
#endif
29
30
/*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
31
  We are assuming that directory we are reading is either has less than
1 by brian
clean slate
32
  100 files and so can be read in one initial chunk or has more than 1000
33
  files and so big increment are suitable.
34
*/
35
#define ENTRIES_START_SIZE (8192/sizeof(FILEINFO))
36
#define ENTRIES_INCREMENT  (65536/sizeof(FILEINFO))
37
#define NAMES_START_SIZE   32768
38
39
266.7.12 by Andy Lester
putting in proper const qualifiers
40
static int comp_names(const struct fileinfo *a, const struct fileinfo *b);
1165.1.91 by Stewart Smith
make directory_file_name() static to mysys/my_lib.cc
41
static char* directory_file_name(char* dst, const char* src);
1 by brian
clean slate
42
43
	/* We need this because program don't know with malloc we used */
44
45
void my_dirend(MY_DIR *buffer)
46
{
47
  if (buffer)
48
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
49
    delete_dynamic((DYNAMIC_ARRAY*)((char*)buffer +
1 by brian
clean slate
50
                                    ALIGN_SIZE(sizeof(MY_DIR))));
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
51
    free_root((MEM_ROOT*)((char*)buffer + ALIGN_SIZE(sizeof(MY_DIR)) +
1 by brian
clean slate
52
                          ALIGN_SIZE(sizeof(DYNAMIC_ARRAY))), MYF(0));
481 by Brian Aker
Remove all of uchar.
53
    free((unsigned char*) buffer);
1 by brian
clean slate
54
  }
51.3.13 by Jay Pipes
Phase 1 removal of DBUG in mysys
55
  return;
1 by brian
clean slate
56
} /* my_dirend */
57
58
59
	/* Compare in sort of filenames */
60
266.7.12 by Andy Lester
putting in proper const qualifiers
61
static int comp_names(const struct fileinfo *a, const struct fileinfo *b)
1 by brian
clean slate
62
{
63
  return (strcmp(a->name,b->name));
64
} /* comp_names */
65
66
67
MY_DIR	*my_dir(const char *path, myf MyFlags)
68
{
69
  char          *buffer;
70
  MY_DIR        *result= 0;
71
  FILEINFO      finfo;
72
  DYNAMIC_ARRAY *dir_entries_storage;
73
  MEM_ROOT      *names_storage;
74
  DIR		*dirp;
75
  struct dirent *dp;
76
  char		tmp_path[FN_REFLEN+1],*tmp_file;
77
  char	dirent_tmp[sizeof(struct dirent)+_POSIX_PATH_MAX+1];
78
79
  dirp = opendir(directory_file_name(tmp_path,(char *) path));
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
80
  if (dirp == NULL ||
656.1.26 by Monty Taylor
Finally removed all of the my_malloc stuff.
81
      ! (buffer= (char *) malloc(ALIGN_SIZE(sizeof(MY_DIR)) + 
82
                                 ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)) +
83
                                 sizeof(MEM_ROOT))))
1 by brian
clean slate
84
    goto error;
85
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
86
  dir_entries_storage= (DYNAMIC_ARRAY*)(buffer + ALIGN_SIZE(sizeof(MY_DIR)));
1 by brian
clean slate
87
  names_storage= (MEM_ROOT*)(buffer + ALIGN_SIZE(sizeof(MY_DIR)) +
88
                             ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)));
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
89
1 by brian
clean slate
90
  if (my_init_dynamic_array(dir_entries_storage, sizeof(FILEINFO),
91
                            ENTRIES_START_SIZE, ENTRIES_INCREMENT))
92
  {
481 by Brian Aker
Remove all of uchar.
93
    free((unsigned char*) buffer);
1 by brian
clean slate
94
    goto error;
95
  }
96
  init_alloc_root(names_storage, NAMES_START_SIZE, NAMES_START_SIZE);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
97
1 by brian
clean slate
98
  /* MY_DIR structure is allocated and completly initialized at this point */
99
  result= (MY_DIR*)buffer;
100
376 by Brian Aker
strend remove
101
  tmp_file= strchr(tmp_path, '\0');
1 by brian
clean slate
102
103
  dp= (struct dirent*) dirent_tmp;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
104
1 by brian
clean slate
105
  while (!(READDIR(dirp,(struct dirent*) dirent_tmp,dp)))
106
  {
107
    if (!(finfo.name= strdup_root(names_storage, dp->d_name)))
108
      goto error;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
109
1 by brian
clean slate
110
    if (MyFlags & MY_WANT_STAT)
111
    {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
112
      if (!(finfo.mystat= (struct stat*)alloc_root(names_storage,
15 by brian
Fix for stat, NETWARE removal
113
                                               sizeof(struct stat))))
1 by brian
clean slate
114
        goto error;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
115
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
116
      memset(finfo.mystat, 0, sizeof(struct stat));
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
117
      strcpy(tmp_file,dp->d_name);
398.1.10 by Monty Taylor
Actually removed VOID() this time.
118
      stat(tmp_path, finfo.mystat);
212.5.37 by Monty Taylor
Removed my_stat.
119
      if (!(finfo.mystat->st_mode & S_IREAD))
1 by brian
clean slate
120
        continue;
121
    }
122
    else
123
      finfo.mystat= NULL;
124
481 by Brian Aker
Remove all of uchar.
125
    if (push_dynamic(dir_entries_storage, (unsigned char*)&finfo))
1 by brian
clean slate
126
      goto error;
127
  }
128
129
  (void) closedir(dirp);
1034.1.8 by Brian Aker
Remove locks around my_open(). Open file counts are now "best effort" (not
130
1 by brian
clean slate
131
  result->dir_entry= (FILEINFO *)dir_entries_storage->buffer;
1126.8.1 by Joe Daly
changes to allow -Wconversion flag to be turned on
132
  result->number_off_files= static_cast<uint>(dir_entries_storage->elements);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
133
1 by brian
clean slate
134
  if (!(MyFlags & MY_DONT_SORT))
135
    my_qsort((void *) result->dir_entry, result->number_off_files,
136
          sizeof(FILEINFO), (qsort_cmp) comp_names);
51.3.13 by Jay Pipes
Phase 1 removal of DBUG in mysys
137
  return(result);
1 by brian
clean slate
138
139
 error:
1034.1.8 by Brian Aker
Remove locks around my_open(). Open file counts are now "best effort" (not
140
1 by brian
clean slate
141
  my_errno=errno;
142
  if (dirp)
143
    (void) closedir(dirp);
144
  my_dirend(result);
145
  if (MyFlags & (MY_FAE | MY_WME))
146
    my_error(EE_DIR,MYF(ME_BELL+ME_WAITTANG),path,my_errno);
1034.1.8 by Brian Aker
Remove locks around my_open(). Open file counts are now "best effort" (not
147
51.3.13 by Jay Pipes
Phase 1 removal of DBUG in mysys
148
  return((MY_DIR *) NULL);
1 by brian
clean slate
149
} /* my_dir */
150
151
152
/*
153
 * Convert from directory name to filename.
154
 * On VMS:
155
 *	 xyzzy:[mukesh.emacs] => xyzzy:[mukesh]emacs.dir.1
156
 *	 xyzzy:[mukesh] => xyzzy:[000000]mukesh.dir.1
157
 * On UNIX, it's simple: just make sure there is a terminating /
158
159
 * Returns pointer to dst;
160
 */
161
1165.1.91 by Stewart Smith
make directory_file_name() static to mysys/my_lib.cc
162
static char* directory_file_name(char* dst, const char* src)
1 by brian
clean slate
163
{
164
  /* Process as Unix format: just remove test the final slash. */
165
166
  char * end;
167
168
  if (src[0] == 0)
169
    src= (char*) ".";				/* Use empty as current */
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
170
  end= strcpy(dst, src)+strlen(src);
1 by brian
clean slate
171
  if (end[-1] != FN_LIBCHAR)
172
  {
173
    end[0]=FN_LIBCHAR;				/* Add last '/' */
174
    end[1]='\0';
175
  }
176
  return dst;
177
} /* directory_file_name */
178