| 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 <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 | 70 |
| 71 | 71 |
| 72 int64_t File::Write(const void* buffer, int64_t num_bytes) { | 72 int64_t File::Write(const void* buffer, int64_t num_bytes) { |
| 73 ASSERT(handle_->fd() >= 0); | 73 ASSERT(handle_->fd() >= 0); |
| 74 return TEMP_FAILURE_RETRY(write(handle_->fd(), buffer, num_bytes)); | 74 return TEMP_FAILURE_RETRY(write(handle_->fd(), buffer, num_bytes)); |
| 75 } | 75 } |
| 76 | 76 |
| 77 | 77 |
| 78 off64_t File::Position() { | 78 off64_t File::Position() { |
| 79 ASSERT(handle_->fd() >= 0); | 79 ASSERT(handle_->fd() >= 0); |
| 80 return lseek64(handle_->fd(), 0, SEEK_CUR); | 80 return lseek(handle_->fd(), 0, SEEK_CUR); |
| 81 } | 81 } |
| 82 | 82 |
| 83 | 83 |
| 84 bool File::SetPosition(off64_t position) { | 84 bool File::SetPosition(off64_t position) { |
| 85 ASSERT(handle_->fd() >= 0); | 85 ASSERT(handle_->fd() >= 0); |
| 86 return lseek64(handle_->fd(), position, SEEK_SET) >= 0; | 86 return lseek(handle_->fd(), position, SEEK_SET) >= 0; |
| 87 } | 87 } |
| 88 | 88 |
| 89 | 89 |
| 90 bool File::Truncate(off64_t length) { | 90 bool File::Truncate(off64_t length) { |
| 91 ASSERT(handle_->fd() >= 0); | 91 ASSERT(handle_->fd() >= 0); |
| 92 return TEMP_FAILURE_RETRY(ftruncate64(handle_->fd(), length) != -1); | 92 return TEMP_FAILURE_RETRY(ftruncate(handle_->fd(), length) != -1); |
| 93 } | 93 } |
| 94 | 94 |
| 95 | 95 |
| 96 bool File::Flush() { | 96 bool File::Flush() { |
| 97 ASSERT(handle_->fd() >= 0); | 97 ASSERT(handle_->fd() >= 0); |
| 98 return TEMP_FAILURE_RETRY(fsync(handle_->fd()) != -1); | 98 return TEMP_FAILURE_RETRY(fsync(handle_->fd()) != -1); |
| 99 } | 99 } |
| 100 | 100 |
| 101 | 101 |
| 102 off64_t File::Length() { | 102 off64_t File::Length() { |
| 103 ASSERT(handle_->fd() >= 0); | 103 ASSERT(handle_->fd() >= 0); |
| 104 struct stat64 st; | 104 struct stat st; |
| 105 if (TEMP_FAILURE_RETRY(fstat64(handle_->fd(), &st)) == 0) { | 105 if (TEMP_FAILURE_RETRY(fstat(handle_->fd(), &st)) == 0) { |
| 106 return st.st_size; | 106 return st.st_size; |
| 107 } | 107 } |
| 108 return -1; | 108 return -1; |
| 109 } | 109 } |
| 110 | 110 |
| 111 | 111 |
| 112 File* File::Open(const char* name, FileOpenMode mode) { | 112 File* File::Open(const char* name, FileOpenMode mode) { |
| 113 // Report errors for non-regular files. | 113 // Report errors for non-regular files. |
| 114 struct stat64 st; | 114 struct stat st; |
| 115 if (TEMP_FAILURE_RETRY(stat64(name, &st)) == 0) { | 115 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { |
| 116 // Only accept regular files and character devices. | 116 // Only accept regular files and character devices. |
| 117 if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) { | 117 if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) { |
| 118 errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT; | 118 errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT; |
| 119 return NULL; | 119 return NULL; |
| 120 } | 120 } |
| 121 } | 121 } |
| 122 int flags = O_RDONLY; | 122 int flags = O_RDONLY; |
| 123 if ((mode & kWrite) != 0) { | 123 if ((mode & kWrite) != 0) { |
| 124 flags = (O_RDWR | O_CREAT); | 124 flags = (O_RDWR | O_CREAT); |
| 125 } | 125 } |
| 126 if ((mode & kTruncate) != 0) { | 126 if ((mode & kTruncate) != 0) { |
| 127 flags = flags | O_TRUNC; | 127 flags = flags | O_TRUNC; |
| 128 } | 128 } |
| 129 int fd = TEMP_FAILURE_RETRY(open64(name, flags, 0666)); | 129 int fd = TEMP_FAILURE_RETRY(open(name, flags, 0666)); |
| 130 if (fd < 0) { | 130 if (fd < 0) { |
| 131 return NULL; | 131 return NULL; |
| 132 } | 132 } |
| 133 FDUtils::SetCloseOnExec(fd); | 133 FDUtils::SetCloseOnExec(fd); |
| 134 if (((mode & kWrite) != 0) && ((mode & kTruncate) == 0)) { | 134 if (((mode & kWrite) != 0) && ((mode & kTruncate) == 0)) { |
| 135 off64_t position = lseek64(fd, 0, SEEK_END); | 135 off64_t position = lseek(fd, 0, SEEK_END); |
| 136 if (position < 0) { | 136 if (position < 0) { |
| 137 return NULL; | 137 return NULL; |
| 138 } | 138 } |
| 139 } | 139 } |
| 140 return new File(new FileHandle(fd)); | 140 return new File(new FileHandle(fd)); |
| 141 } | 141 } |
| 142 | 142 |
| 143 | 143 |
| 144 File* File::OpenStdio(int fd) { | 144 File* File::OpenStdio(int fd) { |
| 145 if (fd < 0 || 2 < fd) return NULL; | 145 if (fd < 0 || 2 < fd) return NULL; |
| 146 return new File(new FileHandle(fd)); | 146 return new File(new FileHandle(fd)); |
| 147 } | 147 } |
| 148 | 148 |
| 149 | 149 |
| 150 bool File::Exists(const char* name) { | 150 bool File::Exists(const char* name) { |
| 151 struct stat64 st; | 151 struct stat st; |
| 152 if (TEMP_FAILURE_RETRY(stat64(name, &st)) == 0) { | 152 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { |
| 153 return S_ISREG(st.st_mode); | 153 return S_ISREG(st.st_mode); |
| 154 } else { | 154 } else { |
| 155 return false; | 155 return false; |
| 156 } | 156 } |
| 157 } | 157 } |
| 158 | 158 |
| 159 | 159 |
| 160 bool File::Create(const char* name) { | 160 bool File::Create(const char* name) { |
| 161 int fd = TEMP_FAILURE_RETRY(open64(name, O_RDONLY | O_CREAT, 0666)); | 161 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CREAT, 0666)); |
| 162 if (fd < 0) { | 162 if (fd < 0) { |
| 163 return false; | 163 return false; |
| 164 } | 164 } |
| 165 return (close(fd) == 0); | 165 return (close(fd) == 0); |
| 166 } | 166 } |
| 167 | 167 |
| 168 | 168 |
| 169 bool File::CreateLink(const char* name, const char* target) { | 169 bool File::CreateLink(const char* name, const char* target) { |
| 170 int status = TEMP_FAILURE_RETRY(symlink(target, name)); | 170 int status = TEMP_FAILURE_RETRY(symlink(target, name)); |
| 171 return (status == 0); | 171 return (status == 0); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 } else if (type == kIsDirectory) { | 215 } else if (type == kIsDirectory) { |
| 216 errno = EISDIR; | 216 errno = EISDIR; |
| 217 } else { | 217 } else { |
| 218 errno = EINVAL; | 218 errno = EINVAL; |
| 219 } | 219 } |
| 220 return false; | 220 return false; |
| 221 } | 221 } |
| 222 | 222 |
| 223 | 223 |
| 224 off64_t File::LengthFromPath(const char* name) { | 224 off64_t File::LengthFromPath(const char* name) { |
| 225 struct stat64 st; | 225 struct stat st; |
| 226 if (TEMP_FAILURE_RETRY(stat64(name, &st)) == 0) { | 226 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { |
| 227 return st.st_size; | 227 return st.st_size; |
| 228 } | 228 } |
| 229 return -1; | 229 return -1; |
| 230 } | 230 } |
| 231 | 231 |
| 232 | 232 |
| 233 void File::Stat(const char* name, int64_t* data) { | 233 void File::Stat(const char* name, int64_t* data) { |
| 234 struct stat64 st; | 234 struct stat st; |
| 235 if (TEMP_FAILURE_RETRY(stat64(name, &st)) == 0) { | 235 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { |
| 236 if (S_ISREG(st.st_mode)) { | 236 if (S_ISREG(st.st_mode)) { |
| 237 data[kType] = kIsFile; | 237 data[kType] = kIsFile; |
| 238 } else if (S_ISDIR(st.st_mode)) { | 238 } else if (S_ISDIR(st.st_mode)) { |
| 239 data[kType] = kIsDirectory; | 239 data[kType] = kIsDirectory; |
| 240 } else if (S_ISLNK(st.st_mode)) { | 240 } else if (S_ISLNK(st.st_mode)) { |
| 241 data[kType] = kIsLink; | 241 data[kType] = kIsLink; |
| 242 } else { | 242 } else { |
| 243 data[kType] = kDoesNotExist; | 243 data[kType] = kDoesNotExist; |
| 244 } | 244 } |
| 245 data[kCreatedTime] = st.st_ctime; | 245 data[kCreatedTime] = st.st_ctime; |
| 246 data[kModifiedTime] = st.st_mtime; | 246 data[kModifiedTime] = st.st_mtime; |
| 247 data[kAccessedTime] = st.st_atime; | 247 data[kAccessedTime] = st.st_atime; |
| 248 data[kMode] = st.st_mode; | 248 data[kMode] = st.st_mode; |
| 249 data[kSize] = st.st_size; | 249 data[kSize] = st.st_size; |
| 250 } else { | 250 } else { |
| 251 data[kType] = kDoesNotExist; | 251 data[kType] = kDoesNotExist; |
| 252 } | 252 } |
| 253 } | 253 } |
| 254 | 254 |
| 255 | 255 |
| 256 time_t File::LastModified(const char* name) { | 256 time_t File::LastModified(const char* name) { |
| 257 struct stat64 st; | 257 struct stat st; |
| 258 if (TEMP_FAILURE_RETRY(stat64(name, &st)) == 0) { | 258 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { |
| 259 return st.st_mtime; | 259 return st.st_mtime; |
| 260 } | 260 } |
| 261 return -1; | 261 return -1; |
| 262 } | 262 } |
| 263 | 263 |
| 264 | 264 |
| 265 char* File::LinkTarget(const char* pathname) { | 265 char* File::LinkTarget(const char* pathname) { |
| 266 struct stat64 link_stats; | 266 struct stat link_stats; |
| 267 if (lstat64(pathname, &link_stats) != 0) return NULL; | 267 if (lstat(pathname, &link_stats) != 0) return NULL; |
| 268 if (!S_ISLNK(link_stats.st_mode)) { | 268 if (!S_ISLNK(link_stats.st_mode)) { |
| 269 errno = ENOENT; | 269 errno = ENOENT; |
| 270 return NULL; | 270 return NULL; |
| 271 } | 271 } |
| 272 size_t target_size = link_stats.st_size; | 272 size_t target_size = link_stats.st_size; |
| 273 char* target_name = reinterpret_cast<char*>(malloc(target_size + 1)); | 273 char* target_name = reinterpret_cast<char*>(malloc(target_size + 1)); |
| 274 size_t read_size = readlink(pathname, target_name, target_size + 1); | 274 size_t read_size = readlink(pathname, target_name, target_size + 1); |
| 275 if (read_size != target_size) { | 275 if (read_size != target_size) { |
| 276 free(target_name); | 276 free(target_name); |
| 277 return NULL; | 277 return NULL; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 } | 309 } |
| 310 | 310 |
| 311 | 311 |
| 312 const char* File::StringEscapedPathSeparator() { | 312 const char* File::StringEscapedPathSeparator() { |
| 313 return "/"; | 313 return "/"; |
| 314 } | 314 } |
| 315 | 315 |
| 316 | 316 |
| 317 File::StdioHandleType File::GetStdioHandleType(int fd) { | 317 File::StdioHandleType File::GetStdioHandleType(int fd) { |
| 318 ASSERT(0 <= fd && fd <= 2); | 318 ASSERT(0 <= fd && fd <= 2); |
| 319 struct stat64 buf; | 319 struct stat buf; |
| 320 int result = fstat64(fd, &buf); | 320 int result = fstat(fd, &buf); |
| 321 if (result == -1) { | 321 if (result == -1) { |
| 322 const int kBufferSize = 1024; | 322 const int kBufferSize = 1024; |
| 323 char error_message[kBufferSize]; | 323 char error_message[kBufferSize]; |
| 324 strerror_r(errno, error_message, kBufferSize); | 324 strerror_r(errno, error_message, kBufferSize); |
| 325 FATAL2("Failed stat on file descriptor %d: %s", fd, error_message); | 325 FATAL2("Failed stat on file descriptor %d: %s", fd, error_message); |
| 326 } | 326 } |
| 327 if (S_ISCHR(buf.st_mode)) return kTerminal; | 327 if (S_ISCHR(buf.st_mode)) return kTerminal; |
| 328 if (S_ISFIFO(buf.st_mode)) return kPipe; | 328 if (S_ISFIFO(buf.st_mode)) return kPipe; |
| 329 if (S_ISSOCK(buf.st_mode)) return kSocket; | 329 if (S_ISSOCK(buf.st_mode)) return kSocket; |
| 330 if (S_ISREG(buf.st_mode)) return kFile; | 330 if (S_ISREG(buf.st_mode)) return kFile; |
| 331 return kOther; | 331 return kOther; |
| 332 } | 332 } |
| 333 | 333 |
| 334 | 334 |
| 335 File::Type File::GetType(const char* pathname, bool follow_links) { | 335 File::Type File::GetType(const char* pathname, bool follow_links) { |
| 336 struct stat64 entry_info; | 336 struct stat entry_info; |
| 337 int stat_success; | 337 int stat_success; |
| 338 if (follow_links) { | 338 if (follow_links) { |
| 339 stat_success = TEMP_FAILURE_RETRY(stat64(pathname, &entry_info)); | 339 stat_success = TEMP_FAILURE_RETRY(stat(pathname, &entry_info)); |
| 340 } else { | 340 } else { |
| 341 stat_success = TEMP_FAILURE_RETRY(lstat64(pathname, &entry_info)); | 341 stat_success = TEMP_FAILURE_RETRY(lstat(pathname, &entry_info)); |
| 342 } | 342 } |
| 343 if (stat_success == -1) return File::kDoesNotExist; | 343 if (stat_success == -1) return File::kDoesNotExist; |
| 344 if (S_ISDIR(entry_info.st_mode)) return File::kIsDirectory; | 344 if (S_ISDIR(entry_info.st_mode)) return File::kIsDirectory; |
| 345 if (S_ISREG(entry_info.st_mode)) return File::kIsFile; | 345 if (S_ISREG(entry_info.st_mode)) return File::kIsFile; |
| 346 if (S_ISLNK(entry_info.st_mode)) return File::kIsLink; | 346 if (S_ISLNK(entry_info.st_mode)) return File::kIsLink; |
| 347 return File::kDoesNotExist; | 347 return File::kDoesNotExist; |
| 348 } | 348 } |
| 349 | 349 |
| 350 | 350 |
| 351 File::Identical File::AreIdentical(const char* file_1, const char* file_2) { | 351 File::Identical File::AreIdentical(const char* file_1, const char* file_2) { |
| 352 struct stat64 file_1_info; | 352 struct stat file_1_info; |
| 353 struct stat64 file_2_info; | 353 struct stat file_2_info; |
| 354 if (TEMP_FAILURE_RETRY(lstat64(file_1, &file_1_info)) == -1 || | 354 if (TEMP_FAILURE_RETRY(lstat(file_1, &file_1_info)) == -1 || |
| 355 TEMP_FAILURE_RETRY(lstat64(file_2, &file_2_info)) == -1) { | 355 TEMP_FAILURE_RETRY(lstat(file_2, &file_2_info)) == -1) { |
| 356 return File::kError; | 356 return File::kError; |
| 357 } | 357 } |
| 358 return (file_1_info.st_ino == file_2_info.st_ino && | 358 return (file_1_info.st_ino == file_2_info.st_ino && |
| 359 file_1_info.st_dev == file_2_info.st_dev) ? | 359 file_1_info.st_dev == file_2_info.st_dev) ? |
| 360 File::kIdentical : | 360 File::kIdentical : |
| 361 File::kDifferent; | 361 File::kDifferent; |
| 362 } | 362 } |
| 363 | 363 |
| 364 } // namespace bin | 364 } // namespace bin |
| 365 } // namespace dart | 365 } // namespace dart |
| 366 | 366 |
| 367 #endif // defined(TARGET_OS_MACOS) | 367 #endif // defined(TARGET_OS_MACOS) |
| OLD | NEW |