blob: 298ed82eb91865f5bd9b8a5afec6fbdb78691375 [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
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG SYNC
Dan Albert33134262015-03-19 15:21:08 -070018
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;
Elliott Hughes65fe2512015-10-07 15:59:35 -070059 for (const auto& path_component : path_components) {
Elliott Hughes5c742702015-07-30 17:42:01 -070060 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)) {
Elliott Hughese0b43a12015-07-30 20:07:12 -070064 fs_config(partial_path.c_str(), 1, nullptr, &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 Hughes38344402015-08-25 16:33:50 -070074 // Not all filesystems support setting SELinux labels. http://b/23530370.
Elliott Hughes5c742702015-07-30 17:42:01 -070075 selinux_android_restorecon(partial_path.c_str(), 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077 }
Alex Vallée47d67c92015-05-06 16:26:00 -040078 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079}
80
Elliott Hughesaa245492015-08-03 10:38:08 -070081static bool do_stat(int s, const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083 msg.stat.id = ID_STAT;
84
Elliott Hughesaa245492015-08-03 10:38:08 -070085 struct stat st;
86 memset(&st, 0, sizeof(st));
87 // TODO: add a way to report that the stat failed!
88 lstat(path, &st);
Elliott Hughesf4465202015-08-24 14:27:03 -070089 msg.stat.mode = st.st_mode;
90 msg.stat.size = st.st_size;
91 msg.stat.time = st.st_mtime;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092
Elliott Hughesaa245492015-08-03 10:38:08 -070093 return WriteFdExactly(s, &msg.stat, sizeof(msg.stat));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094}
95
Elliott Hughesaa245492015-08-03 10:38:08 -070096static bool do_list(int s, const char* path) {
97 dirent* de;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098
Elliott Hughes5c742702015-07-30 17:42:01 -070099 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100 msg.dent.id = ID_DENT;
101
Elliott Hughes5c742702015-07-30 17:42:01 -0700102 std::unique_ptr<DIR, int(*)(DIR*)> d(opendir(path), closedir);
103 if (!d) goto done;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104
Elliott Hughes5c742702015-07-30 17:42:01 -0700105 while ((de = readdir(d.get()))) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700106 std::string filename(android::base::StringPrintf("%s/%s", path, de->d_name));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107
Elliott Hughesaa245492015-08-03 10:38:08 -0700108 struct stat st;
109 if (lstat(filename.c_str(), &st) == 0) {
110 size_t d_name_length = strlen(de->d_name);
Elliott Hughesf4465202015-08-24 14:27:03 -0700111 msg.dent.mode = st.st_mode;
112 msg.dent.size = st.st_size;
113 msg.dent.time = st.st_mtime;
114 msg.dent.namelen = d_name_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115
Elliott Hughesaa245492015-08-03 10:38:08 -0700116 if (!WriteFdExactly(s, &msg.dent, sizeof(msg.dent)) ||
117 !WriteFdExactly(s, de->d_name, d_name_length)) {
118 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 }
120 }
121 }
122
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123done:
124 msg.dent.id = ID_DONE;
125 msg.dent.mode = 0;
126 msg.dent.size = 0;
127 msg.dent.time = 0;
128 msg.dent.namelen = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700129 return WriteFdExactly(s, &msg.dent, sizeof(msg.dent));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130}
131
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700132static bool SendSyncFail(int fd, const std::string& reason) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700133 D("sync: failure: %s", reason.c_str());
Elliott Hughesaa245492015-08-03 10:38:08 -0700134
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 msg.data.id = ID_FAIL;
Elliott Hughesf4465202015-08-24 14:27:03 -0700137 msg.data.size = reason.size();
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700138 return WriteFdExactly(fd, &msg.data, sizeof(msg.data)) && WriteFdExactly(fd, reason);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139}
140
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700141static bool SendSyncFailErrno(int fd, const std::string& reason) {
142 return SendSyncFail(fd, android::base::StringPrintf("%s: %s", reason.c_str(), strerror(errno)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143}
144
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700145static bool handle_send_file(int s, const char* path, uid_t uid,
Elliott Hughesaa245492015-08-03 10:38:08 -0700146 gid_t gid, mode_t mode, std::vector<char>& buffer, bool do_unlink) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147 syncmsg msg;
148 unsigned int timestamp = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149
Elliott Hughesaa245492015-08-03 10:38:08 -0700150 int fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700151 if (fd < 0 && errno == ENOENT) {
Alex Vallée47d67c92015-05-06 16:26:00 -0400152 if (!secure_mkdirs(path)) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700153 SendSyncFailErrno(s, "secure_mkdirs failed");
154 goto fail;
Liang Cheng20d33f42014-01-02 18:27:51 -0800155 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700156 fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700158 if (fd < 0 && errno == EEXIST) {
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700159 fd = adb_open_mode(path, O_WRONLY | O_CLOEXEC, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700161 if (fd < 0) {
162 SendSyncFailErrno(s, "couldn't create file");
163 goto fail;
Liang Cheng20d33f42014-01-02 18:27:51 -0800164 } else {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700165 if (fchown(fd, uid, gid) == -1) {
166 SendSyncFailErrno(s, "fchown failed");
167 goto fail;
Liang Cheng20d33f42014-01-02 18:27:51 -0800168 }
Nick Kralevich72917832014-01-17 16:16:42 -0800169
Elliott Hughes38344402015-08-25 16:33:50 -0700170 // Not all filesystems support setting SELinux labels. http://b/23530370.
171 selinux_android_restorecon(path, 0);
Elliott Hughes0adc0972015-08-25 13:14:07 -0700172
173 // fchown clears the setuid bit - restore it if present.
174 // Ignore the result of calling fchmod. It's not supported
175 // by all filesystems. b/12441485
Nick Kralevich72917832014-01-17 16:16:42 -0800176 fchmod(fd, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 }
178
Elliott Hughesaa245492015-08-03 10:38:08 -0700179 while (true) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 unsigned int len;
181
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700182 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700184 if (msg.data.id != ID_DATA) {
185 if (msg.data.id == ID_DONE) {
Elliott Hughesf4465202015-08-24 14:27:03 -0700186 timestamp = msg.data.size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 break;
188 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700189 SendSyncFail(s, "invalid data message");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 goto fail;
191 }
Elliott Hughesf4465202015-08-24 14:27:03 -0700192 len = msg.data.size;
Elliott Hughesaa245492015-08-03 10:38:08 -0700193 if (len > buffer.size()) { // TODO: resize buffer?
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700194 SendSyncFail(s, "oversize data message");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 goto fail;
196 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700197
Elliott Hughesaa245492015-08-03 10:38:08 -0700198 if (!ReadFdExactly(s, &buffer[0], len)) goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199
Elliott Hughesaa245492015-08-03 10:38:08 -0700200 if (!WriteFdExactly(fd, &buffer[0], len)) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700201 SendSyncFailErrno(s, "write failed");
202 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 }
204 }
205
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700206 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700208 utimbuf u;
209 u.actime = timestamp;
210 u.modtime = timestamp;
211 utime(path, &u);
212
213 msg.status.id = ID_OKAY;
214 msg.status.msglen = 0;
215 return WriteFdExactly(s, &msg.status, sizeof(msg.status));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216
217fail:
Elliott Hughesaa245492015-08-03 10:38:08 -0700218 if (fd >= 0) adb_close(fd);
JP Abgrall55b6c842014-03-07 17:31:25 -0800219 if (do_unlink) adb_unlink(path);
Elliott Hughesaa245492015-08-03 10:38:08 -0700220 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221}
222
Elliott Hughes0a049b12015-01-12 14:26:36 -0800223#if defined(_WIN32)
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700224extern 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 -0800225#else
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700226static bool handle_send_link(int s, const std::string& path, std::vector<char>& buffer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227 syncmsg msg;
228 unsigned int len;
229 int ret;
230
Elliott Hughesaa245492015-08-03 10:38:08 -0700231 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232
Elliott Hughesaa245492015-08-03 10:38:08 -0700233 if (msg.data.id != ID_DATA) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700234 SendSyncFail(s, "invalid data message: expected ID_DATA");
Elliott Hughesaa245492015-08-03 10:38:08 -0700235 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 }
237
Elliott Hughesf4465202015-08-24 14:27:03 -0700238 len = msg.data.size;
Elliott Hughesaa245492015-08-03 10:38:08 -0700239 if (len > buffer.size()) { // TODO: resize buffer?
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700240 SendSyncFail(s, "oversize data message");
Elliott Hughesaa245492015-08-03 10:38:08 -0700241 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700243 if (!ReadFdExactly(s, &buffer[0], len)) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700245 ret = symlink(&buffer[0], path.c_str());
Elliott Hughesaa245492015-08-03 10:38:08 -0700246 if (ret && errno == ENOENT) {
Alex Vallée47d67c92015-05-06 16:26:00 -0400247 if (!secure_mkdirs(path)) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700248 SendSyncFailErrno(s, "secure_mkdirs failed");
Elliott Hughesaa245492015-08-03 10:38:08 -0700249 return false;
Liang Cheng20d33f42014-01-02 18:27:51 -0800250 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700251 ret = symlink(&buffer[0], path.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700253 if (ret) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700254 SendSyncFailErrno(s, "symlink failed");
Elliott Hughesaa245492015-08-03 10:38:08 -0700255 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256 }
257
Elliott Hughesaa245492015-08-03 10:38:08 -0700258 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259
Elliott Hughesaa245492015-08-03 10:38:08 -0700260 if (msg.data.id == ID_DONE) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261 msg.status.id = ID_OKAY;
262 msg.status.msglen = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700263 if (!WriteFdExactly(s, &msg.status, sizeof(msg.status))) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800264 } else {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700265 SendFail(s, "invalid data message: expected ID_DONE");
Elliott Hughesaa245492015-08-03 10:38:08 -0700266 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267 }
268
Elliott Hughesaa245492015-08-03 10:38:08 -0700269 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270}
Elliott Hughes0a049b12015-01-12 14:26:36 -0800271#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700273static bool do_send(int s, const std::string& spec, std::vector<char>& buffer) {
274 // 'spec' is of the form "/some/path,0755". Break it up.
275 size_t comma = spec.find_last_of(',');
276 if (comma == std::string::npos) {
277 SendFail(s, "missing , in ID_SEND");
278 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800279 }
280
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700281 std::string path = spec.substr(0, comma);
282
283 errno = 0;
284 mode_t mode = strtoul(spec.substr(comma + 1).c_str(), nullptr, 0);
285 if (errno != 0) {
286 SendFail(s, "bad mode");
287 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 }
289
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700290 // Don't delete files before copying if they are not "regular" or symlinks.
291 struct stat st;
292 bool do_unlink = (lstat(path.c_str(), &st) == -1) || S_ISREG(st.st_mode) || S_ISLNK(st.st_mode);
293 if (do_unlink) {
294 adb_unlink(path.c_str());
295 }
296
297 if (S_ISLNK(mode)) {
298 return handle_send_link(s, path.c_str(), buffer);
299 }
300
301 // Copy user permission bits to "group" and "other" permissions.
302 mode &= 0777;
303 mode |= ((mode >> 3) & 0070);
304 mode |= ((mode >> 3) & 0007);
305
Elliott Hughes0a049b12015-01-12 14:26:36 -0800306 uid_t uid = -1;
307 gid_t gid = -1;
308 uint64_t cap = 0;
Elliott Hughesec7a6672015-03-16 21:58:32 +0000309 if (should_use_fs_config(path)) {
Elliott Hughes7baecbe2015-08-25 11:09:04 -0700310 unsigned int broken_api_hack = mode;
Elliott Hughes9bc80052015-08-25 11:21:58 -0700311 fs_config(path.c_str(), 0, nullptr, &uid, &gid, &broken_api_hack, &cap);
Elliott Hughes56bf3092015-08-25 11:01:39 -0700312 mode = broken_api_hack;
Elliott Hughes0a049b12015-01-12 14:26:36 -0800313 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700314 return handle_send_file(s, path.c_str(), uid, gid, mode, buffer, do_unlink);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315}
316
Elliott Hughesaa245492015-08-03 10:38:08 -0700317static bool do_recv(int s, const char* path, std::vector<char>& buffer) {
318 int fd = adb_open(path, O_RDONLY | O_CLOEXEC);
319 if (fd < 0) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700320 SendSyncFailErrno(s, "open failed");
321 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 }
323
Elliott Hughesaa245492015-08-03 10:38:08 -0700324 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 msg.data.id = ID_DATA;
Elliott Hughesaa245492015-08-03 10:38:08 -0700326 while (true) {
327 int r = adb_read(fd, &buffer[0], buffer.size());
328 if (r <= 0) {
329 if (r == 0) break;
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700330 SendSyncFailErrno(s, "read failed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 adb_close(fd);
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700332 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 }
Elliott Hughesf4465202015-08-24 14:27:03 -0700334 msg.data.size = r;
Elliott Hughesaa245492015-08-03 10:38:08 -0700335 if (!WriteFdExactly(s, &msg.data, sizeof(msg.data)) || !WriteFdExactly(s, &buffer[0], r)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 adb_close(fd);
Elliott Hughesaa245492015-08-03 10:38:08 -0700337 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 }
339 }
340
341 adb_close(fd);
342
343 msg.data.id = ID_DONE;
344 msg.data.size = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700345 return WriteFdExactly(s, &msg.data, sizeof(msg.data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346}
347
Elliott Hughesaa245492015-08-03 10:38:08 -0700348static bool handle_sync_command(int fd, std::vector<char>& buffer) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700349 D("sync: waiting for request");
Elliott Hughesaa245492015-08-03 10:38:08 -0700350
351 SyncRequest request;
352 if (!ReadFdExactly(fd, &request, sizeof(request))) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700353 SendSyncFail(fd, "command read failure");
Elliott Hughesaa245492015-08-03 10:38:08 -0700354 return false;
355 }
Elliott Hughesf4465202015-08-24 14:27:03 -0700356 size_t path_length = request.path_length;
Elliott Hughesaa245492015-08-03 10:38:08 -0700357 if (path_length > 1024) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700358 SendSyncFail(fd, "path too long");
Elliott Hughesaa245492015-08-03 10:38:08 -0700359 return false;
360 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 char name[1025];
Elliott Hughesaa245492015-08-03 10:38:08 -0700362 if (!ReadFdExactly(fd, name, path_length)) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700363 SendSyncFail(fd, "filename read failure");
Elliott Hughesaa245492015-08-03 10:38:08 -0700364 return false;
365 }
366 name[path_length] = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367
Elliott Hughesaa245492015-08-03 10:38:08 -0700368 const char* id = reinterpret_cast<const char*>(&request.id);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700369 D("sync: '%.4s' '%s'", id, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370
Elliott Hughesaa245492015-08-03 10:38:08 -0700371 switch (request.id) {
372 case ID_STAT:
373 if (!do_stat(fd, name)) return false;
374 break;
375 case ID_LIST:
376 if (!do_list(fd, name)) return false;
377 break;
378 case ID_SEND:
379 if (!do_send(fd, name, buffer)) return false;
380 break;
381 case ID_RECV:
382 if (!do_recv(fd, name, buffer)) return false;
383 break;
384 case ID_QUIT:
385 return false;
386 default:
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700387 SendSyncFail(fd, android::base::StringPrintf("unknown command '%.4s' (%08x)",
Elliott Hughesaa245492015-08-03 10:38:08 -0700388 id, request.id));
389 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390 }
391
Elliott Hughesaa245492015-08-03 10:38:08 -0700392 return true;
393}
394
395void file_sync_service(int fd, void* cookie) {
396 std::vector<char> buffer(SYNC_DATA_MAX);
397
398 while (handle_sync_command(fd, buffer)) {
399 }
400
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700401 D("sync: done");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 adb_close(fd);
403}