The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG SYNC |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | #include "file_sync_service.h" |
| 21 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 22 | #include <dirent.h> |
| 23 | #include <errno.h> |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 24 | #include <linux/xattr.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 26 | #include <stdlib.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | #include <string.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 30 | #include <sys/xattr.h> |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 31 | #include <unistd.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 32 | #include <utime.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 | |
Colin Cross | 58021d1 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 34 | #include <android-base/file.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 35 | #include <android-base/stringprintf.h> |
| 36 | #include <android-base/strings.h> |
| 37 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | 6debf98 | 2016-10-05 08:13:56 -0700 | [diff] [blame] | 38 | #include <private/android_logger.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 39 | #include <selinux/android.h> |
| 40 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | #include "adb.h" |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 42 | #include "adb_io.h" |
Josh Gao | fd12aaa | 2016-11-22 14:33:08 -0800 | [diff] [blame] | 43 | #include "adb_trace.h" |
Josh Gao | 45b6fc8 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 44 | #include "adb_utils.h" |
Rubin Xu | d61a25c | 2016-01-11 10:23:47 +0000 | [diff] [blame] | 45 | #include "security_log_tags.h" |
Josh Gao | 5a1e3fd | 2016-12-05 17:11:34 -0800 | [diff] [blame] | 46 | #include "sysdeps/errno.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | |
Josh Gao | fd12aaa | 2016-11-22 14:33:08 -0800 | [diff] [blame] | 48 | using android::base::StringPrintf; |
| 49 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 50 | static bool should_use_fs_config(const std::string& path) { |
Elliott Hughes | 792581f | 2018-04-03 10:54:39 -0700 | [diff] [blame] | 51 | // TODO: use fs_config to configure permissions on /data too. |
| 52 | return !android::base::StartsWith(path, "/data/"); |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 55 | static bool update_capabilities(const char* path, uint64_t capabilities) { |
| 56 | if (capabilities == 0) { |
| 57 | // Ensure we clean up in case the capabilities weren't 0 in the past. |
| 58 | removexattr(path, XATTR_NAME_CAPS); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | vfs_cap_data cap_data = {}; |
Nick Kralevich | 65b8d74 | 2017-11-27 13:34:19 -0800 | [diff] [blame] | 63 | cap_data.magic_etc = VFS_CAP_REVISION_2 | VFS_CAP_FLAGS_EFFECTIVE; |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 64 | cap_data.data[0].permitted = (capabilities & 0xffffffff); |
| 65 | cap_data.data[0].inheritable = 0; |
| 66 | cap_data.data[1].permitted = (capabilities >> 32); |
| 67 | cap_data.data[1].inheritable = 0; |
| 68 | return setxattr(path, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) != -1; |
| 69 | } |
| 70 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 71 | static bool secure_mkdirs(const std::string& path) { |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 72 | if (path[0] != '/') return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 73 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 74 | std::vector<std::string> path_components = android::base::Split(path, "/"); |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 75 | std::string partial_path; |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 76 | for (const auto& path_component : path_components) { |
Josh Gao | cfe4011 | 2018-06-28 18:43:19 -0700 | [diff] [blame] | 77 | uid_t uid = -1; |
| 78 | gid_t gid = -1; |
| 79 | unsigned int mode = 0775; |
| 80 | uint64_t capabilities = 0; |
| 81 | |
| 82 | if (path_component.empty()) { |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | if (partial_path.empty() || partial_path.back() != OS_PATH_SEPARATOR) { |
| 87 | partial_path += OS_PATH_SEPARATOR; |
| 88 | } |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 89 | partial_path += path_component; |
| 90 | |
| 91 | if (should_use_fs_config(partial_path)) { |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 92 | fs_config(partial_path.c_str(), 1, nullptr, &uid, &gid, &mode, &capabilities); |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 93 | } |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 94 | if (adb_mkdir(partial_path.c_str(), mode) == -1) { |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 95 | if (errno != EEXIST) { |
| 96 | return false; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 97 | } |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 98 | } else { |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 99 | if (chown(partial_path.c_str(), uid, gid) == -1) return false; |
| 100 | |
Elliott Hughes | 3834440 | 2015-08-25 16:33:50 -0700 | [diff] [blame] | 101 | // Not all filesystems support setting SELinux labels. http://b/23530370. |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 102 | selinux_android_restorecon(partial_path.c_str(), 0); |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 103 | |
| 104 | if (!update_capabilities(partial_path.c_str(), capabilities)) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 105 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 106 | } |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 107 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Josh Gao | 5a1e3fd | 2016-12-05 17:11:34 -0800 | [diff] [blame] | 110 | static bool do_lstat_v1(int s, const char* path) { |
| 111 | syncmsg msg = {}; |
| 112 | msg.stat_v1.id = ID_LSTAT_V1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 113 | |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 114 | struct stat st = {}; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 115 | lstat(path, &st); |
Josh Gao | 5a1e3fd | 2016-12-05 17:11:34 -0800 | [diff] [blame] | 116 | msg.stat_v1.mode = st.st_mode; |
| 117 | msg.stat_v1.size = st.st_size; |
| 118 | msg.stat_v1.time = st.st_mtime; |
| 119 | return WriteFdExactly(s, &msg.stat_v1, sizeof(msg.stat_v1)); |
| 120 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 121 | |
Josh Gao | 5a1e3fd | 2016-12-05 17:11:34 -0800 | [diff] [blame] | 122 | static bool do_stat_v2(int s, uint32_t id, const char* path) { |
| 123 | syncmsg msg = {}; |
| 124 | msg.stat_v2.id = id; |
| 125 | |
| 126 | decltype(&stat) stat_fn; |
| 127 | if (id == ID_STAT_V2) { |
| 128 | stat_fn = stat; |
| 129 | } else { |
| 130 | stat_fn = lstat; |
| 131 | } |
| 132 | |
| 133 | struct stat st = {}; |
| 134 | int rc = stat_fn(path, &st); |
| 135 | if (rc == -1) { |
| 136 | msg.stat_v2.error = errno_to_wire(errno); |
| 137 | } else { |
| 138 | msg.stat_v2.dev = st.st_dev; |
| 139 | msg.stat_v2.ino = st.st_ino; |
| 140 | msg.stat_v2.mode = st.st_mode; |
| 141 | msg.stat_v2.nlink = st.st_nlink; |
| 142 | msg.stat_v2.uid = st.st_uid; |
| 143 | msg.stat_v2.gid = st.st_gid; |
| 144 | msg.stat_v2.size = st.st_size; |
| 145 | msg.stat_v2.atime = st.st_atime; |
| 146 | msg.stat_v2.mtime = st.st_mtime; |
| 147 | msg.stat_v2.ctime = st.st_ctime; |
| 148 | } |
| 149 | |
| 150 | return WriteFdExactly(s, &msg.stat_v2, sizeof(msg.stat_v2)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 153 | static bool do_list(int s, const char* path) { |
| 154 | dirent* de; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 155 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 156 | syncmsg msg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 157 | msg.dent.id = ID_DENT; |
| 158 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 159 | std::unique_ptr<DIR, int(*)(DIR*)> d(opendir(path), closedir); |
| 160 | if (!d) goto done; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 161 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 162 | while ((de = readdir(d.get()))) { |
Josh Gao | fd12aaa | 2016-11-22 14:33:08 -0800 | [diff] [blame] | 163 | std::string filename(StringPrintf("%s/%s", path, de->d_name)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 164 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 165 | struct stat st; |
| 166 | if (lstat(filename.c_str(), &st) == 0) { |
| 167 | size_t d_name_length = strlen(de->d_name); |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 168 | msg.dent.mode = st.st_mode; |
| 169 | msg.dent.size = st.st_size; |
| 170 | msg.dent.time = st.st_mtime; |
| 171 | msg.dent.namelen = d_name_length; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 172 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 173 | if (!WriteFdExactly(s, &msg.dent, sizeof(msg.dent)) || |
| 174 | !WriteFdExactly(s, de->d_name, d_name_length)) { |
| 175 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 180 | done: |
| 181 | msg.dent.id = ID_DONE; |
| 182 | msg.dent.mode = 0; |
| 183 | msg.dent.size = 0; |
| 184 | msg.dent.time = 0; |
| 185 | msg.dent.namelen = 0; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 186 | return WriteFdExactly(s, &msg.dent, sizeof(msg.dent)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Josh Gao | 6ad06b9 | 2016-01-28 16:21:12 -0800 | [diff] [blame] | 189 | // Make sure that SendFail from adb_io.cpp isn't accidentally used in this file. |
| 190 | #pragma GCC poison SendFail |
| 191 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 192 | static bool SendSyncFail(int fd, const std::string& reason) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 193 | D("sync: failure: %s", reason.c_str()); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 194 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 195 | syncmsg msg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 196 | msg.data.id = ID_FAIL; |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 197 | msg.data.size = reason.size(); |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 198 | return WriteFdExactly(fd, &msg.data, sizeof(msg.data)) && WriteFdExactly(fd, reason); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 201 | static bool SendSyncFailErrno(int fd, const std::string& reason) { |
Josh Gao | fd12aaa | 2016-11-22 14:33:08 -0800 | [diff] [blame] | 202 | return SendSyncFail(fd, StringPrintf("%s: %s", reason.c_str(), strerror(errno))); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 205 | static bool handle_send_file(int s, const char* path, uid_t uid, gid_t gid, uint64_t capabilities, |
| 206 | mode_t mode, std::vector<char>& buffer, bool do_unlink) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 207 | syncmsg msg; |
| 208 | unsigned int timestamp = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 209 | |
Rubin Xu | d61a25c | 2016-01-11 10:23:47 +0000 | [diff] [blame] | 210 | __android_log_security_bswrite(SEC_TAG_ADB_SEND_FILE, path); |
| 211 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 212 | int fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode); |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 213 | |
| 214 | if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE | POSIX_FADV_WILLNEED) < |
| 215 | 0) { |
| 216 | D("[ Failed to fadvise: %d ]", errno); |
| 217 | } |
| 218 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 219 | if (fd < 0 && errno == ENOENT) { |
Colin Cross | 58021d1 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 220 | if (!secure_mkdirs(android::base::Dirname(path))) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 221 | SendSyncFailErrno(s, "secure_mkdirs failed"); |
| 222 | goto fail; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 223 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 224 | fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 225 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 226 | if (fd < 0 && errno == EEXIST) { |
Nick Kralevich | fe8d7f4 | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 227 | fd = adb_open_mode(path, O_WRONLY | O_CLOEXEC, mode); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 228 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 229 | if (fd < 0) { |
| 230 | SendSyncFailErrno(s, "couldn't create file"); |
| 231 | goto fail; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 232 | } else { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 233 | if (fchown(fd, uid, gid) == -1) { |
| 234 | SendSyncFailErrno(s, "fchown failed"); |
| 235 | goto fail; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 236 | } |
Nick Kralevich | 7291783 | 2014-01-17 16:16:42 -0800 | [diff] [blame] | 237 | |
Elliott Hughes | 3834440 | 2015-08-25 16:33:50 -0700 | [diff] [blame] | 238 | // Not all filesystems support setting SELinux labels. http://b/23530370. |
| 239 | selinux_android_restorecon(path, 0); |
Elliott Hughes | 0adc097 | 2015-08-25 13:14:07 -0700 | [diff] [blame] | 240 | |
| 241 | // fchown clears the setuid bit - restore it if present. |
| 242 | // Ignore the result of calling fchmod. It's not supported |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 243 | // by all filesystems, so we don't check for success. b/12441485 |
Nick Kralevich | 7291783 | 2014-01-17 16:16:42 -0800 | [diff] [blame] | 244 | fchmod(fd, mode); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 247 | while (true) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 248 | if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) goto fail; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 249 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 250 | if (msg.data.id != ID_DATA) { |
| 251 | if (msg.data.id == ID_DONE) { |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 252 | timestamp = msg.data.size; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 253 | break; |
| 254 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 255 | SendSyncFail(s, "invalid data message"); |
Josh Gao | 20a96c7 | 2016-02-19 14:33:14 -0800 | [diff] [blame] | 256 | goto abort; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 257 | } |
Josh Gao | 20a96c7 | 2016-02-19 14:33:14 -0800 | [diff] [blame] | 258 | |
| 259 | if (msg.data.size > buffer.size()) { // TODO: resize buffer? |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 260 | SendSyncFail(s, "oversize data message"); |
Josh Gao | 20a96c7 | 2016-02-19 14:33:14 -0800 | [diff] [blame] | 261 | goto abort; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 262 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 263 | |
Josh Gao | 20a96c7 | 2016-02-19 14:33:14 -0800 | [diff] [blame] | 264 | if (!ReadFdExactly(s, &buffer[0], msg.data.size)) goto abort; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 265 | |
Josh Gao | 20a96c7 | 2016-02-19 14:33:14 -0800 | [diff] [blame] | 266 | if (!WriteFdExactly(fd, &buffer[0], msg.data.size)) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 267 | SendSyncFailErrno(s, "write failed"); |
| 268 | goto fail; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 272 | adb_close(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 273 | |
Jin Qian | d0515e7 | 2016-11-02 16:27:17 -0700 | [diff] [blame] | 274 | if (!update_capabilities(path, capabilities)) { |
| 275 | SendSyncFailErrno(s, "update_capabilities failed"); |
| 276 | goto fail; |
| 277 | } |
| 278 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 279 | utimbuf u; |
| 280 | u.actime = timestamp; |
| 281 | u.modtime = timestamp; |
| 282 | utime(path, &u); |
| 283 | |
| 284 | msg.status.id = ID_OKAY; |
| 285 | msg.status.msglen = 0; |
| 286 | return WriteFdExactly(s, &msg.status, sizeof(msg.status)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 287 | |
| 288 | fail: |
Josh Gao | 20a96c7 | 2016-02-19 14:33:14 -0800 | [diff] [blame] | 289 | // If there's a problem on the device, we'll send an ID_FAIL message and |
| 290 | // close the socket. Unfortunately the kernel will sometimes throw that |
| 291 | // data away if the other end keeps writing without reading (which is |
| 292 | // the case with old versions of adb). To maintain compatibility, keep |
| 293 | // reading and throwing away ID_DATA packets until the other side notices |
| 294 | // that we've reported an error. |
| 295 | while (true) { |
Josh Gao | 1d6c01b | 2017-08-28 11:21:31 -0700 | [diff] [blame] | 296 | if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) break; |
Josh Gao | 20a96c7 | 2016-02-19 14:33:14 -0800 | [diff] [blame] | 297 | |
| 298 | if (msg.data.id == ID_DONE) { |
Josh Gao | 1d6c01b | 2017-08-28 11:21:31 -0700 | [diff] [blame] | 299 | break; |
Josh Gao | 20a96c7 | 2016-02-19 14:33:14 -0800 | [diff] [blame] | 300 | } else if (msg.data.id != ID_DATA) { |
| 301 | char id[5]; |
| 302 | memcpy(id, &msg.data.id, sizeof(msg.data.id)); |
| 303 | id[4] = '\0'; |
| 304 | D("handle_send_fail received unexpected id '%s' during failure", id); |
Josh Gao | 1d6c01b | 2017-08-28 11:21:31 -0700 | [diff] [blame] | 305 | break; |
Josh Gao | 20a96c7 | 2016-02-19 14:33:14 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | if (msg.data.size > buffer.size()) { |
| 309 | D("handle_send_fail received oversized packet of length '%u' during failure", |
| 310 | msg.data.size); |
Josh Gao | 1d6c01b | 2017-08-28 11:21:31 -0700 | [diff] [blame] | 311 | break; |
Josh Gao | 20a96c7 | 2016-02-19 14:33:14 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Josh Gao | 1d6c01b | 2017-08-28 11:21:31 -0700 | [diff] [blame] | 314 | if (!ReadFdExactly(s, &buffer[0], msg.data.size)) break; |
Josh Gao | 20a96c7 | 2016-02-19 14:33:14 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | abort: |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 318 | if (fd >= 0) adb_close(fd); |
JP Abgrall | 55b6c84 | 2014-03-07 17:31:25 -0800 | [diff] [blame] | 319 | if (do_unlink) adb_unlink(path); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 320 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 323 | #if defined(_WIN32) |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 324 | extern bool handle_send_link(int s, const std::string& path, std::vector<char>& buffer) __attribute__((error("no symlinks on Windows"))); |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 325 | #else |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 326 | static bool handle_send_link(int s, const std::string& path, std::vector<char>& buffer) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 327 | syncmsg msg; |
| 328 | unsigned int len; |
| 329 | int ret; |
| 330 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 331 | if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 332 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 333 | if (msg.data.id != ID_DATA) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 334 | SendSyncFail(s, "invalid data message: expected ID_DATA"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 335 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 338 | len = msg.data.size; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 339 | if (len > buffer.size()) { // TODO: resize buffer? |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 340 | SendSyncFail(s, "oversize data message"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 341 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 342 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 343 | if (!ReadFdExactly(s, &buffer[0], len)) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 344 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 345 | ret = symlink(&buffer[0], path.c_str()); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 346 | if (ret && errno == ENOENT) { |
Colin Cross | 58021d1 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 347 | if (!secure_mkdirs(android::base::Dirname(path))) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 348 | SendSyncFailErrno(s, "secure_mkdirs failed"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 349 | return false; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 350 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 351 | ret = symlink(&buffer[0], path.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 352 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 353 | if (ret) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 354 | SendSyncFailErrno(s, "symlink failed"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 355 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 358 | if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 359 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 360 | if (msg.data.id == ID_DONE) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 361 | msg.status.id = ID_OKAY; |
| 362 | msg.status.msglen = 0; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 363 | if (!WriteFdExactly(s, &msg.status, sizeof(msg.status))) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 364 | } else { |
Josh Gao | 6ad06b9 | 2016-01-28 16:21:12 -0800 | [diff] [blame] | 365 | SendSyncFail(s, "invalid data message: expected ID_DONE"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 366 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 369 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 370 | } |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 371 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 372 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 373 | static bool do_send(int s, const std::string& spec, std::vector<char>& buffer) { |
| 374 | // 'spec' is of the form "/some/path,0755". Break it up. |
| 375 | size_t comma = spec.find_last_of(','); |
| 376 | if (comma == std::string::npos) { |
Josh Gao | 6ad06b9 | 2016-01-28 16:21:12 -0800 | [diff] [blame] | 377 | SendSyncFail(s, "missing , in ID_SEND"); |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 378 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 381 | std::string path = spec.substr(0, comma); |
| 382 | |
| 383 | errno = 0; |
| 384 | mode_t mode = strtoul(spec.substr(comma + 1).c_str(), nullptr, 0); |
| 385 | if (errno != 0) { |
Josh Gao | 6ad06b9 | 2016-01-28 16:21:12 -0800 | [diff] [blame] | 386 | SendSyncFail(s, "bad mode"); |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 387 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 390 | // Don't delete files before copying if they are not "regular" or symlinks. |
| 391 | struct stat st; |
| 392 | bool do_unlink = (lstat(path.c_str(), &st) == -1) || S_ISREG(st.st_mode) || S_ISLNK(st.st_mode); |
| 393 | if (do_unlink) { |
| 394 | adb_unlink(path.c_str()); |
| 395 | } |
| 396 | |
| 397 | if (S_ISLNK(mode)) { |
| 398 | return handle_send_link(s, path.c_str(), buffer); |
| 399 | } |
| 400 | |
| 401 | // Copy user permission bits to "group" and "other" permissions. |
| 402 | mode &= 0777; |
| 403 | mode |= ((mode >> 3) & 0070); |
| 404 | mode |= ((mode >> 3) & 0007); |
| 405 | |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 406 | uid_t uid = -1; |
| 407 | gid_t gid = -1; |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 408 | uint64_t capabilities = 0; |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 409 | if (should_use_fs_config(path)) { |
Elliott Hughes | 7baecbe | 2015-08-25 11:09:04 -0700 | [diff] [blame] | 410 | unsigned int broken_api_hack = mode; |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 411 | fs_config(path.c_str(), 0, nullptr, &uid, &gid, &broken_api_hack, &capabilities); |
Elliott Hughes | 56bf309 | 2015-08-25 11:01:39 -0700 | [diff] [blame] | 412 | mode = broken_api_hack; |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 413 | } |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 414 | return handle_send_file(s, path.c_str(), uid, gid, capabilities, mode, buffer, do_unlink); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 417 | static bool do_recv(int s, const char* path, std::vector<char>& buffer) { |
Rubin Xu | d61a25c | 2016-01-11 10:23:47 +0000 | [diff] [blame] | 418 | __android_log_security_bswrite(SEC_TAG_ADB_RECV_FILE, path); |
| 419 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 420 | int fd = adb_open(path, O_RDONLY | O_CLOEXEC); |
| 421 | if (fd < 0) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 422 | SendSyncFailErrno(s, "open failed"); |
| 423 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 426 | if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE) < 0) { |
| 427 | D("[ Failed to fadvise: %d ]", errno); |
| 428 | } |
| 429 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 430 | syncmsg msg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 431 | msg.data.id = ID_DATA; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 432 | while (true) { |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 433 | int r = adb_read(fd, &buffer[0], buffer.size() - sizeof(msg.data)); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 434 | if (r <= 0) { |
| 435 | if (r == 0) break; |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 436 | SendSyncFailErrno(s, "read failed"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 437 | adb_close(fd); |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 438 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 439 | } |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 440 | msg.data.size = r; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 441 | if (!WriteFdExactly(s, &msg.data, sizeof(msg.data)) || !WriteFdExactly(s, &buffer[0], r)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 442 | adb_close(fd); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 443 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
| 447 | adb_close(fd); |
| 448 | |
| 449 | msg.data.id = ID_DONE; |
| 450 | msg.data.size = 0; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 451 | return WriteFdExactly(s, &msg.data, sizeof(msg.data)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Josh Gao | fd12aaa | 2016-11-22 14:33:08 -0800 | [diff] [blame] | 454 | static const char* sync_id_to_name(uint32_t id) { |
| 455 | switch (id) { |
| 456 | case ID_LSTAT_V1: |
| 457 | return "lstat_v1"; |
| 458 | case ID_LSTAT_V2: |
| 459 | return "lstat_v2"; |
| 460 | case ID_STAT_V2: |
| 461 | return "stat_v2"; |
| 462 | case ID_LIST: |
| 463 | return "list"; |
| 464 | case ID_SEND: |
| 465 | return "send"; |
| 466 | case ID_RECV: |
| 467 | return "recv"; |
| 468 | case ID_QUIT: |
| 469 | return "quit"; |
| 470 | default: |
| 471 | return "???"; |
| 472 | } |
| 473 | } |
| 474 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 475 | static bool handle_sync_command(int fd, std::vector<char>& buffer) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 476 | D("sync: waiting for request"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 477 | |
Josh Gao | fd12aaa | 2016-11-22 14:33:08 -0800 | [diff] [blame] | 478 | ATRACE_CALL(); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 479 | SyncRequest request; |
| 480 | if (!ReadFdExactly(fd, &request, sizeof(request))) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 481 | SendSyncFail(fd, "command read failure"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 482 | return false; |
| 483 | } |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 484 | size_t path_length = request.path_length; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 485 | if (path_length > 1024) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 486 | SendSyncFail(fd, "path too long"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 487 | return false; |
| 488 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 489 | char name[1025]; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 490 | if (!ReadFdExactly(fd, name, path_length)) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 491 | SendSyncFail(fd, "filename read failure"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 492 | return false; |
| 493 | } |
| 494 | name[path_length] = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 495 | |
Josh Gao | fd12aaa | 2016-11-22 14:33:08 -0800 | [diff] [blame] | 496 | std::string id_name = sync_id_to_name(request.id); |
| 497 | std::string trace_name = StringPrintf("%s(%s)", id_name.c_str(), name); |
| 498 | ATRACE_NAME(trace_name.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 499 | |
Josh Gao | fd12aaa | 2016-11-22 14:33:08 -0800 | [diff] [blame] | 500 | D("sync: %s('%s')", id_name.c_str(), name); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 501 | switch (request.id) { |
Josh Gao | 5a1e3fd | 2016-12-05 17:11:34 -0800 | [diff] [blame] | 502 | case ID_LSTAT_V1: |
| 503 | if (!do_lstat_v1(fd, name)) return false; |
| 504 | break; |
| 505 | case ID_LSTAT_V2: |
| 506 | case ID_STAT_V2: |
| 507 | if (!do_stat_v2(fd, request.id, name)) return false; |
| 508 | break; |
| 509 | case ID_LIST: |
| 510 | if (!do_list(fd, name)) return false; |
| 511 | break; |
| 512 | case ID_SEND: |
| 513 | if (!do_send(fd, name, buffer)) return false; |
| 514 | break; |
| 515 | case ID_RECV: |
| 516 | if (!do_recv(fd, name, buffer)) return false; |
| 517 | break; |
| 518 | case ID_QUIT: |
| 519 | return false; |
| 520 | default: |
Josh Gao | fd12aaa | 2016-11-22 14:33:08 -0800 | [diff] [blame] | 521 | SendSyncFail(fd, StringPrintf("unknown command %08x", request.id)); |
Josh Gao | 5a1e3fd | 2016-12-05 17:11:34 -0800 | [diff] [blame] | 522 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 525 | return true; |
| 526 | } |
| 527 | |
Josh Gao | 5a1e3fd | 2016-12-05 17:11:34 -0800 | [diff] [blame] | 528 | void file_sync_service(int fd, void*) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 529 | std::vector<char> buffer(SYNC_DATA_MAX); |
| 530 | |
| 531 | while (handle_sync_command(fd, buffer)) { |
| 532 | } |
| 533 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 534 | D("sync: done"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 535 | adb_close(fd); |
| 536 | } |