~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_lib.cc

  • Committer: Monty Taylor
  • Date: 2009-09-30 07:01:32 UTC
  • mto: This revision was merged to the branch mainline in revision 1184.
  • Revision ID: mordred@inaugust.com-20090930070132-b1ol1xu1rpajdddy
Small namespace cleanup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
/* TODO: check for overun of memory for names. */
17
 
/*       Convert MSDOS-TIME to standar time_t (still needed?) */
18
17
 
19
 
#include        "mysys_priv.h"
 
18
#include        "mysys/mysys_priv.h"
20
19
#include        <mystrings/m_string.h>
21
 
#include        <my_dir.h>      /* Structs used by my_dir,includes sys/types */
22
 
#include        "mysys_err.h"
 
20
#include        "mysys/mysys_err.h"
 
21
#include        "my_dir.h"      /* Structs used by my_dir,includes sys/types */
23
22
#if defined(HAVE_DIRENT_H)
24
23
# include <dirent.h>
25
24
#else
37
36
 
38
37
#if defined(HAVE_READDIR_R)
39
38
#define READDIR(A,B,C) ((errno=readdir_r(A,B,&C)) != 0 || !C)
40
 
#else
41
 
#define READDIR(A,B,C) (!(C=readdir(A)))
 
39
#else 
 
40
#error You must have a thread safe readdir() 
42
41
#endif
43
42
 
44
43
/*
45
 
  We are assuming that directory we are reading is either has less than 
 
44
  We are assuming that directory we are reading is either has less than
46
45
  100 files and so can be read in one initial chunk or has more than 1000
47
46
  files and so big increment are suitable.
48
47
*/
60
59
{
61
60
  if (buffer)
62
61
  {
63
 
    delete_dynamic((DYNAMIC_ARRAY*)((char*)buffer + 
 
62
    delete_dynamic((DYNAMIC_ARRAY*)((char*)buffer +
64
63
                                    ALIGN_SIZE(sizeof(MY_DIR))));
65
 
    free_root((MEM_ROOT*)((char*)buffer + ALIGN_SIZE(sizeof(MY_DIR)) + 
 
64
    free_root((MEM_ROOT*)((char*)buffer + ALIGN_SIZE(sizeof(MY_DIR)) +
66
65
                          ALIGN_SIZE(sizeof(DYNAMIC_ARRAY))), MYF(0));
67
66
    free((unsigned char*) buffer);
68
67
  }
90
89
  char          tmp_path[FN_REFLEN+1],*tmp_file;
91
90
  char  dirent_tmp[sizeof(struct dirent)+_POSIX_PATH_MAX+1];
92
91
 
93
 
#if !defined(HAVE_READDIR_R)
94
 
  pthread_mutex_lock(&THR_LOCK_open);
95
 
#endif
96
 
 
97
92
  dirp = opendir(directory_file_name(tmp_path,(char *) path));
98
 
  if (dirp == NULL || 
99
 
      ! (buffer= (char *) my_malloc(ALIGN_SIZE(sizeof(MY_DIR)) + 
100
 
                                    ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)) +
101
 
                                    sizeof(MEM_ROOT), MyFlags)))
 
93
  if (dirp == NULL ||
 
94
      ! (buffer= (char *) malloc(ALIGN_SIZE(sizeof(MY_DIR)) + 
 
95
                                 ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)) +
 
96
                                 sizeof(MEM_ROOT))))
102
97
    goto error;
103
98
 
104
 
  dir_entries_storage= (DYNAMIC_ARRAY*)(buffer + ALIGN_SIZE(sizeof(MY_DIR))); 
 
99
  dir_entries_storage= (DYNAMIC_ARRAY*)(buffer + ALIGN_SIZE(sizeof(MY_DIR)));
105
100
  names_storage= (MEM_ROOT*)(buffer + ALIGN_SIZE(sizeof(MY_DIR)) +
106
101
                             ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)));
107
 
  
 
102
 
108
103
  if (my_init_dynamic_array(dir_entries_storage, sizeof(FILEINFO),
109
104
                            ENTRIES_START_SIZE, ENTRIES_INCREMENT))
110
105
  {
112
107
    goto error;
113
108
  }
114
109
  init_alloc_root(names_storage, NAMES_START_SIZE, NAMES_START_SIZE);
115
 
  
 
110
 
116
111
  /* MY_DIR structure is allocated and completly initialized at this point */
117
112
  result= (MY_DIR*)buffer;
118
113
 
119
114
  tmp_file= strchr(tmp_path, '\0');
120
115
 
121
116
  dp= (struct dirent*) dirent_tmp;
122
 
  
 
117
 
123
118
  while (!(READDIR(dirp,(struct dirent*) dirent_tmp,dp)))
124
119
  {
125
120
    if (!(finfo.name= strdup_root(names_storage, dp->d_name)))
126
121
      goto error;
127
 
    
 
122
 
128
123
    if (MyFlags & MY_WANT_STAT)
129
124
    {
130
 
      if (!(finfo.mystat= (struct stat*)alloc_root(names_storage, 
 
125
      if (!(finfo.mystat= (struct stat*)alloc_root(names_storage,
131
126
                                               sizeof(struct stat))))
132
127
        goto error;
133
 
      
 
128
 
134
129
      memset(finfo.mystat, 0, sizeof(struct stat));
135
 
      my_stpcpy(tmp_file,dp->d_name);
 
130
      strcpy(tmp_file,dp->d_name);
136
131
      stat(tmp_path, finfo.mystat);
137
132
      if (!(finfo.mystat->st_mode & S_IREAD))
138
133
        continue;
145
140
  }
146
141
 
147
142
  (void) closedir(dirp);
148
 
#if !defined(HAVE_READDIR_R)
149
 
  pthread_mutex_unlock(&THR_LOCK_open);
150
 
#endif
 
143
 
151
144
  result->dir_entry= (FILEINFO *)dir_entries_storage->buffer;
152
 
  result->number_off_files= dir_entries_storage->elements;
153
 
  
 
145
  result->number_off_files= static_cast<uint>(dir_entries_storage->elements);
 
146
 
154
147
  if (!(MyFlags & MY_DONT_SORT))
155
148
    my_qsort((void *) result->dir_entry, result->number_off_files,
156
149
          sizeof(FILEINFO), (qsort_cmp) comp_names);
157
150
  return(result);
158
151
 
159
152
 error:
160
 
#if !defined(HAVE_READDIR_R)
161
 
  pthread_mutex_unlock(&THR_LOCK_open);
162
 
#endif
 
153
 
163
154
  my_errno=errno;
164
155
  if (dirp)
165
156
    (void) closedir(dirp);
166
157
  my_dirend(result);
167
158
  if (MyFlags & (MY_FAE | MY_WME))
168
159
    my_error(EE_DIR,MYF(ME_BELL+ME_WAITTANG),path,my_errno);
 
160
 
169
161
  return((MY_DIR *) NULL);
170
162
} /* my_dir */
171
163
 
188
180
 
189
181
  if (src[0] == 0)
190
182
    src= (char*) ".";                           /* Use empty as current */
191
 
  end=my_stpcpy(dst, src);
 
183
  end= strcpy(dst, src)+strlen(src);
192
184
  if (end[-1] != FN_LIBCHAR)
193
185
  {
194
186
    end[0]=FN_LIBCHAR;                          /* Add last '/' */