| 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_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 Loading... |
| 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 struct stat64 st; |
| 228 if (TEMP_FAILURE_RETRY(stat64(old_path, &st)) != 0) { |
| 229 return false; |
| 230 } |
| 231 int old_fd = TEMP_FAILURE_RETRY(open64(old_path, O_RDONLY | O_CLOEXEC)); |
| 232 if (old_fd < 0) { |
| 233 return false; |
| 234 } |
| 235 int new_fd = TEMP_FAILURE_RETRY( |
| 236 open64(new_path, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, st.st_mode)); |
| 237 if (new_fd < 0) { |
| 238 VOID_TEMP_FAILURE_RETRY(close(old_fd)); |
| 239 return false; |
| 240 } |
| 241 off64_t offset = 0; |
| 242 int bytes = 1; |
| 243 while (bytes > 0) { |
| 244 // Loop to ensure we copy everything, and not only up to 2GB. |
| 245 bytes = TEMP_FAILURE_RETRY(sendfile64(new_fd, old_fd, &offset, -1)); |
| 246 } |
| 247 if (bytes < 0) { |
| 248 int e = errno; |
| 249 VOID_TEMP_FAILURE_RETRY(close(old_fd)); |
| 250 VOID_TEMP_FAILURE_RETRY(close(new_fd)); |
| 251 VOID_TEMP_FAILURE_RETRY(unlink(new_path)); |
| 252 errno = e; |
| 253 return false; |
| 254 } |
| 255 return true; |
| 256 } else if (type == kIsDirectory) { |
| 257 errno = EISDIR; |
| 258 } else { |
| 259 errno = ENOENT; |
| 260 } |
| 261 return false; |
| 262 } |
| 263 |
| 264 |
| 223 off64_t File::LengthFromPath(const char* name) { | 265 off64_t File::LengthFromPath(const char* name) { |
| 224 struct stat64 st; | 266 struct stat64 st; |
| 225 if (TEMP_FAILURE_RETRY(stat64(name, &st)) == 0) { | 267 if (TEMP_FAILURE_RETRY(stat64(name, &st)) == 0) { |
| 226 return st.st_size; | 268 return st.st_size; |
| 227 } | 269 } |
| 228 return -1; | 270 return -1; |
| 229 } | 271 } |
| 230 | 272 |
| 231 | 273 |
| 232 void File::Stat(const char* name, int64_t* data) { | 274 void File::Stat(const char* name, int64_t* data) { |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 return (file_1_info.st_ino == file_2_info.st_ino && | 393 return (file_1_info.st_ino == file_2_info.st_ino && |
| 352 file_1_info.st_dev == file_2_info.st_dev) ? | 394 file_1_info.st_dev == file_2_info.st_dev) ? |
| 353 File::kIdentical : | 395 File::kIdentical : |
| 354 File::kDifferent; | 396 File::kDifferent; |
| 355 } | 397 } |
| 356 | 398 |
| 357 } // namespace bin | 399 } // namespace bin |
| 358 } // namespace dart | 400 } // namespace dart |
| 359 | 401 |
| 360 #endif // defined(TARGET_OS_LINUX) | 402 #endif // defined(TARGET_OS_LINUX) |
| OLD | NEW |