Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1273)

Side by Side Diff: third_party/leveldatabase/env_chromium_stdio.cc

Issue 113373002: Unrevert r245135 "Created new Windows LevelDB environment." (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: IDB env always Win32, and other requested changes. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
4
5 #include <errno.h>
6
7 #include "base/debug/trace_event.h"
8 #include "base/metrics/histogram.h"
9 #include "base/posix/eintr_wrapper.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chromium_logger.h"
12 #include "env_chromium_stdio.h"
13
14 #if defined(OS_WIN)
15 #include <io.h>
16 #include "base/win/win_util.h"
17 #endif
18
19 #if defined(OS_POSIX)
20 #include <dirent.h>
21 #include <fcntl.h>
22 #include <sys/resource.h>
23 #endif
24
25 using namespace leveldb;
26
27 namespace leveldb_env {
28
29 namespace {
30
31 #if (defined(OS_POSIX) && !defined(OS_LINUX)) || defined(OS_WIN)
32 // The following are glibc-specific
33
34 size_t fread_unlocked(void* ptr, size_t size, size_t n, FILE* file) {
35 return fread(ptr, size, n, file);
36 }
37
38 size_t fwrite_unlocked(const void* ptr, size_t size, size_t n, FILE* file) {
39 return fwrite(ptr, size, n, file);
40 }
41
42 int fflush_unlocked(FILE* file) { return fflush(file); }
43
44 #if !defined(OS_ANDROID)
45 int fdatasync(int fildes) {
46 #if defined(OS_WIN)
47 return _commit(fildes);
48 #else
49 return HANDLE_EINTR(fsync(fildes));
50 #endif
51 }
52 #endif
53
54 #endif
55
56 // Wide-char safe fopen wrapper.
57 FILE* fopen_internal(const char* fname, const char* mode) {
58 #if defined(OS_WIN)
59 return _wfopen(base::UTF8ToUTF16(fname).c_str(),
60 base::ASCIIToUTF16(mode).c_str());
61 #else
62 return fopen(fname, mode);
63 #endif
64 }
65
66 class ChromiumSequentialFile : public SequentialFile {
67 private:
68 std::string filename_;
69 FILE* file_;
70 const UMALogger* uma_logger_;
71
72 public:
73 ChromiumSequentialFile(const std::string& fname,
74 FILE* f,
75 const UMALogger* uma_logger)
76 : filename_(fname), file_(f), uma_logger_(uma_logger) {}
77 virtual ~ChromiumSequentialFile() { fclose(file_); }
78
79 virtual Status Read(size_t n, Slice* result, char* scratch) {
80 Status s;
81 size_t r = fread_unlocked(scratch, 1, n, file_);
82 *result = Slice(scratch, r);
83 if (r < n) {
84 if (feof(file_)) {
85 // We leave status as ok if we hit the end of the file
86 } else {
87 // A partial read with an error: return a non-ok status
88 s = MakeIOError(filename_, strerror(errno), kSequentialFileRead, errno);
89 uma_logger_->RecordErrorAt(kSequentialFileRead);
90 }
91 }
92 return s;
93 }
94
95 virtual Status Skip(uint64_t n) {
96 if (fseek(file_, n, SEEK_CUR)) {
97 int saved_errno = errno;
98 uma_logger_->RecordErrorAt(kSequentialFileSkip);
99 return MakeIOError(
100 filename_, strerror(saved_errno), kSequentialFileSkip, saved_errno);
101 }
102 return Status::OK();
103 }
104 };
105
106 class ChromiumRandomAccessFile : public RandomAccessFile {
107 private:
108 std::string filename_;
109 ::base::PlatformFile file_;
110 const UMALogger* uma_logger_;
111
112 public:
113 ChromiumRandomAccessFile(const std::string& fname,
114 ::base::PlatformFile file,
115 const UMALogger* uma_logger)
116 : filename_(fname), file_(file), uma_logger_(uma_logger) {}
117 virtual ~ChromiumRandomAccessFile() { ::base::ClosePlatformFile(file_); }
118
119 virtual Status Read(uint64_t offset, size_t n, Slice* result, char* scratch)
120 const {
121 Status s;
122 int r = ::base::ReadPlatformFile(file_, offset, scratch, n);
123 *result = Slice(scratch, (r < 0) ? 0 : r);
124 if (r < 0) {
125 // An error: return a non-ok status
126 s = MakeIOError(
127 filename_, "Could not perform read", kRandomAccessFileRead);
128 uma_logger_->RecordErrorAt(kRandomAccessFileRead);
129 }
130 return s;
131 }
132 };
133
134 } // unnamed namespace
135
136 ChromiumWritableFile::ChromiumWritableFile(const std::string& fname,
137 FILE* f,
138 const UMALogger* uma_logger,
139 WriteTracker* tracker,
140 bool make_backup)
141 : filename_(fname),
142 file_(f),
143 uma_logger_(uma_logger),
144 tracker_(tracker),
145 file_type_(kOther),
146 make_backup_(make_backup) {
147 base::FilePath path = base::FilePath::FromUTF8Unsafe(fname);
148 if (FilePathToString(path.BaseName()).find("MANIFEST") == 0)
149 file_type_ = kManifest;
150 else if (ChromiumEnv::HasTableExtension(path))
151 file_type_ = kTable;
152 if (file_type_ != kManifest)
153 tracker_->DidCreateNewFile(filename_);
154 parent_dir_ = FilePathToString(ChromiumEnv::CreateFilePath(fname).DirName());
155 }
156
157 ChromiumWritableFile::~ChromiumWritableFile() {
158 if (file_ != NULL) {
159 // Ignoring any potential errors
160 fclose(file_);
161 }
162 }
163
164 Status ChromiumWritableFile::SyncParent() {
165 Status s;
166 #if !defined(OS_WIN)
167 TRACE_EVENT0("leveldb", "SyncParent");
168
169 int parent_fd = HANDLE_EINTR(open(parent_dir_.c_str(), O_RDONLY));
170 if (parent_fd < 0) {
171 int saved_errno = errno;
172 return MakeIOError(
173 parent_dir_, strerror(saved_errno), kSyncParent, saved_errno);
174 }
175 if (HANDLE_EINTR(fsync(parent_fd)) != 0) {
176 int saved_errno = errno;
177 s = MakeIOError(
178 parent_dir_, strerror(saved_errno), kSyncParent, saved_errno);
179 };
180 close(parent_fd);
181 #endif
182 return s;
183 }
184
185 Status ChromiumWritableFile::Append(const Slice& data) {
186 if (file_type_ == kManifest && tracker_->DoesDirNeedSync(filename_)) {
187 Status s = SyncParent();
188 if (!s.ok())
189 return s;
190 tracker_->DidSyncDir(filename_);
191 }
192
193 size_t r = fwrite_unlocked(data.data(), 1, data.size(), file_);
194 if (r != data.size()) {
195 int saved_errno = errno;
196 uma_logger_->RecordOSError(kWritableFileAppend, saved_errno);
197 return MakeIOError(
198 filename_, strerror(saved_errno), kWritableFileAppend, saved_errno);
199 }
200 return Status::OK();
201 }
202
203 Status ChromiumWritableFile::Close() {
204 Status result;
205 if (fclose(file_) != 0) {
206 result = MakeIOError(filename_, strerror(errno), kWritableFileClose, errno);
207 uma_logger_->RecordErrorAt(kWritableFileClose);
208 }
209 file_ = NULL;
210 return result;
211 }
212
213 Status ChromiumWritableFile::Flush() {
214 Status result;
215 if (HANDLE_EINTR(fflush_unlocked(file_))) {
216 int saved_errno = errno;
217 result = MakeIOError(
218 filename_, strerror(saved_errno), kWritableFileFlush, saved_errno);
219 uma_logger_->RecordOSError(kWritableFileFlush, saved_errno);
220 }
221 return result;
222 }
223
224 Status ChromiumWritableFile::Sync() {
225 TRACE_EVENT0("leveldb", "ChromiumEnvStdio::Sync");
226 Status result;
227 int error = 0;
228
229 if (HANDLE_EINTR(fflush_unlocked(file_)))
230 error = errno;
231 // Sync even if fflush gave an error; perhaps the data actually got out,
232 // even though something went wrong.
233 if (fdatasync(fileno(file_)) && !error)
234 error = errno;
235 // Report the first error we found.
236 if (error) {
237 result = MakeIOError(filename_, strerror(error), kWritableFileSync, error);
238 uma_logger_->RecordErrorAt(kWritableFileSync);
239 } else if (make_backup_ && file_type_ == kTable) {
240 bool success = ChromiumEnv::MakeBackup(filename_);
241 uma_logger_->RecordBackupResult(success);
242 }
243 return result;
244 }
245
246 ChromiumEnvStdio::ChromiumEnvStdio() {}
247
248 ChromiumEnvStdio::~ChromiumEnvStdio() {}
249
250 Status ChromiumEnvStdio::NewSequentialFile(const std::string& fname,
251 SequentialFile** result) {
252 FILE* f = fopen_internal(fname.c_str(), "rb");
253 if (f == NULL) {
254 *result = NULL;
255 int saved_errno = errno;
256 RecordOSError(kNewSequentialFile, saved_errno);
257 return MakeIOError(
258 fname, strerror(saved_errno), kNewSequentialFile, saved_errno);
259 } else {
260 *result = new ChromiumSequentialFile(fname, f, this);
261 return Status::OK();
262 }
263 }
264
265 void ChromiumEnvStdio::RecordOpenFilesLimit(const std::string& type) {
266 #if defined(OS_POSIX)
267 struct rlimit nofile;
268 if (getrlimit(RLIMIT_NOFILE, &nofile))
269 return;
270 GetMaxFDHistogram(type)->Add(nofile.rlim_cur);
271 #endif
272 }
273
274 Status ChromiumEnvStdio::NewRandomAccessFile(const std::string& fname,
275 RandomAccessFile** result) {
276 int flags = ::base::PLATFORM_FILE_READ | ::base::PLATFORM_FILE_OPEN;
277 bool created;
278 ::base::PlatformFileError error_code;
279 ::base::PlatformFile file = ::base::CreatePlatformFile(
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");
284 return Status::OK();
285 }
286 if (error_code == ::base::PLATFORM_FILE_ERROR_TOO_MANY_OPENED)
287 RecordOpenFilesLimit("TooManyOpened");
288 else
289 RecordOpenFilesLimit("OtherError");
290 *result = NULL;
291 RecordOSError(kNewRandomAccessFile, error_code);
292 return MakeIOError(fname,
293 PlatformFileErrorString(error_code),
294 kNewRandomAccessFile,
295 error_code);
296 }
297
298 Status ChromiumEnvStdio::NewWritableFile(const std::string& fname,
299 WritableFile** result) {
300 *result = NULL;
301 FILE* f = fopen_internal(fname.c_str(), "wb");
302 if (f == NULL) {
303 int saved_errno = errno;
304 RecordErrorAt(kNewWritableFile);
305 return MakeIOError(
306 fname, strerror(saved_errno), kNewWritableFile, saved_errno);
307 } else {
308 *result = new ChromiumWritableFile(fname, f, this, this, make_backup_);
309 return Status::OK();
310 }
311 }
312
313 #if defined(OS_WIN)
314 base::PlatformFileError ChromiumEnvStdio::GetDirectoryEntries(
315 const base::FilePath& dir_param,
316 std::vector<base::FilePath>* result) const {
317 result->clear();
318 base::FilePath dir_filepath = dir_param.Append(FILE_PATH_LITERAL("*"));
319 WIN32_FIND_DATA find_data;
320 HANDLE find_handle = FindFirstFile(dir_filepath.value().c_str(), &find_data);
321 if (find_handle == INVALID_HANDLE_VALUE) {
322 DWORD last_error = GetLastError();
323 if (last_error == ERROR_FILE_NOT_FOUND)
324 return base::PLATFORM_FILE_OK;
325 return base::LastErrorToPlatformFileError(last_error);
326 }
327 do {
328 base::FilePath filepath(find_data.cFileName);
329 base::FilePath::StringType basename = filepath.BaseName().value();
330 if (basename == FILE_PATH_LITERAL(".") ||
331 basename == FILE_PATH_LITERAL(".."))
332 continue;
333 result->push_back(filepath.BaseName());
334 } while (FindNextFile(find_handle, &find_data));
335 DWORD last_error = GetLastError();
336 base::PlatformFileError return_value = base::PLATFORM_FILE_OK;
337 if (last_error != ERROR_NO_MORE_FILES)
338 return_value = base::LastErrorToPlatformFileError(last_error);
339 FindClose(find_handle);
340 return return_value;
341 }
342 #else
343 base::PlatformFileError ChromiumEnvStdio::GetDirectoryEntries(
344 const base::FilePath& dir_filepath,
345 std::vector<base::FilePath>* result) const {
346 const std::string dir_string = FilePathToString(dir_filepath);
347 result->clear();
348 DIR* dir = opendir(dir_string.c_str());
349 if (!dir)
350 return base::ErrnoToPlatformFileError(errno);
351 struct dirent dent_buf;
352 struct dirent* dent;
353 int readdir_result;
354 while ((readdir_result = readdir_r(dir, &dent_buf, &dent)) == 0 && dent) {
355 if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0)
356 continue;
357 result->push_back(ChromiumEnv::CreateFilePath(dent->d_name));
358 }
359 int saved_errno = errno;
360 closedir(dir);
361 if (readdir_result != 0)
362 return base::ErrnoToPlatformFileError(saved_errno);
363 return base::PLATFORM_FILE_OK;
364 }
365 #endif
366
367 Status ChromiumEnvStdio::NewLogger(const std::string& fname, Logger** result) {
368 FILE* f = fopen_internal(fname.c_str(), "w");
369 if (f == NULL) {
370 *result = NULL;
371 int saved_errno = errno;
372 RecordOSError(kNewLogger, saved_errno);
373 return MakeIOError(fname, strerror(saved_errno), kNewLogger, saved_errno);
374 } else {
375 *result = new ChromiumLogger(f);
376 return Status::OK();
377 }
378 }
379
380 } // namespace leveldb_env
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698