summaryrefslogtreecommitdiff
path: root/src/pkg_event.c
blob: bae1df08ec6fddd712db7a3ded75a217c6d31e60 (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
 /*
  * 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://www.tizenopensource.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 <ail.h>
#include <errno.h>
#include <Elementary.h>
#include <Ecore.h>
#include <Ecore_File.h>
#include <stdio.h>
#include <sys/inotify.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <stdbool.h>
#include <unistd.h>
#include <vconf.h>

#include "desktop_to_db.h"
#include "parser.h"
#include "pkg_event.h"
#include "util.h"


#define BUFSZE 1024


extern int errno;

struct desktop_notifier s_desktop_notifier = {
	.ad = NULL,
	.handler = NULL,
	.this_module = NULL,
};


struct cb_item {
	void *data;
	int (*event_cb)(void *ai_name, void *data, int visible);
};



struct inotify_path paths[CONF_PATH_NUMBER];


static Eina_Bool
directory_notify(void* data, Ecore_Fd_Handler* fd_handler)
{
	char *buf;
	ssize_t read_size, len, i = 0;
	int fd;

	fd = ecore_main_fd_handler_fd_get(fd_handler);
	_D("There are some modification, ifd [%d]", fd);

	if (ioctl(fd, FIONREAD, &read_size) < 0) {
		_E("Failed to get q size");
		return ECORE_CALLBACK_CANCEL;
	}

	if (read_size <= 0) {
		_E("Buffer is not ready!!!");
		return ECORE_CALLBACK_RENEW;
	}

	buf = malloc(read_size);
	if (!buf) {
		_E("Failed to allocate heap for event handling");
		return ECORE_CALLBACK_RENEW;
	}

	len = read(fd, buf, read_size);
	if (len < 0) {
		free(buf);
		// Stop monitoring about this invalid file descriptor
		return ECORE_CALLBACK_CANCEL;
	}

	while (i < len) {
		struct inotify_event* event = (struct inotify_event*) &buf[i];
		char *str_potksed = "potksed.";
		char *cut;
		char *package = NULL;
		ssize_t idx;
		int nev_name;

		// 1. check the extension of a file
		nev_name = strlen(event->name) - 1;
		for (idx = 0; nev_name >= 0 && str_potksed[idx]; idx++) {
			if (event->name[nev_name] != str_potksed[idx]) {
				break;
			}
			nev_name --;
		}

		if (str_potksed[idx] != '\0') {
			_D("This is not a desktop file : %s", event->name);
			i += sizeof(struct inotify_event) + event->len;
			continue;
		}

		package = strdup(event->name);
		cut = strstr(package, ".desktop");
		*cut = '\0';
		_D("Package : %s", package);

		// add & update
		if (event->mask & IN_CREATE || event->mask & IN_CLOSE_WRITE || event->mask & IN_MOVED_TO) { // for moving
			ail_appinfo_h ai = NULL;
			ail_error_e ret;

			ret = ail_package_get_appinfo(package, &ai);
			if (ai) ail_package_destroy_appinfo(ai);


			if (AIL_ERROR_NO_DATA == ret) {
				if (ail_desktop_add(package) < 0) {
					_D("Failed to add a new package (%s)", event->name);
				}
			} else if (AIL_ERROR_OK == ret) {
				if (ail_desktop_update(package) < 0) {
					_D("Failed to add a new package (%s)", event->name);
				}
			} else
				;
		// delete
		} else if (event->mask & IN_DELETE) { // for deleting
			if (ail_desktop_remove(package) < 0) 
				_D("Failed to remove a package (%s)", event->name);
		} else {
			_D("this event is not dealt with inotify");
		}

		free(package);

		i += sizeof(struct inotify_event) + event->len;
	}

	free(buf);
	return ECORE_CALLBACK_RENEW;
}



void pkg_event_init()
{
	int wd = 0;
	int i;

	s_desktop_notifier.ifd = inotify_init();
	if (s_desktop_notifier.ifd == -1) {
		_E("inotify_init error: %s", strerror(errno));
		return;
	}

	conf_search(paths, CONF_PATH_NUMBER);

	for (i = 0; i < CONF_PATH_NUMBER && paths[i].path; i++)
	{
		_D("Configuration file for desktop file monitoring [%s] is added", paths[i].path);
		if (access(paths[i].path, R_OK) != 0)
		{
			ecore_file_mkpath(paths[i].path);
			if (chmod(paths[i].path, 0777) == -1) {
				_E("cannot chmod %s", paths[i].path);
			}
		}

		wd = inotify_add_watch(s_desktop_notifier.ifd, paths[i].path, IN_CREATE | IN_CLOSE_WRITE | IN_MOVED_TO | IN_DELETE);
		if (wd == -1) {
			_E("inotify_add_watch error: %s", strerror(errno));
			close(s_desktop_notifier.ifd);
			return;
		}

		paths[i].wd = wd;
	}

	s_desktop_notifier.handler = ecore_main_fd_handler_add(s_desktop_notifier.ifd, ECORE_FD_READ, directory_notify, NULL, NULL, NULL);
	if (!s_desktop_notifier.handler) {
		// TODO: Handles me.. EXCEPTION!!
		_E("cannot add handler for inotify");
	}
}



void pkg_event_fini(void)
{
	register int i;

	if (s_desktop_notifier.handler) {
		ecore_main_fd_handler_del(s_desktop_notifier.handler);
	}

	for (i = 0; i < CONF_PATH_NUMBER; i ++) {
		if (paths[i].wd) {
			if (inotify_rm_watch(s_desktop_notifier.ifd, paths[i].wd) < 0) {
				char log[BUFSZE] = {0,};
				int ret;

				ret = strerror_r(errno, log, sizeof(log));
				_E("Error: %s", ret == 0? log : "unknown error");
			}
			paths[i].wd = 0;
		}
	}

	if (s_desktop_notifier.ifd) {
		close(s_desktop_notifier.ifd);
		s_desktop_notifier.ifd = 0;
	}
}


// End of a file