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

Side by Side 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: 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_LINUX) 6 #if defined(TARGET_OS_LINUX)
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 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 return TEMP_FAILURE_RETRY(rename(old_path, new_path)) == 0; 214 return TEMP_FAILURE_RETRY(rename(old_path, new_path)) == 0;
214 } else if (type == kIsDirectory) { 215 } else if (type == kIsDirectory) {
215 errno = EISDIR; 216 errno = EISDIR;
216 } else { 217 } else {
217 errno = EINVAL; 218 errno = EINVAL;
218 } 219 }
219 return false; 220 return false;
220 } 221 }
221 222
222 223
224 bool File::Copy(const char* old_path, const char* new_path) {
225 File::Type type = File::GetType(old_path, true);
226 if (type == kIsFile) {
227 int old_fd = TEMP_FAILURE_RETRY(open64(old_path, O_RDONLY | O_CLOEXEC));
228 if (old_fd < 0) {
229 return false;
230 }
231 int new_fd = TEMP_FAILURE_RETRY(
232 open64(new_path, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, 0666));
233 if (new_fd < 0) {
234 VOID_TEMP_FAILURE_RETRY(close(old_fd));
235 return false;
236 }
237 off64_t offset = 0;
238 int bytes = 1;
239 while (bytes > 0) {
240 // Loop to ensure we copy everything, and not only up to 2GB.
241 bytes = TEMP_FAILURE_RETRY(sendfile64(new_fd, old_fd, &offset, -1));
242 }
243 if (bytes < 0) {
244 int e = errno;
245 VOID_TEMP_FAILURE_RETRY(close(old_fd));
246 VOID_TEMP_FAILURE_RETRY(close(new_fd));
247 VOID_TEMP_FAILURE_RETRY(unlink(new_path));
248 errno = e;
249 return false;
250 }
251 return true;
252 } else if (type == kIsDirectory) {
253 errno = EISDIR;
254 } else {
255 errno = ENOENT;
256 }
257 return false;
258 }
259
260
223 off64_t File::LengthFromPath(const char* name) { 261 off64_t File::LengthFromPath(const char* name) {
224 struct stat64 st; 262 struct stat64 st;
225 if (TEMP_FAILURE_RETRY(stat64(name, &st)) == 0) { 263 if (TEMP_FAILURE_RETRY(stat64(name, &st)) == 0) {
226 return st.st_size; 264 return st.st_size;
227 } 265 }
228 return -1; 266 return -1;
229 } 267 }
230 268
231 269
232 void File::Stat(const char* name, int64_t* data) { 270 void File::Stat(const char* name, int64_t* data) {
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 return (file_1_info.st_ino == file_2_info.st_ino && 389 return (file_1_info.st_ino == file_2_info.st_ino &&
352 file_1_info.st_dev == file_2_info.st_dev) ? 390 file_1_info.st_dev == file_2_info.st_dev) ?
353 File::kIdentical : 391 File::kIdentical :
354 File::kDifferent; 392 File::kDifferent;
355 } 393 }
356 394
357 } // namespace bin 395 } // namespace bin
358 } // namespace dart 396 } // namespace dart
359 397
360 #endif // defined(TARGET_OS_LINUX) 398 #endif // defined(TARGET_OS_LINUX)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698