summaryrefslogtreecommitdiff
path: root/src/common/mf-ug-search-internal.c
blob: 55e6029734fb31d4fd422dcce25a9eff9ba16529 (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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
/*
 * Copyright 2012          Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.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://floralicense.org/license/
 *
 * 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 <glib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <pthread.h>
#include <Ecore.h>

#include "mf-ug-main.h"
#include "mf-ug-search.h"
#include "mf-ug-search-internal.h"
#include "mf-ug-fs-util.h"

#define APPEND_SIZE 2		/* for null and slash */
#define MF_ERR_BUF		256

#define NORMALIZE_OPTION G_NORMALIZE_NFD

#ifdef CHECK_RESTRICTED_PATH
/* TODO
 *  This code should be revised.
 *  How to get restricted path information?
 *  I think this module should not depend on other lib(except glib and stdlib).
*/
#define ROOT_UMS "/opt/usr/media"
#define ROOT_MMC "/opt/storage/sdcard"
#endif /* CHECK_RESTRICTED_PATH */

int flagSearchMsg = 1;
pthread_mutex_t gLockSearchMsg;
pthread_cond_t gCondSearchMsg;

static void __mf_ug_search_tx_wait();
static void __mf_ug_search_result_publish_msg(mf_search_pipe_msg_type type, void *result, void *user_data);

inline static void __mf_ug_search_cmd_lock(ms_handle_t *handle)
{
	if (handle && handle->cmd_lock) {
		g_mutex_lock(handle->cmd_lock);
	}
	return;
}

inline static void __mf_ug_search_cmd_unlock(ms_handle_t *handle)
{
	if (handle && handle->cmd_lock) {
		g_mutex_unlock(handle->cmd_lock);
	}
	return;
}

inline static void __mf_ug_search_thread_lock(ms_handle_t *handle)
{
	if (handle && handle->thread_mutex) {
		g_mutex_lock(handle->thread_mutex);
	}
	return;
}

inline static void __mf_ug_search_thread_unlock(ms_handle_t *handle)
{
	if (handle && handle->thread_mutex) {
		g_mutex_unlock(handle->thread_mutex);
	}
	return;
}

inline static void __mf_ug_search_args_free(ms_args_t *args)
{
	if (args) {
		if (args->root_path) {
			g_list_foreach(args->root_path, (GFunc) g_free, NULL);
			g_list_free(args->root_path);
			args->root_path = NULL;
		}

		if (args->needle)
			g_free(args->needle);

		g_free(args);
	}
	return;
}

inline static void __mf_ug_search_result_free(mf_search_result_t *result)
{
	if (result) {
		if (result->current_dir) {
			g_free(result->current_dir);
			result->current_dir = NULL;
		}
		if (result->dir_list) {
			g_list_foreach(result->dir_list, (GFunc) g_free, NULL);
			g_list_free(result->dir_list);
			result->dir_list = NULL;
		}
		if (result->file_list) {
			g_list_foreach(result->file_list, (GFunc) g_free, NULL);
			g_list_free(result->file_list);
			result->file_list = NULL;
		}
		g_free(result);
	}
	return;
}

#ifdef CHECK_RESTRICTED_PATH
gboolean __mf_ug_search_check_licet_path(const char *path)
{
	return (gboolean) (strstr(path, ROOT_UMS) || strstr(path, ROOT_MMC));
}
#endif /*CHECK_RESTRICTED_PATH*/


 /*This function is for testing and should be revised for performance before applying*/
static inline gboolean __mf_ug_search_has_nonspacing_mark(const char *nstr)
{
	if (nstr) {
		const char *p_str = nstr;
		while (p_str && *p_str) {
			gunichar uc;
			uc = g_utf8_get_char(p_str);
			if (g_unichar_type(uc) == G_UNICODE_NON_SPACING_MARK) {
				return TRUE;
			} else {
				p_str = g_utf8_next_char(p_str);
			}
		}
	}
	return FALSE;
}

static gboolean __mf_ug_search_NFD_ext(const char *str, const char *needle)
{
	int s_len = 0;
	int n_len = 0;
	if (!str) {
		return FALSE;
	}
	s_len = strlen(str);

	if (!needle) {
		return FALSE;
	} else {
		n_len = strlen(needle);
		if (n_len == 0) {
			return FALSE;
		}
	}
	if (s_len < n_len)
		return FALSE;
	char *pdot = strrchr(str, '.');

	if (!pdot) {
		return FALSE;
	} else if (pdot != str) {
		char *ext = NULL;;
		ext = g_strdup(pdot+1);
		if (g_strcmp0(ext, needle)== 0) {
			g_free(ext);
			ext = NULL;
			return TRUE;
		}
		else {
			g_free(ext);
			ext = NULL;
			return FALSE;
		}
	} else {
		return FALSE;
	}

}
static gboolean __mf_ug_search_NFD_strstr(const char *str, const char *needle)
{
	int s_len = 0;
	int n_len = 0;

	if (!str) {
		return FALSE;
	}
	s_len = strlen(str);

	if (!needle) {
		return FALSE;
	} else {
		n_len = strlen(needle);
		if (n_len == 0) {
			return FALSE;
		}
	}

	if (s_len < n_len)
		return FALSE;

	if (__mf_ug_search_has_nonspacing_mark(str)) {
		const char *p_str = str;
		const char *end = p_str + s_len - n_len;

		while (p_str && p_str <= end && *p_str) {
			const char *s = p_str;
			const char *n = needle;
			while (n && *n) {
				if (s && *s) {
					gunichar sc, nc;
					sc = g_utf8_get_char(s);
					nc = g_utf8_get_char(n);
					if (g_unichar_type(sc) == G_UNICODE_NON_SPACING_MARK) {
						if (g_unichar_type(nc) == G_UNICODE_NON_SPACING_MARK) {
							if (sc != nc) {
								goto next;
							} else {
								s = g_utf8_next_char(s);
								n = g_utf8_next_char(n);
							}
						} else {
							s = g_utf8_next_char(s);
						}
					} else if (sc != nc) {
						goto next;
					} else {
						s = g_utf8_next_char(s);
						n = g_utf8_next_char(n);
					}
				} else {
					return FALSE;
				}
			}

			return TRUE;
next:
			p_str = g_utf8_next_char(p_str);
		}
	} else {
		return (gboolean) (!(!strstr(str, needle)));
	}
	return FALSE;
}

static GList *__mf_ug_search_do_find(const char *root, const char *needle, mf_search_option option, ms_handle_t *handle)
{
	DIR *directory = NULL;
	GList *candidate = NULL;

	char *up_needle = NULL;
	char *up_name = NULL;

	if (!handle) {
		ms_error("handle is NULL");
		return NULL;
	}

	if (!handle->result) {
		ms_error("handle->result is NULL");
		return NULL;
	}

	if (!root || !needle) {
		ms_error("invaild args");
		return NULL;
	}

	if (!g_file_test(root, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
		ms_error("invaild root_path : %s", root);
		return NULL;
	}

	directory = opendir(root);
	if (directory) {
		mf_search_result_t *result = NULL;
		struct dirent *entry = NULL;

		result = handle->result;
		__mf_ug_search_thread_lock(handle);
		if (result->current_dir) {
			g_free(result->current_dir);
		}
		result->current_dir = g_strdup(root);
		__mf_ug_search_thread_unlock(handle);
		while ((entry = readdir(directory)) != NULL) {
			if (!(option & MF_SEARCH_OPT_HIDDEN) && (0 == strncmp(entry->d_name, ".", 1))) {
				ms_debug("[%s] is hidden file. Skip it", entry->d_name);
				continue;
			}

			if (handle->is_stop == TRUE) {
				ms_debug("break from do find");
				break;
			}

			if (entry->d_type & DT_REG) {
				if (option & MF_SEARCH_OPT_FILE) {
					__mf_ug_search_thread_lock(handle);
					result->total_count++;
					__mf_ug_search_thread_unlock(handle);

					up_name = g_utf8_strup(entry->d_name, strlen(entry->d_name));
					up_needle = g_utf8_strup(needle, strlen(needle));

					/*Todo:*/
					/*      should we check the return value for further use? */
					gchar *nor_str = g_utf8_normalize(up_name, -1, NORMALIZE_OPTION);
					if (__mf_ug_search_NFD_strstr(nor_str, up_needle))
					{
						gchar *path = NULL;
						gssize len = strlen(root) + strlen(entry->d_name) + APPEND_SIZE;	/* for null and slash*/
						path = g_malloc(sizeof(gchar) * len);
						if (path) {
							g_snprintf(path, len, "%s/%s", root, entry->d_name);

							__mf_ug_search_thread_lock(handle);
							result->file_list = g_list_append(result->file_list, (gpointer) path);
							result->is_end = FALSE;
							__mf_ug_search_thread_unlock(handle);

							__mf_ug_search_tx_wait();
							__mf_ug_search_result_publish_msg(MF_SEARCH_PIPE_MSG_RESULT_REPORT, result,
										       handle->args->user_data);
						}
						/*1  TODO: how can i handle else case?*/
					}
					g_free(nor_str);
					free(up_needle);
					up_needle = NULL;
					free(up_name);
					up_name = NULL;
				}else if (option & MF_SEARCH_OPT_EXT) {
					__mf_ug_search_thread_lock(handle);
					result->total_count++;
					__mf_ug_search_thread_unlock(handle);

					up_name = g_utf8_strup(entry->d_name, strlen(entry->d_name));
					up_needle = g_utf8_strup(needle, strlen(needle));

					/*Todo:*/
					/*      should we check the return value for further use? */
					gchar *nor_str = g_utf8_normalize(up_name, -1, NORMALIZE_OPTION);
					if (__mf_ug_search_NFD_ext(nor_str, up_needle))
					{
						gchar *path = NULL;
						gssize len = strlen(root) + strlen(entry->d_name) + APPEND_SIZE;	/* for null and slash*/
						path = g_malloc(sizeof(gchar) * len);
						if (path) {
							g_snprintf(path, len, "%s/%s", root, entry->d_name);

							__mf_ug_search_thread_lock(handle);
							result->file_list = g_list_append(result->file_list, (gpointer) path);
							result->is_end = FALSE;
							__mf_ug_search_thread_unlock(handle);

							__mf_ug_search_tx_wait();
							__mf_ug_search_result_publish_msg(MF_SEARCH_PIPE_MSG_RESULT_REPORT, result,
										       handle->args->user_data);
						}
						/*1  TODO: how can i handle else case?*/
					}
					g_free(nor_str);
					free(up_needle);
					up_needle = NULL;
					free(up_name);
					up_name = NULL;

				}
			} else if (entry->d_type & DT_DIR) {
				gchar *path = NULL;
				gssize len = 0;

				len = strlen(entry->d_name);
				/*skip current and upper directory*/
				if (0 == strncmp(entry->d_name, ".", strlen(".")) || 0 == strncmp(entry->d_name, "..", strlen(".."))) {
					continue;
				}
				/* we are not going to search /opt/media/SLP_Debug folder */
				if ((strlen(result->current_dir) == strlen(PHONE_FOLDER)) && (strcmp(result->current_dir, PHONE_FOLDER) == 0)
				    && (strlen(entry->d_name) == strlen(DEBUG_FOLDER)) && (strcmp(entry->d_name, DEBUG_FOLDER) == 0)) {
					ms_debug("[%s] is hidden folder. Skip it", entry->d_name);
					continue;
				}

				len = strlen(root) + strlen(entry->d_name) + APPEND_SIZE;	/* for null and slash */
				path = g_malloc(sizeof(gchar) * len);
				if (path) {
					g_snprintf(path, len, "%s/%s", root, entry->d_name);
					candidate = g_list_append(candidate, (gpointer) path);
				}
				/*1  TODO: how can i handle else case?*/
				if (option & MF_SEARCH_OPT_DIR) {
					__mf_ug_search_thread_lock(handle);
					result->total_count++;
					__mf_ug_search_thread_unlock(handle);

					up_name = g_utf8_strup(entry->d_name, strlen(entry->d_name));
					if (needle && strlen(needle)) {
						up_needle = g_utf8_strup(needle, strlen(needle));
						gchar *nor_str = g_utf8_normalize(up_name, -1, NORMALIZE_OPTION);
						if (__mf_ug_search_NFD_strstr(nor_str, up_needle))
						{
							__mf_ug_search_thread_lock(handle);
							result->dir_list = g_list_append(result->dir_list, (gpointer) g_strdup(path));
							result->is_end = FALSE;
							__mf_ug_search_thread_unlock(handle);
							__mf_ug_search_tx_wait();
							__mf_ug_search_result_publish_msg(MF_SEARCH_PIPE_MSG_RESULT_REPORT, result, handle->args->user_data);
						}
						g_free(nor_str);
						free(up_name);
						up_name = NULL;

						free(up_needle);
						up_needle = NULL;
					} else {
						free(up_name);
						up_name = NULL;
					}
				}
			}
		}
		closedir(directory);
		directory = NULL;
	}

	return candidate;
}

static gpointer __mf_ug_search_find_thread(gpointer data)
{
	ms_handle_t *handle = (ms_handle_t *) data;
	if (handle) {
		ms_args_t *args = NULL;
		mf_search_result_t *result = NULL;

		result = handle->result;
		args = handle->args;

		if (args && result) {
			GList *root = NULL;
			GList *candidate = NULL;	/*use this list as stack*/
			root = args->root_path;
			while (root) {
				char *path = (char *)root->data;
				if (path) {
					/*push root paths to stack*/
					candidate = g_list_append(candidate, (gpointer) g_strdup(path));
				}
				root = g_list_next(root);
			}

			while (candidate) {
				GList *new_list = NULL;
				GList *list = NULL;
				gchar *item = NULL;

				__mf_ug_search_thread_lock(handle);
				if (handle->is_stop) {
					__mf_ug_search_thread_unlock(handle);
					result->is_end = TRUE;
					goto MF_FIND_THREAD_EXIT;
				}
				__mf_ug_search_thread_unlock(handle);

				list = g_list_first(candidate);
				/*pop one path from stack*/
				candidate = g_list_remove_link(candidate, list);
				item = (gchar *) list->data;
				if (item) {
					ms_debug("current : %s", item);
#if 0
					__mf_ug_search_thread_lock(handle);
					if (result->current_dir) {
						g_free(result->current_dir);
					}
					result->current_dir = g_strdup(item);
					__mf_ug_search_thread_unlock(handle);
#endif
					/*publish root change message here*/
					__mf_ug_search_tx_wait();
					if (handle->is_stop) {
						result->is_end = TRUE;
						goto MF_FIND_THREAD_EXIT;
					}
					__mf_ug_search_result_publish_msg(MF_SEARCH_PIPE_MSG_ROOT_CHANGE, item, args->user_data);
					new_list = __mf_ug_search_do_find(item, args->needle, args->option, handle);
					g_free(item);
					item = NULL;
					g_list_free(list);
					list = NULL;
				}
				/*push new paths to stack*/
				candidate = g_list_concat(new_list, candidate);
			}

			__mf_ug_search_thread_lock(handle);
			result->is_end = TRUE;
			__mf_ug_search_thread_unlock(handle);
			__mf_ug_search_tx_wait();
			__mf_ug_search_result_publish_msg(MF_SEARCH_PIPE_MSG_FINISHED, handle->result, args->user_data);
MF_FIND_THREAD_EXIT:
			if (candidate) {
				g_list_foreach(candidate, (GFunc) g_free, NULL);
				g_list_free(candidate);
				candidate = NULL;
			}
		} else {
			ms_error("args : %p or result : %p is not allocated yet!!", handle->args, handle->result);
		}
	}
	/*g_thread_exit(NULL);*/
	return NULL;
}

int _mf_ug_search_init(ms_handle_t **handle)
{
	GMutex *lock = NULL;
	ms_handle_t *ms_handle = NULL;

	ms_debug("");

	if (!handle) {
		return MF_SEARCH_ERROR_INVAL_P;
	}

	ms_handle = g_malloc0(sizeof(ms_handle_t));
	if (ms_handle == NULL) {
		ms_error("Fail to allocate memory for handle ");
		*handle = NULL;
		return MF_SEARCH_ERROR_ALLOC;
	}

	ms_handle->state = MF_SEARCH_STATE_INIT;
	ms_handle->is_stop = FALSE;

	lock = g_mutex_new();
	if (!lock) {
		ms_error("Fail to create cmd_lock");
		g_free(ms_handle);
		return MF_SEARCH_ERROR_ALLOC;
	}
	ms_handle->cmd_lock = lock;

	*handle = ms_handle;

	ms_info("Success to make search handle : %p", ms_handle);
	return MF_SEARCH_ERROR_NONE;
}

int _mf_ug_search_start(ms_handle_t *handle, const char **root_path, unsigned int path_num, const char *needle, mf_search_option option, void *user_data)
{
	ms_args_t *args = NULL;
	mf_search_result_t *result = NULL;
	mf_search_option l_opt = MF_SEARCH_OPT_NONE;
	int ret = MF_SEARCH_ERROR_NONE;
	int i = 0;

	if (!handle) {
		ms_error("handle is NULL");
		return MF_SEARCH_ERROR_INVAL_P;
	}

	if (handle->state != MF_SEARCH_STATE_INIT) {
		ms_error("invaild state : %d", handle->state);
		return MF_SEARCH_ERROR_INVAL_S;
	}

	if (!root_path || !needle || path_num < 1) {
		ms_error("invaild arguments - root[%p], path_num[%d], needle[%p]", root_path, path_num, needle);
		return MF_SEARCH_ERROR_INVAL_P;
	}

	__mf_ug_search_cmd_lock(handle);

	if (handle->args) {
		__mf_ug_search_args_free(handle->args);
		handle->args = NULL;
	}
	handle->args = args = g_malloc0(sizeof(ms_args_t));
	if (!args) {
		ms_error("fail to alloc args");
		ret = MF_SEARCH_ERROR_ALLOC;
		goto FAIL_FREE_MEM;
	}

	if (option == MF_SEARCH_OPT_NONE) {
		ms_warn("option is MF_SEARCH_OPT_NONE, set all option automatically ");
		l_opt = MF_SEARCH_OPT_HIDDEN | MF_SEARCH_OPT_DIR | MF_SEARCH_OPT_FILE;
	} else {
		l_opt = option;
	}

	for (i = 0; i < path_num; i++) {
		const char *path = root_path[i];
		ms_debug("%d th root path is %s", i, path);
#ifdef CHECK_RESTRICTED_PATH
		if (!__mf_ug_search_check_licet_path(path)) {
			ms_error("%dth root path[%s] is invaild", i, path);
			ret = MF_SEARCH_ERROR_INVAL_P;
			goto FAIL_FREE_MEM;
		}
#endif /*CHECK_RESTRICTED_PATH*/
		if (g_file_test(path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)
		    && ((l_opt & MF_SEARCH_OPT_HIDDEN) || strncmp(path, ".", 1))
		    && TRUE) {
			gchar *new_path = NULL;
			gssize len = strlen(path);

			if (path[len - 1] == '/') {
				new_path = g_strndup(path, len - 1);
			} else {
				new_path = g_strndup(path, len);
			}
			args->root_path = g_list_append(args->root_path, (gpointer) new_path);
		} else {
			ms_error("Fail to test %dthe root path[%s]", i, path);
			ret = MF_SEARCH_ERROR_INVAL_P;
			goto FAIL_FREE_MEM;
		}
	}
	args->user_data = user_data;


	args->needle = g_utf8_normalize(needle, -1, NORMALIZE_OPTION);
	if (!args->needle) {
		ms_error("fail to alloc args->needle");
		goto FAIL_FREE_MEM;
	}

	args->option = l_opt;

	if (handle->result) {
		__mf_ug_search_result_free(handle->result);
		handle->result = NULL;
	}
	handle->result = result = g_malloc0(sizeof(ms_args_t));
	if (!result) {
		ms_error("fail to alloc result");
		ret = MF_SEARCH_ERROR_ALLOC;
		goto FAIL_FREE_MEM;
	}

	handle->thread_mutex = g_mutex_new();
	if (!handle->thread_mutex) {
		ms_error("fail to alloc handle->thread_mutex");
		ret = MF_SEARCH_ERROR_ALLOC;
		goto FAIL_FREE_MEM;
	}

	handle->is_stop = FALSE;
	handle->result->is_end = FALSE;

	/*create thread for find item.*/
	handle->thread_h = g_thread_create(__mf_ug_search_find_thread, handle, TRUE, NULL);
	if (!handle->thread_h) {
		ms_error("fail to create __mf_ug_search_find_thread");
		ret = MF_SEARCH_ERROR_INTERNAL;
		goto FAIL_FREE_MEM;
	}
	/*create idler for reporting find result.*/
	handle->state = MF_SEARCH_STATE_SEARCH;
	__mf_ug_search_cmd_unlock(handle);
	return MF_SEARCH_ERROR_NONE;

FAIL_FREE_MEM:
	if (args) {
		__mf_ug_search_args_free(args);
		handle->args = NULL;
	}

	if (result) {
		__mf_ug_search_result_free(result);
		handle->result = NULL;
	}

	if (handle->thread_mutex) {
		g_mutex_free(handle->thread_mutex);
		handle->thread_mutex = NULL;
	}

	if (handle->thread_h) {
		__mf_ug_search_thread_lock(handle);
		handle->is_stop = TRUE;
		__mf_ug_search_thread_unlock(handle);
		g_thread_join(handle->thread_h);
		handle->thread_h = NULL;
	}
	__mf_ug_search_cmd_unlock(handle);

	return ret;
}

int _mf_ug_search_stop(ms_handle_t *handle)
{
	ms_debug("");

	if (!handle) {
		ms_error("handle is NULL");
		return MF_SEARCH_ERROR_INVAL_P;
	}

	if (handle->state != MF_SEARCH_STATE_SEARCH) {
		ms_error("invaild state : %d", handle->state);
		return MF_SEARCH_ERROR_INVAL_S;
	}

	__mf_ug_search_cmd_lock(handle);

	__mf_ug_search_thread_lock(handle);
	handle->is_stop = TRUE;
	__mf_ug_search_thread_unlock(handle);

	pthread_mutex_lock(&gLockSearchMsg);
	if (flagSearchMsg == 0) {
		flagSearchMsg = 1;
		pthread_cond_signal(&gCondSearchMsg);
	}
	pthread_mutex_unlock(&gLockSearchMsg);

	if (handle->thread_h) {
		g_thread_join(handle->thread_h);
		handle->thread_h = NULL;
	}

	if (handle->thread_mutex) {
		g_mutex_free(handle->thread_mutex);
		handle->thread_mutex = NULL;
	}

	if (handle->args) {
		__mf_ug_search_args_free(handle->args);
		handle->args = NULL;
	}
	if (handle->result) {
		__mf_ug_search_result_free(handle->result);
		handle->result = NULL;
	}

	handle->state = MF_SEARCH_STATE_INIT;
	handle->is_stop = FALSE;

	__mf_ug_search_cmd_unlock(handle);

	return MF_SEARCH_ERROR_NONE;
}

void _mf_ug_search_finalize(ms_handle_t **handle)
{
	ms_handle_t *ms_handle = *handle;

	ms_debug("");

	if (!ms_handle) {
		ms_warn("invaild handle");
		return;
	}

	if (ms_handle->state == MF_SEARCH_STATE_SEARCH) {
		mf_ug_search_stop(ms_handle);
	}
/*      __mf_ug_search_cmd_lock(ms_handle); */
/*      __mf_ug_search_cmd_unlock(ms_handle); */

	if (ms_handle->cmd_lock) {
		g_mutex_free(ms_handle->cmd_lock);
		ms_handle->cmd_lock = NULL;
	}
	g_free(ms_handle);
	*handle = NULL;

	return;
}

/*+++++++++++++++++++++++++ UTIL APIs ++++++++++++++++++++++++++++++ */
static void __mf_ug_search_tx_wait()
{
	pthread_mutex_lock(&gLockSearchMsg);
	while (flagSearchMsg == 0) {
		pthread_cond_wait(&gCondSearchMsg, &gLockSearchMsg);
	}
	flagSearchMsg = 0;
	pthread_mutex_unlock(&gLockSearchMsg);
}

static void __mf_ug_search_result_publish_msg(mf_search_pipe_msg_type type, void *result, void *user_data)
{
	ugData *ugd = (ugData *)user_data;
	/*generate message block*/
	mf_search_pipe_msg msg;
	memset(&msg, 0, sizeof(mf_search_pipe_msg));

	msg.mf_sp_msg_type = type;
	if (msg.mf_sp_msg_type == MF_SEARCH_PIPE_MSG_RESULT_REPORT) {
		msg.report_result = g_strdup((gchar *) result);
		msg.current_path = NULL;
	} else if (msg.mf_sp_msg_type == MF_SEARCH_PIPE_MSG_ROOT_CHANGE) {
		msg.report_result = NULL;
		msg.current_path = g_strdup((gchar *) result);
		ms_debug("current path is %s", msg.current_path);
	} else if (msg.mf_sp_msg_type == MF_SEARCH_PIPE_MSG_FINISHED) {
		msg.report_result = result;
		msg.current_path = NULL;
	} else {
		msg.report_result = NULL;
		msg.current_path = NULL;
	}

	/*write message to pipe*/
	ecore_pipe_write(ugd->ug_UiGadget.ug_pSyncPipe, &msg, sizeof(msg));
}

gchar *_mf_ug_search_result_dir_get(mf_search_result_t * result)
{
	gchar *name = NULL;
	if (result) {
		GList *list = NULL;
		list = result->dir_list;
		if (list && list->data) {
			gchar *item = (gchar *) list->data;
			result->dir_list = g_list_remove(list, item);
			name = item;
		}
	}
	return name;
}

gchar *_mf_ug_search_result_file_get(mf_search_result_t * result)
{
	gchar *name = NULL;
	if (result) {
		GList *list = NULL;
		list = result->file_list;
		if (list && list->data) {
			gchar *item = (gchar *) list->data;
			result->file_list = g_list_remove(list, item);
			name = item;
		}
	}
	return name;
}

gboolean _mf_ug_search_result_is_end(mf_search_result_t *result)
{
	gboolean end = FALSE;
	if (result) {
		end = result->is_end;
	}
	return end;
}

guint _mf_ug_search_result_total_count_get(mf_search_result_t *result)
{
	guint count = 0;
	if (result) {
		count = result->total_count;
	}
	return count;
}

gchar *_mf_ug_search_result_current_dir_get(mf_search_result_t * result)
{
	gchar *c_dir = NULL;
	if (result) {
		if (result->current_dir) {
			c_dir = result->current_dir;
			result->current_dir = NULL;
		}
	}
	return c_dir;
}