summaryrefslogtreecommitdiffstats
path: root/util/src/dir_handler.c
blob: 50d00b83e7990806d929f0f266deb0c81868d443 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <axutil_dir_handler.h>
#include <axutil_file.h>
#include <platforms/axutil_platform_auto_sense.h>
#include <axutil_string.h>
#ifndef S_ISDIR
#   define S_ISDIR(m) ((m & S_IFMT) == S_IFDIR)
#endif

#ifdef AXIS2_ARCHIVE_ENABLED
#include <minizip/axis2_archive_extract.h>
#endif


extern int AXIS2_ALPHASORT(
    );
#ifdef IS_MACOSX
int dir_select(
    struct dirent *entry);
int file_select(
    struct dirent *entry);
#else
int dir_select(
    const struct dirent *entry);
int file_select(
    const struct dirent *entry);
#endif

/**
 * List the dll files in the given service or module folder path
 * @param pathname path to your service or module directory
 * @return array list of dll file names
 */
AXIS2_EXTERN axutil_array_list_t *AXIS2_CALL
axutil_dir_handler_list_services_or_modules_in_dir(
    const axutil_env_t *env,
    const axis2_char_t *pathname)
{
    axutil_array_list_t *file_list = NULL;
    struct stat *buf = NULL;
    int count = 1;
    int i = 0;
    struct dirent **files = NULL;
    /*int file_select(
     );*/
    /* Removed un-wanted redefinition leading to warnings on
     * Windows. If this is the desired behaviour, please look
     * into the file_select function definition below and comment
     * out the code if neccessary.
     */
    axis2_status_t status = AXIS2_FAILURE;

    AXIS2_ENV_CHECK(env, NULL);
    file_list = axutil_array_list_create(env, 100);
    count = AXIS2_SCANDIR(pathname, &files, file_select, AXIS2_ALPHASORT);
    /* If no files found, make a non-selectable menu item */
    if(count <= 0)
    {
        axutil_array_list_free(file_list, env);
        AXIS2_LOG_DEBUG(env->log, AXIS2_LOG_SI, "No files in the path %s.", pathname);
        return NULL;
    }

    for(i = 1; i < (count + 1); ++i)
    {
        axis2_char_t *fname = NULL;
        axutil_file_t *arch_file = NULL;
        axis2_char_t *path = NULL;
        axis2_char_t *temp_path = NULL;

        fname = files[i - 1]->d_name;
        arch_file = (axutil_file_t *)axutil_file_create(env);
        if(!arch_file)
        {
            int size = 0;
            int j = 0;
            axutil_file_t *del_file = NULL;

            size = axutil_array_list_size(file_list, env);
            for(j = 0; j < size; j++)
            {
                del_file = axutil_array_list_get(file_list, env, j);
                axutil_file_free(del_file, env);
            }
            axutil_array_list_free(file_list, env);
            AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
            return NULL;
        }
        axutil_file_set_name(arch_file, env, fname);
        temp_path = axutil_stracat(env, pathname, AXIS2_PATH_SEP_STR);
        path = axutil_stracat(env, temp_path, fname);
        AXIS2_FREE(env->allocator, temp_path);
        if(!path)
        {
            int size = 0;
            int j = 0;
            axutil_file_t *del_file = NULL;

            axutil_file_free(arch_file, env);
            size = axutil_array_list_size(file_list, env);
            for(j = 0; j < size; j++)
            {
                del_file = axutil_array_list_get(file_list, env, j);
                axutil_file_free(del_file, env);
            }
            axutil_array_list_free(file_list, env);
            AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
            return NULL;
        }
        axutil_file_set_path(arch_file, env, path);
        buf = AXIS2_MALLOC(env->allocator, sizeof(struct stat));
        if(!buf)
        {
            int size = 0;
            int j = 0;
            axutil_file_t *del_file = NULL;

            AXIS2_FREE(env->allocator, path);
            axutil_file_free(arch_file, env);
            size = axutil_array_list_size(file_list, env);
            for(j = 0; j < size; j++)
            {
                del_file = axutil_array_list_get(file_list, env, j);
                axutil_file_free(del_file, env);
            }
            axutil_array_list_free(file_list, env);
            AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
            return NULL;
        }
        stat(path, buf);
        axutil_file_set_timestamp(arch_file, env, (time_t)buf->st_ctime);
        status = axutil_array_list_add(file_list, env, arch_file);
        if(AXIS2_SUCCESS != status)
        {
            int size = 0;
            int j = 0;
            axutil_file_t *del_file = NULL;

            axutil_file_free(arch_file, env);
            AXIS2_FREE(env->allocator, path);
            AXIS2_FREE(env->allocator, buf);
            size = axutil_array_list_size(file_list, env);
            for(j = 0; j < size; j++)
            {
                del_file = axutil_array_list_get(file_list, env, j);
                axutil_file_free(del_file, env);
            }
            axutil_array_list_free(file_list, env);
            return NULL;
        }
        AXIS2_FREE(env->allocator, path);
        AXIS2_FREE(env->allocator, buf);

    }
    return file_list;
}

/**
 * List services or modules directories in the services or modules folder
 * respectively
 * @param pathname path  your modules or services folder
 * @return array list of contents of services or modules folder
 */
AXIS2_EXTERN axutil_array_list_t *AXIS2_CALL
axutil_dir_handler_list_service_or_module_dirs(
    const axutil_env_t *env,
    const axis2_char_t *pathname)
{
    axutil_array_list_t *file_list = NULL;
    struct stat *buf = NULL;
    int count = 1;
    int i = 0;
    struct dirent **files = NULL;
    char cwd[500];
    int chdir_result = 0;

    /**FIXME:
     * This magic number 500 was selected as a temporary solution. It has to be
     * replaced with dynamic memory allocation. This will be done once the use of
     * errno after getwcd() on Windows is figured out.
     */

    axis2_status_t status = AXIS2_FAILURE;

    if(!AXIS2_GETCWD(cwd, 500))
    {
        /* if we can't identify working directory, it will be a critical failure */
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Cannot identify working directory");
        exit(1);
    }

    /* pathname is path of services directory or modules directory. */
    chdir_result = AXIS2_CHDIR(pathname);
    if(chdir_result == -1)
    {
        /* we could not be able to find the path given. However, this will not be a problem
         * because services and modules are optional.
         */
        AXIS2_LOG_INFO(env->log, "Cannot find path %s.", pathname);
        return NULL;
    }
#ifdef AXIS2_ARCHIVE_ENABLED
    axis2_archive_extract();
#endif

    count = AXIS2_SCANDIR(pathname, &files, dir_select, AXIS2_ALPHASORT);
    chdir_result = AXIS2_CHDIR(cwd);
    if(chdir_result == -1)
    {
        /* we are changing back to working directory. If we can't change, this will be critical*/
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Cannot change directory to working directory");
        exit(1);
    }

    /* If no files found, make a non-selectable menu item */
    if(count <= 0)
    {
        AXIS2_LOG_INFO(env->log, "No files in the path %s.", pathname);
        return NULL;
    }

    file_list = axutil_array_list_create(env, 0);
    if(!file_list)
    {
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Cannot create file list.");
        AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
        return NULL;
    }

    for(i = 1; i < (count + 1); ++i)
    {
        axis2_char_t *fname = NULL;
        axutil_file_t *arch_file = NULL;
        axis2_char_t *path = NULL;
        axis2_char_t *temp_path = NULL;

        fname = files[i - 1]->d_name;
        arch_file = (axutil_file_t *)axutil_file_create(env);
        if(!arch_file)
        {
            int size = 0;
            int j = 0;
            axutil_file_t *del_file = NULL;

            size = axutil_array_list_size(file_list, env);
            for(j = 0; j < size; j++)
            {
                del_file = axutil_array_list_get(file_list, env, j);
                axutil_file_free(del_file, env);
            }
            axutil_array_list_free(file_list, env);
            AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
            return NULL;
        }
        axutil_file_set_name(arch_file, env, fname);
        temp_path = axutil_stracat(env, pathname, AXIS2_PATH_SEP_STR);
        path = axutil_stracat(env, temp_path, fname);
        if(!path)
        {
            int size = 0;
            int j = 0;
            axutil_file_t *del_file = NULL;

            axutil_file_free(arch_file, env);
            size = axutil_array_list_size(file_list, env);
            for(j = 0; j < size; j++)
            {
                del_file = axutil_array_list_get(file_list, env, j);
                axutil_file_free(del_file, env);
            }
            axutil_array_list_free(file_list, env);
            AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
            return NULL;
        }
        axutil_file_set_path(arch_file, env, path);
        AXIS2_FREE(env->allocator, temp_path);
        buf = AXIS2_MALLOC(env->allocator, sizeof(struct stat));
        if(!buf)
        {
            int size = 0;
            int j = 0;
            axutil_file_t *del_file = NULL;

            axutil_file_free(arch_file, env);
            AXIS2_FREE(env->allocator, path);
            size = axutil_array_list_size(file_list, env);
            for(j = 0; j < size; j++)
            {
                del_file = axutil_array_list_get(file_list, env, j);
                axutil_file_free(del_file, env);
            }
            axutil_array_list_free(file_list, env);
            AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
            return NULL;
        }
        stat(path, buf);
        axutil_file_set_timestamp(arch_file, env, (time_t)buf->st_ctime);
        status = axutil_array_list_add(file_list, env, arch_file);
        if(AXIS2_SUCCESS != status)
        {
            int size = 0;
            int j = 0;
            axutil_file_t *del_file = NULL;

            axutil_file_free(arch_file, env);
            AXIS2_FREE(env->allocator, path);
            AXIS2_FREE(env->allocator, buf);
            size = axutil_array_list_size(file_list, env);
            for(j = 0; j < size; j++)
            {
                del_file = axutil_array_list_get(file_list, env, j);
                axutil_file_free(del_file, env);
            }
            axutil_array_list_free(file_list, env);
            return NULL;
        }
        AXIS2_FREE(env->allocator, path);
        AXIS2_FREE(env->allocator, buf);
    }

    for(i = 0; i < count; i++)
    {
        free(files[i]);
    }
    free(files);
    return file_list;
}

#ifdef IS_MACOSX
    int file_select(struct dirent *entry)
{
#else
	int file_select(const struct dirent *entry)
{
#endif
    /** FIXME:
     * This block of code has been sitting here doing nothing.
     * I have made the existing logic use this code portion.
     * Have no idea about the side-effects of this modification.
     * If this code block is not required, we might as well remove
     * it.
     */
    axis2_char_t *ptr;

    if((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0))
        return (AXIS2_FALSE);

    /* Check for filename extensions */
    ptr = axutil_rindex(entry->d_name, '.');
    if((ptr) && ((strcmp(ptr, AXIS2_LIB_SUFFIX) == 0)))
    {
        return (AXIS2_TRUE);
    }
    else
        return (AXIS2_FALSE);
}

#ifdef IS_MACOSX
int
dir_select(
    struct dirent *entry)
#else
int
dir_select(
    const struct dirent *entry)
#endif
{
    struct stat stat_p;

    if(-1 == stat(entry->d_name, &stat_p))
        return (AXIS2_FALSE);

    if((entry->d_name[0] == '.') || (!S_ISDIR(stat_p.st_mode)))
    {
        return (AXIS2_FALSE);
    }

    return AXIS2_TRUE;
}