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_MACOS) | 6 #if defined(TARGET_OS_MACOS) |
7 | 7 |
8 #include "bin/file.h" | 8 #include "bin/file.h" |
9 | 9 |
10 #include <copyfile.h> // NOLINT | 10 #include <copyfile.h> // NOLINT |
11 #include <errno.h> // NOLINT | 11 #include <errno.h> // NOLINT |
12 #include <fcntl.h> // NOLINT | 12 #include <fcntl.h> // NOLINT |
13 #include <libgen.h> // NOLINT | 13 #include <libgen.h> // NOLINT |
14 #include <limits.h> // NOLINT | 14 #include <limits.h> // NOLINT |
15 #include <sys/mman.h> // NOLINT | 15 #include <sys/mman.h> // NOLINT |
16 #include <sys/stat.h> // NOLINT | 16 #include <sys/stat.h> // NOLINT |
17 #include <unistd.h> // NOLINT | 17 #include <unistd.h> // NOLINT |
| 18 #include <utime.h> // NOLINT |
18 | 19 |
19 #include "bin/builtin.h" | 20 #include "bin/builtin.h" |
20 #include "bin/fdutils.h" | 21 #include "bin/fdutils.h" |
21 #include "bin/log.h" | 22 #include "bin/log.h" |
22 | 23 |
23 #include "platform/signal_blocker.h" | 24 #include "platform/signal_blocker.h" |
24 #include "platform/utils.h" | 25 #include "platform/utils.h" |
25 | 26 |
26 namespace dart { | 27 namespace dart { |
27 namespace bin { | 28 namespace bin { |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 (NO_RETRY_EXPECTED(rename(old_path, new_path)) == 0); | 338 (NO_RETRY_EXPECTED(rename(old_path, new_path)) == 0); |
338 } | 339 } |
339 | 340 |
340 | 341 |
341 bool File::Copy(const char* old_path, const char* new_path) { | 342 bool File::Copy(const char* old_path, const char* new_path) { |
342 return CheckTypeAndSetErrno(old_path, kIsFile, true) && | 343 return CheckTypeAndSetErrno(old_path, kIsFile, true) && |
343 (copyfile(old_path, new_path, NULL, COPYFILE_ALL) == 0); | 344 (copyfile(old_path, new_path, NULL, COPYFILE_ALL) == 0); |
344 } | 345 } |
345 | 346 |
346 | 347 |
| 348 static bool StatHelper(const char* name, struct stat* st) { |
| 349 if (NO_RETRY_EXPECTED(stat(name, st)) != 0) { |
| 350 return false; |
| 351 } |
| 352 // Signal an error if it's a directory. |
| 353 if (S_ISDIR(st->st_mode)) { |
| 354 errno = EISDIR; |
| 355 return false; |
| 356 } |
| 357 // Otherwise assume the caller knows what it's doing. |
| 358 return true; |
| 359 } |
| 360 |
| 361 |
347 int64_t File::LengthFromPath(const char* name) { | 362 int64_t File::LengthFromPath(const char* name) { |
348 struct stat st; | 363 struct stat st; |
349 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { | 364 if (!StatHelper(name, &st)) { |
350 // Signal an error if it's a directory. | 365 return -1; |
351 if (S_ISDIR(st.st_mode)) { | |
352 errno = EISDIR; | |
353 return -1; | |
354 } | |
355 // Otherwise assume the caller knows what it's doing. | |
356 return st.st_size; | |
357 } | 366 } |
358 return -1; | 367 return st.st_size; |
359 } | 368 } |
360 | 369 |
361 | 370 |
362 static int64_t TimespecToMilliseconds(const struct timespec& t) { | 371 static int64_t TimespecToMilliseconds(const struct timespec& t) { |
363 return static_cast<int64_t>(t.tv_sec) * 1000L + | 372 return static_cast<int64_t>(t.tv_sec) * 1000L + |
364 static_cast<int64_t>(t.tv_nsec) / 1000000L; | 373 static_cast<int64_t>(t.tv_nsec) / 1000000L; |
365 } | 374 } |
366 | 375 |
367 | 376 |
368 void File::Stat(const char* name, int64_t* data) { | 377 void File::Stat(const char* name, int64_t* data) { |
(...skipping 17 matching lines...) Expand all Loading... |
386 data[kMode] = st.st_mode; | 395 data[kMode] = st.st_mode; |
387 data[kSize] = st.st_size; | 396 data[kSize] = st.st_size; |
388 } else { | 397 } else { |
389 data[kType] = kDoesNotExist; | 398 data[kType] = kDoesNotExist; |
390 } | 399 } |
391 } | 400 } |
392 | 401 |
393 | 402 |
394 time_t File::LastModified(const char* name) { | 403 time_t File::LastModified(const char* name) { |
395 struct stat st; | 404 struct stat st; |
396 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { | 405 if (!StatHelper(name, &st)) { |
397 // Signal an error if it's a directory. | 406 return -1; |
398 if (S_ISDIR(st.st_mode)) { | |
399 errno = EISDIR; | |
400 return -1; | |
401 } | |
402 // Otherwise assume the caller knows what it's doing. | |
403 return st.st_mtime; | |
404 } | 407 } |
405 return -1; | 408 return st.st_mtime; |
406 } | 409 } |
407 | 410 |
408 | 411 |
| 412 time_t File::LastAccessed(const char* name) { |
| 413 struct stat st; |
| 414 if (!StatHelper(name, &st)) { |
| 415 return -1; |
| 416 } |
| 417 return st.st_atime; |
| 418 } |
| 419 |
| 420 |
| 421 bool File::SetLastAccessed(const char* name, int64_t millis) { |
| 422 // First get the current times. |
| 423 struct stat st; |
| 424 if (!StatHelper(name, &st)) { |
| 425 return false; |
| 426 } |
| 427 |
| 428 // Set the new time: |
| 429 struct utimbuf times; |
| 430 times.actime = millis / kMillisecondsPerSecond; |
| 431 times.modtime = st.st_mtime; |
| 432 return utime(name, ×) == 0; |
| 433 } |
| 434 |
| 435 |
| 436 bool File::SetLastModified(const char* name, int64_t millis) { |
| 437 // First get the current times. |
| 438 struct stat st; |
| 439 if (!StatHelper(name, &st)) { |
| 440 return false; |
| 441 } |
| 442 |
| 443 // Set the new time: |
| 444 struct utimbuf times; |
| 445 times.actime = st.st_atime; |
| 446 times.modtime = millis / kMillisecondsPerSecond; |
| 447 return utime(name, ×) == 0; |
| 448 } |
| 449 |
| 450 |
409 const char* File::LinkTarget(const char* pathname) { | 451 const char* File::LinkTarget(const char* pathname) { |
410 struct stat link_stats; | 452 struct stat link_stats; |
411 if (lstat(pathname, &link_stats) != 0) { | 453 if (lstat(pathname, &link_stats) != 0) { |
412 return NULL; | 454 return NULL; |
413 } | 455 } |
414 if (!S_ISLNK(link_stats.st_mode)) { | 456 if (!S_ISLNK(link_stats.st_mode)) { |
415 errno = ENOENT; | 457 errno = ENOENT; |
416 return NULL; | 458 return NULL; |
417 } | 459 } |
418 // Don't rely on the link_stats.st_size for the size of the link | 460 // Don't rely on the link_stats.st_size for the size of the link |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 return ((file_1_info.st_ino == file_2_info.st_ino) && | 543 return ((file_1_info.st_ino == file_2_info.st_ino) && |
502 (file_1_info.st_dev == file_2_info.st_dev)) | 544 (file_1_info.st_dev == file_2_info.st_dev)) |
503 ? File::kIdentical | 545 ? File::kIdentical |
504 : File::kDifferent; | 546 : File::kDifferent; |
505 } | 547 } |
506 | 548 |
507 } // namespace bin | 549 } // namespace bin |
508 } // namespace dart | 550 } // namespace dart |
509 | 551 |
510 #endif // defined(TARGET_OS_MACOS) | 552 #endif // defined(TARGET_OS_MACOS) |
OLD | NEW |