forked from Xyaneon/grive-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrive-daemon.cpp
232 lines (206 loc) · 7.37 KB
/
grive-daemon.cpp
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
/*
grive-daemon syncs your ~/Google Drive folder by calling grive.
Copyright (C) 2014 Christopher Kyle Horton
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* Programmed with help from this StackExchange answer:
http://stackoverflow.com/a/17955149
Also, from this blog post:
http://darkeside.blogspot.com/2007/12/linux-inotify-example.html
And this:
http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/index.html
And this (for recursive directory watching):
https://gist.github.com/pkrnjevic/6016356
And this too, for more recursive directory watching:
http://stackoverflow.com/a/11097815
*/
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#include <pwd.h>
#include <string.h>
#include <errno.h>
#include <sys/inotify.h>
#include "daemon.h"
#include "watch.h"
#include "recursive_watch.h"
/* Allow for 1024 simultaneous events */
#define BUFF_SIZE ((sizeof(struct inotify_event)+FILENAME_MAX)*1024)
static bool get_event (int fd, const char * target, Watch& watch)
{
ssize_t len, i = 0;
char action[81+FILENAME_MAX] = {0};
char buff[BUFF_SIZE] = {0};
string current_dir, new_dir;
bool sync_required = false;
int wd;
len = read (fd, buff, BUFF_SIZE);
while (i < len) {
struct inotify_event *pevent = (struct inotify_event *)&buff[i];
char action[81+FILENAME_MAX] = {0};
char system_message[200+FILENAME_MAX] = {0};
bool relevant_change = false;
if (pevent->len)
strcpy (action, pevent->name);
else
strcpy (action, target);
if (pevent->mask & IN_ATTRIB)
strcat(action, "'s metadata changed but this is ignored");
if (pevent->mask & IN_CREATE)
{
current_dir = watch.get(pevent->wd);
if (pevent->mask & IN_ISDIR)
{
new_dir = current_dir + "/" + pevent->name;
wd = inotify_add_watch(fd, new_dir.c_str(), WATCH_FLAGS);
watch.insert(pevent->wd, pevent->name, wd);
//printf("New directory %s created.\n", new_dir.c_str());
} else {
//printf("New file %s/%s created.\n", current_dir.c_str(), pevent->name);
}
strcat(action, " was created in a watched directory");
relevant_change = true;
}
if (pevent->mask & IN_DELETE)
{
if (pevent->mask & IN_ISDIR) {
new_dir = watch.erase(pevent->wd, pevent->name, &wd);
inotify_rm_watch(fd, wd);
//printf( "Directory %s deleted.\n", new_dir.c_str() );
} else {
current_dir = watch.get(pevent->wd);
//printf( "File %s/%s deleted.\n", current_dir.c_str(), pevent->name );
}
strcat(action, " was deleted in a watched directory");
relevant_change = true;
}
if (pevent->mask & IN_DELETE_SELF) {
strcat(action, ", the watched file/directory, was itself deleted");
relevant_change = true;
}
if (pevent->mask & IN_MODIFY) {
strcat(action, " was modified");
relevant_change = true;
}
if (pevent->mask & IN_MOVE_SELF) {
strcat(action, ", the watched file/directory, was itself moved");
relevant_change = true;
}
if (pevent->mask & IN_MOVED_FROM) {
strcat(action, " was moved out of a watched directory");
relevant_change = true;
}
if (pevent->mask & IN_MOVED_TO) {
strcat(action, " was moved into a watched directory");
relevant_change = true;
}
// Ignore hidden grive files.
if (pevent->len) {
if ((strcmp (pevent->name, ".grive") != 0) && (strcmp (pevent->name, ".grive_state") != 0)) {
syslog (LOG_INFO, "Noticed %s.", action);
while (true) {
// Replace dangerous char
char *char_ptr = strchr(action, '"');
if (!char_ptr) {
break;
}
*char_ptr = '_';
}
while (true) {
// Replace dangerous char
char *char_ptr = strchr(action, '$');
if (!char_ptr) {
break;
}
*char_ptr = '_';
}
while (true) {
// Replace dangerous char
char *char_ptr = strchr(action, '\'');
if (!char_ptr) {
break;
}
*char_ptr = '_';
}
strcat(system_message, "notify-send \"Grive Daemon: \" \"");
strcat(system_message, action);
strcat(system_message, "\"");
system (system_message);
if (relevant_change) {
sync_required = true;
}
}
}
i += sizeof(struct inotify_event) + pevent->len;
}
return sync_required;
}
void handle_error (int error)
{
syslog (LOG_WARNING, "Needed to handle error %s.", strerror(error));
fprintf (stderr, "Error: %s\n", strerror(error));
}
int main()
{
Watch watch;
char target[FILENAME_MAX];
int result, fd, wd;
struct passwd *pw = getpwuid(getuid());
char *gd_dir = strcat(pw->pw_dir, "/Google Drive");
daemonize();
syslog (LOG_NOTICE, "Started.");
if (chdir(gd_dir) < 0)
{
syslog (LOG_WARNING, "Could not switch to ~/Google Drive.");
system ("notify-send \"Google Drive sync failed\" \"grive-daemon could not switch to ~/Google Drive.\"");
exit(EXIT_FAILURE);
}
// Initial syncronization in case stuff changed before starting.
system ("notify-send \"Google Drive sync started\" \"Ensuring both local and remote are up-to-date...\"");
system ("grive");
syslog (LOG_INFO, "Performed an initial syncronization.");
system ("notify-send \"Google Drive sync complete\" \"Local and remote are now both synced.\"");
fd = inotify_init();
if (fd < 0) {
handle_error (errno);
return EXIT_FAILURE;
}
wd = inotify_add_watch (fd, gd_dir, WATCH_FLAGS);
if (wd < 0) {
syslog (LOG_WARNING, "gd_dir = %s.", gd_dir);
handle_error (errno);
system ("notify-send \"Google Drive sync failed\" \"grive-daemon encountered an error and exited.\"");
return EXIT_FAILURE;
}
watch.insert(-1, gd_dir, wd);
DirectoryReader::parseDirectory(string(gd_dir), fd, watch);
while (1)
{
if (get_event(fd, target, watch))
{
system ("notify-send \"Google Drive sync started\" \"Syncing recent local changes with remote...\"");
system ("grive");
syslog (LOG_INFO, "Performed a syncronization.");
system ("notify-send \"Google Drive sync complete\" \"Remote is now up-to-date with local changes.\"");
}
sleep (2);
}
inotify_rm_watch(fd, wd);
watch.cleanup(fd);
close(fd);
system ("notify-send \"Google Drive sync stopped\" \"grive-daemon was terminated. Your changes will not sync until it is restarted.\"");
syslog (LOG_NOTICE, "Terminated.");
closelog();
return EXIT_SUCCESS;
}