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