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
|
/*
* Copyright (C) 2007 The Android Open Source Project
*
* 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.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <utime.h>
#include <errno.h>
#include "sysdeps.h"
#define TRACE_TAG TRACE_SYNC
#include "adb.h"
#include "file_sync_service.h"
static int mkdirs(char *name)
{
int ret;
char *x = name + 1;
if(name[0] != '/') return -1;
for(;;) {
x = adb_dirstart(x);
if(x == 0) return 0;
*x = 0;
ret = adb_mkdir(name, 0775);
if((ret < 0) && (errno != EEXIST)) {
D("mkdir(\"%s\") -> %s\n", name, strerror(errno));
*x = '/';
return ret;
}
*x++ = '/';
}
return 0;
}
static int do_stat(int s, const char *path)
{
syncmsg msg;
struct stat st;
msg.stat.id = ID_STAT;
if(lstat(path, &st)) {
msg.stat.mode = 0;
msg.stat.size = 0;
msg.stat.time = 0;
} else {
msg.stat.mode = htoll(st.st_mode);
msg.stat.size = htoll(st.st_size);
msg.stat.time = htoll(st.st_mtime);
}
return writex(s, &msg.stat, sizeof(msg.stat));
}
static int do_list(int s, const char *path)
{
DIR *d;
struct dirent *de;
struct stat st;
syncmsg msg;
int len;
char tmp[1024 + 256 + 1];
char *fname;
len = strlen(path);
memcpy(tmp, path, len);
tmp[len] = '/';
fname = tmp + len + 1;
msg.dent.id = ID_DENT;
d = opendir(path);
if(d == 0) goto done;
while((de = readdir(d))) {
int len = strlen(de->d_name);
/* not supposed to be possible, but
if it does happen, let's not buffer overrun */
if(len > 256) continue;
strcpy(fname, de->d_name);
if(lstat(tmp, &st) == 0) {
msg.dent.mode = htoll(st.st_mode);
msg.dent.size = htoll(st.st_size);
msg.dent.time = htoll(st.st_mtime);
msg.dent.namelen = htoll(len);
if(writex(s, &msg.dent, sizeof(msg.dent)) ||
writex(s, de->d_name, len)) {
closedir(d);
return -1;
}
}
}
closedir(d);
done:
msg.dent.id = ID_DONE;
msg.dent.mode = 0;
msg.dent.size = 0;
msg.dent.time = 0;
msg.dent.namelen = 0;
return writex(s, &msg.dent, sizeof(msg.dent));
}
static int fail_message(int s, const char *reason)
{
syncmsg msg;
int len = strlen(reason);
D("sync: failure: %s\n", reason);
msg.data.id = ID_FAIL;
msg.data.size = htoll(len);
if(writex(s, &msg.data, sizeof(msg.data)) ||
writex(s, reason, len)) {
return -1;
} else {
return 0;
}
}
static int fail_errno(int s)
{
return fail_message(s, strerror(errno));
}
static int handle_send_file(int s, char *path, mode_t mode, char *buffer)
{
syncmsg msg;
unsigned int timestamp = 0;
int fd;
fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
if(fd < 0 && errno == ENOENT) {
mkdirs(path);
fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
}
if(fd < 0 && errno == EEXIST) {
fd = adb_open_mode(path, O_WRONLY, mode);
}
if(fd < 0) {
if(fail_errno(s))
return -1;
fd = -1;
}
for(;;) {
unsigned int len;
if(readx(s, &msg.data, sizeof(msg.data)))
goto fail;
if(msg.data.id != ID_DATA) {
if(msg.data.id == ID_DONE) {
timestamp = ltohl(msg.data.size);
break;
}
fail_message(s, "invalid data message");
goto fail;
}
len = ltohl(msg.data.size);
if(len > SYNC_DATA_MAX) {
fail_message(s, "oversize data message");
goto fail;
}
if(readx(s, buffer, len))
goto fail;
if(fd < 0)
continue;
if(writex(fd, buffer, len)) {
int saved_errno = errno;
adb_close(fd);
adb_unlink(path);
fd = -1;
errno = saved_errno;
if(fail_errno(s)) return -1;
}
}
if(fd >= 0) {
struct utimbuf u;
adb_close(fd);
u.actime = timestamp;
u.modtime = timestamp;
utime(path, &u);
msg.status.id = ID_OKAY;
msg.status.msglen = 0;
if(writex(s, &msg.status, sizeof(msg.status)))
return -1;
}
return 0;
fail:
if(fd >= 0)
adb_close(fd);
adb_unlink(path);
return -1;
}
#ifdef HAVE_SYMLINKS
static int handle_send_link(int s, char *path, char *buffer)
{
syncmsg msg;
unsigned int len;
int ret;
if(readx(s, &msg.data, sizeof(msg.data)))
return -1;
if(msg.data.id != ID_DATA) {
fail_message(s, "invalid data message: expected ID_DATA");
return -1;
}
len = ltohl(msg.data.size);
if(len > SYNC_DATA_MAX) {
fail_message(s, "oversize data message");
return -1;
}
if(readx(s, buffer, len))
return -1;
ret = symlink(buffer, path);
if(ret && errno == ENOENT) {
mkdirs(path);
ret = symlink(buffer, path);
}
if(ret) {
fail_errno(s);
return -1;
}
if(readx(s, &msg.data, sizeof(msg.data)))
return -1;
if(msg.data.id == ID_DONE) {
msg.status.id = ID_OKAY;
msg.status.msglen = 0;
if(writex(s, &msg.status, sizeof(msg.status)))
return -1;
} else {
fail_message(s, "invalid data message: expected ID_DONE");
return -1;
}
return 0;
}
#endif /* HAVE_SYMLINKS */
static int do_send(int s, char *path, char *buffer)
{
char *tmp;
mode_t mode;
int is_link, ret;
tmp = strrchr(path,',');
if(tmp) {
*tmp = 0;
errno = 0;
mode = strtoul(tmp + 1, NULL, 0);
#ifndef HAVE_SYMLINKS
is_link = 0;
#else
is_link = S_ISLNK(mode);
#endif
mode &= 0777;
}
if(!tmp || errno) {
mode = 0644;
is_link = 0;
}
adb_unlink(path);
#ifdef HAVE_SYMLINKS
if(is_link)
ret = handle_send_link(s, path, buffer);
else {
#else
{
#endif
/* copy user permission bits to "group" and "other" permissions */
mode |= ((mode >> 3) & 0070);
mode |= ((mode >> 3) & 0007);
ret = handle_send_file(s, path, mode, buffer);
}
return ret;
}
static int do_recv(int s, const char *path, char *buffer)
{
syncmsg msg;
int fd, r;
fd = adb_open(path, O_RDONLY);
if(fd < 0) {
if(fail_errno(s)) return -1;
return 0;
}
msg.data.id = ID_DATA;
for(;;) {
r = adb_read(fd, buffer, SYNC_DATA_MAX);
if(r <= 0) {
if(r == 0) break;
if(errno == EINTR) continue;
r = fail_errno(s);
adb_close(fd);
return r;
}
msg.data.size = htoll(r);
if(writex(s, &msg.data, sizeof(msg.data)) ||
writex(s, buffer, r)) {
adb_close(fd);
return -1;
}
}
adb_close(fd);
msg.data.id = ID_DONE;
msg.data.size = 0;
if(writex(s, &msg.data, sizeof(msg.data))) {
return -1;
}
return 0;
}
void file_sync_service(int fd, void *cookie)
{
syncmsg msg;
char name[1025];
unsigned namelen;
char *buffer = malloc(SYNC_DATA_MAX);
if(buffer == 0) goto fail;
for(;;) {
D("sync: waiting for command\n");
if(readx(fd, &msg.req, sizeof(msg.req))) {
fail_message(fd, "command read failure");
break;
}
namelen = ltohl(msg.req.namelen);
if(namelen > 1024) {
fail_message(fd, "invalid namelen");
break;
}
if(readx(fd, name, namelen)) {
fail_message(fd, "filename read failure");
break;
}
name[namelen] = 0;
msg.req.namelen = 0;
D("sync: '%s' '%s'\n", (char*) &msg.req, name);
switch(msg.req.id) {
case ID_STAT:
if(do_stat(fd, name)) goto fail;
break;
case ID_LIST:
if(do_list(fd, name)) goto fail;
break;
case ID_SEND:
if(do_send(fd, name, buffer)) goto fail;
break;
case ID_RECV:
if(do_recv(fd, name, buffer)) goto fail;
break;
case ID_QUIT:
goto fail;
default:
fail_message(fd, "unknown command");
goto fail;
}
}
fail:
if(buffer != 0) free(buffer);
D("sync: done\n");
adb_close(fd);
}
|