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 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 return TEMP_FAILURE_RETRY(ftruncate64(handle_->fd(), length) != -1); | 102 return TEMP_FAILURE_RETRY(ftruncate64(handle_->fd(), length) != -1); |
103 } | 103 } |
104 | 104 |
105 | 105 |
106 bool File::Flush() { | 106 bool File::Flush() { |
107 ASSERT(handle_->fd() >= 0); | 107 ASSERT(handle_->fd() >= 0); |
108 return NO_RETRY_EXPECTED(fsync(handle_->fd())) != -1; | 108 return NO_RETRY_EXPECTED(fsync(handle_->fd())) != -1; |
109 } | 109 } |
110 | 110 |
111 | 111 |
| 112 bool File::Lock(File::LockType lock, int64_t start, int64_t end) { |
| 113 ASSERT(handle_->fd() >= 0); |
| 114 ASSERT(end == -1 || end > start); |
| 115 struct flock fl; |
| 116 switch (lock) { |
| 117 case File::kLockUnlock: |
| 118 fl.l_type = F_UNLCK; |
| 119 break; |
| 120 case File::kLockShared: |
| 121 fl.l_type = F_RDLCK; |
| 122 break; |
| 123 case File::kLockExclusive: |
| 124 fl.l_type = F_WRLCK; |
| 125 break; |
| 126 default: |
| 127 return false; |
| 128 } |
| 129 fl.l_whence = SEEK_SET; |
| 130 fl.l_start = start; |
| 131 fl.l_len = end == -1 ? 0 : end - start; |
| 132 // fcntl does not block, but fails if the lock cannot be acquired. |
| 133 int rc = fcntl(handle_->fd(), F_SETLK, &fl); |
| 134 return rc != -1; |
| 135 } |
| 136 |
| 137 |
112 int64_t File::Length() { | 138 int64_t File::Length() { |
113 ASSERT(handle_->fd() >= 0); | 139 ASSERT(handle_->fd() >= 0); |
114 struct stat64 st; | 140 struct stat64 st; |
115 if (TEMP_FAILURE_RETRY(fstat64(handle_->fd(), &st)) == 0) { | 141 if (TEMP_FAILURE_RETRY(fstat64(handle_->fd(), &st)) == 0) { |
116 return st.st_size; | 142 return st.st_size; |
117 } | 143 } |
118 return -1; | 144 return -1; |
119 } | 145 } |
120 | 146 |
121 | 147 |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 return (file_1_info.st_ino == file_2_info.st_ino && | 452 return (file_1_info.st_ino == file_2_info.st_ino && |
427 file_1_info.st_dev == file_2_info.st_dev) ? | 453 file_1_info.st_dev == file_2_info.st_dev) ? |
428 File::kIdentical : | 454 File::kIdentical : |
429 File::kDifferent; | 455 File::kDifferent; |
430 } | 456 } |
431 | 457 |
432 } // namespace bin | 458 } // namespace bin |
433 } // namespace dart | 459 } // namespace dart |
434 | 460 |
435 #endif // defined(TARGET_OS_LINUX) | 461 #endif // defined(TARGET_OS_LINUX) |
OLD | NEW |