Index: third_party/leveldatabase/env_chromium.cc |
diff --git a/third_party/leveldatabase/env_chromium.cc b/third_party/leveldatabase/env_chromium.cc |
index 17927cd89f7f55754cbacbdd5af788e66dea8d0e..3857be19499671e5a105f642608355bb79aa9be0 100644 |
--- a/third_party/leveldatabase/env_chromium.cc |
+++ b/third_party/leveldatabase/env_chromium.cc |
@@ -2,19 +2,46 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. See the AUTHORS file for names of contributors. |
+#include <errno.h> |
+#include <stdio.h> |
+#include <string.h> |
+ |
+#include <deque> |
+ |
+#include "base/at_exit.h" |
#include "base/debug/trace_event.h" |
#include "base/file_util.h" |
+#include "base/files/file_enumerator.h" |
+#include "base/files/file_path.h" |
#include "base/lazy_instance.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/message_loop/message_loop.h" |
#include "base/metrics/histogram.h" |
+#include "base/platform_file.h" |
+#include "base/posix/eintr_wrapper.h" |
#include "base/strings/utf_string_conversions.h" |
-#include "env_chromium_stdio.h" |
+#include "base/synchronization/lock.h" |
+#include "base/sys_info.h" |
+#include "base/threading/platform_thread.h" |
+#include "base/threading/thread.h" |
+#include "chromium_logger.h" |
+#include "env_chromium.h" |
+#include "leveldb/env.h" |
+#include "leveldb/slice.h" |
+#include "port/port.h" |
#include "third_party/re2/re2/re2.h" |
+#include "util/logging.h" |
#if defined(OS_WIN) |
#include <io.h> |
-#include "base/command_line.h" |
#include "base/win/win_util.h" |
-#include "env_chromium_win.h" |
+#endif |
+ |
+#if defined(OS_POSIX) |
+#include <dirent.h> |
+#include <fcntl.h> |
+#include <sys/resource.h> |
+#include <sys/time.h> |
#endif |
using namespace leveldb; |
@@ -27,8 +54,162 @@ |
FILE_PATH_LITERAL(".bak"); |
const base::FilePath::CharType table_extension[] = FILE_PATH_LITERAL(".ldb"); |
+#if (defined(OS_POSIX) && !defined(OS_LINUX)) || defined(OS_WIN) |
+// The following are glibc-specific |
+ |
+size_t fread_unlocked(void *ptr, size_t size, size_t n, FILE *file) { |
+ return fread(ptr, size, n, file); |
+} |
+ |
+size_t fwrite_unlocked(const void *ptr, size_t size, size_t n, FILE *file) { |
+ return fwrite(ptr, size, n, file); |
+} |
+ |
+int fflush_unlocked(FILE *file) { |
+ return fflush(file); |
+} |
+ |
+#if !defined(OS_ANDROID) |
+int fdatasync(int fildes) { |
+#if defined(OS_WIN) |
+ return _commit(fildes); |
+#else |
+ return HANDLE_EINTR(fsync(fildes)); |
+#endif |
+} |
+#endif |
+ |
+#endif |
+ |
+// Wide-char safe fopen wrapper. |
+FILE* fopen_internal(const char* fname, const char* mode) { |
+#if defined(OS_WIN) |
+ return _wfopen(base::UTF8ToUTF16(fname).c_str(), |
+ base::ASCIIToUTF16(mode).c_str()); |
+#else |
+ return fopen(fname, mode); |
+#endif |
+} |
+ |
+base::FilePath CreateFilePath(const std::string& file_path) { |
+#if defined(OS_WIN) |
+ return base::FilePath(base::UTF8ToUTF16(file_path)); |
+#else |
+ return base::FilePath(file_path); |
+#endif |
+} |
+ |
static const base::FilePath::CharType kLevelDBTestDirectoryPrefix[] |
= FILE_PATH_LITERAL("leveldb-test-"); |
+ |
+const char* PlatformFileErrorString(const ::base::PlatformFileError& error) { |
+ switch (error) { |
+ case ::base::PLATFORM_FILE_ERROR_FAILED: |
+ return "No further details."; |
+ case ::base::PLATFORM_FILE_ERROR_IN_USE: |
+ return "File currently in use."; |
+ case ::base::PLATFORM_FILE_ERROR_EXISTS: |
+ return "File already exists."; |
+ case ::base::PLATFORM_FILE_ERROR_NOT_FOUND: |
+ return "File not found."; |
+ case ::base::PLATFORM_FILE_ERROR_ACCESS_DENIED: |
+ return "Access denied."; |
+ case ::base::PLATFORM_FILE_ERROR_TOO_MANY_OPENED: |
+ return "Too many files open."; |
+ case ::base::PLATFORM_FILE_ERROR_NO_MEMORY: |
+ return "Out of memory."; |
+ case ::base::PLATFORM_FILE_ERROR_NO_SPACE: |
+ return "No space left on drive."; |
+ case ::base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY: |
+ return "Not a directory."; |
+ case ::base::PLATFORM_FILE_ERROR_INVALID_OPERATION: |
+ return "Invalid operation."; |
+ case ::base::PLATFORM_FILE_ERROR_SECURITY: |
+ return "Security error."; |
+ case ::base::PLATFORM_FILE_ERROR_ABORT: |
+ return "File operation aborted."; |
+ case ::base::PLATFORM_FILE_ERROR_NOT_A_FILE: |
+ return "The supplied path was not a file."; |
+ case ::base::PLATFORM_FILE_ERROR_NOT_EMPTY: |
+ return "The file was not empty."; |
+ case ::base::PLATFORM_FILE_ERROR_INVALID_URL: |
+ return "Invalid URL."; |
+ case ::base::PLATFORM_FILE_ERROR_IO: |
+ return "OS or hardware error."; |
+ case ::base::PLATFORM_FILE_OK: |
+ return "OK."; |
+ case ::base::PLATFORM_FILE_ERROR_MAX: |
+ NOTREACHED(); |
+ } |
+ NOTIMPLEMENTED(); |
+ return "Unknown error."; |
+} |
+ |
+class ChromiumSequentialFile: public SequentialFile { |
+ private: |
+ std::string filename_; |
+ FILE* file_; |
+ const UMALogger* uma_logger_; |
+ |
+ public: |
+ ChromiumSequentialFile(const std::string& fname, FILE* f, |
+ const UMALogger* uma_logger) |
+ : filename_(fname), file_(f), uma_logger_(uma_logger) { } |
+ virtual ~ChromiumSequentialFile() { fclose(file_); } |
+ |
+ virtual Status Read(size_t n, Slice* result, char* scratch) { |
+ Status s; |
+ size_t r = fread_unlocked(scratch, 1, n, file_); |
+ *result = Slice(scratch, r); |
+ if (r < n) { |
+ if (feof(file_)) { |
+ // We leave status as ok if we hit the end of the file |
+ } else { |
+ // A partial read with an error: return a non-ok status |
+ s = MakeIOError(filename_, strerror(errno), kSequentialFileRead, errno); |
+ uma_logger_->RecordErrorAt(kSequentialFileRead); |
+ } |
+ } |
+ return s; |
+ } |
+ |
+ virtual Status Skip(uint64_t n) { |
+ if (fseek(file_, n, SEEK_CUR)) { |
+ int saved_errno = errno; |
+ uma_logger_->RecordErrorAt(kSequentialFileSkip); |
+ return MakeIOError( |
+ filename_, strerror(saved_errno), kSequentialFileSkip, saved_errno); |
+ } |
+ return Status::OK(); |
+ } |
+}; |
+ |
+class ChromiumRandomAccessFile: public RandomAccessFile { |
+ private: |
+ std::string filename_; |
+ ::base::PlatformFile file_; |
+ const UMALogger* uma_logger_; |
+ |
+ public: |
+ ChromiumRandomAccessFile(const std::string& fname, ::base::PlatformFile file, |
+ const UMALogger* uma_logger) |
+ : filename_(fname), file_(file), uma_logger_(uma_logger) { } |
+ virtual ~ChromiumRandomAccessFile() { ::base::ClosePlatformFile(file_); } |
+ |
+ virtual Status Read(uint64_t offset, size_t n, Slice* result, |
+ char* scratch) const { |
+ Status s; |
+ int r = ::base::ReadPlatformFile(file_, offset, scratch, n); |
+ *result = Slice(scratch, (r < 0) ? 0 : r); |
+ if (r < 0) { |
+ // An error: return a non-ok status |
+ s = MakeIOError( |
+ filename_, "Could not perform read", kRandomAccessFileRead); |
+ uma_logger_->RecordErrorAt(kRandomAccessFileRead); |
+ } |
+ return s; |
+ } |
+}; |
class ChromiumFileLock : public FileLock { |
public: |
@@ -80,33 +261,17 @@ |
RetrierProvider* provider_; |
}; |
-class IDBEnvStdio : public ChromiumEnvStdio { |
+class IDBEnv : public ChromiumEnv { |
public: |
- IDBEnvStdio() : ChromiumEnvStdio() { |
+ IDBEnv() : ChromiumEnv() { |
name_ = "LevelDBEnv.IDB"; |
make_backup_ = true; |
} |
}; |
-#if defined(OS_WIN) |
-class IDBEnvWin : public ChromiumEnvWin { |
- public: |
- IDBEnvWin() : ChromiumEnvWin() { |
- name_ = "LevelDBEnv.IDB"; |
- make_backup_ = true; |
- } |
-}; |
-#endif |
- |
-#if defined(OS_WIN) |
-::base::LazyInstance<IDBEnvWin>::Leaky idb_env = |
- LAZY_INSTANCE_INITIALIZER; |
-#else |
-::base::LazyInstance<IDBEnvStdio>::Leaky idb_env = |
- LAZY_INSTANCE_INITIALIZER; |
-#endif |
- |
-::base::LazyInstance<ChromiumEnvStdio>::Leaky default_env = |
+::base::LazyInstance<IDBEnv>::Leaky idb_env = LAZY_INSTANCE_INITIALIZER; |
+ |
+::base::LazyInstance<ChromiumEnv>::Leaky default_env = |
LAZY_INSTANCE_INITIALIZER; |
} // unnamed namespace |
@@ -329,24 +494,122 @@ |
#endif |
} |
-base::FilePath ChromiumEnv::CreateFilePath(const std::string& file_path) { |
-#if defined(OS_WIN) |
- return base::FilePath(base::UTF8ToUTF16(file_path)); |
-#else |
- return base::FilePath(file_path); |
+ChromiumWritableFile::ChromiumWritableFile(const std::string& fname, |
+ FILE* f, |
+ const UMALogger* uma_logger, |
+ WriteTracker* tracker, |
+ bool make_backup) |
+ : filename_(fname), |
+ file_(f), |
+ uma_logger_(uma_logger), |
+ tracker_(tracker), |
+ file_type_(kOther), |
+ make_backup_(make_backup) { |
+ base::FilePath path = base::FilePath::FromUTF8Unsafe(fname); |
+ if (FilePathToString(path.BaseName()).find("MANIFEST") == 0) |
+ file_type_ = kManifest; |
+ else if (path.MatchesExtension(table_extension)) |
+ file_type_ = kTable; |
+ if (file_type_ != kManifest) |
+ tracker_->DidCreateNewFile(filename_); |
+ parent_dir_ = FilePathToString(CreateFilePath(fname).DirName()); |
+} |
+ |
+ChromiumWritableFile::~ChromiumWritableFile() { |
+ if (file_ != NULL) { |
+ // Ignoring any potential errors |
+ fclose(file_); |
+ } |
+} |
+ |
+Status ChromiumWritableFile::SyncParent() { |
+ Status s; |
+#if !defined(OS_WIN) |
+ TRACE_EVENT0("leveldb", "SyncParent"); |
+ |
+ int parent_fd = |
+ HANDLE_EINTR(open(parent_dir_.c_str(), O_RDONLY)); |
+ if (parent_fd < 0) { |
+ int saved_errno = errno; |
+ return MakeIOError( |
+ parent_dir_, strerror(saved_errno), kSyncParent, saved_errno); |
+ } |
+ if (HANDLE_EINTR(fsync(parent_fd)) != 0) { |
+ int saved_errno = errno; |
+ s = MakeIOError( |
+ parent_dir_, strerror(saved_errno), kSyncParent, saved_errno); |
+ }; |
+ close(parent_fd); |
#endif |
-} |
- |
-bool ChromiumEnv::MakeBackup(const std::string& fname) { |
+ return s; |
+} |
+ |
+Status ChromiumWritableFile::Append(const Slice& data) { |
+ if (file_type_ == kManifest && tracker_->DoesDirNeedSync(filename_)) { |
+ Status s = SyncParent(); |
+ if (!s.ok()) |
+ return s; |
+ tracker_->DidSyncDir(filename_); |
+ } |
+ |
+ size_t r = fwrite_unlocked(data.data(), 1, data.size(), file_); |
+ if (r != data.size()) { |
+ int saved_errno = errno; |
+ uma_logger_->RecordOSError(kWritableFileAppend, saved_errno); |
+ return MakeIOError( |
+ filename_, strerror(saved_errno), kWritableFileAppend, saved_errno); |
+ } |
+ return Status::OK(); |
+} |
+ |
+Status ChromiumWritableFile::Close() { |
+ Status result; |
+ if (fclose(file_) != 0) { |
+ result = MakeIOError(filename_, strerror(errno), kWritableFileClose, errno); |
+ uma_logger_->RecordErrorAt(kWritableFileClose); |
+ } |
+ file_ = NULL; |
+ return result; |
+} |
+ |
+Status ChromiumWritableFile::Flush() { |
+ Status result; |
+ if (HANDLE_EINTR(fflush_unlocked(file_))) { |
+ int saved_errno = errno; |
+ result = MakeIOError( |
+ filename_, strerror(saved_errno), kWritableFileFlush, saved_errno); |
+ uma_logger_->RecordOSError(kWritableFileFlush, saved_errno); |
+ } |
+ return result; |
+} |
+ |
+static bool MakeBackup(const std::string& fname) { |
base::FilePath original_table_name = CreateFilePath(fname); |
base::FilePath backup_table_name = |
original_table_name.ReplaceExtension(backup_table_extension); |
return base::CopyFile(original_table_name, backup_table_name); |
} |
-bool ChromiumEnv::HasTableExtension(const base::FilePath& path) |
-{ |
- return path.MatchesExtension(table_extension); |
+Status ChromiumWritableFile::Sync() { |
+ TRACE_EVENT0("leveldb", "ChromiumEnv::Sync"); |
+ Status result; |
+ int error = 0; |
+ |
+ if (HANDLE_EINTR(fflush_unlocked(file_))) |
+ error = errno; |
+ // Sync even if fflush gave an error; perhaps the data actually got out, |
+ // even though something went wrong. |
+ if (fdatasync(fileno(file_)) && !error) |
+ error = errno; |
+ // Report the first error we found. |
+ if (error) { |
+ result = MakeIOError(filename_, strerror(error), kWritableFileSync, error); |
+ uma_logger_->RecordErrorAt(kWritableFileSync); |
+ } else if (make_backup_ && file_type_ == kTable) { |
+ bool success = MakeBackup(filename_); |
+ uma_logger_->RecordBackupResult(success); |
+ } |
+ return result; |
} |
ChromiumEnv::ChromiumEnv() |
@@ -363,51 +626,71 @@ |
// a unit test that is deleted. |
} |
+Status ChromiumEnv::NewSequentialFile(const std::string& fname, |
+ SequentialFile** result) { |
+ FILE* f = fopen_internal(fname.c_str(), "rb"); |
+ if (f == NULL) { |
+ *result = NULL; |
+ int saved_errno = errno; |
+ RecordOSError(kNewSequentialFile, saved_errno); |
+ return MakeIOError( |
+ fname, strerror(saved_errno), kNewSequentialFile, saved_errno); |
+ } else { |
+ *result = new ChromiumSequentialFile(fname, f, this); |
+ return Status::OK(); |
+ } |
+} |
+ |
+void ChromiumEnv::RecordOpenFilesLimit(const std::string& type) { |
+#if defined(OS_POSIX) |
+ struct rlimit nofile; |
+ if (getrlimit(RLIMIT_NOFILE, &nofile)) |
+ return; |
+ GetMaxFDHistogram(type)->Add(nofile.rlim_cur); |
+#endif |
+} |
+ |
+Status ChromiumEnv::NewRandomAccessFile(const std::string& fname, |
+ RandomAccessFile** result) { |
+ int flags = ::base::PLATFORM_FILE_READ | ::base::PLATFORM_FILE_OPEN; |
+ bool created; |
+ ::base::PlatformFileError error_code; |
+ ::base::PlatformFile file = ::base::CreatePlatformFile( |
+ CreateFilePath(fname), flags, &created, &error_code); |
+ if (error_code == ::base::PLATFORM_FILE_OK) { |
+ *result = new ChromiumRandomAccessFile(fname, file, this); |
+ RecordOpenFilesLimit("Success"); |
+ return Status::OK(); |
+ } |
+ if (error_code == ::base::PLATFORM_FILE_ERROR_TOO_MANY_OPENED) |
+ RecordOpenFilesLimit("TooManyOpened"); |
+ else |
+ RecordOpenFilesLimit("OtherError"); |
+ *result = NULL; |
+ RecordOSError(kNewRandomAccessFile, error_code); |
+ return MakeIOError(fname, |
+ PlatformFileErrorString(error_code), |
+ kNewRandomAccessFile, |
+ error_code); |
+} |
+ |
+Status ChromiumEnv::NewWritableFile(const std::string& fname, |
+ WritableFile** result) { |
+ *result = NULL; |
+ FILE* f = fopen_internal(fname.c_str(), "wb"); |
+ if (f == NULL) { |
+ int saved_errno = errno; |
+ RecordErrorAt(kNewWritableFile); |
+ return MakeIOError( |
+ fname, strerror(saved_errno), kNewWritableFile, saved_errno); |
+ } else { |
+ *result = new ChromiumWritableFile(fname, f, this, this, make_backup_); |
+ return Status::OK(); |
+ } |
+} |
+ |
bool ChromiumEnv::FileExists(const std::string& fname) { |
return ::base::PathExists(CreateFilePath(fname)); |
-} |
- |
-const char* ChromiumEnv::PlatformFileErrorString(const ::base::PlatformFileError& error) { |
- switch (error) { |
- case ::base::PLATFORM_FILE_ERROR_FAILED: |
- return "No further details."; |
- case ::base::PLATFORM_FILE_ERROR_IN_USE: |
- return "File currently in use."; |
- case ::base::PLATFORM_FILE_ERROR_EXISTS: |
- return "File already exists."; |
- case ::base::PLATFORM_FILE_ERROR_NOT_FOUND: |
- return "File not found."; |
- case ::base::PLATFORM_FILE_ERROR_ACCESS_DENIED: |
- return "Access denied."; |
- case ::base::PLATFORM_FILE_ERROR_TOO_MANY_OPENED: |
- return "Too many files open."; |
- case ::base::PLATFORM_FILE_ERROR_NO_MEMORY: |
- return "Out of memory."; |
- case ::base::PLATFORM_FILE_ERROR_NO_SPACE: |
- return "No space left on drive."; |
- case ::base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY: |
- return "Not a directory."; |
- case ::base::PLATFORM_FILE_ERROR_INVALID_OPERATION: |
- return "Invalid operation."; |
- case ::base::PLATFORM_FILE_ERROR_SECURITY: |
- return "Security error."; |
- case ::base::PLATFORM_FILE_ERROR_ABORT: |
- return "File operation aborted."; |
- case ::base::PLATFORM_FILE_ERROR_NOT_A_FILE: |
- return "The supplied path was not a file."; |
- case ::base::PLATFORM_FILE_ERROR_NOT_EMPTY: |
- return "The file was not empty."; |
- case ::base::PLATFORM_FILE_ERROR_INVALID_URL: |
- return "Invalid URL."; |
- case ::base::PLATFORM_FILE_ERROR_IO: |
- return "OS or hardware error."; |
- case ::base::PLATFORM_FILE_OK: |
- return "OK."; |
- case ::base::PLATFORM_FILE_ERROR_MAX: |
- NOTREACHED(); |
- } |
- NOTIMPLEMENTED(); |
- return "Unknown error."; |
} |
base::FilePath ChromiumEnv::RestoreFromBackup(const base::FilePath& base_name) { |
@@ -463,6 +746,62 @@ |
} |
} |
+namespace { |
+#if defined(OS_WIN) |
+static base::PlatformFileError GetDirectoryEntries( |
+ const base::FilePath& dir_param, |
+ std::vector<base::FilePath>* result) { |
+ result->clear(); |
+ base::FilePath dir_filepath = dir_param.Append(FILE_PATH_LITERAL("*")); |
+ WIN32_FIND_DATA find_data; |
+ HANDLE find_handle = FindFirstFile(dir_filepath.value().c_str(), &find_data); |
+ if (find_handle == INVALID_HANDLE_VALUE) { |
+ DWORD last_error = GetLastError(); |
+ if (last_error == ERROR_FILE_NOT_FOUND) |
+ return base::PLATFORM_FILE_OK; |
+ return base::LastErrorToPlatformFileError(last_error); |
+ } |
+ do { |
+ base::FilePath filepath(find_data.cFileName); |
+ base::FilePath::StringType basename = filepath.BaseName().value(); |
+ if (basename == FILE_PATH_LITERAL(".") || |
+ basename == FILE_PATH_LITERAL("..")) |
+ continue; |
+ result->push_back(filepath.BaseName()); |
+ } while (FindNextFile(find_handle, &find_data)); |
+ DWORD last_error = GetLastError(); |
+ base::PlatformFileError return_value = base::PLATFORM_FILE_OK; |
+ if (last_error != ERROR_NO_MORE_FILES) |
+ return_value = base::LastErrorToPlatformFileError(last_error); |
+ FindClose(find_handle); |
+ return return_value; |
+} |
+#else |
+static base::PlatformFileError GetDirectoryEntries( |
+ const base::FilePath& dir_filepath, |
+ std::vector<base::FilePath>* result) { |
+ const std::string dir_string = FilePathToString(dir_filepath); |
+ result->clear(); |
+ DIR* dir = opendir(dir_string.c_str()); |
+ if (!dir) |
+ return base::ErrnoToPlatformFileError(errno); |
+ struct dirent dent_buf; |
+ struct dirent* dent; |
+ int readdir_result; |
+ while ((readdir_result = readdir_r(dir, &dent_buf, &dent)) == 0 && dent) { |
+ if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) |
+ continue; |
+ result->push_back(CreateFilePath(dent->d_name)); |
+ } |
+ int saved_errno = errno; |
+ closedir(dir); |
+ if (readdir_result != 0) |
+ return base::ErrnoToPlatformFileError(saved_errno); |
+ return base::PLATFORM_FILE_OK; |
+} |
+#endif |
+} |
+ |
Status ChromiumEnv::GetChildren(const std::string& dir_string, |
std::vector<std::string>* result) { |
std::vector<base::FilePath> entries; |
@@ -670,6 +1009,19 @@ |
return Status::OK(); |
} |
+Status ChromiumEnv::NewLogger(const std::string& fname, Logger** result) { |
+ FILE* f = fopen_internal(fname.c_str(), "w"); |
+ if (f == NULL) { |
+ *result = NULL; |
+ int saved_errno = errno; |
+ RecordOSError(kNewLogger, saved_errno); |
+ return MakeIOError(fname, strerror(saved_errno), kNewLogger, saved_errno); |
+ } else { |
+ *result = new ChromiumLogger(f); |
+ return Status::OK(); |
+ } |
+} |
+ |
uint64_t ChromiumEnv::NowMicros() { |
return ::base::TimeTicks::Now().ToInternalValue(); |
} |