summaryrefslogtreecommitdiff
path: root/common/media-server-inotify.c
blob: 5a000392930cf0342c449bc710755bbc9fec3adb (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
/*
 *  Media Server
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
 *
 * Licensed 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.
 *
 */

/**
 * This file defines api utilities of contents manager engines.
 *
 * @file		media-server-inotify.c
 * @author	Yong Yeon Kim(yy9875.kim@samsung.com)
 * @version	1.0
 * @brief
 */
#include "media-server-utils.h"
#include "media-server-db-svc.h"
#include "media-server-inotify-internal.h"
#include "media-server-inotify.h"

extern int inoti_fd;
ms_dir_data *first_inoti_node;
ms_ignore_file_info *latest_ignore_file;

int _ms_inoti_directory_scan_and_register_file(void *handle, char *dir_path)
{
	MS_DBG_START();
	struct dirent ent;
	struct dirent *res = NULL;
	DIR *dp = NULL;
	char path[MS_FILE_PATH_LEN_MAX] = { 0 };
	int err;

	if (dir_path == NULL)
		return MS_ERR_INVALID_DIR_PATH;

	dp = opendir(dir_path);
	if (dp == NULL) {
		MS_DBG("Fail to open dir");
		return MS_ERR_DIR_OPEN_FAIL;
	}

	ms_inoti_add_watch(dir_path);

	while (!readdir_r(dp, &ent, &res)) {
		if (res == NULL)
			break;

		if (ent.d_name[0] == '.')
			continue;

		err = ms_strappend(path, sizeof(path), "%s/%s", dir_path, ent.d_name);
		if (err != MS_ERR_NONE) {
			MS_DBG("ms_strappend error : %d", err);
			continue;
		}

		/*in case of directory */
		if (ent.d_type == DT_DIR) {
			_ms_inoti_directory_scan_and_register_file(handle, path);
		} else {

			err = ms_register_file(handle, path, NULL);
			if (err != MS_ERR_NONE) {
				MS_DBG("ms_register_file error : %d", err);
				continue;
			}
		}
	}

	closedir(dp);

	MS_DBG_END();

	return 0;
}

int _ms_inoti_scan_renamed_folder(void *handle, char *org_path, char *chg_path)
{
	if (org_path == NULL || chg_path == NULL) {
		MS_DBG("Parameter is wrong");
		return MS_ERR_ARG_INVALID;
	}

	MS_DBG_START();

	int err = -1;
	struct dirent ent;
	struct dirent *res = NULL;

	DIR *dp = NULL;
	char path_from[MS_FILE_PATH_LEN_MAX] = { 0 };
	char path_to[MS_FILE_PATH_LEN_MAX] = { 0 };
	ms_store_type_t src_storage = 0;
	ms_store_type_t des_storage = 0;

	dp = opendir(chg_path);
	if (dp == NULL) {
		MS_DBG("Fail to open dir");
		return MS_ERR_DIR_OPEN_FAIL;
	} else {
		MS_DBG("Modify added watch");
		ms_inoti_modify_watch(org_path, chg_path);
	}

	while (!readdir_r(dp, &ent, &res)) {
		if (res == NULL)
			break;

		if (ent.d_name[0] == '.')
			continue;

		err = ms_strappend(path_from, sizeof(path_from), "%s/%s", org_path, ent.d_name);
		if (err != MS_ERR_NONE) {
			MS_DBG("ms_strappend error : %d", err);
			continue;
		}

		err =  ms_strappend(path_to, sizeof(path_to), "%s/%s", chg_path, ent.d_name);
		if (err != MS_ERR_NONE) {
			MS_DBG("ms_strappend error : %d", err);
			continue;
		}

		/*in case of directory */
		if (ent.d_type == DT_DIR) {
			_ms_inoti_scan_renamed_folder(handle, path_from, path_to);
		}

		/*in case of file */
		if (ent.d_type == DT_REG) {
			src_storage = ms_get_store_type_by_full(path_from);
			des_storage = ms_get_store_type_by_full(path_to);

			if ((src_storage != MS_ERR_INVALID_FILE_PATH)
			    && (des_storage != MS_ERR_INVALID_FILE_PATH))
				ms_move_item(handle, src_storage, des_storage, path_from, path_to);
			else {
				MS_DBG("ms_get_store_type_by_full error");
			}
		}
	}

	closedir(dp);

	MS_DBG_END();
	return 0;
}

int ms_inoti_add_ignore_file(const char *path)
{
	ms_ignore_file_info *new_node;

	new_node = malloc(sizeof(ms_ignore_file_info));
	new_node->path = strdup(path);

	/*first created file */
	if (latest_ignore_file == NULL) {
		latest_ignore_file = malloc(sizeof(ms_ignore_file_info));
		new_node->previous = NULL;
	} else {
		latest_ignore_file->next = new_node;
		new_node->previous = latest_ignore_file;
	}
	new_node->next = NULL;

	latest_ignore_file = new_node;

	return MS_ERR_NONE;
}

int ms_inoti_delete_ignore_file(ms_ignore_file_info * delete_node)
{
	MS_DBG("");
	if (delete_node->previous != NULL)
		delete_node->previous->next = delete_node->next;
	if (delete_node->next != NULL)
		delete_node->next->previous = delete_node->previous;

	if (delete_node == latest_ignore_file) {
		latest_ignore_file = delete_node->previous;
	}

	free(delete_node->path);
	free(delete_node);

	MS_DBG("");

	return MS_ERR_NONE;
}

ms_ignore_file_info *ms_inoti_find_ignore_file(const char *path)
{
	ms_ignore_file_info *node = NULL;
	
	node = latest_ignore_file;
	while (node != NULL) {
		if (strcmp(node->path, path) == 0) {
			return node;
		}

		node = node->previous;
	}

	return NULL;
}

void ms_inoti_delete_mmc_ignore_file(void)
{
	MS_DBG_START();
	ms_ignore_file_info *prv_node = NULL;
	ms_ignore_file_info *cur_node = NULL;
	ms_ignore_file_info *del_node = NULL;

	if (latest_ignore_file != NULL) {
		cur_node = latest_ignore_file;
		while (cur_node != NULL) {
			if (strstr(cur_node->path, MS_MMC_ROOT_PATH) != NULL) {
				if (prv_node != NULL) {
					prv_node->previous = cur_node->previous;
				}

				if (cur_node == latest_ignore_file)
					latest_ignore_file = latest_ignore_file->previous;

				del_node = cur_node;
			} else {
				prv_node = cur_node;
			}

			cur_node = cur_node->previous;

			if (del_node != NULL) {
				free(del_node->path);
				free(del_node);
				del_node = NULL;
			}
		}
	}

	/*active flush */
	malloc_trim(0);

	MS_DBG_END();
}

int ms_inoti_init(void)
{
	inoti_fd = inotify_init();
	if (inoti_fd < 0) {
		perror("inotify_init");
		MS_DBG("inotify_init failed");
		return inoti_fd;
	}

	return MS_ERR_NONE;
}

void ms_inoti_add_watch(char *path)
{
	MS_DBG("");
	ms_dir_data *current_dir = NULL;
	ms_dir_data *prv_node = NULL;
	ms_dir_data *last_node = NULL;

	/*find same folder */
	if (first_inoti_node != NULL) {
		MS_DBG("find same folder");
		last_node = first_inoti_node;
		while (last_node != NULL) {
			if (strcmp(path, last_node->name) == 0) {
				MS_DBG("watch is already added: %s", path);
				return;
			}
			prv_node = last_node;
			last_node = last_node->next;
		}
	}

	MS_DBG("start add watch");

	/*there is no same path. */
	current_dir = malloc(sizeof(ms_dir_data));
	current_dir->wd = inotify_add_watch(inoti_fd, path,
			      IN_CLOSE_WRITE | IN_CREATE | IN_DELETE |
			      IN_MOVED_FROM | IN_MOVED_TO);

	if (current_dir->wd > 0) {
		MS_DBG("wd : %d", current_dir->wd);

		current_dir->name = strdup(path);
		current_dir->next = NULL;

		if (first_inoti_node == NULL) {
			first_inoti_node = current_dir;
		} else {
			/*if next node of current node is NULL, it is the lastest node. */
			MS_DBG("last_node : %s", prv_node->name);
			prv_node->next = current_dir;
		}

		MS_DBG("add watch : %s", path);
	} else {
		MS_DBG("wd : %d", current_dir->wd);
		free(current_dir);
	}
}

int ms_inoti_add_watch_with_node(ms_dir_scan_info * const node)
{
	char full_path[MS_FILE_PATH_LEN_MAX] = { 0 };
	ms_dir_data *current_dir = NULL;
	ms_dir_data *prv_node = NULL;
	ms_dir_data *last_node = NULL;

	ms_get_full_path_from_node(node, full_path);

	/*find same folder */
	if (first_inoti_node != NULL) {
		last_node = first_inoti_node;
		while (last_node != NULL) {
			if (strcmp(full_path, last_node->name) == 0) {
				MS_DBG("watch is already added: %s", full_path);
				return MS_ERR_NONE;
			}
			prv_node = last_node;
			last_node = last_node->next;
		}
	}

	/*there is no same path. */
	current_dir = malloc(sizeof(ms_dir_data));
	current_dir->wd = inotify_add_watch(inoti_fd, full_path,
			      IN_CLOSE_WRITE | IN_CREATE | IN_DELETE |
			      IN_MOVED_FROM | IN_MOVED_TO);
	if( current_dir->wd > 0) {
		MS_DBG("wd : %d", current_dir->wd);
		current_dir->name = strdup(full_path);
		current_dir->next = NULL;
		current_dir->db_updated = false;

		if (first_inoti_node == NULL) {
			first_inoti_node = current_dir;
		} else {
			/*if next node of current node is NULL, it is the lastest node. */
			MS_DBG("last_node : %s", prv_node->name);
			prv_node->next = current_dir;
		}
		MS_DBG("add watch : %s", full_path);
	} else {
		MS_DBG("wd : %d", current_dir->wd);
		free(current_dir);
	}

	return MS_ERR_NONE;
}

void ms_inoti_remove_watch_recursive(char *path)
{
	MS_DBG_START();
	ms_dir_data *prv_node = NULL;
	ms_dir_data *cur_node = NULL;
	ms_dir_data *del_node = NULL;

	if (first_inoti_node != NULL) {
		cur_node = first_inoti_node;
		while (cur_node != NULL) {
			if (strstr(cur_node->name, path) != NULL) {
				if (prv_node != NULL) {
					prv_node->next =
					    cur_node->next;
				}

				if (cur_node == first_inoti_node)
					first_inoti_node =
					    first_inoti_node->next;

				del_node = cur_node;
			} else {
				prv_node = cur_node;
			}

			cur_node = cur_node->next;

			if (del_node != NULL) {
				free(del_node->name);
				free(del_node);
				del_node = NULL;
			}
		}
	}

	/*active flush */
	 malloc_trim(0);

	MS_DBG_END();
}

void ms_inoti_remove_watch(char *path)
{
	ms_dir_data *del_node = NULL;
	ms_dir_data *prv_node = NULL;

	if (strcmp(first_inoti_node->name, path) == 0) {
		del_node = first_inoti_node;
		first_inoti_node = first_inoti_node->next;
	} else {
		/*find same folder */
		if (first_inoti_node != NULL) {
			del_node = first_inoti_node;
			while (del_node != NULL) {
				MS_DBG("current node %s", del_node->name);
				if (strcmp(path, del_node->name) == 0) {
					MS_DBG("find delete node: %s", del_node->name);
					if (prv_node != NULL) {
						MS_DBG("previous_node : %s", prv_node->name);
						prv_node->next = del_node->next;
					}
					/*free deleted node */
					free(del_node->name);
					free(del_node);
					break;
				}
				prv_node = del_node;
				del_node = del_node->next;
			}
		}
	}

	/*active flush */
	malloc_trim(0);
}

void ms_inoti_modify_watch(char *path_from, char *path_to)
{
	bool find = false;
	ms_dir_data *mod_node;

	if (strcmp(first_inoti_node->name, path_from) == 0) {
		mod_node = first_inoti_node;
	} else {
		/*find same folder */
		if (first_inoti_node != NULL) {
			mod_node = first_inoti_node;
			while (mod_node != NULL) {
				/*find previous directory*/
				if (strcmp(path_from, mod_node->name) == 0) { 
					MS_DBG("find change node: %s", mod_node->name);
					MS_DBG("new name : %s", path_to);
					/*change path of directory*/
					/*free previous name of node */
					free(mod_node->name);
					mod_node->name = NULL;

					/*add new name */
					mod_node->name = strdup(path_to);

					/*active flush */
					malloc_trim(0);

					find = true;
					break;
				}
				mod_node = mod_node->next;
			}
		}
	}

	/*this is new directory*/
	if (find == false) {
		MS_DBG("This is new directory");
		ms_inoti_add_watch(path_to);
	}
}

gboolean ms_inoti_thread(void *data)
{
	uint32_t i;
	int length;
	int err;
	int prev_mask = 0;
	int prev_wd = -1;
	bool res;
	char name[MS_FILE_NAME_LEN_MAX + 1] = { 0 };
	char prev_name[MS_FILE_NAME_LEN_MAX + 1] = { 0 };
	char buffer[INOTI_BUF_LEN] = { 0 };
	char path[MS_FILE_PATH_LEN_MAX] = { 0 };
	struct inotify_event *event;
	void *handle = NULL;

	MS_DBG("START INOTIFY");

	err = ms_connect_db(&handle);
	if (err != MS_ERR_NONE) {
		MS_DBG(" INOTIFY : sqlite3_open: ret = %d", err);
		return false;
	}

	while (1) {
		i = 0;
		length = read(inoti_fd, buffer, sizeof(buffer) - 1);

		if (length < 0 || length > sizeof(buffer)) {	/*this is error */
			MS_DBG("fail read");
			perror("read");
			continue;
		}

		while (i < length && i < INOTI_BUF_LEN) {
			/*it's possible that ums lets reset phone data... */
			event = (struct inotify_event *)&buffer[i];

			if(strcmp(event->name, "_FILEOPERATION_END") == 0) {
				/*file operation is end*/
				/* announce db is updated*/
				ms_set_db_status(MS_DB_UPDATED);
				rmdir("/opt/data/file-manager-service/_FILEOPERATION_END");
				goto NEXT_INOTI_EVENT;
			} else if (event->name[0] == '.') {
				/*event of hidden folder is ignored */
				MS_DBG("Ignore : First character of event->name includes invalid character");
				goto NEXT_INOTI_EVENT;
			} else if (event->wd < 1) {
				/*this is error */
				MS_DBG("invalid wd : %d", event->wd);
				goto NEXT_INOTI_EVENT;
			}

			/*start of one event */
			if (event->len && (event->len <= MS_FILE_NAME_LEN_MAX)) {
				/*Add for fixing prevent defect 2011-02-15 */
				err = ms_strcopy(name, sizeof(name), "%s", event->name);
				if (err != MS_ERR_NONE) {
					MS_DBG("ms_strcopy error : %d", err);
					goto NEXT_INOTI_EVENT;
				}

				/*get full path of file or directory */
				res = _ms_inoti_get_full_path(event->wd, name, path, sizeof(path));
				if (res == false) {
					MS_DBG("_ms_inoti_get_full_path error");
					goto NEXT_INOTI_EVENT;
				}

				MS_DBG("INOTIFY[%d : %s]", event->wd, name);
				if (event->mask & IN_ISDIR) {
					MS_DBG("DIRECTORY INOTIFY");
					
					if (event->mask & IN_MOVED_FROM) {
						MS_DBG("MOVED_FROM");

						prev_mask = event->mask;
						prev_wd = event->wd;

						err = ms_strcopy(prev_name, sizeof(prev_name), "%s", event->name);
						if (err != MS_ERR_NONE) {
							MS_DBG("ms_strcopy fail");
							goto NEXT_INOTI_EVENT;
						}
					} 
					else if (event->mask & IN_MOVED_TO) {
						MS_DBG("MOVED_TO");

						char full_path_from[MS_FILE_PATH_LEN_MAX] = { 0 };

						res = _ms_inoti_get_full_path(prev_wd, prev_name, full_path_from, sizeof(full_path_from));
						if (res == false) {
							MS_DBG("_ms_inoti_get_full_path error");
							goto NEXT_INOTI_EVENT;
						}
						/*enable bundle commit*/
						ms_move_start(handle);

						/*need update file information under renamed directory */
						_ms_inoti_scan_renamed_folder(handle, full_path_from, path);

						/*disable bundle commit*/
						ms_move_end(handle);

						prev_mask = prev_wd = 0;	/*reset */
					}
					else if (event->mask & IN_CREATE) {
						MS_DBG("CREATE");

						_ms_inoti_directory_scan_and_register_file(handle, path);
						prev_mask = event->mask;
					}
					else if (event->mask & IN_DELETE) {
						MS_DBG("DELETE");

						ms_inoti_remove_watch(path);
					}
				}
				else {
					MS_DBG("FILE INOTIFY");

					if (event->mask & IN_MOVED_FROM) {
						MS_DBG("MOVED_FROM");

						err = ms_delete_item(handle, path);
						if (err != MS_ERR_NONE) {
							MS_DBG("ms_media_db_delete fail error : %d", err);
						}
					}
					else if (event->mask & IN_MOVED_TO) {
						MS_DBG("MOVED_TO");

						err = ms_register_file(handle, path, NULL);
						if (err != MS_ERR_NONE) {
							MS_DBG("ms_register_file error : %d", err);
						}
					}
					else if (event->mask & IN_CREATE) {
						MS_DBG("CREATE");

						_ms_inoti_add_create_file_list(event->wd, name);
					}
					else if (event->mask & IN_DELETE) {
						MS_DBG("DELETE");
						
						err = ms_delete_item(handle, path);
						if (err != MS_ERR_NONE) {
							MS_DBG("ms_media_db_delete error : %d", err);
						}
					}
					else if (event->mask & IN_CLOSE_WRITE) {
						MS_DBG("CLOSE_WRITE");

						ms_create_file_info *node;
						node = _ms_inoti_find_create_file_list (event->wd, name);

						if (node != NULL || ((prev_mask & IN_ISDIR) & IN_CREATE)) {

							err = ms_register_file(handle, path, NULL);
							if (err != MS_ERR_NONE) {
								MS_DBG("ms_register_file error : %d", err);
							}
							if (node != NULL)
								_ms_inoti_delete_create_file_list(node);
						}
						else {
							/*in case of replace */
							MS_DBG("This case is replacement or changing meta data.");
							ms_ignore_file_info  *ignore_file;

							ignore_file = ms_inoti_find_ignore_file(path);
							if (ignore_file == NULL) {
								err = ms_delete_item(handle, path);
								if (err != MS_ERR_NONE) {
									MS_DBG("ms_media_db_delete error : %d", err);
								}
								/*update = delete + regitster */
								err = ms_register_file(handle, path, NULL);
								if (err != MS_ERR_NONE) {
									MS_DBG("ms_register_file error : %d", err);
									goto NEXT_INOTI_EVENT;
								}
							} else {
								MS_DBG(" Ignore this file");
							}
						}
						prev_mask = prev_wd = 0;	/*reset */
					}
				}
			} /*end of one event */
			else {
				MS_DBG("Event length is zero or over MS_FILE_NAME_LEN_MAX");
				if (event->mask & IN_IGNORED) {
					MS_DBG("This case is ignored");
				}
			}
 NEXT_INOTI_EVENT:	;
			i += INOTI_EVENT_SIZE + event->len;
		}
		/*Active flush */
		sqlite3_release_memory(-1);
		malloc_trim(0);
	}

	ms_inoti_remove_watch(MS_DB_UPDATE_NOTI_PATH);

	ms_inoti_remove_watch_recursive(MS_PHONE_ROOT_PATH);
	ms_inoti_remove_watch_recursive(MS_MMC_ROOT_PATH);

	close(inoti_fd);

	err = ms_disconnect_db(handle);
	if (err != MS_ERR_NONE) {
		MS_DBG("ms_media_db_close error : %d", err);
		return false;
	}
	MS_DBG("Disconnect MEDIA DB");

	return false;
}