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

Unified Diff: runtime/bin/file_linux.cc

Issue 105133004: Add File.copy(Sync) to dart:io. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix permissions. Created 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/bin/file_android.cc ('k') | runtime/bin/file_macos.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/file_linux.cc
diff --git a/runtime/bin/file_linux.cc b/runtime/bin/file_linux.cc
index f5db4e579e1aaefcaa584cfec91b0296d0b7e67f..69dff89a2e811c2300e0247ebd01abe42832a93f 100644
--- a/runtime/bin/file_linux.cc
+++ b/runtime/bin/file_linux.cc
@@ -11,6 +11,7 @@
#include <fcntl.h> // NOLINT
#include <sys/stat.h> // NOLINT
#include <sys/types.h> // NOLINT
+#include <sys/sendfile.h> // NOLINT
#include <unistd.h> // NOLINT
#include <libgen.h> // NOLINT
@@ -220,6 +221,47 @@ bool File::RenameLink(const char* old_path, const char* new_path) {
}
+bool File::Copy(const char* old_path, const char* new_path) {
+ File::Type type = File::GetType(old_path, true);
+ if (type == kIsFile) {
+ struct stat64 st;
+ if (TEMP_FAILURE_RETRY(stat64(old_path, &st)) != 0) {
+ return false;
+ }
+ int old_fd = TEMP_FAILURE_RETRY(open64(old_path, O_RDONLY | O_CLOEXEC));
+ if (old_fd < 0) {
+ return false;
+ }
+ int new_fd = TEMP_FAILURE_RETRY(
+ open64(new_path, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, st.st_mode));
+ if (new_fd < 0) {
+ VOID_TEMP_FAILURE_RETRY(close(old_fd));
+ return false;
+ }
+ off64_t offset = 0;
+ int bytes = 1;
+ while (bytes > 0) {
+ // Loop to ensure we copy everything, and not only up to 2GB.
+ bytes = TEMP_FAILURE_RETRY(sendfile64(new_fd, old_fd, &offset, -1));
+ }
+ if (bytes < 0) {
+ int e = errno;
+ VOID_TEMP_FAILURE_RETRY(close(old_fd));
+ VOID_TEMP_FAILURE_RETRY(close(new_fd));
+ VOID_TEMP_FAILURE_RETRY(unlink(new_path));
+ errno = e;
+ return false;
+ }
+ return true;
+ } else if (type == kIsDirectory) {
+ errno = EISDIR;
+ } else {
+ errno = ENOENT;
+ }
+ return false;
+}
+
+
off64_t File::LengthFromPath(const char* name) {
struct stat64 st;
if (TEMP_FAILURE_RETRY(stat64(name, &st)) == 0) {
« no previous file with comments | « runtime/bin/file_android.cc ('k') | runtime/bin/file_macos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698