| OLD | NEW |
| 1 // Copyright (c) 2011-2013 The LevelDB Authors. All rights reserved. | 1 // Copyright (c) 2011-2013 The LevelDB Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. See the AUTHORS file for names of contributors. | 3 // found in the LICENSE file. See the AUTHORS file for names of contributors. |
| 4 | 4 |
| 5 #include <errno.h> | 5 #include <errno.h> |
| 6 | 6 |
| 7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/posix/eintr_wrapper.h" | 9 #include "base/posix/eintr_wrapper.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 return MakeIOError( | 99 return MakeIOError( |
| 100 filename_, strerror(saved_errno), kSequentialFileSkip, saved_errno); | 100 filename_, strerror(saved_errno), kSequentialFileSkip, saved_errno); |
| 101 } | 101 } |
| 102 return Status::OK(); | 102 return Status::OK(); |
| 103 } | 103 } |
| 104 }; | 104 }; |
| 105 | 105 |
| 106 class ChromiumRandomAccessFile : public RandomAccessFile { | 106 class ChromiumRandomAccessFile : public RandomAccessFile { |
| 107 private: | 107 private: |
| 108 std::string filename_; | 108 std::string filename_; |
| 109 ::base::PlatformFile file_; | 109 mutable ::base::File file_; |
| 110 const UMALogger* uma_logger_; | 110 const UMALogger* uma_logger_; |
| 111 | 111 |
| 112 public: | 112 public: |
| 113 ChromiumRandomAccessFile(const std::string& fname, | 113 ChromiumRandomAccessFile(const std::string& fname, |
| 114 ::base::PlatformFile file, | 114 ::base::File file, |
| 115 const UMALogger* uma_logger) | 115 const UMALogger* uma_logger) |
| 116 : filename_(fname), file_(file), uma_logger_(uma_logger) {} | 116 : filename_(fname), file_(file.Pass()), uma_logger_(uma_logger) {} |
| 117 virtual ~ChromiumRandomAccessFile() { ::base::ClosePlatformFile(file_); } | 117 virtual ~ChromiumRandomAccessFile() {} |
| 118 | 118 |
| 119 virtual Status Read(uint64_t offset, size_t n, Slice* result, char* scratch) | 119 virtual Status Read(uint64_t offset, size_t n, Slice* result, char* scratch) |
| 120 const { | 120 const { |
| 121 Status s; | 121 Status s; |
| 122 int r = ::base::ReadPlatformFile(file_, offset, scratch, n); | 122 int r = file_.Read(offset, scratch, n); |
| 123 *result = Slice(scratch, (r < 0) ? 0 : r); | 123 *result = Slice(scratch, (r < 0) ? 0 : r); |
| 124 if (r < 0) { | 124 if (r < 0) { |
| 125 // An error: return a non-ok status | 125 // An error: return a non-ok status |
| 126 s = MakeIOError( | 126 s = MakeIOError( |
| 127 filename_, "Could not perform read", kRandomAccessFileRead); | 127 filename_, "Could not perform read", kRandomAccessFileRead); |
| 128 uma_logger_->RecordErrorAt(kRandomAccessFileRead); | 128 uma_logger_->RecordErrorAt(kRandomAccessFileRead); |
| 129 } | 129 } |
| 130 return s; | 130 return s; |
| 131 } | 131 } |
| 132 }; | 132 }; |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 #if defined(OS_POSIX) | 266 #if defined(OS_POSIX) |
| 267 struct rlimit nofile; | 267 struct rlimit nofile; |
| 268 if (getrlimit(RLIMIT_NOFILE, &nofile)) | 268 if (getrlimit(RLIMIT_NOFILE, &nofile)) |
| 269 return; | 269 return; |
| 270 GetMaxFDHistogram(type)->Add(nofile.rlim_cur); | 270 GetMaxFDHistogram(type)->Add(nofile.rlim_cur); |
| 271 #endif | 271 #endif |
| 272 } | 272 } |
| 273 | 273 |
| 274 Status ChromiumEnvStdio::NewRandomAccessFile(const std::string& fname, | 274 Status ChromiumEnvStdio::NewRandomAccessFile(const std::string& fname, |
| 275 RandomAccessFile** result) { | 275 RandomAccessFile** result) { |
| 276 int flags = ::base::PLATFORM_FILE_READ | ::base::PLATFORM_FILE_OPEN; | 276 int flags = ::base::File::FLAG_READ | ::base::File::FLAG_OPEN; |
| 277 bool created; | 277 ::base::File file(ChromiumEnv::CreateFilePath(fname), flags); |
| 278 ::base::PlatformFileError error_code; | 278 if (file.IsValid()) { |
| 279 ::base::PlatformFile file = ::base::CreatePlatformFile( | 279 *result = new ChromiumRandomAccessFile(fname, file.Pass(), this); |
| 280 ChromiumEnv::CreateFilePath(fname), flags, &created, &error_code); | |
| 281 if (error_code == ::base::PLATFORM_FILE_OK) { | |
| 282 *result = new ChromiumRandomAccessFile(fname, file, this); | |
| 283 RecordOpenFilesLimit("Success"); | 280 RecordOpenFilesLimit("Success"); |
| 284 return Status::OK(); | 281 return Status::OK(); |
| 285 } | 282 } |
| 286 if (error_code == ::base::PLATFORM_FILE_ERROR_TOO_MANY_OPENED) | 283 ::base::File::Error error_code = file.error_details(); |
| 284 if (error_code == ::base::File::FILE_ERROR_TOO_MANY_OPENED) |
| 287 RecordOpenFilesLimit("TooManyOpened"); | 285 RecordOpenFilesLimit("TooManyOpened"); |
| 288 else | 286 else |
| 289 RecordOpenFilesLimit("OtherError"); | 287 RecordOpenFilesLimit("OtherError"); |
| 290 *result = NULL; | 288 *result = NULL; |
| 291 RecordOSError(kNewRandomAccessFile, error_code); | 289 RecordOSError(kNewRandomAccessFile, error_code); |
| 292 return MakeIOError(fname, | 290 return MakeIOError(fname, |
| 293 PlatformFileErrorString(error_code), | 291 FileErrorString(error_code), |
| 294 kNewRandomAccessFile, | 292 kNewRandomAccessFile, |
| 295 error_code); | 293 error_code); |
| 296 } | 294 } |
| 297 | 295 |
| 298 Status ChromiumEnvStdio::NewWritableFile(const std::string& fname, | 296 Status ChromiumEnvStdio::NewWritableFile(const std::string& fname, |
| 299 WritableFile** result) { | 297 WritableFile** result) { |
| 300 *result = NULL; | 298 *result = NULL; |
| 301 FILE* f = fopen_internal(fname.c_str(), "wb"); | 299 FILE* f = fopen_internal(fname.c_str(), "wb"); |
| 302 if (f == NULL) { | 300 if (f == NULL) { |
| 303 int saved_errno = errno; | 301 int saved_errno = errno; |
| 304 RecordErrorAt(kNewWritableFile); | 302 RecordErrorAt(kNewWritableFile); |
| 305 return MakeIOError( | 303 return MakeIOError( |
| 306 fname, strerror(saved_errno), kNewWritableFile, saved_errno); | 304 fname, strerror(saved_errno), kNewWritableFile, saved_errno); |
| 307 } else { | 305 } else { |
| 308 *result = new ChromiumWritableFile(fname, f, this, this, make_backup_); | 306 *result = new ChromiumWritableFile(fname, f, this, this, make_backup_); |
| 309 return Status::OK(); | 307 return Status::OK(); |
| 310 } | 308 } |
| 311 } | 309 } |
| 312 | 310 |
| 313 #if defined(OS_WIN) | 311 #if defined(OS_WIN) |
| 314 base::PlatformFileError ChromiumEnvStdio::GetDirectoryEntries( | 312 base::File::Error ChromiumEnvStdio::GetDirectoryEntries( |
| 315 const base::FilePath& dir_param, | 313 const base::FilePath& dir_param, |
| 316 std::vector<base::FilePath>* result) const { | 314 std::vector<base::FilePath>* result) const { |
| 317 result->clear(); | 315 result->clear(); |
| 318 base::FilePath dir_filepath = dir_param.Append(FILE_PATH_LITERAL("*")); | 316 base::FilePath dir_filepath = dir_param.Append(FILE_PATH_LITERAL("*")); |
| 319 WIN32_FIND_DATA find_data; | 317 WIN32_FIND_DATA find_data; |
| 320 HANDLE find_handle = FindFirstFile(dir_filepath.value().c_str(), &find_data); | 318 HANDLE find_handle = FindFirstFile(dir_filepath.value().c_str(), &find_data); |
| 321 if (find_handle == INVALID_HANDLE_VALUE) { | 319 if (find_handle == INVALID_HANDLE_VALUE) { |
| 322 DWORD last_error = GetLastError(); | 320 DWORD last_error = GetLastError(); |
| 323 if (last_error == ERROR_FILE_NOT_FOUND) | 321 if (last_error == ERROR_FILE_NOT_FOUND) |
| 324 return base::PLATFORM_FILE_OK; | 322 return base::File::FILE_OK; |
| 325 return base::LastErrorToPlatformFileError(last_error); | 323 return base::File::OSErrorToFileError(last_error); |
| 326 } | 324 } |
| 327 do { | 325 do { |
| 328 base::FilePath filepath(find_data.cFileName); | 326 base::FilePath filepath(find_data.cFileName); |
| 329 base::FilePath::StringType basename = filepath.BaseName().value(); | 327 base::FilePath::StringType basename = filepath.BaseName().value(); |
| 330 if (basename == FILE_PATH_LITERAL(".") || | 328 if (basename == FILE_PATH_LITERAL(".") || |
| 331 basename == FILE_PATH_LITERAL("..")) | 329 basename == FILE_PATH_LITERAL("..")) |
| 332 continue; | 330 continue; |
| 333 result->push_back(filepath.BaseName()); | 331 result->push_back(filepath.BaseName()); |
| 334 } while (FindNextFile(find_handle, &find_data)); | 332 } while (FindNextFile(find_handle, &find_data)); |
| 335 DWORD last_error = GetLastError(); | 333 DWORD last_error = GetLastError(); |
| 336 base::PlatformFileError return_value = base::PLATFORM_FILE_OK; | 334 base::File::Error return_value = base::File::FILE_OK; |
| 337 if (last_error != ERROR_NO_MORE_FILES) | 335 if (last_error != ERROR_NO_MORE_FILES) |
| 338 return_value = base::LastErrorToPlatformFileError(last_error); | 336 return_value = base::File::OSErrorToFileError(last_error); |
| 339 FindClose(find_handle); | 337 FindClose(find_handle); |
| 340 return return_value; | 338 return return_value; |
| 341 } | 339 } |
| 342 #else | 340 #else |
| 343 base::PlatformFileError ChromiumEnvStdio::GetDirectoryEntries( | 341 base::File::Error ChromiumEnvStdio::GetDirectoryEntries( |
| 344 const base::FilePath& dir_filepath, | 342 const base::FilePath& dir_filepath, |
| 345 std::vector<base::FilePath>* result) const { | 343 std::vector<base::FilePath>* result) const { |
| 346 const std::string dir_string = FilePathToString(dir_filepath); | 344 const std::string dir_string = FilePathToString(dir_filepath); |
| 347 result->clear(); | 345 result->clear(); |
| 348 DIR* dir = opendir(dir_string.c_str()); | 346 DIR* dir = opendir(dir_string.c_str()); |
| 349 if (!dir) | 347 if (!dir) |
| 350 return base::ErrnoToPlatformFileError(errno); | 348 return base::File::OSErrorToFileError(errno); |
| 351 struct dirent dent_buf; | 349 struct dirent dent_buf; |
| 352 struct dirent* dent; | 350 struct dirent* dent; |
| 353 int readdir_result; | 351 int readdir_result; |
| 354 while ((readdir_result = readdir_r(dir, &dent_buf, &dent)) == 0 && dent) { | 352 while ((readdir_result = readdir_r(dir, &dent_buf, &dent)) == 0 && dent) { |
| 355 if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) | 353 if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) |
| 356 continue; | 354 continue; |
| 357 result->push_back(ChromiumEnv::CreateFilePath(dent->d_name)); | 355 result->push_back(ChromiumEnv::CreateFilePath(dent->d_name)); |
| 358 } | 356 } |
| 359 int saved_errno = errno; | 357 int saved_errno = errno; |
| 360 closedir(dir); | 358 closedir(dir); |
| 361 if (readdir_result != 0) | 359 if (readdir_result != 0) |
| 362 return base::ErrnoToPlatformFileError(saved_errno); | 360 return base::File::OSErrorToFileError(saved_errno); |
| 363 return base::PLATFORM_FILE_OK; | 361 return base::File::FILE_OK; |
| 364 } | 362 } |
| 365 #endif | 363 #endif |
| 366 | 364 |
| 367 Status ChromiumEnvStdio::NewLogger(const std::string& fname, Logger** result) { | 365 Status ChromiumEnvStdio::NewLogger(const std::string& fname, Logger** result) { |
| 368 FILE* f = fopen_internal(fname.c_str(), "w"); | 366 FILE* f = fopen_internal(fname.c_str(), "w"); |
| 369 if (f == NULL) { | 367 if (f == NULL) { |
| 370 *result = NULL; | 368 *result = NULL; |
| 371 int saved_errno = errno; | 369 int saved_errno = errno; |
| 372 RecordOSError(kNewLogger, saved_errno); | 370 RecordOSError(kNewLogger, saved_errno); |
| 373 return MakeIOError(fname, strerror(saved_errno), kNewLogger, saved_errno); | 371 return MakeIOError(fname, strerror(saved_errno), kNewLogger, saved_errno); |
| 374 } else { | 372 } else { |
| 375 *result = new ChromiumLogger(f); | 373 *result = new ChromiumLogger(f); |
| 376 return Status::OK(); | 374 return Status::OK(); |
| 377 } | 375 } |
| 378 } | 376 } |
| 379 | 377 |
| 380 } // namespace leveldb_env | 378 } // namespace leveldb_env |
| OLD | NEW |