| 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_ANDROID) | 6 #if defined(TARGET_OS_ANDROID) |
| 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 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 int64_t File::Length() { | 141 int64_t File::Length() { |
| 142 ASSERT(handle_->fd() >= 0); | 142 ASSERT(handle_->fd() >= 0); |
| 143 struct stat st; | 143 struct stat st; |
| 144 if (NO_RETRY_EXPECTED(fstat(handle_->fd(), &st)) == 0) { | 144 if (NO_RETRY_EXPECTED(fstat(handle_->fd(), &st)) == 0) { |
| 145 return st.st_size; | 145 return st.st_size; |
| 146 } | 146 } |
| 147 return -1; | 147 return -1; |
| 148 } | 148 } |
| 149 | 149 |
| 150 | 150 |
| 151 File* File::Open(const char* name, FileOpenMode mode) { | 151 File* File::FileOpenW(const wchar_t* system_name, FileOpenMode mode) { |
| 152 UNREACHABLE(); |
| 153 return NULL; |
| 154 } |
| 155 |
| 156 |
| 157 File* File::ScopedOpen(const char* name, FileOpenMode mode) { |
| 152 // Report errors for non-regular files. | 158 // Report errors for non-regular files. |
| 153 struct stat st; | 159 struct stat st; |
| 154 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { | 160 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { |
| 155 if (!S_ISREG(st.st_mode)) { | 161 if (!S_ISREG(st.st_mode)) { |
| 156 errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT; | 162 errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT; |
| 157 return NULL; | 163 return NULL; |
| 158 } | 164 } |
| 159 } | 165 } |
| 160 int flags = O_RDONLY; | 166 int flags = O_RDONLY; |
| 161 if ((mode & kWrite) != 0) { | 167 if ((mode & kWrite) != 0) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 178 (((mode & kWriteOnly) != 0) && ((mode & kTruncate) == 0))) { | 184 (((mode & kWriteOnly) != 0) && ((mode & kTruncate) == 0))) { |
| 179 int64_t position = lseek64(fd, 0, SEEK_END); | 185 int64_t position = lseek64(fd, 0, SEEK_END); |
| 180 if (position < 0) { | 186 if (position < 0) { |
| 181 return NULL; | 187 return NULL; |
| 182 } | 188 } |
| 183 } | 189 } |
| 184 return new File(new FileHandle(fd)); | 190 return new File(new FileHandle(fd)); |
| 185 } | 191 } |
| 186 | 192 |
| 187 | 193 |
| 194 File* File::Open(const char* path, FileOpenMode mode) { |
| 195 // ScopedOpen doesn't actually need a scope. |
| 196 return ScopedOpen(path, mode); |
| 197 } |
| 198 |
| 199 |
| 188 File* File::OpenStdio(int fd) { | 200 File* File::OpenStdio(int fd) { |
| 189 if (fd < 0 || 2 < fd) return NULL; | 201 if ((fd < 0) || (2 < fd)) { |
| 202 return NULL; |
| 203 } |
| 190 return new File(new FileHandle(fd)); | 204 return new File(new FileHandle(fd)); |
| 191 } | 205 } |
| 192 | 206 |
| 193 | 207 |
| 194 bool File::Exists(const char* name) { | 208 bool File::Exists(const char* name) { |
| 195 struct stat st; | 209 struct stat st; |
| 196 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { | 210 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { |
| 197 return S_ISREG(st.st_mode); | 211 return S_ISREG(st.st_mode); |
| 198 } else { | 212 } else { |
| 199 return false; | 213 return false; |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 off_t offset = 0; | 299 off_t offset = 0; |
| 286 int result = 1; | 300 int result = 1; |
| 287 while (result > 0) { | 301 while (result > 0) { |
| 288 // Loop to ensure we copy everything, and not only up to 2GB. | 302 // Loop to ensure we copy everything, and not only up to 2GB. |
| 289 result = NO_RETRY_EXPECTED( | 303 result = NO_RETRY_EXPECTED( |
| 290 sendfile(new_fd, old_fd, &offset, kMaxUint32)); | 304 sendfile(new_fd, old_fd, &offset, kMaxUint32)); |
| 291 } | 305 } |
| 292 // From sendfile man pages: | 306 // From sendfile man pages: |
| 293 // Applications may wish to fall back to read(2)/write(2) in the case | 307 // Applications may wish to fall back to read(2)/write(2) in the case |
| 294 // where sendfile() fails with EINVAL or ENOSYS. | 308 // where sendfile() fails with EINVAL or ENOSYS. |
| 295 if (result < 0 && (errno == EINVAL || errno == ENOSYS)) { | 309 if ((result < 0) && ((errno == EINVAL) || (errno == ENOSYS))) { |
| 296 const intptr_t kBufferSize = 8 * KB; | 310 const intptr_t kBufferSize = 8 * KB; |
| 297 uint8_t buffer[kBufferSize]; | 311 uint8_t buffer[kBufferSize]; |
| 298 while ((result = TEMP_FAILURE_RETRY( | 312 while ((result = TEMP_FAILURE_RETRY( |
| 299 read(old_fd, buffer, kBufferSize))) > 0) { | 313 read(old_fd, buffer, kBufferSize))) > 0) { |
| 300 int wrote = TEMP_FAILURE_RETRY(write(new_fd, buffer, result)); | 314 int wrote = TEMP_FAILURE_RETRY(write(new_fd, buffer, result)); |
| 301 if (wrote != result) { | 315 if (wrote != result) { |
| 302 result = -1; | 316 result = -1; |
| 303 break; | 317 break; |
| 304 } | 318 } |
| 305 } | 319 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 | 370 |
| 357 time_t File::LastModified(const char* name) { | 371 time_t File::LastModified(const char* name) { |
| 358 struct stat st; | 372 struct stat st; |
| 359 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { | 373 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { |
| 360 return st.st_mtime; | 374 return st.st_mtime; |
| 361 } | 375 } |
| 362 return -1; | 376 return -1; |
| 363 } | 377 } |
| 364 | 378 |
| 365 | 379 |
| 366 char* File::LinkTarget(const char* pathname) { | 380 const char* File::LinkTarget(const char* pathname) { |
| 367 struct stat link_stats; | 381 struct stat link_stats; |
| 368 if (lstat(pathname, &link_stats) != 0) return NULL; | 382 if (lstat(pathname, &link_stats) != 0) { |
| 383 return NULL; |
| 384 } |
| 369 if (!S_ISLNK(link_stats.st_mode)) { | 385 if (!S_ISLNK(link_stats.st_mode)) { |
| 370 errno = ENOENT; | 386 errno = ENOENT; |
| 371 return NULL; | 387 return NULL; |
| 372 } | 388 } |
| 373 size_t target_size = link_stats.st_size; | 389 size_t target_size = link_stats.st_size; |
| 374 char* target_name = reinterpret_cast<char*>(malloc(target_size + 1)); | 390 char* target_name = DartUtils::ScopedCString(target_size + 1); |
| 391 ASSERT(target_name != NULL); |
| 375 size_t read_size = readlink(pathname, target_name, target_size + 1); | 392 size_t read_size = readlink(pathname, target_name, target_size + 1); |
| 376 if (read_size != target_size) { | 393 if (read_size != target_size) { |
| 377 free(target_name); | |
| 378 return NULL; | 394 return NULL; |
| 379 } | 395 } |
| 380 target_name[target_size] = '\0'; | 396 target_name[target_size] = '\0'; |
| 381 return target_name; | 397 return target_name; |
| 382 } | 398 } |
| 383 | 399 |
| 384 | 400 |
| 385 bool File::IsAbsolutePath(const char* pathname) { | 401 bool File::IsAbsolutePath(const char* pathname) { |
| 386 return (pathname != NULL && pathname[0] == '/'); | 402 return (pathname != NULL && pathname[0] == '/'); |
| 387 } | 403 } |
| 388 | 404 |
| 389 | 405 |
| 390 char* File::GetCanonicalPath(const char* pathname) { | 406 const char* File::GetCanonicalPath(const char* pathname) { |
| 391 char* abs_path = NULL; | 407 char* abs_path = NULL; |
| 392 if (pathname != NULL) { | 408 if (pathname != NULL) { |
| 393 // A null second argument to realpath crashes Android. Fixed in Mar 2013, | 409 char* resolved_path = DartUtils::ScopedCString(PATH_MAX + 1); |
| 394 // but not in earlier releases of Android. | 410 ASSERT(resolved_path != NULL); |
| 395 char* resolved = reinterpret_cast<char*>(malloc(PATH_MAX)); | |
| 396 if (resolved == NULL) return NULL; | |
| 397 do { | 411 do { |
| 398 abs_path = realpath(pathname, resolved); | 412 abs_path = realpath(pathname, resolved_path); |
| 399 } while (abs_path == NULL && errno == EINTR); | 413 } while ((abs_path == NULL) && (errno == EINTR)); |
| 400 ASSERT(abs_path == NULL || IsAbsolutePath(abs_path)); | 414 ASSERT((abs_path == NULL) || IsAbsolutePath(abs_path)); |
| 401 if (abs_path != resolved) { | 415 ASSERT((abs_path == NULL) || (abs_path == resolved_path)); |
| 402 free(resolved); | |
| 403 } | |
| 404 } | 416 } |
| 405 return abs_path; | 417 return abs_path; |
| 406 } | 418 } |
| 407 | 419 |
| 408 | 420 |
| 409 const char* File::PathSeparator() { | 421 const char* File::PathSeparator() { |
| 410 return "/"; | 422 return "/"; |
| 411 } | 423 } |
| 412 | 424 |
| 413 | 425 |
| 414 const char* File::StringEscapedPathSeparator() { | 426 const char* File::StringEscapedPathSeparator() { |
| 415 return "/"; | 427 return "/"; |
| 416 } | 428 } |
| 417 | 429 |
| 418 | 430 |
| 419 File::StdioHandleType File::GetStdioHandleType(int fd) { | 431 File::StdioHandleType File::GetStdioHandleType(int fd) { |
| 420 ASSERT(0 <= fd && fd <= 2); | 432 ASSERT((0 <= fd) && (fd <= 2)); |
| 421 struct stat buf; | 433 struct stat buf; |
| 422 int result = fstat(fd, &buf); | 434 int result = fstat(fd, &buf); |
| 423 if (result == -1) { | 435 if (result == -1) { |
| 424 const int kBufferSize = 1024; | 436 const int kBufferSize = 1024; |
| 425 char error_message[kBufferSize]; | 437 char error_message[kBufferSize]; |
| 426 Utils::StrError(errno, error_message, kBufferSize); | 438 Utils::StrError(errno, error_message, kBufferSize); |
| 427 FATAL2("Failed stat on file descriptor %d: %s", fd, error_message); | 439 FATAL2("Failed stat on file descriptor %d: %s", fd, error_message); |
| 428 } | 440 } |
| 429 if (S_ISCHR(buf.st_mode)) return kTerminal; | 441 if (S_ISCHR(buf.st_mode)) { |
| 430 if (S_ISFIFO(buf.st_mode)) return kPipe; | 442 return kTerminal; |
| 431 if (S_ISSOCK(buf.st_mode)) return kSocket; | 443 } |
| 432 if (S_ISREG(buf.st_mode)) return kFile; | 444 if (S_ISFIFO(buf.st_mode)) { |
| 445 return kPipe; |
| 446 } |
| 447 if (S_ISSOCK(buf.st_mode)) { |
| 448 return kSocket; |
| 449 } |
| 450 if (S_ISREG(buf.st_mode)) { |
| 451 return kFile; |
| 452 } |
| 433 return kOther; | 453 return kOther; |
| 434 } | 454 } |
| 435 | 455 |
| 436 | 456 |
| 437 File::Type File::GetType(const char* pathname, bool follow_links) { | 457 File::Type File::GetType(const char* pathname, bool follow_links) { |
| 438 struct stat entry_info; | 458 struct stat entry_info; |
| 439 int stat_success; | 459 int stat_success; |
| 440 if (follow_links) { | 460 if (follow_links) { |
| 441 stat_success = NO_RETRY_EXPECTED(stat(pathname, &entry_info)); | 461 stat_success = NO_RETRY_EXPECTED(stat(pathname, &entry_info)); |
| 442 } else { | 462 } else { |
| 443 stat_success = NO_RETRY_EXPECTED(lstat(pathname, &entry_info)); | 463 stat_success = NO_RETRY_EXPECTED(lstat(pathname, &entry_info)); |
| 444 } | 464 } |
| 445 if (stat_success == -1) return File::kDoesNotExist; | 465 if (stat_success == -1) { |
| 446 if (S_ISDIR(entry_info.st_mode)) return File::kIsDirectory; | 466 return File::kDoesNotExist; |
| 447 if (S_ISREG(entry_info.st_mode)) return File::kIsFile; | 467 } |
| 448 if (S_ISLNK(entry_info.st_mode)) return File::kIsLink; | 468 if (S_ISDIR(entry_info.st_mode)) { |
| 469 return File::kIsDirectory; |
| 470 } |
| 471 if (S_ISREG(entry_info.st_mode)) { |
| 472 return File::kIsFile; |
| 473 } |
| 474 if (S_ISLNK(entry_info.st_mode)) { |
| 475 return File::kIsLink; |
| 476 } |
| 449 return File::kDoesNotExist; | 477 return File::kDoesNotExist; |
| 450 } | 478 } |
| 451 | 479 |
| 452 | 480 |
| 453 File::Identical File::AreIdentical(const char* file_1, const char* file_2) { | 481 File::Identical File::AreIdentical(const char* file_1, const char* file_2) { |
| 454 struct stat file_1_info; | 482 struct stat file_1_info; |
| 455 struct stat file_2_info; | 483 struct stat file_2_info; |
| 456 if (NO_RETRY_EXPECTED(lstat(file_1, &file_1_info)) == -1 || | 484 if ((NO_RETRY_EXPECTED(lstat(file_1, &file_1_info)) == -1) || |
| 457 NO_RETRY_EXPECTED(lstat(file_2, &file_2_info)) == -1) { | 485 (NO_RETRY_EXPECTED(lstat(file_2, &file_2_info)) == -1)) { |
| 458 return File::kError; | 486 return File::kError; |
| 459 } | 487 } |
| 460 return (file_1_info.st_ino == file_2_info.st_ino && | 488 return ((file_1_info.st_ino == file_2_info.st_ino) && |
| 461 file_1_info.st_dev == file_2_info.st_dev) ? | 489 (file_1_info.st_dev == file_2_info.st_dev)) ? |
| 462 File::kIdentical : | 490 File::kIdentical : |
| 463 File::kDifferent; | 491 File::kDifferent; |
| 464 } | 492 } |
| 465 | 493 |
| 466 } // namespace bin | 494 } // namespace bin |
| 467 } // namespace dart | 495 } // namespace dart |
| 468 | 496 |
| 469 #endif // defined(TARGET_OS_ANDROID) | 497 #endif // defined(TARGET_OS_ANDROID) |
| OLD | NEW |