Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved. | |
| 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. | |
| 4 | |
| 5 #include "base/debug/trace_event.h" | |
| 6 #include "base/files/file_path.h" | |
| 7 #include "base/posix/eintr_wrapper.h" | |
| 8 #include "base/strings/utf_string_conversions.h" | |
| 9 #include "chromium_logger.h" | |
| 10 #include "env_chromium_posix.h" | |
| 11 #include "env_chromium_win32.h" | |
| 12 | |
| 13 #if defined(OS_WIN) | |
| 14 #include <io.h> | |
| 15 #include "base/win/win_util.h" | |
| 16 #endif | |
| 17 | |
| 18 #if defined(OS_POSIX) | |
| 19 #include <dirent.h> | |
| 20 #include <fcntl.h> | |
| 21 #include <sys/resource.h> | |
| 22 #endif | |
| 23 | |
| 24 using namespace leveldb; | |
| 25 | |
| 26 namespace leveldb_env { | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 static std::string GetWin32ErrorMessage(DWORD err) | |
| 31 { | |
| 32 LPTSTR errorText(NULL); | |
| 33 FormatMessage( | |
| 34 FORMAT_MESSAGE_FROM_SYSTEM | |
| 35 |FORMAT_MESSAGE_ALLOCATE_BUFFER | |
|
jsbell
2013/12/12 01:11:42
nit: spaces around the | operator, and wrap the li
cmumford
2013/12/12 17:40:51
thx. didn't know abot git cl format. Next patch wi
| |
| 36 |FORMAT_MESSAGE_IGNORE_INSERTS, | |
| 37 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
| 38 (LPTSTR)&errorText, 0, NULL); | |
| 39 if (errorText != NULL) { | |
| 40 if (errorText[0] != L'\0') { | |
|
jsbell
2013/12/12 19:03:14
Is there a reason to expect trailing whitespace he
cmumford
2013/12/12 20:08:39
Yes, FormatMessage adds CR/LF :-(, but you are cor
| |
| 41 size_t idx = wcslen(errorText) - 1; | |
| 42 while (idx >= 0 && iswspace(errorText[idx])) { | |
| 43 errorText[idx] = L'\0'; | |
| 44 --idx; | |
| 45 } | |
| 46 } | |
| 47 std::string message(UTF16ToUTF8(errorText)); | |
| 48 LocalFree(errorText); | |
| 49 return message; | |
| 50 } | |
| 51 else { | |
| 52 return std::string(); | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 class ChromiumSequentialFile: public SequentialFile { | |
|
jsbell
2013/12/12 19:03:14
I'm slightly nervous that the re-use of this name
cmumford
2013/12/12 20:08:39
Good point - I'll change it. Will do the same for
| |
| 57 private: | |
| 58 std::string filename_; | |
| 59 HANDLE file_; | |
| 60 const UMALogger* uma_logger_; | |
| 61 | |
| 62 public: | |
| 63 ChromiumSequentialFile(const std::string& fname, HANDLE f, | |
| 64 const UMALogger* uma_logger) | |
| 65 : filename_(fname), file_(f), uma_logger_(uma_logger) { | |
| 66 DCHECK(file_ != INVALID_HANDLE_VALUE); | |
| 67 } | |
| 68 virtual ~ChromiumSequentialFile() { | |
| 69 DCHECK(file_ != INVALID_HANDLE_VALUE); | |
| 70 CloseHandle(file_); | |
| 71 file_ = INVALID_HANDLE_VALUE; | |
| 72 } | |
| 73 | |
| 74 virtual Status Read(size_t n, Slice* result, char* scratch) { | |
| 75 Status s; | |
| 76 DWORD bytes_read(0); | |
| 77 DCHECK(file_ != INVALID_HANDLE_VALUE); | |
| 78 if (ReadFile(file_, scratch, n, &bytes_read, NULL)) { | |
| 79 *result = Slice(scratch, bytes_read); | |
| 80 } | |
| 81 else { | |
| 82 DWORD err = GetLastError(); | |
| 83 s = MakeIOErrorWin32( | |
| 84 filename_, GetWin32ErrorMessage(err), kSequentialFileRead, err); | |
| 85 uma_logger_->RecordErrorAt(kSequentialFileRead); | |
| 86 if (bytes_read > 0) | |
| 87 *result = Slice(scratch, bytes_read); | |
| 88 } | |
| 89 return s; | |
| 90 } | |
| 91 | |
| 92 virtual Status Skip(uint64_t n) { | |
| 93 LARGE_INTEGER li; | |
| 94 li.QuadPart = n; | |
| 95 if (SetFilePointer(file_, li.LowPart, &li.HighPart, FILE_CURRENT) == | |
| 96 INVALID_SET_FILE_POINTER) { | |
| 97 DWORD err = GetLastError(); | |
| 98 uma_logger_->RecordErrorAt(kSequentialFileSkip); | |
| 99 return MakeIOErrorWin32( | |
| 100 filename_, GetWin32ErrorMessage(err), kSequentialFileSkip, err); | |
| 101 } | |
| 102 return Status::OK(); | |
| 103 } | |
| 104 }; | |
| 105 | |
| 106 class ChromiumRandomAccessFile: public RandomAccessFile { | |
| 107 private: | |
| 108 std::string filename_; | |
| 109 ::base::PlatformFile file_; | |
|
jsbell
2013/12/12 01:11:42
Isn't the point of this patch to use Windows APIs
cmumford
2013/12/12 17:40:51
I can do that if you prefer. This is the first sen
jsbell
2013/12/12 19:03:14
I think we should go farther; one of our hypothese
| |
| 110 const UMALogger* uma_logger_; | |
| 111 | |
| 112 public: | |
| 113 ChromiumRandomAccessFile(const std::string& fname, ::base::PlatformFile file, | |
| 114 const UMALogger* uma_logger) | |
| 115 : filename_(fname), file_(file), uma_logger_(uma_logger) { } | |
| 116 virtual ~ChromiumRandomAccessFile() { ::base::ClosePlatformFile(file_); } | |
| 117 | |
| 118 virtual Status Read(uint64_t offset, size_t n, Slice* result, | |
| 119 char* scratch) const { | |
| 120 Status s; | |
| 121 int r = ::base::ReadPlatformFile(file_, offset, scratch, n); | |
| 122 *result = Slice(scratch, (r < 0) ? 0 : r); | |
| 123 if (r < 0) { | |
| 124 // An error: return a non-ok status | |
| 125 s = MakeIOError( | |
| 126 filename_, "Could not perform read", kRandomAccessFileRead); | |
| 127 uma_logger_->RecordErrorAt(kRandomAccessFileRead); | |
| 128 } | |
| 129 return s; | |
| 130 } | |
| 131 }; | |
| 132 | |
| 133 } // unnamed namespace | |
| 134 | |
| 135 Status MakeIOErrorWin32(Slice filename, | |
| 136 const std::string& message, | |
|
jsbell
2013/12/12 01:11:42
indentation
| |
| 137 MethodID method, | |
| 138 DWORD error) { | |
| 139 char buf[512]; | |
| 140 if (snprintf(buf, | |
| 141 sizeof(buf), | |
|
jsbell
2013/12/12 01:11:42
indentation
| |
| 142 "%s (ChromeMethodErrno: %d::%s::%u)", | |
| 143 message.c_str(), | |
| 144 method, | |
| 145 MethodIDToString(method), | |
| 146 error) >= 0) { | |
| 147 return Status::IOError(filename, buf); | |
| 148 } | |
| 149 else { | |
|
jsbell
2013/12/12 01:11:42
else should be on the same line as the closing bra
| |
| 150 return Status::IOError(filename, "<unknown>"); | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 ChromiumWritableFileWin32::ChromiumWritableFileWin32(const std::string& fname, | |
| 155 HANDLE f, | |
|
jsbell
2013/12/12 01:11:42
indentation (etc)
| |
| 156 const UMALogger* uma_logger, | |
| 157 WriteTracker* tracker, | |
| 158 bool make_backup) | |
| 159 : filename_(fname), | |
| 160 file_(f), | |
| 161 uma_logger_(uma_logger), | |
| 162 tracker_(tracker), | |
| 163 file_type_(kOther), | |
| 164 make_backup_(make_backup) { | |
| 165 DCHECK(f != INVALID_HANDLE_VALUE); | |
| 166 base::FilePath path = base::FilePath::FromUTF8Unsafe(fname); | |
| 167 if (FilePathToString(path.BaseName()).find("MANIFEST") == 0) | |
| 168 file_type_ = kManifest; | |
| 169 else if (ChromiumEnv::HasTableExtension(path)) | |
| 170 file_type_ = kTable; | |
| 171 if (file_type_ != kManifest) | |
| 172 tracker_->DidCreateNewFile(filename_); | |
| 173 parent_dir_ = FilePathToString(ChromiumEnv::CreateFilePath(fname).DirName()); | |
| 174 } | |
| 175 | |
| 176 ChromiumWritableFileWin32::~ChromiumWritableFileWin32() { | |
| 177 if (file_ != INVALID_HANDLE_VALUE) { | |
| 178 // Ignoring any potential errors | |
| 179 | |
| 180 CloseHandle(file_); | |
| 181 file_ = INVALID_HANDLE_VALUE; | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 Status ChromiumWritableFileWin32::SyncParent() { | |
| 186 return Status(); | |
|
jsbell
2013/12/12 01:11:42
Add a comment here that SyncParent() is (apparentl
cmumford
2013/12/12 17:40:51
Will do. I found the original review (https://chro
jsbell
2013/12/12 19:03:14
Thanks! dgrogan@ - should we just close crrev.com/
| |
| 187 } | |
| 188 | |
| 189 Status ChromiumWritableFileWin32::Append(const Slice& data) { | |
| 190 if (file_type_ == kManifest && tracker_->DoesDirNeedSync(filename_)) { | |
| 191 Status s = SyncParent(); | |
| 192 if (!s.ok()) | |
| 193 return s; | |
| 194 tracker_->DidSyncDir(filename_); | |
| 195 } | |
| 196 | |
| 197 DWORD written(0); | |
| 198 if (!WriteFile(file_, data.data(), data.size(), &written, NULL)) { | |
| 199 DWORD err = GetLastError(); | |
| 200 uma_logger_->RecordOSError(kWritableFileAppend, | |
| 201 base::LastErrorToPlatformFileError(err)); | |
| 202 return MakeIOErrorWin32( | |
| 203 filename_, GetWin32ErrorMessage(err), kWritableFileAppend, err); | |
| 204 } | |
| 205 return Status::OK(); | |
| 206 } | |
| 207 | |
| 208 Status ChromiumWritableFileWin32::Close() { | |
| 209 Status result; | |
| 210 DCHECK(file_ != INVALID_HANDLE_VALUE); | |
| 211 if (!CloseHandle(file_)) { | |
| 212 DWORD err = GetLastError(); | |
| 213 result = MakeIOErrorWin32( | |
| 214 filename_, GetWin32ErrorMessage(err), kWritableFileClose, err); | |
| 215 uma_logger_->RecordErrorAt(kWritableFileClose); | |
| 216 } | |
| 217 file_ = INVALID_HANDLE_VALUE; | |
| 218 return result; | |
| 219 } | |
| 220 | |
| 221 Status ChromiumWritableFileWin32::Flush() { | |
| 222 Status result; | |
| 223 if (!FlushFileBuffers(file_)) { | |
| 224 DWORD err = GetLastError(); | |
| 225 result = MakeIOErrorWin32( | |
| 226 filename_, GetWin32ErrorMessage(err), kWritableFileFlush, err); | |
| 227 uma_logger_->RecordOSError(kWritableFileFlush, base::LastErrorToPlatformFil eError(err)); | |
| 228 } | |
| 229 return result; | |
| 230 } | |
| 231 | |
| 232 Status ChromiumWritableFileWin32::Sync() { | |
| 233 TRACE_EVENT0("leveldb", "ChromiumEnvWin32::Sync"); | |
| 234 Status result; | |
| 235 DWORD error = ERROR_SUCCESS; | |
| 236 | |
| 237 DCHECK(file_ != INVALID_HANDLE_VALUE); | |
| 238 if (!FlushFileBuffers(file_)) | |
| 239 error = GetLastError(); | |
| 240 if (error != ERROR_SUCCESS) { | |
| 241 result = MakeIOErrorWin32( | |
| 242 filename_, GetWin32ErrorMessage(error), kWritableFileSync, error); | |
| 243 uma_logger_->RecordErrorAt(kWritableFileSync); | |
| 244 } else if (make_backup_ && file_type_ == kTable) { | |
| 245 bool success = ChromiumEnv::MakeBackup(filename_); | |
| 246 uma_logger_->RecordBackupResult(success); | |
| 247 } | |
| 248 return result; | |
| 249 } | |
| 250 | |
| 251 ChromiumEnvWin32::ChromiumEnvWin32() { | |
| 252 } | |
| 253 | |
| 254 ChromiumEnvWin32::~ChromiumEnvWin32() { | |
| 255 } | |
| 256 | |
| 257 Status ChromiumEnvWin32::NewSequentialFile(const std::string& fname, | |
| 258 SequentialFile** result) { | |
| 259 HANDLE f = CreateFile(UTF8ToUTF16(fname).c_str(), GENERIC_READ, 0, NULL, | |
| 260 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); | |
| 261 if (f == INVALID_HANDLE_VALUE) { | |
| 262 *result = NULL; | |
| 263 DWORD err = GetLastError(); | |
| 264 RecordOSError(kNewSequentialFile, err); | |
| 265 return MakeIOErrorWin32( | |
| 266 fname, GetWin32ErrorMessage(err), kNewSequentialFile, err); | |
| 267 } else { | |
| 268 *result = new ChromiumSequentialFile(fname, f, this); | |
| 269 return Status::OK(); | |
| 270 } | |
| 271 } | |
| 272 | |
| 273 void ChromiumEnvWin32::RecordOpenFilesLimit(const std::string& type) { | |
| 274 #if defined(OS_POSIX) | |
|
jsbell
2013/12/12 19:03:14
How could this ever be true?
We may want to leave
cmumford
2013/12/12 20:08:39
I agree. I've deleted the #ifdef and added a comme
| |
| 275 struct rlimit nofile; | |
| 276 if (getrlimit(RLIMIT_NOFILE, &nofile)) | |
| 277 return; | |
| 278 GetMaxFDHistogram(type)->Add(nofile.rlim_cur); | |
| 279 #endif | |
| 280 } | |
| 281 | |
| 282 Status ChromiumEnvWin32::NewRandomAccessFile(const std::string& fname, | |
| 283 RandomAccessFile** result) { | |
| 284 int flags = ::base::PLATFORM_FILE_READ | ::base::PLATFORM_FILE_OPEN; | |
| 285 bool created; | |
| 286 ::base::PlatformFileError error_code; | |
| 287 ::base::PlatformFile file = ::base::CreatePlatformFile( | |
| 288 ChromiumEnv::CreateFilePath(fname), flags, &created, &error_code); | |
| 289 if (error_code == ::base::PLATFORM_FILE_OK) { | |
| 290 *result = new ChromiumRandomAccessFile(fname, file, this); | |
| 291 RecordOpenFilesLimit("Success"); | |
| 292 return Status::OK(); | |
| 293 } | |
| 294 if (error_code == ::base::PLATFORM_FILE_ERROR_TOO_MANY_OPENED) | |
| 295 RecordOpenFilesLimit("TooManyOpened"); | |
| 296 else | |
| 297 RecordOpenFilesLimit("OtherError"); | |
| 298 *result = NULL; | |
| 299 RecordOSError(kNewRandomAccessFile, error_code); | |
| 300 return MakeIOErrorWin32(fname, | |
| 301 PlatformFileErrorString(error_code), | |
| 302 kNewRandomAccessFile, | |
| 303 error_code); | |
| 304 } | |
| 305 | |
| 306 Status ChromiumEnvWin32::NewWritableFile(const std::string& fname, | |
| 307 WritableFile** result) { | |
| 308 *result = NULL; | |
| 309 HANDLE f = CreateFile(UTF8ToUTF16(fname).c_str(), GENERIC_WRITE, FILE_SHARE_RE AD, NULL, | |
| 310 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | |
| 311 if (f == INVALID_HANDLE_VALUE) { | |
| 312 DWORD err = GetLastError(); | |
| 313 RecordErrorAt(kNewWritableFile); | |
| 314 return MakeIOErrorWin32( | |
| 315 fname, GetWin32ErrorMessage(err), kNewWritableFile, err); | |
| 316 } else { | |
| 317 *result = new ChromiumWritableFileWin32(fname, f, this, this, make_backup_); | |
| 318 return Status::OK(); | |
| 319 } | |
| 320 } | |
| 321 | |
| 322 base::PlatformFileError ChromiumEnvWin32::GetDirectoryEntries( | |
| 323 const base::FilePath& dir_param, | |
| 324 std::vector<base::FilePath>* result) const { | |
| 325 result->clear(); | |
| 326 base::FilePath dir_filepath = dir_param.Append(FILE_PATH_LITERAL("*")); | |
| 327 WIN32_FIND_DATA find_data; | |
| 328 HANDLE find_handle = FindFirstFile(dir_filepath.value().c_str(), &find_data); | |
| 329 if (find_handle == INVALID_HANDLE_VALUE) { | |
| 330 DWORD last_error = GetLastError(); | |
| 331 if (last_error == ERROR_FILE_NOT_FOUND) | |
| 332 return base::PLATFORM_FILE_OK; | |
| 333 return base::LastErrorToPlatformFileError(last_error); | |
| 334 } | |
| 335 do { | |
| 336 base::FilePath filepath(find_data.cFileName); | |
| 337 base::FilePath::StringType basename = filepath.BaseName().value(); | |
| 338 if (basename == FILE_PATH_LITERAL(".") || | |
| 339 basename == FILE_PATH_LITERAL("..")) | |
| 340 continue; | |
| 341 result->push_back(filepath.BaseName()); | |
| 342 } while (FindNextFile(find_handle, &find_data)); | |
| 343 DWORD last_error = GetLastError(); | |
| 344 base::PlatformFileError return_value = base::PLATFORM_FILE_OK; | |
| 345 if (last_error != ERROR_NO_MORE_FILES) | |
| 346 return_value = base::LastErrorToPlatformFileError(last_error); | |
| 347 FindClose(find_handle); | |
| 348 return return_value; | |
| 349 } | |
| 350 | |
| 351 Status ChromiumEnvWin32::NewLogger(const std::string& fname, Logger** result) { | |
| 352 FILE* f = _wfopen(UTF8ToUTF16(fname).c_str(), L"w"); | |
| 353 if (f == NULL) { | |
| 354 *result = NULL; | |
| 355 int saved_errno = errno; | |
| 356 RecordOSError(kNewLogger, saved_errno); | |
| 357 return MakeIOError(fname, strerror(saved_errno), kNewLogger, saved_errno); | |
| 358 } else { | |
| 359 *result = new ChromiumLogger(f); | |
| 360 return Status::OK(); | |
| 361 } | |
| 362 } | |
| 363 | |
| 364 void ChromiumEnvWin32::RecordOSError(MethodID method, DWORD error) const { | |
| 365 RecordErrorAt(method); | |
| 366 GetOSErrorHistogram(method, ERANGE + 1)->Add(error); | |
| 367 } | |
| 368 | |
| 369 } // namespace leveldb_env | |
| OLD | NEW |