5257ddb219a5432c5d670c7fb78368fbd3f39b75
[apps/core/preloaded/ug-myfile-efl.git] / src / common / file-system / mf-ug-fs-oper.c
1 /*
2  * Copyright 2012          Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *  http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18
19
20 #include <libgen.h>
21 #include <glib.h>
22 #include "mf-ug-fs-util.h"
23 #include "mf-ug-util.h"
24
25 static int __mf_ug_fs_oper_sort_by_date_cb_O2R(const void *d1, const void *d2);
26 static int __mf_ug_fs_oper_sort_by_name_cb_A2Z(const void *d1, const void *d2);
27 static int __mf_ug_fs_oper_sort_by_type_cb_A2Z(const void *d1, const void *d2);
28 static int __mf_ug_fs_oper_sort_by_size_cb_S2L(const void *d1, const void *d2);
29 static int __mf_ug_fs_oper_sort_by_name_cb_Z2A(const void *d1, const void *d2);
30 static int __mf_ug_fs_oper_sort_by_date_cb_R2O(const void *d1, const void *d2);
31 static int __mf_ug_fs_oper_sort_by_type_cb_Z2A(const void *d1, const void *d2);
32 static int __mf_ug_fs_oper_sort_by_size_cb_L2S(const void *d1, const void *d2);
33
34 /*********************
35 **Function name:        __mf_ug_fs_oper_file_system_error
36 **Parameter:
37 **      const char* src:        source path
38 **      const char* dst:        destination path
39 **      int check_option:       check option
40 **
41 **Return value:
42 **      error code
43 **
44 **Action:
45 **      input parameter checking
46 **
47 *********************/
48 static const char *__mf_ug_fs_oper_get_file(const char *path)
49 {
50         char *result = NULL;
51
52         if (!path) {
53                 return NULL;
54         }
55         if ((result = strrchr(path, '/'))) {
56                 result++;
57         } else {
58                 result = (char *)path;
59         }
60         return result;
61 }
62
63
64 static int __mf_ug_fs_oper_file_system_error(const char *src, const char *dst, int check_option)
65 {
66         if ((check_option & MF_ERROR_CHECK_SRC_ARG_VALID) && (src == NULL)) {
67                 return MYFILE_ERR_SRC_ARG_INVALID;
68         }
69         if ((check_option & MF_ERROR_CHECK_SRC_EXIST) && (!ecore_file_exists(src))) {
70                 return MYFILE_ERR_SRC_NOT_EXIST;
71         }
72
73         if (check_option & MF_ERROR_CHECK_SRC_PATH_VALID) {
74                 if (!ecore_file_is_dir(src)) {
75                         if (mf_ug_file_attr_is_right_file_path(src)) {
76                                 return MYFILE_ERR_INVALID_FILE_PATH;
77                         }
78                 } else {
79                         if (mf_ug_file_attr_is_right_dir_path(src)) {
80                                 return MYFILE_ERR_INVALID_DIR_PATH;
81                         }
82                 }
83         }
84
85         if (check_option & MF_ERROR_CHECK_DUPLICATED) {
86                 char *parent_path = NULL;
87
88                 if (!mf_ug_file_attr_get_parent_path(dst, &parent_path)) {
89                         if (mf_ug_file_attr_is_duplicated_name(parent_path, __mf_ug_fs_oper_get_file(dst))) {
90                                 UG_SAFE_FREE_CHAR(parent_path);
91                                 return MYFILE_ERR_DUPLICATED_NAME;
92                         }
93                         UG_SAFE_FREE_CHAR(parent_path);
94                 } else {
95                         UG_SAFE_FREE_CHAR(parent_path);
96                         return MYFILE_ERR_GET_PARENT_PATH_FAIL;
97                 }
98         }
99         return MYFILE_ERR_NONE;
100 }
101
102 /*********************
103 **Function name:        mf_ug_fs_oper_read_dir
104 **Parameter:
105 **      char *path:                             path which we need to read
106 **      Eina_List** dir_list:   output parameter of dir list under specified path
107 **      Eina_List** file_list:  output parameter of file list under specified path
108 **
109 **Return value:
110 **      error code
111 **
112 **Action:
113 **      read element under the specified path
114 **
115 *********************/
116 int mf_ug_fs_oper_read_dir(const char *path, Eina_List **dir_list, Eina_List **file_list)
117 {
118         UG_TRACE_BEGIN;
119         DIR *pDir = NULL;
120         struct dirent *ent;
121
122         ug_mf_retvm_if(path == NULL, MYFILE_ERR_INVALID_ARG, "path is null");
123         ug_mf_retvm_if(dir_list == NULL, MYFILE_ERR_INVALID_ARG, "dir_list is null");
124         ug_mf_retvm_if(file_list == NULL, MYFILE_ERR_INVALID_ARG, "file_list is null");
125
126         int option = MF_ERROR_CHECK_SRC_ARG_VALID | MF_ERROR_CHECK_SRC_EXIST | MF_ERROR_CHECK_SRC_PATH_VALID;
127         int ret = __mf_ug_fs_oper_file_system_error(path, NULL, option);
128
129         if (ret != MYFILE_ERR_NONE) {
130                 return ret;
131         }
132
133         pDir = opendir(path);
134
135         if (pDir == NULL) {
136                 return MYFILE_ERR_DIR_OPEN_FAIL;
137         }
138
139         while ((ent = readdir(pDir)) != NULL) {
140                 GString *childpath = NULL;
141                 ugFsNodeInfo *pNode = NULL;
142
143                 if (strncmp(ent->d_name, ".", 1) == 0 || strcmp(ent->d_name, "..") == 0) {
144                         continue;
145                 }
146
147                 if ((ent->d_type & DT_DIR) == 0 && (ent->d_type & DT_REG) == 0) {
148                         continue;
149                 }
150 #ifdef  UG_DEBUG_FOLDER_OPTION
151                 if ((ent->d_type & DT_DIR) != 0) {
152                         if ((strlen(path) == strlen(PHONE_FOLDER)) && (strcmp(path, PHONE_FOLDER) == 0)
153                             && (strlen(ent->d_name) == strlen(DEBUG_FOLDER)) && (strcmp(ent->d_name, DEBUG_FOLDER) == 0)) {
154                                 continue;
155                         }
156                 }
157 #endif
158                 pNode = (ugFsNodeInfo *) malloc(sizeof(ugFsNodeInfo));
159
160                 if (pNode == NULL) {
161                         continue;
162                 }
163                 memset(pNode, 0, sizeof(ugFsNodeInfo));
164                 snprintf(pNode->path, sizeof(pNode->path), "%s", path);
165                 snprintf(pNode->name, sizeof(pNode->name), "%s", ent->d_name);
166                 if (ent->d_type & DT_DIR) {
167                         pNode->type = UG_FILE_TYPE_DIR;
168                 } else if (ent->d_type & DT_REG) {
169                         mf_ug_file_attr_get_file_category(ent->d_name, &(pNode->type));
170                 }
171                 childpath = g_string_new(path);
172                 if (childpath == NULL) {
173                         free(pNode);
174                         pNode = NULL;
175                         continue;
176                 }
177                 g_string_append_printf(childpath, "/%s", ent->d_name);
178                 mf_ug_file_attr_get_file_stat(childpath->str, &pNode);
179                 if (pNode->type == UG_FILE_TYPE_DIR) {
180                         ug_mf_debug("dir append\n");
181                         *dir_list = eina_list_append(*dir_list, pNode);
182                 } else {
183                         ug_mf_debug("file append\n");
184                         ret = mf_ug_file_attr_get_file_ext(childpath->str, &pNode->ext);
185                         if (ret != MYFILE_ERR_NONE) {
186                                 pNode->ext = NULL;
187                         }
188                         *file_list = eina_list_append(*file_list, pNode);
189                 }
190
191                 g_string_free(childpath, TRUE);
192         }
193         closedir(pDir);
194         UG_TRACE_END;
195
196         return MYFILE_ERR_NONE;
197 }
198
199 /*********************
200 **Function name:        __mf_ug_fs_oper_exec_filter
201 **Parameter:
202 **      ugFsNodeInfo* pnode_info:       the node we need to check for filter
203 **      int option:                             filter
204 **
205 **Return value:
206 **      error code
207 **
208 **Action:
209 **      check if the node satisfied the filter option
210 *********************/
211 static mf_ug_drm_file_mime_type __mf_ug_fs_oper_get_drm_file_type_by_mime(char *mime_type)
212 {
213         gchar **result = NULL;
214         if (mime_type != NULL) {
215                 result = g_strsplit(mime_type, "/", 0);
216                 if (result && (*result)) {
217                         ug_mf_debug("*result is [%s]", *result);
218                         if (g_strcmp0(*result, "audio") == 0) {
219                                 g_strfreev(result);
220                                 return MF_UG_DRM_RINGTONE_FILE;
221                         } else if (g_strcmp0(*result, "image") == 0) {
222                                 g_strfreev(result);
223                                 return MF_UG_DRM_IMAGE_FILE;
224                         } else {
225                                 g_strfreev(result);
226                                 return MF_UG_DRM_UNKNOW_FILE;
227                         }
228                 } else {
229                         g_strfreev(result);
230                         return MF_UG_DRM_UNKNOW_FILE;
231                 }
232         } else {
233                 return MF_UG_DRM_UNKNOW_FILE;
234         }
235 }
236
237 mf_ug_drm_file_mime_type mf_ug_fs_oper_get_drm_type(char *path)
238 {
239         drm_result_e res = 0;
240         drm_content_info_s dcf_content_info;
241
242         res = drm_get_content_info(path, &dcf_content_info);
243
244
245         if (res == DRM_RETURN_SUCCESS) {
246                 mf_ug_drm_file_mime_type drm_mime_type = __mf_ug_fs_oper_get_drm_file_type_by_mime(dcf_content_info.mime_type);
247                 return drm_mime_type;
248         } else {
249                 return MF_UG_DRM_UNKNOW_FILE;
250         }
251 }
252
253 static int __drm_is_action_allowed(const char *path, drm_action_type_e action)
254 {
255         drm_bool_type_e is_allowed = 0;
256         drm_action_allowed_data_s action_data;
257
258         int ret = -1;
259         memset(&action_data,0x0,sizeof(drm_action_allowed_data_s));
260         UG_SAFE_STRCPY(action_data.file_path, path);
261
262         ret = drm_is_action_allowed(action,&action_data,&is_allowed);
263         if(DRM_RETURN_SUCCESS == ret && DRM_TRUE == is_allowed){
264                 return true;
265         }else{
266                 return false;
267         }
268 }
269
270 static bool __mf_ug_fs_oper_exec_drm_filter(ugFsNodeInfo *pnode_info, int option)
271 {
272         if (pnode_info == NULL) {
273                 return FALSE;
274         }
275
276         int result = -1;
277         char *fullpath = NULL;
278         drm_result_e res = DRM_RETURN_INTERNAL_ERROR;
279         if (option & MF_UG_FILTER_DRM_ALL) {
280                 return TRUE;
281         }
282
283         if (option & MF_UG_FILTER_DRM_WITHOUT_FL) {
284                 fullpath = g_strconcat(pnode_info->path, "/", pnode_info->name, NULL);
285                 res = __drm_is_action_allowed(fullpath, DRM_IS_FORWARDING_ALLOWED);
286                 if (res == DRM_RETURN_SUCCESS) {
287                         return FALSE;
288                 } else {
289                         return TRUE;
290                 }
291         }
292
293         if (option & MF_UG_FILTER_DRM_IMAGE) {
294                 fullpath = g_strconcat(pnode_info->path, "/", pnode_info->name, NULL);
295
296                 result = __drm_is_action_allowed(fullpath, DRM_HAS_VALID_SETAS_STATUS);
297                 if (result) {
298                         mf_ug_drm_file_mime_type drm_mime_type = mf_ug_fs_oper_get_drm_type(fullpath);
299                         if (drm_mime_type == MF_UG_DRM_IMAGE_FILE)
300                                 return TRUE;
301                         else
302                                 return FALSE;
303                 } else {
304                         return FALSE;
305                 }
306         }
307
308         if (option & MF_UG_FILTER_DRM_RINGTONE) {
309                 fullpath = g_strconcat(pnode_info->path, "/", pnode_info->name, NULL);
310
311                 result = __drm_is_action_allowed(fullpath, DRM_HAS_VALID_SETAS_STATUS);
312                 if (result) {
313                         mf_ug_drm_file_mime_type drm_mime_type = mf_ug_fs_oper_get_drm_type(fullpath);
314                         if (drm_mime_type == MF_UG_DRM_RINGTONE_FILE)
315                                 return TRUE;
316                         else
317                                 return FALSE;
318                 } else {
319                         return FALSE;
320                 }
321
322         }
323
324         return FALSE;
325 }
326
327 static bool __mf_ug_fs_oper_exec_filter(ugFsNodeInfo *pnode_info, int option)
328 {
329         if (pnode_info == NULL) {
330                 return FALSE;
331         }
332         if (option & UG_FILTER_CATEGORY_IMAGE) {
333                 if (pnode_info->type == UG_FILE_TYPE_IMAGE) {
334                         return TRUE;
335                 }
336         }
337         if (option & UG_FILTER_CATEGORY_VIDEO) {
338                 if (pnode_info->type == UG_FILE_TYPE_VIDEO) {
339                         return TRUE;
340                 }
341         }
342         if (option & UG_FILTER_CATEGORY_SOUND) {
343                 if (pnode_info->type == UG_FILE_TYPE_SOUND) {
344                         return TRUE;
345                 }
346         }
347         if (option & UG_FILTER_CATEGORY_VOICE) {
348                 if (pnode_info->type == UG_FILE_TYPE_VOICE) {
349                         return TRUE;
350                 }
351         }
352         if (option & UG_FILTER_CATEGORY_MUSIC) {
353                 if (pnode_info->type == UG_FILE_TYPE_MUSIC) {
354                         return TRUE;
355                 }
356         }
357         if (option & UG_FILTER_CATEGORY_HTML) {
358                 if (pnode_info->type == UG_FILE_TYPE_HTML) {
359                         return TRUE;
360                 }
361         }
362         if (option & UG_FILTER_CATEGORY_FLASH) {
363                 if (pnode_info->type == UG_FILE_TYPE_FLASH) {
364                         return TRUE;
365                 }
366         }
367         if (option & UG_FILTER_CATEGORY_GAME) {
368                 if (pnode_info->type == UG_FILE_TYPE_GAME) {
369                         return TRUE;
370                 }
371         }
372         if (option & UG_FILTER_CATEGORY_APP) {
373                 if (pnode_info->type == UG_FILE_TYPE_APP) {
374                         return TRUE;
375                 }
376         }
377         if (option & UG_FILTER_CATEGORY_THEME) {
378                 if (pnode_info->type == UG_FILE_TYPE_THEME) {
379                         return TRUE;
380                 }
381         }
382         if (option & UG_FILTER_CATEGORY_DOC) {
383                 if (pnode_info->type == UG_FILE_TYPE_DOC) {
384                         return TRUE;
385                 }
386         }
387         if (option & UG_FILTER_CATEGORY_EXCEL) {
388                 if (pnode_info->type == UG_FILE_TYPE_EXCEL) {
389                         return TRUE;
390                 }
391         }
392         if (option & UG_FILTER_CATEGORY_PPT) {
393                 if (pnode_info->type == UG_FILE_TYPE_PPT) {
394                         return TRUE;
395                 }
396         }
397         if (option & UG_FILTER_CATEGORY_PDF) {
398                 if (pnode_info->type == UG_FILE_TYPE_PDF) {
399                         return TRUE;
400                 }
401         }
402         if (option & UG_FILTER_CATEGORY_TXT) {
403                 if (pnode_info->type == UG_FILE_TYPE_TXT) {
404                         return TRUE;
405                 }
406         }
407         if (option & UG_FILTER_CATEGORY_VCONTACT) {
408                 if (pnode_info->type == UG_FILE_TYPE_VCONTACT) {
409                         return TRUE;
410                 }
411         }
412         if (option & UG_FILTER_CATEGORY_VCALENDAR) {
413                 if (pnode_info->type == UG_FILE_TYPE_VCALENDAR) {
414                         return TRUE;
415                 }
416         }
417         if (option & UG_FILTER_CATEGORY_VNOTE) {
418                 if (pnode_info->type == UG_FILE_TYPE_VNOTE) {
419                         return TRUE;
420                 }
421         }
422         if (option & UG_FILTER_CATEGORY_VBOOKMARK) {
423                 if (pnode_info->type == UG_FILE_TYPE_VBOOKMARK) {
424                         return TRUE;
425                 }
426         }
427         if (option & UG_FILTER_CATEGORY_VIDEO_PROJECT) {
428                 if (pnode_info->type == UG_FILE_TYPE_VIDEO_PROJECT) {
429                         return TRUE;
430                 }
431         }
432         if (option & UG_FILTER_CATEGORY_SVG) {
433                 if (pnode_info->type == UG_FILE_TYPE_SVG) {
434                         return TRUE;
435                 }
436         }
437         if (option & UG_FILTER_CATEGORY_RSS) {
438                 if (pnode_info->type == UG_FILE_TYPE_RSS) {
439                         return TRUE;
440                 }
441         }
442         if (option & UG_FILTER_CATEGORY_ETC) {
443                 if (pnode_info->type == UG_FILE_TYPE_ETC) {
444                         return TRUE;
445                 }
446         }
447         return FALSE;
448 }
449
450 int mf_ug_fs_oper_list_filter(Eina_List *in_list, Eina_List **out_list, int option, int drm_opt)
451 {
452         ug_mf_debug();
453         if (in_list == NULL) {
454                 return MYFILE_ERR_SRC_ARG_INVALID;
455         }
456
457         if (out_list == NULL) {
458                 return MYFILE_ERR_DST_ARG_INVALID;
459         }
460
461         if (option == 0) {
462                 *out_list = in_list;
463                 return MYFILE_ERR_NONE;
464         }
465
466         Eina_List *l = NULL;
467         ugFsNodeInfo *data = NULL;
468         EINA_LIST_FOREACH(in_list, l, data) {
469                 if (data->type == UG_FILE_TYPE_DRM && __mf_ug_fs_oper_exec_drm_filter(data, drm_opt)) {
470                         ug_debug("file [%s] is drm file", data->name);
471                         *out_list = eina_list_append(*out_list, data);
472                 } else if (__mf_ug_fs_oper_exec_filter(data, option)) {
473                         *out_list = eina_list_append(*out_list, data);
474                 }
475         }
476         return MYFILE_ERR_NONE;
477 }
478
479 /******************************
480 ** Prototype    : ug_mf_list_filter_by_extention
481 ** Description  : filter from list by extension
482 ** Input        : Eina_List *in_list
483 **                Eina_List **out_list
484 **                char* ext
485 ** Output       : None
486 ** Return Value :
487 ** Calls        :
488 ** Called By    :
489 **
490 **  History        :
491 **  1.Date         : 2010/12/10
492 **    Author       : Samsung
493 **    Modification : Created function
494 **
495 ******************************/
496 int mf_ug_fs_oper_list_filter_by_extension(Eina_List *in_list, Eina_List **out_list, char *ext)
497 {
498         if (in_list == NULL) {
499                 return MYFILE_ERR_SRC_ARG_INVALID;
500         }
501
502         if (out_list == NULL) {
503                 return MYFILE_ERR_DST_ARG_INVALID;
504         }
505
506         if (ext == NULL) {
507                 *out_list = in_list;
508                 return MYFILE_ERR_NONE;
509         }
510
511         Eina_List *l = NULL;
512         ugFsNodeInfo *data = NULL;
513
514         char *seps = ";";
515         char *temp_ext = malloc(strlen(ext) + 1);
516         gchar **result = NULL;
517         gchar **params = NULL;
518
519         EINA_LIST_FOREACH(in_list, l, data) {
520                 memset(temp_ext, 0, strlen(ext) + 1);
521                 strncpy(temp_ext, ext, strlen(ext));
522                 result = g_strsplit(temp_ext, seps, 0);
523                 if (result == NULL) {
524                         continue;
525                 }
526                 for (params = result; *params; params++) {
527                         if (data->ext == NULL)
528                                 break;
529                         if (strcasecmp(data->ext, *params) == 0) {
530                                 *out_list = eina_list_append(*out_list, data);
531                                 break;
532                         }
533                 }
534
535                 g_strfreev(result);
536                 result = NULL;
537         }
538         free(temp_ext);
539         return MYFILE_ERR_NONE;
540 }
541
542 static int __mf_ug_fs_oper_sort_by_priority(const void *d1, const void *d2, int sequence_type)
543 {
544         int ret = 0;
545         switch(sequence_type) {
546         case MF_UG_SORT_BY_PRIORITY_TYPE_A2Z:
547                 ret = __mf_ug_fs_oper_sort_by_date_cb_O2R(d1, d2);
548                 if (ret == 0) {
549                         ret = __mf_ug_fs_oper_sort_by_size_cb_S2L(d1, d2);
550                         if (ret == 0) {
551                                 ret = __mf_ug_fs_oper_sort_by_name_cb_A2Z(d1, d2);
552                         }
553                 }
554                 break;
555         case MF_UG_SORT_BY_PRIORITY_TYPE_Z2A:
556                 ret = __mf_ug_fs_oper_sort_by_date_cb_R2O(d1, d2);
557                 if (ret == 0) {
558                         ret = __mf_ug_fs_oper_sort_by_size_cb_L2S(d1, d2);
559                         if (ret == 0) {
560                                 ret = __mf_ug_fs_oper_sort_by_name_cb_Z2A(d1, d2);
561                         }
562                 }
563                 break;
564         case MF_UG_SORT_BY_PRIORITY_DATE_O2R:
565                 ret = __mf_ug_fs_oper_sort_by_size_cb_S2L(d1, d2);
566                 if (ret == 0) {
567                         ret = __mf_ug_fs_oper_sort_by_name_cb_A2Z(d1, d2);
568                 }
569                 break;
570         case MF_UG_SORT_BY_PRIORITY_DATE_R2O:
571                 ret = __mf_ug_fs_oper_sort_by_size_cb_L2S(d1, d2);
572                 if (ret == 0) {
573                         ret = __mf_ug_fs_oper_sort_by_name_cb_Z2A(d1, d2);
574                 }
575                 break;
576         case MF_UG_SORT_BY_PRIORITY_SIZE_S2L:
577                 ret = __mf_ug_fs_oper_sort_by_name_cb_A2Z(d1, d2);
578                 break;
579         case MF_UG_SORT_BY_PRIORITY_SIZE_L2S:
580                 ret = __mf_ug_fs_oper_sort_by_name_cb_Z2A(d1, d2);
581                 break;
582         default:
583                 break;
584         }
585         return ret;
586 }
587 /*********************
588 **Function name:        __sort_by_name_cb
589 **Parameter:
590 **      const void *d1: node1 to compare
591 **      const void *d2: node2 to compare
592 **
593 **Return value:
594 **      -1      if d1 > d2
595 **      0       if d1 = d2
596 **      1       if d1 > d2
597 **
598 **Action:
599 **      sort the list order by the Assic table
600
601 **
602 *********************/
603 static int __mf_ug_fs_oper_sort_by_name_cb_A2Z(const void *d1, const void *d2)
604 {
605         ugFsNodeInfo *txt1 = (ugFsNodeInfo *) d1;
606         ugFsNodeInfo *txt2 = (ugFsNodeInfo *) d2;
607         gchar *name1 = NULL;
608         gchar *name2 = NULL;
609         int result = 0;
610
611         if (!txt1) {
612                 return (1);
613         }
614         if (!txt2) {
615                 return (-1);
616         }
617
618         name1 = g_ascii_strdown(txt1->name, strlen(txt1->name));
619         if (name1 == NULL) {
620                 return (-1);
621         }
622         name2 = g_ascii_strdown(txt2->name, strlen(txt2->name));
623         if (name2 == NULL) {
624                 g_free(name1);
625                 name1 = NULL;
626                 return (-1);
627         }
628         result = g_strcmp0(name1, name2);
629
630         g_free(name1);
631         name1 = NULL;
632         g_free(name2);
633         name2 = NULL;
634         return result;
635
636 }
637
638 /*********************
639 **Function name:        __sort_by_date_cb
640 **Parameter:
641 **      const void *d1: node1 to compare
642 **      const void *d2: node2 to compare
643 **
644 **Return value:
645 **      -1      if d1 > d2
646 **      0       if d1 = d2
647 **      1       if d1 > d2
648 **
649 **Action:
650 **      sort the list order by the later created the later shown
651 *********************/
652 static int __mf_ug_fs_oper_sort_by_date_cb_O2R(const void *d1, const void *d2)
653 {
654         int ret = 0;
655         ugFsNodeInfo *time1 = (ugFsNodeInfo *) d1;
656         ugFsNodeInfo *time2 = (ugFsNodeInfo *) d2;
657
658         if (!d1) {
659                 return 1;
660         }
661         if (!d2) {
662                 return -1;
663         }
664
665         if (time1->date > time2->date) {
666                 ret = 1;
667         } else if (time1->date < time2->date) {
668                 ret = -1;
669         } else {
670                 ret = 0;
671         }
672
673         if (ret == 0) {
674                 ret = __mf_ug_fs_oper_sort_by_priority(d1, d2, MF_UG_SORT_BY_PRIORITY_DATE_O2R);
675         }
676         return ret;
677 }
678
679 /*********************
680 **Function name:        __sort_by_type_cb
681 **Parameter:
682 **      const void *d1: node1 to compare
683 **      const void *d2: node2 to compare
684 **
685 **Return value:
686 **      -1      if d1 < d2
687 **      0       if d1 = d2
688 **      1       if d1 > d2
689 **
690 **Action:
691 **      sort the list order by the category type value
692 *********************/
693 static int __mf_ug_fs_oper_sort_by_type_cb_A2Z(const void *d1, const void *d2)
694 {
695         ugFsNodeInfo *type1 = (ugFsNodeInfo *) d1;
696         ugFsNodeInfo *type2 = (ugFsNodeInfo *) d2;
697         gchar *ext1 = NULL;
698         gchar *ext2 = NULL;
699         int result = 0;
700
701         if (type1 == NULL || type1->ext == NULL) {
702                 return 1;
703         }
704
705         if (type2 == NULL || type2->ext == NULL) {
706                 return -1;
707         }
708         ext1 = g_ascii_strdown(type1->ext, strlen(type1->ext));
709         if (ext1 == NULL) {
710                 return (-1);
711         }
712         ext2 = g_ascii_strdown(type2->ext, strlen(type2->ext));
713         if (ext2 == NULL) {
714                 g_free(ext1);
715                 ext1 = NULL;
716                 return (-1);
717         }
718         result = g_strcmp0(ext1, ext2);
719
720         g_free(ext1);
721         ext1 = NULL;
722         g_free(ext2);
723         ext2 = NULL;
724
725         if (result == 0) {
726                 result = __mf_ug_fs_oper_sort_by_priority(d1, d2, MF_UG_SORT_BY_PRIORITY_TYPE_A2Z);
727         }
728
729         return result;
730 }
731
732 /*order:        the one with smaller size will be shown earlier*/
733 /*********************
734 **Function name:        __sort_by_name_cb
735 **Parameter:
736 **      const void *d1: node1 to compare
737 **      const void *d2: node2 to compare
738 **
739 **Return value:
740 **      -1      if d1 > d2
741 **      0       if d1 = d2
742 **      1       if d1 > d2
743 **
744 **Action:
745 **      sort the list order by size, rule is the smaller the later shown
746 *********************/
747 static int __mf_ug_fs_oper_sort_by_size_cb_S2L(const void *d1, const void *d2)
748 {
749         int ret = 0;
750         ugFsNodeInfo *size1 = (ugFsNodeInfo *) d1;
751         ugFsNodeInfo *size2 = (ugFsNodeInfo *) d2;
752
753         if (!d1) {
754                 return 1;
755         }
756
757         if (!d2) {
758                 return -1;
759         }
760
761         if (size1->size > size2->size) {
762                 ret = 1;
763         } else if (size1->size < size2->size) {
764                 ret = -1;
765         } else {
766                 ret = 0;
767         }
768
769         if (ret == 0) {
770                 ret = __mf_ug_fs_oper_sort_by_priority(d1, d2, MF_UG_SORT_BY_PRIORITY_SIZE_S2L);
771         }
772         return ret;
773 }
774
775 /*********************
776 **Function name:        __mf_fs_oper_sort_by_name_cb_Z2A
777 **Parameter:
778 **      const void *d1: node1 to compare
779 **      const void *d2: node2 to compare
780 **
781 **Return value:
782 **      1       if d1 > d2
783 **      -1      if d1 <= d2
784 **
785 **Action:
786 **      sort the list order by the Assic table
787
788 **
789 *********************/
790 static int __mf_ug_fs_oper_sort_by_name_cb_Z2A(const void *d1, const void *d2)
791 {
792         ugFsNodeInfo *txt1 = (ugFsNodeInfo *) d1;
793         ugFsNodeInfo *txt2 = (ugFsNodeInfo *) d2;
794
795         int result = 0;
796
797         if (!txt1) {
798                 return (1);
799         }
800         if (!txt2) {
801                 return (-1);
802         }
803         result = strcasecmp(txt1->name, txt2->name);
804
805         if (result < 0) {
806                 return (1);
807         } else {
808                 return (-1);
809         }
810 }
811
812 /*********************
813 **Function name:        __sort_by_date_cb
814 **Parameter:
815 **      const void *d1: node1 to compare
816 **      const void *d2: node2 to compare
817 **
818 **Return value:
819 **      -1      if d1 > d2
820 **      0       if d1 = d2
821 **      1       if d1 < d2
822 **
823 **Action:
824 **      sort the list order by the later created the later shown
825 *********************/
826 static int __mf_ug_fs_oper_sort_by_date_cb_R2O(const void *d1, const void *d2)
827 {
828         int ret = 0;
829         ugFsNodeInfo *time1 = (ugFsNodeInfo *) d1;
830         ugFsNodeInfo *time2 = (ugFsNodeInfo *) d2;
831
832         if (!d1) {
833                 return -1;
834         }
835         if (!d2) {
836                 return 1;
837         }
838         if (time1->date > time2->date) {
839                 ret = -1;
840         } else if (time1->date < time2->date) {
841                 ret = 1;
842         } else {
843                 ret = 0;
844         }
845
846         if (ret == 0) {
847                 ret = __mf_ug_fs_oper_sort_by_priority(d1, d2, MF_UG_SORT_BY_PRIORITY_DATE_R2O);
848         }
849         return ret;
850 }
851
852 /*********************
853 **Function name:        __sort_by_type_cb
854 **Parameter:
855 **      const void *d1: node1 to compare
856 **      const void *d2: node2 to compare
857 **
858 **Return value:
859 **      -1      if d1 > d2
860 **      0       if d1 = d2
861 **      1       if d1 < d2
862 **
863 **Action:
864 **      sort the list order by the category type value
865 *********************/
866 static int __mf_ug_fs_oper_sort_by_type_cb_Z2A(const void *d1, const void *d2)
867 {
868         ugFsNodeInfo *type1 = (ugFsNodeInfo *) d1;
869         ugFsNodeInfo *type2 = (ugFsNodeInfo *) d2;
870         gchar *ext1 = NULL;
871         gchar *ext2 = NULL;
872         int result = 0;
873
874         if (type1 == NULL || type1->ext == NULL) {
875                 return -1;
876         }
877
878         if (type2 == NULL || type2->ext == NULL) {
879                 return 1;
880         }
881
882         ext1 = g_ascii_strdown(type1->ext, strlen(type1->ext));
883         if (ext1 == NULL) {
884                 return (1);
885         }
886         ext2 = g_ascii_strdown(type2->ext, strlen(type2->ext));
887         if (ext2 == NULL) {
888                 g_free(ext1);
889                 ext1 = NULL;
890                 return (-1);
891         }
892         result = g_strcmp0(ext1, ext2);
893         g_free(ext1);
894         ext1 = NULL;
895         g_free(ext2);
896         ext2 = NULL;
897         if (result == 0) {
898                 result = __mf_ug_fs_oper_sort_by_priority(d1, d2, MF_UG_SORT_BY_PRIORITY_TYPE_Z2A);
899         }
900
901         return -result;
902 }
903
904 /*order:        the one with smaller size will be shown earlier*/
905 /*********************
906 **Function name:        __sort_by_name_cb
907 **Parameter:
908 **      const void *d1: node1 to compare
909 **      const void *d2: node2 to compare
910 **
911 **Return value:
912 **      -1      if d1 > d2
913 **      0       if d1 = d2
914 **      1       if d1 < d2
915 **
916 **Action:
917 **      sort the list order by size, rule is the smaller the later shown
918 *********************/
919 static int __mf_ug_fs_oper_sort_by_size_cb_L2S(const void *d1, const void *d2)
920 {
921         int ret = 0;
922         ugFsNodeInfo *size1 = (ugFsNodeInfo *) d1;
923         ugFsNodeInfo *size2 = (ugFsNodeInfo *) d2;
924
925         if (!d1) {
926                 return -1;
927         }
928
929         if (!d2) {
930                 return 1;
931         }
932
933         if (size1->size > size2->size) {
934                 ret = -1;
935         } else if (size1->size < size2->size) {
936                 ret = 1;
937         } else {
938                 ret = 0;
939         }
940
941         if (ret == 0) {
942                 ret = __mf_ug_fs_oper_sort_by_priority(d1, d2, MF_UG_SORT_BY_PRIORITY_SIZE_L2S);
943         }
944         return ret;
945 }
946
947 /*********************
948 **Function name:        mf_fs_oper_sort_list
949 **Parameter:
950 **      Eina_List **list:       the list we need to sort
951 **      int sort_opt:           sort option
952 **
953 **Return value:
954 **      void
955 **
956 **Action:
957 **      sort the list order by sort option with the call back
958 *********************/
959 void mf_ug_fs_oper_sort_list(Eina_List **list, int sort_opt)
960 {
961         Eina_Compare_Cb sort_func = NULL;
962         if (!(*list)) {
963                 return;
964         }
965         switch (sort_opt) {
966         case MF_UG_SORT_BY_NAME_A2Z:
967                 sort_func = __mf_ug_fs_oper_sort_by_name_cb_A2Z;
968                 break;
969         case MF_UG_SORT_BY_TYPE_A2Z:
970                 sort_func = __mf_ug_fs_oper_sort_by_type_cb_A2Z;
971                 break;
972         case MF_UG_SORT_BY_SIZE_S2L:
973                 sort_func = __mf_ug_fs_oper_sort_by_size_cb_S2L;
974                 break;
975         case MF_UG_SORT_BY_DATE_O2R:
976                 sort_func = __mf_ug_fs_oper_sort_by_date_cb_O2R;
977                 break;
978         case MF_UG_SORT_BY_NAME_Z2A:
979                 sort_func = __mf_ug_fs_oper_sort_by_name_cb_Z2A;
980                 break;
981         case MF_UG_SORT_BY_TYPE_Z2A:
982                 sort_func = __mf_ug_fs_oper_sort_by_type_cb_Z2A;
983                 break;
984         case MF_UG_SORT_BY_SIZE_L2S:
985                 sort_func = __mf_ug_fs_oper_sort_by_size_cb_L2S;
986                 break;
987         case MF_UG_SORT_BY_DATE_R2O:
988                 sort_func = __mf_ug_fs_oper_sort_by_date_cb_R2O;
989                 break;
990         default:
991                 sort_func = __mf_ug_fs_oper_sort_by_type_cb_A2Z;
992                 break;
993         }
994         *list = eina_list_sort(*list, eina_list_count(*list), sort_func);
995 }
996
997 int mf_ug_fs_oper_create_dir(const char *dir)
998 {
999         int option = MF_ERROR_CHECK_SRC_ARG_VALID | MF_ERROR_CHECK_DUPLICATED;
1000         int ret = __mf_ug_fs_oper_file_system_error(dir, dir, option);
1001
1002         if (ret != 0) {
1003                 return ret;
1004         }
1005
1006         ret = mf_ug_file_attr_is_right_dir_path(dir);
1007
1008         if (ret != 0) {
1009                 return ret;
1010         }
1011
1012         mode_t default_mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
1013
1014         if (mkdir(dir, default_mode) < 0) {
1015                 return MYFILE_ERR_DIR_CREATE_FAIL;
1016         }
1017         return MYFILE_ERR_NONE;
1018 }
1019