The Android Open Source Project | 9ca14dc | 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 | |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 17 | #define TRACE_TAG TRACE_SYNC |
| 18 | |
| 19 | #include "sysdeps.h" |
| 20 | #include "file_sync_service.h" |
| 21 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 22 | #include <dirent.h> |
| 23 | #include <errno.h> |
| 24 | #include <selinux/android.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 26 | #include <stdlib.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | #include <string.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
Liang Cheng | 155c49a | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 30 | #include <unistd.h> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 31 | #include <utime.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 | #include "adb.h" |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 34 | #include "adb_io.h" |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 35 | #include "private/android_filesystem_config.h" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 37 | #include <base/strings.h> |
| 38 | |
| 39 | static bool should_use_fs_config(const std::string& path) { |
Elliott Hughes | dedf767 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 40 | // TODO: use fs_config to configure permissions on /data. |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 41 | return android::base::StartsWith(path, "/system/") || |
| 42 | android::base::StartsWith(path, "/vendor/") || |
| 43 | android::base::StartsWith(path, "/oem/"); |
Daniel Rosenberg | e9a1c9c | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 46 | static bool secure_mkdirs(const std::string& path) { |
Nick Kralevich | 29e7813 | 2014-01-17 16:16:42 -0800 | [diff] [blame] | 47 | uid_t uid = -1; |
| 48 | gid_t gid = -1; |
Liang Cheng | 155c49a | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 49 | unsigned int mode = 0775; |
| 50 | uint64_t cap = 0; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 52 | if (path[0] != '/') return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 53 | |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 54 | std::vector<std::string> path_components = android::base::Split(path, "/"); |
| 55 | path_components.pop_back(); // For "/system/bin/sh", only create "/system/bin". |
| 56 | |
| 57 | std::string partial_path; |
| 58 | for (auto& path_component : path_components) { |
| 59 | if (partial_path.back() != OS_PATH_SEPARATOR) partial_path += OS_PATH_SEPARATOR; |
| 60 | partial_path += path_component; |
| 61 | |
| 62 | if (should_use_fs_config(partial_path)) { |
| 63 | fs_config(partial_path.c_str(), 1, &uid, &gid, &mode, &cap); |
Liang Cheng | 155c49a | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 64 | } |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 65 | if (adb_mkdir(partial_path.c_str(), mode) == -1) { |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 66 | if (errno != EEXIST) { |
| 67 | return false; |
Liang Cheng | 155c49a | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 68 | } |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 69 | } else { |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 70 | if (chown(partial_path.c_str(), uid, gid) == -1) { |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 71 | return false; |
| 72 | } |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 73 | selinux_android_restorecon(partial_path.c_str(), 0); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 74 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | } |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 76 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static int do_stat(int s, const char *path) |
| 80 | { |
| 81 | syncmsg msg; |
| 82 | struct stat st; |
| 83 | |
| 84 | msg.stat.id = ID_STAT; |
| 85 | |
| 86 | if(lstat(path, &st)) { |
| 87 | msg.stat.mode = 0; |
| 88 | msg.stat.size = 0; |
| 89 | msg.stat.time = 0; |
| 90 | } else { |
| 91 | msg.stat.mode = htoll(st.st_mode); |
| 92 | msg.stat.size = htoll(st.st_size); |
| 93 | msg.stat.time = htoll(st.st_mtime); |
| 94 | } |
| 95 | |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 96 | return WriteFdExactly(s, &msg.stat, sizeof(msg.stat)) ? 0 : -1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static int do_list(int s, const char *path) |
| 100 | { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 101 | struct dirent *de; |
| 102 | struct stat st; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 103 | |
| 104 | char tmp[1024 + 256 + 1]; |
| 105 | char *fname; |
| 106 | |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 107 | size_t len = strlen(path); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 108 | memcpy(tmp, path, len); |
| 109 | tmp[len] = '/'; |
| 110 | fname = tmp + len + 1; |
| 111 | |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 112 | syncmsg msg; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 113 | msg.dent.id = ID_DENT; |
| 114 | |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 115 | std::unique_ptr<DIR, int(*)(DIR*)> d(opendir(path), closedir); |
| 116 | if (!d) goto done; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 117 | |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 118 | while ((de = readdir(d.get()))) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 119 | int len = strlen(de->d_name); |
| 120 | |
| 121 | /* not supposed to be possible, but |
| 122 | if it does happen, let's not buffer overrun */ |
| 123 | if(len > 256) continue; |
| 124 | |
| 125 | strcpy(fname, de->d_name); |
| 126 | if(lstat(tmp, &st) == 0) { |
| 127 | msg.dent.mode = htoll(st.st_mode); |
| 128 | msg.dent.size = htoll(st.st_size); |
| 129 | msg.dent.time = htoll(st.st_mtime); |
| 130 | msg.dent.namelen = htoll(len); |
| 131 | |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 132 | if(!WriteFdExactly(s, &msg.dent, sizeof(msg.dent)) || |
| 133 | !WriteFdExactly(s, de->d_name, len)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 134 | return -1; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 139 | done: |
| 140 | msg.dent.id = ID_DONE; |
| 141 | msg.dent.mode = 0; |
| 142 | msg.dent.size = 0; |
| 143 | msg.dent.time = 0; |
| 144 | msg.dent.namelen = 0; |
Dan Albert | 022f493 | 2015-03-09 13:22:02 -0700 | [diff] [blame] | 145 | return WriteFdExactly(s, &msg.dent, sizeof(msg.dent)) ? 0 : -1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static int fail_message(int s, const char *reason) |
| 149 | { |
| 150 | syncmsg msg; |
| 151 | int len = strlen(reason); |
| 152 | |
| 153 | D("sync: failure: %s\n", reason); |
| 154 | |
| 155 | msg.data.id = ID_FAIL; |
| 156 | msg.data.size = htoll(len); |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 157 | if(!WriteFdExactly(s, &msg.data, sizeof(msg.data)) || |
| 158 | !WriteFdExactly(s, reason, len)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | return -1; |
| 160 | } else { |
| 161 | return 0; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static int fail_errno(int s) |
| 166 | { |
| 167 | return fail_message(s, strerror(errno)); |
| 168 | } |
| 169 | |
Nick Kralevich | 29e7813 | 2014-01-17 16:16:42 -0800 | [diff] [blame] | 170 | static int handle_send_file(int s, char *path, uid_t uid, |
JP Abgrall | bb5f0d2 | 2014-03-07 17:31:25 -0800 | [diff] [blame] | 171 | gid_t gid, mode_t mode, char *buffer, bool do_unlink) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 172 | { |
| 173 | syncmsg msg; |
| 174 | unsigned int timestamp = 0; |
| 175 | int fd; |
| 176 | |
Nick Kralevich | 777523e | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 177 | fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 178 | if(fd < 0 && errno == ENOENT) { |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 179 | if (!secure_mkdirs(path)) { |
Liang Cheng | 155c49a | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 180 | if(fail_errno(s)) |
| 181 | return -1; |
| 182 | fd = -1; |
| 183 | } else { |
Nick Kralevich | 777523e | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 184 | fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode); |
Liang Cheng | 155c49a | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 185 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 186 | } |
| 187 | if(fd < 0 && errno == EEXIST) { |
Nick Kralevich | 777523e | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 188 | fd = adb_open_mode(path, O_WRONLY | O_CLOEXEC, mode); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | } |
| 190 | if(fd < 0) { |
| 191 | if(fail_errno(s)) |
| 192 | return -1; |
| 193 | fd = -1; |
Liang Cheng | 155c49a | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 194 | } else { |
| 195 | if(fchown(fd, uid, gid) != 0) { |
| 196 | fail_errno(s); |
| 197 | errno = 0; |
| 198 | } |
Nick Kralevich | 29e7813 | 2014-01-17 16:16:42 -0800 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * fchown clears the setuid bit - restore it if present. |
| 202 | * Ignore the result of calling fchmod. It's not supported |
| 203 | * by all filesystems. b/12441485 |
| 204 | */ |
| 205 | fchmod(fd, mode); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | for(;;) { |
| 209 | unsigned int len; |
| 210 | |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 211 | if(!ReadFdExactly(s, &msg.data, sizeof(msg.data))) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 212 | goto fail; |
| 213 | |
| 214 | if(msg.data.id != ID_DATA) { |
| 215 | if(msg.data.id == ID_DONE) { |
| 216 | timestamp = ltohl(msg.data.size); |
| 217 | break; |
| 218 | } |
| 219 | fail_message(s, "invalid data message"); |
| 220 | goto fail; |
| 221 | } |
| 222 | len = ltohl(msg.data.size); |
| 223 | if(len > SYNC_DATA_MAX) { |
| 224 | fail_message(s, "oversize data message"); |
| 225 | goto fail; |
| 226 | } |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 227 | if(!ReadFdExactly(s, buffer, len)) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 228 | goto fail; |
| 229 | |
| 230 | if(fd < 0) |
| 231 | continue; |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 232 | if(!WriteFdExactly(fd, buffer, len)) { |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 233 | int saved_errno = errno; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 234 | adb_close(fd); |
JP Abgrall | bb5f0d2 | 2014-03-07 17:31:25 -0800 | [diff] [blame] | 235 | if (do_unlink) adb_unlink(path); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 236 | fd = -1; |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 237 | errno = saved_errno; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 238 | if(fail_errno(s)) return -1; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | if(fd >= 0) { |
| 243 | struct utimbuf u; |
| 244 | adb_close(fd); |
Stephen Smalley | 3392e9c | 2014-02-07 09:14:13 -0500 | [diff] [blame] | 245 | selinux_android_restorecon(path, 0); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 246 | u.actime = timestamp; |
| 247 | u.modtime = timestamp; |
| 248 | utime(path, &u); |
| 249 | |
| 250 | msg.status.id = ID_OKAY; |
| 251 | msg.status.msglen = 0; |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 252 | if(!WriteFdExactly(s, &msg.status, sizeof(msg.status))) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 253 | return -1; |
| 254 | } |
| 255 | return 0; |
| 256 | |
| 257 | fail: |
| 258 | if(fd >= 0) |
| 259 | adb_close(fd); |
JP Abgrall | bb5f0d2 | 2014-03-07 17:31:25 -0800 | [diff] [blame] | 260 | if (do_unlink) adb_unlink(path); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 261 | return -1; |
| 262 | } |
| 263 | |
Elliott Hughes | 944e1d7 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 264 | #if defined(_WIN32) |
| 265 | extern int handle_send_link(int s, char *path, char *buffer) __attribute__((error("no symlinks on Windows"))); |
| 266 | #else |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 267 | static int handle_send_link(int s, char *path, char *buffer) |
| 268 | { |
| 269 | syncmsg msg; |
| 270 | unsigned int len; |
| 271 | int ret; |
| 272 | |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 273 | if(!ReadFdExactly(s, &msg.data, sizeof(msg.data))) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 274 | return -1; |
| 275 | |
| 276 | if(msg.data.id != ID_DATA) { |
| 277 | fail_message(s, "invalid data message: expected ID_DATA"); |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | len = ltohl(msg.data.size); |
| 282 | if(len > SYNC_DATA_MAX) { |
| 283 | fail_message(s, "oversize data message"); |
| 284 | return -1; |
| 285 | } |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 286 | if(!ReadFdExactly(s, buffer, len)) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 287 | return -1; |
| 288 | |
| 289 | ret = symlink(buffer, path); |
| 290 | if(ret && errno == ENOENT) { |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 291 | if (!secure_mkdirs(path)) { |
Liang Cheng | 155c49a | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 292 | fail_errno(s); |
| 293 | return -1; |
| 294 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 295 | ret = symlink(buffer, path); |
| 296 | } |
| 297 | if(ret) { |
| 298 | fail_errno(s); |
| 299 | return -1; |
| 300 | } |
| 301 | |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 302 | if(!ReadFdExactly(s, &msg.data, sizeof(msg.data))) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 303 | return -1; |
| 304 | |
| 305 | if(msg.data.id == ID_DONE) { |
| 306 | msg.status.id = ID_OKAY; |
| 307 | msg.status.msglen = 0; |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 308 | if(!WriteFdExactly(s, &msg.status, sizeof(msg.status))) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 309 | return -1; |
| 310 | } else { |
| 311 | fail_message(s, "invalid data message: expected ID_DONE"); |
| 312 | return -1; |
| 313 | } |
| 314 | |
| 315 | return 0; |
| 316 | } |
Elliott Hughes | 944e1d7 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 317 | #endif |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 318 | |
| 319 | static int do_send(int s, char *path, char *buffer) |
| 320 | { |
Liang Cheng | 155c49a | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 321 | unsigned int mode; |
Elliott Hughes | 944e1d7 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 322 | bool is_link = false; |
JP Abgrall | bb5f0d2 | 2014-03-07 17:31:25 -0800 | [diff] [blame] | 323 | bool do_unlink; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 324 | |
Elliott Hughes | 944e1d7 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 325 | char* tmp = strrchr(path,','); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 326 | if(tmp) { |
| 327 | *tmp = 0; |
| 328 | errno = 0; |
| 329 | mode = strtoul(tmp + 1, NULL, 0); |
Liang Cheng | 155c49a | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 330 | is_link = S_ISLNK((mode_t) mode); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 331 | mode &= 0777; |
| 332 | } |
| 333 | if(!tmp || errno) { |
| 334 | mode = 0644; |
| 335 | is_link = 0; |
JP Abgrall | bb5f0d2 | 2014-03-07 17:31:25 -0800 | [diff] [blame] | 336 | do_unlink = true; |
JP Abgrall | 43874fd | 2013-12-03 14:52:39 -0800 | [diff] [blame] | 337 | } else { |
| 338 | struct stat st; |
| 339 | /* Don't delete files before copying if they are not "regular" */ |
JP Abgrall | bb5f0d2 | 2014-03-07 17:31:25 -0800 | [diff] [blame] | 340 | do_unlink = lstat(path, &st) || S_ISREG(st.st_mode) || S_ISLNK(st.st_mode); |
| 341 | if (do_unlink) { |
JP Abgrall | 43874fd | 2013-12-03 14:52:39 -0800 | [diff] [blame] | 342 | adb_unlink(path); |
| 343 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Elliott Hughes | 944e1d7 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 346 | if (is_link) { |
| 347 | return handle_send_link(s, path, buffer); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Elliott Hughes | 944e1d7 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 350 | uid_t uid = -1; |
| 351 | gid_t gid = -1; |
| 352 | uint64_t cap = 0; |
| 353 | |
| 354 | /* copy user permission bits to "group" and "other" permissions */ |
| 355 | mode |= ((mode >> 3) & 0070); |
| 356 | mode |= ((mode >> 3) & 0007); |
| 357 | |
| 358 | tmp = path; |
| 359 | if(*tmp == '/') { |
| 360 | tmp++; |
| 361 | } |
Elliott Hughes | dedf767 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 362 | if (should_use_fs_config(path)) { |
Elliott Hughes | 944e1d7 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 363 | fs_config(tmp, 0, &uid, &gid, &mode, &cap); |
| 364 | } |
| 365 | return handle_send_file(s, path, uid, gid, mode, buffer, do_unlink); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | static int do_recv(int s, const char *path, char *buffer) |
| 369 | { |
| 370 | syncmsg msg; |
| 371 | int fd, r; |
| 372 | |
Nick Kralevich | 777523e | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 373 | fd = adb_open(path, O_RDONLY | O_CLOEXEC); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 374 | if(fd < 0) { |
| 375 | if(fail_errno(s)) return -1; |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | msg.data.id = ID_DATA; |
| 380 | for(;;) { |
| 381 | r = adb_read(fd, buffer, SYNC_DATA_MAX); |
| 382 | if(r <= 0) { |
| 383 | if(r == 0) break; |
| 384 | if(errno == EINTR) continue; |
| 385 | r = fail_errno(s); |
| 386 | adb_close(fd); |
| 387 | return r; |
| 388 | } |
| 389 | msg.data.size = htoll(r); |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 390 | if(!WriteFdExactly(s, &msg.data, sizeof(msg.data)) || |
| 391 | !WriteFdExactly(s, buffer, r)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 392 | adb_close(fd); |
| 393 | return -1; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | adb_close(fd); |
| 398 | |
| 399 | msg.data.id = ID_DONE; |
| 400 | msg.data.size = 0; |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 401 | if(!WriteFdExactly(s, &msg.data, sizeof(msg.data))) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 402 | return -1; |
| 403 | } |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | void file_sync_service(int fd, void *cookie) |
| 409 | { |
| 410 | syncmsg msg; |
| 411 | char name[1025]; |
| 412 | unsigned namelen; |
| 413 | |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 414 | char *buffer = reinterpret_cast<char*>(malloc(SYNC_DATA_MAX)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 415 | if(buffer == 0) goto fail; |
| 416 | |
| 417 | for(;;) { |
| 418 | D("sync: waiting for command\n"); |
| 419 | |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 420 | if(!ReadFdExactly(fd, &msg.req, sizeof(msg.req))) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 421 | fail_message(fd, "command read failure"); |
| 422 | break; |
| 423 | } |
| 424 | namelen = ltohl(msg.req.namelen); |
| 425 | if(namelen > 1024) { |
| 426 | fail_message(fd, "invalid namelen"); |
| 427 | break; |
| 428 | } |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 429 | if(!ReadFdExactly(fd, name, namelen)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 430 | fail_message(fd, "filename read failure"); |
| 431 | break; |
| 432 | } |
| 433 | name[namelen] = 0; |
| 434 | |
| 435 | msg.req.namelen = 0; |
| 436 | D("sync: '%s' '%s'\n", (char*) &msg.req, name); |
| 437 | |
| 438 | switch(msg.req.id) { |
| 439 | case ID_STAT: |
| 440 | if(do_stat(fd, name)) goto fail; |
| 441 | break; |
| 442 | case ID_LIST: |
| 443 | if(do_list(fd, name)) goto fail; |
| 444 | break; |
| 445 | case ID_SEND: |
| 446 | if(do_send(fd, name, buffer)) goto fail; |
| 447 | break; |
| 448 | case ID_RECV: |
| 449 | if(do_recv(fd, name, buffer)) goto fail; |
| 450 | break; |
| 451 | case ID_QUIT: |
| 452 | goto fail; |
| 453 | default: |
| 454 | fail_message(fd, "unknown command"); |
| 455 | goto fail; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | fail: |
| 460 | if(buffer != 0) free(buffer); |
| 461 | D("sync: done\n"); |
| 462 | adb_close(fd); |
| 463 | } |