* initial contribution for 2.0 beta (0.0.2)
[framework/system/sdbd.git] / src / file_sync_service.c
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <dirent.h>
24 #include <utime.h>
25
26 #include <errno.h>
27
28 #include "sysdeps.h"
29
30 #define TRACE_TAG  TRACE_SYNC
31 #include "sdb.h"
32 #include "file_sync_service.h"
33
34 /* The typical default value for the umask is S_IWGRP | S_IWOTH (octal 022).
35  * Before use the DIR_PERMISSION, the process umask value should be set 0 using umask().
36  */
37 #define DIR_PERMISSION 0777
38
39 static int mkdirs(char *name)
40 {
41     int ret;
42     char *x = name + 1;
43
44     if(name[0] != '/') return -1;
45
46     for(;;) {
47         x = sdb_dirstart(x);
48         if(x == 0) return 0;
49         *x = 0;
50         ret = sdb_mkdir(name, DIR_PERMISSION);
51         if((ret < 0) && (errno != EEXIST)) {
52             D("mkdir(\"%s\") -> %s\n", name, strerror(errno));
53             *x = '/';
54             return ret;
55         }
56         *x++ = '/';
57     }
58     return 0;
59 }
60
61 static int do_stat(int s, const char *path)
62 {
63     syncmsg msg;
64     struct stat st;
65
66     msg.stat.id = ID_STAT;
67
68     if(lstat(path, &st)) {
69         msg.stat.mode = 0;
70         msg.stat.size = 0;
71         msg.stat.time = 0;
72     } else {
73         msg.stat.mode = htoll(st.st_mode);
74         msg.stat.size = htoll(st.st_size);
75         msg.stat.time = htoll(st.st_mtime);
76     }
77
78     return writex(s, &msg.stat, sizeof(msg.stat));
79 }
80
81 static int do_list(int s, const char *path)
82 {
83     DIR *d;
84     struct dirent *de;
85     struct stat st;
86     syncmsg msg;
87     int len;
88
89     char tmp[1024 + 256 + 1];
90     char *fname;
91
92     len = strlen(path);
93     memcpy(tmp, path, len);
94     tmp[len] = '/';
95     fname = tmp + len + 1;
96
97     msg.dent.id = ID_DENT;
98
99     d = opendir(path);
100     if(d == 0) goto done;
101
102     while((de = readdir(d))) {
103         int len = strlen(de->d_name);
104
105             /* not supposed to be possible, but
106                if it does happen, let's not buffer overrun */
107         if(len > 256) continue;
108
109         strcpy(fname, de->d_name);
110         if(lstat(tmp, &st) == 0) {
111             msg.dent.mode = htoll(st.st_mode);
112             msg.dent.size = htoll(st.st_size);
113             msg.dent.time = htoll(st.st_mtime);
114             msg.dent.namelen = htoll(len);
115
116             if(writex(s, &msg.dent, sizeof(msg.dent)) ||
117                writex(s, de->d_name, len)) {
118                 return -1;
119             }
120         }
121     }
122
123     closedir(d);
124
125 done:
126     msg.dent.id = ID_DONE;
127     msg.dent.mode = 0;
128     msg.dent.size = 0;
129     msg.dent.time = 0;
130     msg.dent.namelen = 0;
131     return writex(s, &msg.dent, sizeof(msg.dent));
132 }
133
134 static int fail_message(int s, const char *reason)
135 {
136     syncmsg msg;
137     int len = strlen(reason);
138
139     D("sync: failure: %s\n", reason);
140
141     msg.data.id = ID_FAIL;
142     msg.data.size = htoll(len);
143     if(writex(s, &msg.data, sizeof(msg.data)) ||
144        writex(s, reason, len)) {
145         return -1;
146     } else {
147         return 0;
148     }
149 }
150
151 static int fail_errno(int s)
152 {
153     return fail_message(s, strerror(errno));
154 }
155
156 static int handle_send_file(int s, char *path, mode_t mode, char *buffer)
157 {
158     syncmsg msg;
159     unsigned int timestamp = 0;
160     int fd;
161
162     fd = sdb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
163     if(fd < 0 && errno == ENOENT) {
164         mkdirs(path);
165         fd = sdb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
166     }
167     if(fd < 0 && errno == EEXIST) {
168         fd = sdb_open_mode(path, O_WRONLY, mode);
169     }
170     if(fd < 0) {
171         if(fail_errno(s))
172             return -1;
173         fd = -1;
174     }
175
176     for(;;) {
177         unsigned int len;
178
179         if(readx(s, &msg.data, sizeof(msg.data)))
180             goto fail;
181
182         if(msg.data.id != ID_DATA) {
183             if(msg.data.id == ID_DONE) {
184                 timestamp = ltohl(msg.data.size);
185                 break;
186             }
187             fail_message(s, "invalid data message");
188             goto fail;
189         }
190         len = ltohl(msg.data.size);
191         if(len > SYNC_DATA_MAX) {
192             fail_message(s, "oversize data message");
193             goto fail;
194         }
195         if(readx(s, buffer, len))
196             goto fail;
197
198         if(fd < 0)
199             continue;
200         if(writex(fd, buffer, len)) {
201             sdb_close(fd);
202             sdb_unlink(path);
203             fd = -1;
204             if(fail_errno(s)) return -1;
205         }
206     }
207
208     if(fd >= 0) {
209         struct utimbuf u;
210         sdb_close(fd);
211         u.actime = timestamp;
212         u.modtime = timestamp;
213         utime(path, &u);
214
215         msg.status.id = ID_OKAY;
216         msg.status.msglen = 0;
217         if(writex(s, &msg.status, sizeof(msg.status)))
218             return -1;
219     }
220     return 0;
221
222 fail:
223     if(fd >= 0)
224         sdb_close(fd);
225     sdb_unlink(path);
226     return -1;
227 }
228
229 #ifdef HAVE_SYMLINKS
230 static int handle_send_link(int s, char *path, char *buffer)
231 {
232     syncmsg msg;
233     unsigned int len;
234     int ret;
235
236     if(readx(s, &msg.data, sizeof(msg.data)))
237         return -1;
238
239     if(msg.data.id != ID_DATA) {
240         fail_message(s, "invalid data message: expected ID_DATA");
241         return -1;
242     }
243
244     len = ltohl(msg.data.size);
245     if(len > SYNC_DATA_MAX) {
246         fail_message(s, "oversize data message");
247         return -1;
248     }
249     if(readx(s, buffer, len))
250         return -1;
251
252     ret = symlink(buffer, path);
253     if(ret && errno == ENOENT) {
254         mkdirs(path);
255         ret = symlink(buffer, path);
256     }
257     if(ret) {
258         fail_errno(s);
259         return -1;
260     }
261
262     if(readx(s, &msg.data, sizeof(msg.data)))
263         return -1;
264
265     if(msg.data.id == ID_DONE) {
266         msg.status.id = ID_OKAY;
267         msg.status.msglen = 0;
268         if(writex(s, &msg.status, sizeof(msg.status)))
269             return -1;
270     } else {
271         fail_message(s, "invalid data message: expected ID_DONE");
272         return -1;
273     }
274
275     return 0;
276 }
277 #endif /* HAVE_SYMLINKS */
278
279 static int do_send(int s, char *path, char *buffer)
280 {
281     char *tmp;
282     mode_t mode;
283     int is_link, ret;
284
285     tmp = strrchr(path,',');
286     if(tmp) {
287         *tmp = 0;
288         errno = 0;
289         mode = strtoul(tmp + 1, NULL, 0);
290 #ifndef HAVE_SYMLINKS
291         is_link = 0;
292 #else
293         is_link = S_ISLNK(mode);
294 #endif
295         // extracts file permission from stat.mode. (ex 100644 & 0777 = 644);
296         mode &= 0777; // combination of (S_IRWXU | S_IRWXG | S_IRWXO)
297     }
298     if(!tmp || errno) {
299         mode = 0644; // set default permission value in most of unix system.
300         is_link = 0;
301     }
302
303     // sdb does not allow to check that file exists or not. After deleting old file and creating new file again unconditionally.
304     sdb_unlink(path);
305
306
307 #ifdef HAVE_SYMLINKS
308     if(is_link)
309         ret = handle_send_link(s, path, buffer);
310     else {
311 #else
312     {
313 #endif
314         /* copy user permission bits to "group" and "other" permissions.
315          * ex) 0644 file will be created copied 0666 file.
316          * the following 2 lines should be commented if sdb process has been set to umask 0.
317          */
318
319         //mode |= ((mode >> 3) & 0070);
320         //mode |= ((mode >> 3) & 0007);
321
322         ret = handle_send_file(s, path, mode, buffer);
323     }
324
325     return ret;
326 }
327
328 static int do_recv(int s, const char *path, char *buffer)
329 {
330     syncmsg msg;
331     int fd, r;
332
333     fd = sdb_open(path, O_RDONLY);
334     if(fd < 0) {
335         if(fail_errno(s)) return -1;
336         return 0;
337     }
338
339     msg.data.id = ID_DATA;
340     for(;;) {
341         r = sdb_read(fd, buffer, SYNC_DATA_MAX);
342         if(r <= 0) {
343             if(r == 0) break;
344             if(errno == EINTR) continue;
345             r = fail_errno(s);
346             sdb_close(fd);
347             return r;
348         }
349         msg.data.size = htoll(r);
350         if(writex(s, &msg.data, sizeof(msg.data)) ||
351            writex(s, buffer, r)) {
352             sdb_close(fd);
353             return -1;
354         }
355     }
356
357     sdb_close(fd);
358
359     msg.data.id = ID_DONE;
360     msg.data.size = 0;
361     if(writex(s, &msg.data, sizeof(msg.data))) {
362         return -1;
363     }
364
365     return 0;
366 }
367
368 void file_sync_service(int fd, void *cookie)
369 {
370     syncmsg msg;
371     char name[1025];
372     unsigned namelen;
373
374     char *buffer = malloc(SYNC_DATA_MAX);
375     if(buffer == 0) goto fail;
376
377     for(;;) {
378         D("sync: waiting for command\n");
379
380         if(readx(fd, &msg.req, sizeof(msg.req))) {
381             fail_message(fd, "command read failure");
382             break;
383         }
384         namelen = ltohl(msg.req.namelen);
385         if(namelen > 1024) {
386             fail_message(fd, "invalid namelen");
387             break;
388         }
389         if(readx(fd, name, namelen)) {
390             fail_message(fd, "filename read failure");
391             break;
392         }
393         name[namelen] = 0;
394
395         msg.req.namelen = 0;
396         D("sync: '%s' '%s'\n", (char*) &msg.req, name);
397
398         switch(msg.req.id) {
399         case ID_STAT:
400             if(do_stat(fd, name)) goto fail;
401             break;
402         case ID_LIST:
403             if(do_list(fd, name)) goto fail;
404             break;
405         case ID_SEND:
406             if(do_send(fd, name, buffer)) goto fail;
407             break;
408         case ID_RECV:
409             if(do_recv(fd, name, buffer)) goto fail;
410             break;
411         case ID_QUIT:
412             goto fail;
413         default:
414             fail_message(fd, "unknown command");
415             goto fail;
416         }
417     }
418
419 fail:
420     if(buffer != 0) free(buffer);
421     D("sync: done\n");
422     sdb_close(fd);
423 }