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 | |
Dan Albert | 3313426 | 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 | 7664901 | 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 | 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> |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 30 | #include <unistd.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 31 | #include <utime.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 | #include "adb.h" |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 34 | #include "adb_io.h" |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 35 | #include "private/android_filesystem_config.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 37 | #include <base/stringprintf.h> |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 38 | #include <base/strings.h> |
| 39 | |
| 40 | static bool should_use_fs_config(const std::string& path) { |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 41 | // TODO: use fs_config to configure permissions on /data. |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 42 | return android::base::StartsWith(path, "/system/") || |
| 43 | android::base::StartsWith(path, "/vendor/") || |
| 44 | android::base::StartsWith(path, "/oem/"); |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 47 | static bool secure_mkdirs(const std::string& path) { |
Nick Kralevich | 7291783 | 2014-01-17 16:16:42 -0800 | [diff] [blame] | 48 | uid_t uid = -1; |
| 49 | gid_t gid = -1; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 50 | unsigned int mode = 0775; |
| 51 | uint64_t cap = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 52 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 53 | if (path[0] != '/') return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 55 | 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 Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 65 | } |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 66 | if (adb_mkdir(partial_path.c_str(), mode) == -1) { |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 67 | if (errno != EEXIST) { |
| 68 | return false; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 69 | } |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 70 | } else { |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 71 | if (chown(partial_path.c_str(), uid, gid) == -1) { |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 72 | return false; |
| 73 | } |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 74 | selinux_android_restorecon(partial_path.c_str(), 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 76 | } |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 77 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 80 | static bool do_stat(int s, const char* path) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 81 | syncmsg msg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 82 | msg.stat.id = ID_STAT; |
| 83 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 84 | 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 Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame^] | 88 | msg.stat.mode = st.st_mode; |
| 89 | msg.stat.size = st.st_size; |
| 90 | msg.stat.time = st.st_mtime; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 91 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 92 | return WriteFdExactly(s, &msg.stat, sizeof(msg.stat)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 95 | static bool do_list(int s, const char* path) { |
| 96 | dirent* de; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 97 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 98 | syncmsg msg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 99 | msg.dent.id = ID_DENT; |
| 100 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 101 | std::unique_ptr<DIR, int(*)(DIR*)> d(opendir(path), closedir); |
| 102 | if (!d) goto done; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 103 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 104 | while ((de = readdir(d.get()))) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 105 | std::string filename(android::base::StringPrintf("%s/%s", path, de->d_name)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 106 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 107 | struct stat st; |
| 108 | if (lstat(filename.c_str(), &st) == 0) { |
| 109 | size_t d_name_length = strlen(de->d_name); |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame^] | 110 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 114 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 115 | if (!WriteFdExactly(s, &msg.dent, sizeof(msg.dent)) || |
| 116 | !WriteFdExactly(s, de->d_name, d_name_length)) { |
| 117 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 122 | done: |
| 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 Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 128 | return WriteFdExactly(s, &msg.dent, sizeof(msg.dent)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 131 | static bool fail_message(int s, const std::string& reason) { |
| 132 | D("sync: failure: %s\n", reason.c_str()); |
| 133 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 134 | syncmsg msg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 135 | msg.data.id = ID_FAIL; |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame^] | 136 | msg.data.size = reason.size(); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 137 | return WriteFdExactly(s, &msg.data, sizeof(msg.data)) && WriteFdExactly(s, reason); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 140 | // TODO: callers of this have already failed, and should probably ignore its |
| 141 | // return value (http://b/23437039). |
| 142 | static bool fail_errno(int s) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 143 | return fail_message(s, strerror(errno)); |
| 144 | } |
| 145 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 146 | static bool handle_send_file(int s, char *path, uid_t uid, |
| 147 | gid_t gid, 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] | 148 | syncmsg msg; |
| 149 | unsigned int timestamp = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 150 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 151 | int 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] | 152 | if(fd < 0 && errno == ENOENT) { |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 153 | if (!secure_mkdirs(path)) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 154 | if (fail_errno(s)) return false; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 155 | fd = -1; |
| 156 | } else { |
Nick Kralevich | fe8d7f4 | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 157 | fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode); |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 158 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | } |
| 160 | if(fd < 0 && errno == EEXIST) { |
Nick Kralevich | fe8d7f4 | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 161 | 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] | 162 | } |
| 163 | if(fd < 0) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 164 | if (fail_errno(s)) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 165 | fd = -1; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 166 | } else { |
| 167 | if(fchown(fd, uid, gid) != 0) { |
| 168 | fail_errno(s); |
| 169 | errno = 0; |
| 170 | } |
Nick Kralevich | 7291783 | 2014-01-17 16:16:42 -0800 | [diff] [blame] | 171 | |
| 172 | /* |
| 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 |
| 176 | */ |
| 177 | fchmod(fd, mode); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 180 | while (true) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 181 | unsigned int len; |
| 182 | |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 183 | if(!ReadFdExactly(s, &msg.data, sizeof(msg.data))) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 184 | goto fail; |
| 185 | |
| 186 | if(msg.data.id != ID_DATA) { |
| 187 | if(msg.data.id == ID_DONE) { |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame^] | 188 | timestamp = msg.data.size; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | break; |
| 190 | } |
| 191 | fail_message(s, "invalid data message"); |
| 192 | goto fail; |
| 193 | } |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame^] | 194 | len = msg.data.size; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 195 | if (len > buffer.size()) { // TODO: resize buffer? |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 196 | fail_message(s, "oversize data message"); |
| 197 | goto fail; |
| 198 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 199 | if (!ReadFdExactly(s, &buffer[0], len)) goto fail; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 200 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 201 | if (fd < 0) continue; |
| 202 | |
| 203 | if (!WriteFdExactly(fd, &buffer[0], len)) { |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 204 | int saved_errno = errno; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 205 | adb_close(fd); |
JP Abgrall | 55b6c84 | 2014-03-07 17:31:25 -0800 | [diff] [blame] | 206 | if (do_unlink) adb_unlink(path); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 207 | fd = -1; |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 208 | errno = saved_errno; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 209 | if (fail_errno(s)) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | if(fd >= 0) { |
| 214 | struct utimbuf u; |
| 215 | adb_close(fd); |
Stephen Smalley | 27a9365 | 2014-02-07 09:14:13 -0500 | [diff] [blame] | 216 | selinux_android_restorecon(path, 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 217 | u.actime = timestamp; |
| 218 | u.modtime = timestamp; |
| 219 | utime(path, &u); |
| 220 | |
| 221 | msg.status.id = ID_OKAY; |
| 222 | msg.status.msglen = 0; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 223 | 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] | 224 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 225 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 226 | |
| 227 | fail: |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 228 | if (fd >= 0) adb_close(fd); |
JP Abgrall | 55b6c84 | 2014-03-07 17:31:25 -0800 | [diff] [blame] | 229 | if (do_unlink) adb_unlink(path); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 230 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 233 | #if defined(_WIN32) |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 234 | extern bool handle_send_link(int s, char *path, std::vector<char>& buffer) __attribute__((error("no symlinks on Windows"))); |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 235 | #else |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 236 | static bool handle_send_link(int s, char *path, std::vector<char>& buffer) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 237 | syncmsg msg; |
| 238 | unsigned int len; |
| 239 | int ret; |
| 240 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 241 | 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] | 242 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 243 | if (msg.data.id != ID_DATA) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 244 | fail_message(s, "invalid data message: expected ID_DATA"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 245 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame^] | 248 | len = msg.data.size; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 249 | if (len > buffer.size()) { // TODO: resize buffer? |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 250 | fail_message(s, "oversize data message"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 251 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 252 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 253 | if (!ReadFdExactly(s, &buffer[0], len)) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 254 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 255 | ret = symlink(&buffer[0], path); |
| 256 | if (ret && errno == ENOENT) { |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 257 | if (!secure_mkdirs(path)) { |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 258 | fail_errno(s); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 259 | return false; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 260 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 261 | ret = symlink(&buffer[0], path); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 262 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 263 | if (ret) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 264 | fail_errno(s); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 265 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 268 | 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] | 269 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 270 | if (msg.data.id == ID_DONE) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 271 | msg.status.id = ID_OKAY; |
| 272 | msg.status.msglen = 0; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 273 | 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] | 274 | } else { |
| 275 | fail_message(s, "invalid data message: expected ID_DONE"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 276 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 279 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 280 | } |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 281 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 282 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 283 | static bool do_send(int s, char* path, std::vector<char>& buffer) { |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 284 | unsigned int mode; |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 285 | bool is_link = false; |
JP Abgrall | 55b6c84 | 2014-03-07 17:31:25 -0800 | [diff] [blame] | 286 | bool do_unlink; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 287 | |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 288 | char* tmp = strrchr(path,','); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 289 | if(tmp) { |
| 290 | *tmp = 0; |
| 291 | errno = 0; |
| 292 | mode = strtoul(tmp + 1, NULL, 0); |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 293 | is_link = S_ISLNK((mode_t) mode); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 294 | mode &= 0777; |
| 295 | } |
| 296 | if(!tmp || errno) { |
| 297 | mode = 0644; |
| 298 | is_link = 0; |
JP Abgrall | 55b6c84 | 2014-03-07 17:31:25 -0800 | [diff] [blame] | 299 | do_unlink = true; |
JP Abgrall | 4ce2f83 | 2013-12-03 14:52:39 -0800 | [diff] [blame] | 300 | } else { |
| 301 | struct stat st; |
| 302 | /* Don't delete files before copying if they are not "regular" */ |
JP Abgrall | 55b6c84 | 2014-03-07 17:31:25 -0800 | [diff] [blame] | 303 | do_unlink = lstat(path, &st) || S_ISREG(st.st_mode) || S_ISLNK(st.st_mode); |
| 304 | if (do_unlink) { |
JP Abgrall | 4ce2f83 | 2013-12-03 14:52:39 -0800 | [diff] [blame] | 305 | adb_unlink(path); |
| 306 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 309 | if (is_link) { |
| 310 | return handle_send_link(s, path, buffer); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 313 | uid_t uid = -1; |
| 314 | gid_t gid = -1; |
| 315 | uint64_t cap = 0; |
| 316 | |
| 317 | /* copy user permission bits to "group" and "other" permissions */ |
| 318 | mode |= ((mode >> 3) & 0070); |
| 319 | mode |= ((mode >> 3) & 0007); |
| 320 | |
| 321 | tmp = path; |
| 322 | if(*tmp == '/') { |
| 323 | tmp++; |
| 324 | } |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 325 | if (should_use_fs_config(path)) { |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 326 | fs_config(tmp, 0, &uid, &gid, &mode, &cap); |
| 327 | } |
| 328 | return handle_send_file(s, path, uid, gid, mode, buffer, do_unlink); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 331 | static bool do_recv(int s, const char* path, std::vector<char>& buffer) { |
| 332 | int fd = adb_open(path, O_RDONLY | O_CLOEXEC); |
| 333 | if (fd < 0) { |
| 334 | if (fail_errno(s)) return false; |
| 335 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 338 | syncmsg msg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 339 | msg.data.id = ID_DATA; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 340 | while (true) { |
| 341 | int r = adb_read(fd, &buffer[0], buffer.size()); |
| 342 | if (r <= 0) { |
| 343 | if (r == 0) break; |
| 344 | if (errno == EINTR) continue; |
| 345 | bool status = fail_errno(s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 346 | adb_close(fd); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 347 | return status; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 348 | } |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame^] | 349 | msg.data.size = r; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 350 | 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] | 351 | adb_close(fd); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 352 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
| 356 | adb_close(fd); |
| 357 | |
| 358 | msg.data.id = ID_DONE; |
| 359 | msg.data.size = 0; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 360 | return WriteFdExactly(s, &msg.data, sizeof(msg.data)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 361 | } |
| 362 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 363 | static bool handle_sync_command(int fd, std::vector<char>& buffer) { |
| 364 | D("sync: waiting for request\n"); |
| 365 | |
| 366 | SyncRequest request; |
| 367 | if (!ReadFdExactly(fd, &request, sizeof(request))) { |
| 368 | fail_message(fd, "command read failure"); |
| 369 | return false; |
| 370 | } |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame^] | 371 | size_t path_length = request.path_length; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 372 | if (path_length > 1024) { |
| 373 | fail_message(fd, "path too long"); |
| 374 | return false; |
| 375 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 376 | char name[1025]; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 377 | if (!ReadFdExactly(fd, name, path_length)) { |
| 378 | fail_message(fd, "filename read failure"); |
| 379 | return false; |
| 380 | } |
| 381 | name[path_length] = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 382 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 383 | const char* id = reinterpret_cast<const char*>(&request.id); |
| 384 | D("sync: '%.4s' '%s'\n", id, name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 385 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 386 | switch (request.id) { |
| 387 | case ID_STAT: |
| 388 | if (!do_stat(fd, name)) return false; |
| 389 | break; |
| 390 | case ID_LIST: |
| 391 | if (!do_list(fd, name)) return false; |
| 392 | break; |
| 393 | case ID_SEND: |
| 394 | if (!do_send(fd, name, buffer)) return false; |
| 395 | break; |
| 396 | case ID_RECV: |
| 397 | if (!do_recv(fd, name, buffer)) return false; |
| 398 | break; |
| 399 | case ID_QUIT: |
| 400 | return false; |
| 401 | default: |
| 402 | fail_message(fd, android::base::StringPrintf("unknown command '%.4s' (%08x)", |
| 403 | id, request.id)); |
| 404 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 407 | return true; |
| 408 | } |
| 409 | |
| 410 | void file_sync_service(int fd, void* cookie) { |
| 411 | std::vector<char> buffer(SYNC_DATA_MAX); |
| 412 | |
| 413 | while (handle_sync_command(fd, buffer)) { |
| 414 | } |
| 415 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 416 | D("sync: done\n"); |
| 417 | adb_close(fd); |
| 418 | } |