~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/cslib/CSDirectory.cc

  • Committer: Lee Bieber
  • Date: 2010-10-22 16:47:38 UTC
  • mfrom: (1841.1.7 drizzle_pbms)
  • Revision ID: kalebral@gmail.com-20101022164738-vv8w22b8towpb307
Merge Barry - fix bug 657830: PBMS build failure in GCC 4.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include "CSConfig.h"
30
30
 
31
31
#include <string.h>
32
 
#include <sys/time.h>
33
 
#include <sys/stat.h>
34
 
#include <limits.h>
35
32
 
36
33
#include "CSStrUtil.h"
37
34
#include "CSPath.h"
43
40
 * CORE SYSTEM DIRECTORY
44
41
 */
45
42
 
46
 
CSDirectory::~CSDirectory()
47
 
{
48
 
        enter_();
49
 
        close();
50
 
        if (iPath)
51
 
                iPath->release();
52
 
        if (iEntry)
53
 
                cs_free(iEntry);
54
 
                
55
 
        exit_();
56
 
}
57
 
 
58
43
void CSDirectory::print(CSOutputStream *out)
59
44
{
60
45
        char    buffer[500];
80
65
        }
81
66
}
82
67
 
83
 
/*
84
 
 * ---------------------------------------------------------------
85
 
 * UNIX DIRECTORY
86
 
 */
87
 
 
88
 
/*
89
 
 * The filter may contain one '*' as wildcard.
90
 
 */
91
 
void CSDirectory::open()
92
 
{
93
 
        enter_();
94
 
        if (!(iDir = opendir(iPath->getCString())))
95
 
                CSException::throwFileError(CS_CONTEXT, iPath->getCString(), errno);
96
 
        exit_();
97
 
}
98
 
 
99
 
void CSDirectory::close()
100
 
{
101
 
        enter_();
102
 
        if (iDir) {
103
 
                closedir(iDir);
104
 
                iDir = NULL;
105
 
        }
106
 
        exit_();
107
 
}
108
 
 
109
 
bool CSDirectory::next()
110
 
{
111
 
        int                             err;
112
 
        struct dirent   *result;
113
 
 
114
 
        enter_();
115
 
        for (;;) {
116
 
                err = readdir_r(iDir, &iEntry->entry, &result);
117
 
                self->interrupted();
118
 
                if (err)
119
 
                        CSException::throwFileError(CS_CONTEXT, iPath->getCString(), err);
120
 
                if (!result)
121
 
                        break;
122
 
                /* Filter out '.' and '..': */
123
 
                if (iEntry->entry.d_name[0] == '.') {
124
 
                        if (iEntry->entry.d_name[1] == '.') {
125
 
                                if (iEntry->entry.d_name[2] == '\0')
126
 
                                        continue;
127
 
                        }
128
 
                        else {
129
 
                                if (iEntry->entry.d_name[1] == '\0')
130
 
                                        continue;
131
 
                        }
132
 
                }
133
 
                break;
134
 
        }
135
 
        return_(result ? true : false);
136
 
}
137
 
 
138
 
void CSDirectory::getFilePath(char *path, size_t size)
139
 
{
140
 
        cs_strcpy(size, path, iPath->getCString());
141
 
        cs_add_dir_char(size, path);
142
 
        cs_strcat(size, path, iEntry->entry.d_name);
143
 
}
144
 
 
145
68
void CSDirectory::deleteEntry()
146
69
{
147
70
        char path[PATH_MAX];
148
71
 
149
72
        enter_();
150
 
        getFilePath(path, PATH_MAX);
151
 
 
 
73
        
 
74
        getEntryPath(path, PATH_MAX);
 
75
        
152
76
        CSPath *cs_path = CSPath::newPath(path);
153
77
        push_(cs_path);
154
78
        cs_path->removeFile();
159
83
 
160
84
const char *CSDirectory::name()
161
85
{
162
 
        return iEntry->entry.d_name;
 
86
        return entryName();
163
87
}
164
88
 
165
89
bool CSDirectory::isFile()
166
90
{
167
 
#ifdef OS_SOLARIS
 
91
        return entryIsFile();
 
92
}
 
93
 
 
94
off64_t CSDirectory::getSize()
 
95
{
168
96
        char path[PATH_MAX];
169
 
        struct stat sb;
170
 
 
171
 
        getFilePath(path, PATH_MAX);
172
 
 
173
 
        if (stat(path, &sb) == -1) {
174
 
                CSException::throwFileError(CS_CONTEXT, path, errno);
175
 
                return false; // Never reached.
176
 
        }
177
 
 
178
 
        if ( sb.st_mode & S_IFDIR )
179
 
                return false;
180
 
#else
181
 
        if (iEntry->entry.d_type & DT_DIR)
182
 
                return false;
183
 
#endif
184
 
        return true;
 
97
 
 
98
        getEntryPath(path, PATH_MAX);
 
99
        
 
100
        return CSPath::getSize(path);
185
101
}
186
102
 
187
103
void CSDirectory::info(bool *is_dir, off64_t *size, CSTime *mod_time)
188
104
{
189
105
        char path[PATH_MAX];
190
106
 
 
107
        getEntryPath(path, PATH_MAX);
 
108
        
 
109
        CSPath::info(path, is_dir, size, mod_time);
 
110
}
 
111
 
 
112
bool CSDirectory::exists()
 
113
{
 
114
        CSPath *path;
 
115
        bool yup;
 
116
 
191
117
        enter_();
192
 
        getFilePath(path, PATH_MAX);
193
 
 
194
 
        CSPath *cs_path = CSPath::newPath(path);
195
 
        push_(cs_path);
196
 
        cs_path->info(is_dir, size, mod_time);
197
 
        release_(cs_path);
198
 
 
199
 
        exit_();
 
118
        path = CSPath::newPath(RETAIN(sd_path));
 
119
        push_(path);
 
120
        yup = path->exists();
 
121
        release_(path);
 
122
        return_(yup);
200
123
}
201
124
 
202
125
CSDirectory *CSDirectory::newDirectory(CSString *path)
203
126
{
204
127
        CSDirectory *dir;
205
 
        size_t size;
206
 
        enter_();
207
 
        push_(path);
208
 
        
209
 
#ifdef OS_SOLARIS
210
 
        size = pathconf(path->getCString(), _PC_NAME_MAX) + sizeof(struct dirent)  + 1;
211
 
#else
212
 
        size = sizeof(struct dirent);
213
 
#endif
214
128
 
215
129
        if (!(dir = new CSDirectory())) {
 
130
                path->release();
216
131
                CSException::throwOSError(CS_CONTEXT, ENOMEM);
217
132
        }
218
 
        pop_(path);
219
 
        dir->iPath = path;
220
 
        push_(dir);
221
 
        
222
 
        dir->iEntry = (union var_dirent *) cs_malloc(size);
223
 
        pop_(dir);
224
 
        
 
133
        dir->sd_path = path;
 
134
        return dir;
 
135
}
 
136
 
 
137
CSDirectory *CSDirectory::newDirectory(CSPath *path)
 
138
{
 
139
        CSDirectory *dir;
 
140
        enter_();
 
141
        
 
142
        push_(path);
 
143
        dir = newDirectory(RETAIN(path->getString()));
 
144
        release_(path);
225
145
        return_(dir);
226
146
}
227
147
 
228
 
CSDirectory *CSDirectory::newDirectory(CSPath *path)
 
148
CSDirectory *CSDirectory::newDirectory(const char *path)
229
149
{
230
 
        CSString *str = path->getString();
231
 
        str->retain();
232
 
        path->release();
233
 
        
234
 
        return newDirectory(str);
 
150
        return newDirectory(CSString::newString(path));
235
151
}
236
152
 
237
153