OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_ANDROID) | 6 #if defined(TARGET_OS_ANDROID) |
7 | 7 |
8 #include "bin/file.h" | 8 #include "bin/file.h" |
9 | 9 |
10 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
11 #include <fcntl.h> // NOLINT | 11 #include <fcntl.h> // NOLINT |
12 #include <sys/stat.h> // NOLINT | 12 #include <sys/stat.h> // NOLINT |
13 #include <sys/types.h> // NOLINT | 13 #include <sys/types.h> // NOLINT |
| 14 #include <sys/sendfile.h> // NOLINT |
14 #include <unistd.h> // NOLINT | 15 #include <unistd.h> // NOLINT |
15 #include <libgen.h> // NOLINT | 16 #include <libgen.h> // NOLINT |
16 | 17 |
17 #include "bin/builtin.h" | 18 #include "bin/builtin.h" |
18 #include "bin/log.h" | 19 #include "bin/log.h" |
19 | 20 |
20 | 21 |
21 namespace dart { | 22 namespace dart { |
22 namespace bin { | 23 namespace bin { |
23 | 24 |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 return TEMP_FAILURE_RETRY(rename(old_path, new_path)) == 0; | 213 return TEMP_FAILURE_RETRY(rename(old_path, new_path)) == 0; |
213 } else if (type == kIsDirectory) { | 214 } else if (type == kIsDirectory) { |
214 errno = EISDIR; | 215 errno = EISDIR; |
215 } else { | 216 } else { |
216 errno = EINVAL; | 217 errno = EINVAL; |
217 } | 218 } |
218 return false; | 219 return false; |
219 } | 220 } |
220 | 221 |
221 | 222 |
| 223 bool File::Copy(const char* old_path, const char* new_path) { |
| 224 File::Type type = File::GetType(old_path, true); |
| 225 if (type == kIsFile) { |
| 226 struct stat st; |
| 227 if (TEMP_FAILURE_RETRY(stat(old_path, &st)) != 0) { |
| 228 return false; |
| 229 } |
| 230 int old_fd = TEMP_FAILURE_RETRY(open(old_path, O_RDONLY | O_CLOEXEC)); |
| 231 if (old_fd < 0) { |
| 232 return false; |
| 233 } |
| 234 int new_fd = TEMP_FAILURE_RETRY( |
| 235 open(new_path, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, st.st_mode)); |
| 236 if (new_fd < 0) { |
| 237 VOID_TEMP_FAILURE_RETRY(close(old_fd)); |
| 238 return false; |
| 239 } |
| 240 off64_t offset = 0; |
| 241 int bytes = 1; |
| 242 while (bytes > 0) { |
| 243 // Loop to ensure we copy everything, and not only up to 2GB. |
| 244 bytes = TEMP_FAILURE_RETRY(sendfile(new_fd, old_fd, &offset, -1)); |
| 245 } |
| 246 if (bytes < 0) { |
| 247 int e = errno; |
| 248 VOID_TEMP_FAILURE_RETRY(close(old_fd)); |
| 249 VOID_TEMP_FAILURE_RETRY(close(new_fd)); |
| 250 VOID_TEMP_FAILURE_RETRY(unlink(new_path)); |
| 251 errno = e; |
| 252 return false; |
| 253 } |
| 254 return true; |
| 255 } else if (type == kIsDirectory) { |
| 256 errno = EISDIR; |
| 257 } else { |
| 258 errno = ENOENT; |
| 259 } |
| 260 return false; |
| 261 } |
| 262 |
| 263 |
222 off64_t File::LengthFromPath(const char* name) { | 264 off64_t File::LengthFromPath(const char* name) { |
223 struct stat st; | 265 struct stat st; |
224 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { | 266 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { |
225 return st.st_size; | 267 return st.st_size; |
226 } | 268 } |
227 return -1; | 269 return -1; |
228 } | 270 } |
229 | 271 |
230 | 272 |
231 void File::Stat(const char* name, int64_t* data) { | 273 void File::Stat(const char* name, int64_t* data) { |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 return (file_1_info.st_ino == file_2_info.st_ino && | 399 return (file_1_info.st_ino == file_2_info.st_ino && |
358 file_1_info.st_dev == file_2_info.st_dev) ? | 400 file_1_info.st_dev == file_2_info.st_dev) ? |
359 File::kIdentical : | 401 File::kIdentical : |
360 File::kDifferent; | 402 File::kDifferent; |
361 } | 403 } |
362 | 404 |
363 } // namespace bin | 405 } // namespace bin |
364 } // namespace dart | 406 } // namespace dart |
365 | 407 |
366 #endif // defined(TARGET_OS_ANDROID) | 408 #endif // defined(TARGET_OS_ANDROID) |
OLD | NEW |