aec9ce80057a31ca7c2d11a49942db0ab04dff1b
[platform/core/multimedia/libmm-fileinfo.git] / tests / mm_file_traverser.c
1 /*
2  * libmm-fileinfo
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Haejeong Kim <backto.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <limits.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <stdbool.h>
27 #include <glib.h>
28
29 #include "mm_file_traverse.h"
30
31 int mmfile_get_file_names(char *root_dir, MMFunc cbfunc, void *user_data)
32 {
33         GDir *dir = NULL;
34         GError *error = NULL;
35         GArray *dir_array = NULL;
36         const char *name;
37         char pdirname[MMFILE_PATH_MAX + 1] = {0, };
38         char *current_path = NULL;
39         char *start_path = NULL;
40         char *tmp_path = NULL;
41
42         if (!g_file_test(root_dir, G_FILE_TEST_IS_DIR)) {
43                 printf("it is not directory\n");
44                 return MMFILE_FAIL;
45         }
46
47         dir_array = g_array_new(false, false, sizeof(char*));
48         if (dir_array == NULL) {
49                 printf("g_array_new failed");
50                 return MMFILE_FAIL;
51         }
52
53         start_path = g_strdup(root_dir);
54         if (start_path != NULL)
55                 g_array_append_val(dir_array, start_path);
56
57         while (dir_array->len != 0) {
58                 current_path = g_array_index(dir_array , char*, 0);
59                 g_array_remove_index(dir_array, 0);
60
61                 dir = g_dir_open(current_path, 0, &error);
62                 if (dir != NULL && error == NULL) {
63                         while ((name = g_dir_read_name(dir))) {
64                                 memset(pdirname, 0, sizeof(pdirname));
65                                 snprintf(pdirname, sizeof(pdirname), "%s/%s", current_path, name);
66
67                                 if (g_file_test(pdirname, G_FILE_TEST_IS_DIR)) {
68                                         tmp_path = g_strdup(pdirname);
69                                         g_array_append_val(dir_array, tmp_path);
70                                 } else if (g_file_test(pdirname, G_FILE_TEST_IS_REGULAR)) {
71                                         if (cbfunc != NULL)
72                                                 cbfunc(pdirname, user_data, true);
73                                 }
74                         }
75                 } else {
76                         printf("g_dir_open failed");
77                 }
78
79                 if (dir)
80                         g_dir_close(dir);
81
82                 SAFE_FREE(current_path);
83         }
84
85         if (dir_array)
86                 g_array_free(dir_array, FALSE);
87
88         return MMFILE_SUCCESS;
89 }
90