Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(905)

Side by Side Diff: runtime/bin/file_fuchsia.cc

Issue 2591523002: Fuchsia: Adds File::Copy() (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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_FUCHSIA) 6 #if defined(TARGET_OS_FUCHSIA)
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 <libgen.h> // NOLINT 12 #include <libgen.h> // NOLINT
13 #include <sys/mman.h> // NOLINT 13 #include <sys/mman.h> // NOLINT
14 #include <sys/stat.h> // NOLINT 14 #include <sys/stat.h> // NOLINT
15 #include <sys/types.h> // NOLINT 15 #include <sys/types.h> // NOLINT
16 #include <unistd.h> // NOLINT 16 #include <unistd.h> // NOLINT
17 17
18 #include "bin/builtin.h" 18 #include "bin/builtin.h"
19 #include "bin/fdutils.h"
19 #include "bin/log.h" 20 #include "bin/log.h"
20 #include "platform/signal_blocker.h" 21 #include "platform/signal_blocker.h"
21 #include "platform/utils.h" 22 #include "platform/utils.h"
22 23
23 namespace dart { 24 namespace dart {
24 namespace bin { 25 namespace bin {
25 26
26 class FileHandle { 27 class FileHandle {
27 public: 28 public:
28 explicit FileHandle(int fd) : fd_(fd) {} 29 explicit FileHandle(int fd) : fd_(fd) {}
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 } else if (type == kIsDirectory) { 274 } else if (type == kIsDirectory) {
274 errno = EISDIR; 275 errno = EISDIR;
275 } else { 276 } else {
276 errno = EINVAL; 277 errno = EINVAL;
277 } 278 }
278 return false; 279 return false;
279 } 280 }
280 281
281 282
282 bool File::Copy(const char* old_path, const char* new_path) { 283 bool File::Copy(const char* old_path, const char* new_path) {
283 UNIMPLEMENTED(); 284 File::Type type = File::GetType(old_path, true);
285 if (type == kIsFile) {
zra 2016/12/19 20:29:01 This is copied from the Linux implementation with
286 struct stat64 st;
287 if (NO_RETRY_EXPECTED(stat64(old_path, &st)) != 0) {
288 return false;
289 }
290 int old_fd = NO_RETRY_EXPECTED(open64(old_path, O_RDONLY | O_CLOEXEC));
291 if (old_fd < 0) {
292 return false;
293 }
294 int new_fd = NO_RETRY_EXPECTED(
295 open64(new_path, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, st.st_mode));
296 if (new_fd < 0) {
297 VOID_TEMP_FAILURE_RETRY(close(old_fd));
298 return false;
299 }
300 // TODO(MG-429): Use sendfile/copyfile or equivalent when there is one.
301 intptr_t result;
302 const intptr_t kBufferSize = 8 * KB;
303 uint8_t buffer[kBufferSize];
304 while ((result = NO_RETRY_EXPECTED(read(old_fd, buffer, kBufferSize))) >
305 0) {
306 int wrote = NO_RETRY_EXPECTED(write(new_fd, buffer, result));
307 if (wrote != result) {
308 result = -1;
309 break;
310 }
311 }
312 FDUtils::SaveErrorAndClose(old_fd);
313 FDUtils::SaveErrorAndClose(new_fd);
314 if (result < 0) {
315 int e = errno;
316 VOID_NO_RETRY_EXPECTED(unlink(new_path));
317 errno = e;
318 return false;
319 }
320 return true;
321 } else if (type == kIsDirectory) {
322 errno = EISDIR;
323 } else {
324 errno = ENOENT;
325 }
284 return false; 326 return false;
285 } 327 }
286 328
287 329
288 int64_t File::LengthFromPath(const char* name) { 330 int64_t File::LengthFromPath(const char* name) {
289 struct stat st; 331 struct stat st;
290 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { 332 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) {
291 return st.st_size; 333 return st.st_size;
292 } 334 }
293 return -1; 335 return -1;
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 return ((file_1_info.st_ino == file_2_info.st_ino) && 484 return ((file_1_info.st_ino == file_2_info.st_ino) &&
443 (file_1_info.st_dev == file_2_info.st_dev)) 485 (file_1_info.st_dev == file_2_info.st_dev))
444 ? File::kIdentical 486 ? File::kIdentical
445 : File::kDifferent; 487 : File::kDifferent;
446 } 488 }
447 489
448 } // namespace bin 490 } // namespace bin
449 } // namespace dart 491 } // namespace dart
450 492
451 #endif // defined(TARGET_OS_FUCHSIA) 493 #endif // defined(TARGET_OS_FUCHSIA)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698