blob: cadf5b3c3b1a69aa1d08655cce8cda84631f0036 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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
Dan Albert33134262015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_SYNC
18
19#include "sysdeps.h"
20#include "file_sync_service.h"
21
Dan Albert76649012015-02-24 15:51:19 -080022#include <dirent.h>
23#include <errno.h>
24#include <selinux/android.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <stdio.h>
Dan Albert76649012015-02-24 15:51:19 -080026#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028#include <sys/stat.h>
29#include <sys/types.h>
Liang Cheng20d33f42014-01-02 18:27:51 -080030#include <unistd.h>
Dan Albert76649012015-02-24 15:51:19 -080031#include <utime.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080034#include "adb_io.h"
Dan Albert76649012015-02-24 15:51:19 -080035#include "private/android_filesystem_config.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036
Elliott Hughesaa245492015-08-03 10:38:08 -070037#include <base/stringprintf.h>
Elliott Hughes5c742702015-07-30 17:42:01 -070038#include <base/strings.h>
39
40static bool should_use_fs_config(const std::string& path) {
Elliott Hughesec7a6672015-03-16 21:58:32 +000041 // TODO: use fs_config to configure permissions on /data.
Elliott Hughes5c742702015-07-30 17:42:01 -070042 return android::base::StartsWith(path, "/system/") ||
43 android::base::StartsWith(path, "/vendor/") ||
44 android::base::StartsWith(path, "/oem/");
Daniel Rosenberg686bce62014-06-30 20:29:40 -070045}
46
Alex Vallée47d67c92015-05-06 16:26:00 -040047static bool secure_mkdirs(const std::string& path) {
Nick Kralevich72917832014-01-17 16:16:42 -080048 uid_t uid = -1;
49 gid_t gid = -1;
Liang Cheng20d33f42014-01-02 18:27:51 -080050 unsigned int mode = 0775;
51 uint64_t cap = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052
Alex Vallée47d67c92015-05-06 16:26:00 -040053 if (path[0] != '/') return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054
Elliott Hughes5c742702015-07-30 17:42:01 -070055 std::vector<std::string> path_components = android::base::Split(path, "/");
56 path_components.pop_back(); // For "/system/bin/sh", only create "/system/bin".
57
58 std::string partial_path;
59 for (auto& path_component : path_components) {
60 if (partial_path.back() != OS_PATH_SEPARATOR) partial_path += OS_PATH_SEPARATOR;
61 partial_path += path_component;
62
63 if (should_use_fs_config(partial_path)) {
64 fs_config(partial_path.c_str(), 1, &uid, &gid, &mode, &cap);
Liang Cheng20d33f42014-01-02 18:27:51 -080065 }
Elliott Hughes5c742702015-07-30 17:42:01 -070066 if (adb_mkdir(partial_path.c_str(), mode) == -1) {
Alex Vallée47d67c92015-05-06 16:26:00 -040067 if (errno != EEXIST) {
68 return false;
Liang Cheng20d33f42014-01-02 18:27:51 -080069 }
Alex Vallée47d67c92015-05-06 16:26:00 -040070 } else {
Elliott Hughes5c742702015-07-30 17:42:01 -070071 if (chown(partial_path.c_str(), uid, gid) == -1) {
Alex Vallée47d67c92015-05-06 16:26:00 -040072 return false;
73 }
Elliott Hughes5c742702015-07-30 17:42:01 -070074 selinux_android_restorecon(partial_path.c_str(), 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076 }
Alex Vallée47d67c92015-05-06 16:26:00 -040077 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078}
79
Elliott Hughesaa245492015-08-03 10:38:08 -070080static bool do_stat(int s, const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082 msg.stat.id = ID_STAT;
83
Elliott Hughesaa245492015-08-03 10:38:08 -070084 struct stat st;
85 memset(&st, 0, sizeof(st));
86 // TODO: add a way to report that the stat failed!
87 lstat(path, &st);
Elliott Hughesf4465202015-08-24 14:27:03 -070088 msg.stat.mode = st.st_mode;
89 msg.stat.size = st.st_size;
90 msg.stat.time = st.st_mtime;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091
Elliott Hughesaa245492015-08-03 10:38:08 -070092 return WriteFdExactly(s, &msg.stat, sizeof(msg.stat));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093}
94
Elliott Hughesaa245492015-08-03 10:38:08 -070095static bool do_list(int s, const char* path) {
96 dirent* de;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097
Elliott Hughes5c742702015-07-30 17:42:01 -070098 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099 msg.dent.id = ID_DENT;
100
Elliott Hughes5c742702015-07-30 17:42:01 -0700101 std::unique_ptr<DIR, int(*)(DIR*)> d(opendir(path), closedir);
102 if (!d) goto done;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103
Elliott Hughes5c742702015-07-30 17:42:01 -0700104 while ((de = readdir(d.get()))) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700105 std::string filename(android::base::StringPrintf("%s/%s", path, de->d_name));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106
Elliott Hughesaa245492015-08-03 10:38:08 -0700107 struct stat st;
108 if (lstat(filename.c_str(), &st) == 0) {
109 size_t d_name_length = strlen(de->d_name);
Elliott Hughesf4465202015-08-24 14:27:03 -0700110 msg.dent.mode = st.st_mode;
111 msg.dent.size = st.st_size;
112 msg.dent.time = st.st_mtime;
113 msg.dent.namelen = d_name_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114
Elliott Hughesaa245492015-08-03 10:38:08 -0700115 if (!WriteFdExactly(s, &msg.dent, sizeof(msg.dent)) ||
116 !WriteFdExactly(s, de->d_name, d_name_length)) {
117 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118 }
119 }
120 }
121
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122done:
123 msg.dent.id = ID_DONE;
124 msg.dent.mode = 0;
125 msg.dent.size = 0;
126 msg.dent.time = 0;
127 msg.dent.namelen = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700128 return WriteFdExactly(s, &msg.dent, sizeof(msg.dent));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129}
130
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700131static bool SendSyncFail(int fd, const std::string& reason) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700132 D("sync: failure: %s\n", reason.c_str());
133
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 msg.data.id = ID_FAIL;
Elliott Hughesf4465202015-08-24 14:27:03 -0700136 msg.data.size = reason.size();
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700137 return WriteFdExactly(fd, &msg.data, sizeof(msg.data)) && WriteFdExactly(fd, reason);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138}
139
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700140static bool SendSyncFailErrno(int fd, const std::string& reason) {
141 return SendSyncFail(fd, android::base::StringPrintf("%s: %s", reason.c_str(), strerror(errno)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142}
143
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700144static bool handle_send_file(int s, const char* path, uid_t uid,
Elliott Hughesaa245492015-08-03 10:38:08 -0700145 gid_t gid, mode_t mode, std::vector<char>& buffer, bool do_unlink) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 syncmsg msg;
147 unsigned int timestamp = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148
Elliott Hughesaa245492015-08-03 10:38:08 -0700149 int fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700150 if (fd < 0 && errno == ENOENT) {
Alex Vallée47d67c92015-05-06 16:26:00 -0400151 if (!secure_mkdirs(path)) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700152 SendSyncFailErrno(s, "secure_mkdirs failed");
153 goto fail;
Liang Cheng20d33f42014-01-02 18:27:51 -0800154 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700155 fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700157 if (fd < 0 && errno == EEXIST) {
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700158 fd = adb_open_mode(path, O_WRONLY | O_CLOEXEC, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700160 if (fd < 0) {
161 SendSyncFailErrno(s, "couldn't create file");
162 goto fail;
Liang Cheng20d33f42014-01-02 18:27:51 -0800163 } else {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700164 if (fchown(fd, uid, gid) == -1) {
165 SendSyncFailErrno(s, "fchown failed");
166 goto fail;
Liang Cheng20d33f42014-01-02 18:27:51 -0800167 }
Nick Kralevich72917832014-01-17 16:16:42 -0800168
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700169 // fchown clears the setuid bit - restore it if present.
170 // Ignore the result of calling fchmod. It's not supported
171 // by all filesystems. b/12441485
Nick Kralevich72917832014-01-17 16:16:42 -0800172 fchmod(fd, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 }
174
Elliott Hughesaa245492015-08-03 10:38:08 -0700175 while (true) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 unsigned int len;
177
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700178 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700180 if (msg.data.id != ID_DATA) {
181 if (msg.data.id == ID_DONE) {
Elliott Hughesf4465202015-08-24 14:27:03 -0700182 timestamp = msg.data.size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183 break;
184 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700185 SendSyncFail(s, "invalid data message");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 goto fail;
187 }
Elliott Hughesf4465202015-08-24 14:27:03 -0700188 len = msg.data.size;
Elliott Hughesaa245492015-08-03 10:38:08 -0700189 if (len > buffer.size()) { // TODO: resize buffer?
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700190 SendSyncFail(s, "oversize data message");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 goto fail;
192 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700193
Elliott Hughesaa245492015-08-03 10:38:08 -0700194 if (!ReadFdExactly(s, &buffer[0], len)) goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195
Elliott Hughesaa245492015-08-03 10:38:08 -0700196 if (!WriteFdExactly(fd, &buffer[0], len)) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700197 SendSyncFailErrno(s, "write failed");
198 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 }
200 }
201
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700202 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700204 selinux_android_restorecon(path, 0);
205
206 utimbuf u;
207 u.actime = timestamp;
208 u.modtime = timestamp;
209 utime(path, &u);
210
211 msg.status.id = ID_OKAY;
212 msg.status.msglen = 0;
213 return WriteFdExactly(s, &msg.status, sizeof(msg.status));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214
215fail:
Elliott Hughesaa245492015-08-03 10:38:08 -0700216 if (fd >= 0) adb_close(fd);
JP Abgrall55b6c842014-03-07 17:31:25 -0800217 if (do_unlink) adb_unlink(path);
Elliott Hughesaa245492015-08-03 10:38:08 -0700218 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219}
220
Elliott Hughes0a049b12015-01-12 14:26:36 -0800221#if defined(_WIN32)
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700222extern bool handle_send_link(int s, const std::string& path, std::vector<char>& buffer) __attribute__((error("no symlinks on Windows")));
Elliott Hughes0a049b12015-01-12 14:26:36 -0800223#else
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700224static bool handle_send_link(int s, const std::string& path, std::vector<char>& buffer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 syncmsg msg;
226 unsigned int len;
227 int ret;
228
Elliott Hughesaa245492015-08-03 10:38:08 -0700229 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230
Elliott Hughesaa245492015-08-03 10:38:08 -0700231 if (msg.data.id != ID_DATA) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700232 SendSyncFail(s, "invalid data message: expected ID_DATA");
Elliott Hughesaa245492015-08-03 10:38:08 -0700233 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 }
235
Elliott Hughesf4465202015-08-24 14:27:03 -0700236 len = msg.data.size;
Elliott Hughesaa245492015-08-03 10:38:08 -0700237 if (len > buffer.size()) { // TODO: resize buffer?
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700238 SendSyncFail(s, "oversize data message");
Elliott Hughesaa245492015-08-03 10:38:08 -0700239 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700241 if (!ReadFdExactly(s, &buffer[0], len)) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700243 ret = symlink(&buffer[0], path.c_str());
Elliott Hughesaa245492015-08-03 10:38:08 -0700244 if (ret && errno == ENOENT) {
Alex Vallée47d67c92015-05-06 16:26:00 -0400245 if (!secure_mkdirs(path)) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700246 SendSyncFailErrno(s, "secure_mkdirs failed");
Elliott Hughesaa245492015-08-03 10:38:08 -0700247 return false;
Liang Cheng20d33f42014-01-02 18:27:51 -0800248 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700249 ret = symlink(&buffer[0], path.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700251 if (ret) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700252 SendSyncFailErrno(s, "symlink failed");
Elliott Hughesaa245492015-08-03 10:38:08 -0700253 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254 }
255
Elliott Hughesaa245492015-08-03 10:38:08 -0700256 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257
Elliott Hughesaa245492015-08-03 10:38:08 -0700258 if (msg.data.id == ID_DONE) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259 msg.status.id = ID_OKAY;
260 msg.status.msglen = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700261 if (!WriteFdExactly(s, &msg.status, sizeof(msg.status))) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262 } else {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700263 SendFail(s, "invalid data message: expected ID_DONE");
Elliott Hughesaa245492015-08-03 10:38:08 -0700264 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265 }
266
Elliott Hughesaa245492015-08-03 10:38:08 -0700267 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268}
Elliott Hughes0a049b12015-01-12 14:26:36 -0800269#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700271static bool do_send(int s, const std::string& spec, std::vector<char>& buffer) {
272 // 'spec' is of the form "/some/path,0755". Break it up.
273 size_t comma = spec.find_last_of(',');
274 if (comma == std::string::npos) {
275 SendFail(s, "missing , in ID_SEND");
276 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277 }
278
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700279 std::string path = spec.substr(0, comma);
280
281 errno = 0;
282 mode_t mode = strtoul(spec.substr(comma + 1).c_str(), nullptr, 0);
283 if (errno != 0) {
284 SendFail(s, "bad mode");
285 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286 }
287
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700288 // Don't delete files before copying if they are not "regular" or symlinks.
289 struct stat st;
290 bool do_unlink = (lstat(path.c_str(), &st) == -1) || S_ISREG(st.st_mode) || S_ISLNK(st.st_mode);
291 if (do_unlink) {
292 adb_unlink(path.c_str());
293 }
294
295 if (S_ISLNK(mode)) {
296 return handle_send_link(s, path.c_str(), buffer);
297 }
298
299 // Copy user permission bits to "group" and "other" permissions.
300 mode &= 0777;
301 mode |= ((mode >> 3) & 0070);
302 mode |= ((mode >> 3) & 0007);
303
Elliott Hughes0a049b12015-01-12 14:26:36 -0800304 uid_t uid = -1;
305 gid_t gid = -1;
306 uint64_t cap = 0;
Elliott Hughesec7a6672015-03-16 21:58:32 +0000307 if (should_use_fs_config(path)) {
Elliott Hughes7baecbe2015-08-25 11:09:04 -0700308 unsigned int broken_api_hack = mode;
Elliott Hughes56bf3092015-08-25 11:01:39 -0700309 fs_config(path.c_str(), 0, &uid, &gid, &broken_api_hack, &cap);
310 mode = broken_api_hack;
Elliott Hughes0a049b12015-01-12 14:26:36 -0800311 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700312 return handle_send_file(s, path.c_str(), uid, gid, mode, buffer, do_unlink);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313}
314
Elliott Hughesaa245492015-08-03 10:38:08 -0700315static bool do_recv(int s, const char* path, std::vector<char>& buffer) {
316 int fd = adb_open(path, O_RDONLY | O_CLOEXEC);
317 if (fd < 0) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700318 SendSyncFailErrno(s, "open failed");
319 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320 }
321
Elliott Hughesaa245492015-08-03 10:38:08 -0700322 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 msg.data.id = ID_DATA;
Elliott Hughesaa245492015-08-03 10:38:08 -0700324 while (true) {
325 int r = adb_read(fd, &buffer[0], buffer.size());
326 if (r <= 0) {
327 if (r == 0) break;
328 if (errno == EINTR) continue;
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700329 SendSyncFailErrno(s, "read failed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 adb_close(fd);
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700331 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332 }
Elliott Hughesf4465202015-08-24 14:27:03 -0700333 msg.data.size = r;
Elliott Hughesaa245492015-08-03 10:38:08 -0700334 if (!WriteFdExactly(s, &msg.data, sizeof(msg.data)) || !WriteFdExactly(s, &buffer[0], r)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335 adb_close(fd);
Elliott Hughesaa245492015-08-03 10:38:08 -0700336 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337 }
338 }
339
340 adb_close(fd);
341
342 msg.data.id = ID_DONE;
343 msg.data.size = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700344 return WriteFdExactly(s, &msg.data, sizeof(msg.data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345}
346
Elliott Hughesaa245492015-08-03 10:38:08 -0700347static bool handle_sync_command(int fd, std::vector<char>& buffer) {
348 D("sync: waiting for request\n");
349
350 SyncRequest request;
351 if (!ReadFdExactly(fd, &request, sizeof(request))) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700352 SendSyncFail(fd, "command read failure");
Elliott Hughesaa245492015-08-03 10:38:08 -0700353 return false;
354 }
Elliott Hughesf4465202015-08-24 14:27:03 -0700355 size_t path_length = request.path_length;
Elliott Hughesaa245492015-08-03 10:38:08 -0700356 if (path_length > 1024) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700357 SendSyncFail(fd, "path too long");
Elliott Hughesaa245492015-08-03 10:38:08 -0700358 return false;
359 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360 char name[1025];
Elliott Hughesaa245492015-08-03 10:38:08 -0700361 if (!ReadFdExactly(fd, name, path_length)) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700362 SendSyncFail(fd, "filename read failure");
Elliott Hughesaa245492015-08-03 10:38:08 -0700363 return false;
364 }
365 name[path_length] = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366
Elliott Hughesaa245492015-08-03 10:38:08 -0700367 const char* id = reinterpret_cast<const char*>(&request.id);
368 D("sync: '%.4s' '%s'\n", id, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800369
Elliott Hughesaa245492015-08-03 10:38:08 -0700370 switch (request.id) {
371 case ID_STAT:
372 if (!do_stat(fd, name)) return false;
373 break;
374 case ID_LIST:
375 if (!do_list(fd, name)) return false;
376 break;
377 case ID_SEND:
378 if (!do_send(fd, name, buffer)) return false;
379 break;
380 case ID_RECV:
381 if (!do_recv(fd, name, buffer)) return false;
382 break;
383 case ID_QUIT:
384 return false;
385 default:
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700386 SendSyncFail(fd, android::base::StringPrintf("unknown command '%.4s' (%08x)",
Elliott Hughesaa245492015-08-03 10:38:08 -0700387 id, request.id));
388 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389 }
390
Elliott Hughesaa245492015-08-03 10:38:08 -0700391 return true;
392}
393
394void file_sync_service(int fd, void* cookie) {
395 std::vector<char> buffer(SYNC_DATA_MAX);
396
397 while (handle_sync_command(fd, buffer)) {
398 }
399
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400 D("sync: done\n");
401 adb_close(fd);
402}