1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/dir_reader_bsd.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,101 @@ 1.4 +// Copyright (c) 2010 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +// derived from dir_reader_linux.h 1.9 + 1.10 +#ifndef BASE_DIR_READER_BSD_H_ 1.11 +#define BASE_DIR_READER_BSD_H_ 1.12 +#pragma once 1.13 + 1.14 +#include <dirent.h> 1.15 +#include <errno.h> 1.16 +#include <fcntl.h> 1.17 +#include <stdint.h> 1.18 +#include <unistd.h> 1.19 + 1.20 +#include "base/logging.h" 1.21 +#include "base/eintr_wrapper.h" 1.22 + 1.23 +// See the comments in dir_reader_posix.h about this. 1.24 + 1.25 +namespace base { 1.26 + 1.27 +class DirReaderBSD { 1.28 + public: 1.29 + explicit DirReaderBSD(const char* directory_path) 1.30 +#ifdef O_DIRECTORY 1.31 + : fd_(open(directory_path, O_RDONLY | O_DIRECTORY)), 1.32 +#else 1.33 + : fd_(open(directory_path, O_RDONLY)), 1.34 +#endif 1.35 + offset_(0), 1.36 + size_(0) { 1.37 + memset(buf_, 0, sizeof(buf_)); 1.38 + } 1.39 + 1.40 + ~DirReaderBSD() { 1.41 + if (fd_ >= 0) { 1.42 + if (HANDLE_EINTR(close(fd_))) 1.43 + DLOG(ERROR) << "Failed to close directory handle"; 1.44 + } 1.45 + } 1.46 + 1.47 + bool IsValid() const { 1.48 + return fd_ >= 0; 1.49 + } 1.50 + 1.51 + // Move to the next entry returning false if the iteration is complete. 1.52 + bool Next() { 1.53 + if (size_) { 1.54 + struct dirent* dirent = reinterpret_cast<struct dirent*>(&buf_[offset_]); 1.55 +#ifdef OS_DRAGONFLY 1.56 + offset_ += _DIRENT_DIRSIZ(dirent); 1.57 +#else 1.58 + offset_ += dirent->d_reclen; 1.59 +#endif 1.60 + } 1.61 + 1.62 + if (offset_ != size_) 1.63 + return true; 1.64 + 1.65 + const int r = getdents(fd_, buf_, sizeof(buf_)); 1.66 + if (r == 0) 1.67 + return false; 1.68 + if (r == -1) { 1.69 + DLOG(ERROR) << "getdents returned an error: " << errno; 1.70 + return false; 1.71 + } 1.72 + size_ = r; 1.73 + offset_ = 0; 1.74 + return true; 1.75 + } 1.76 + 1.77 + const char* name() const { 1.78 + if (!size_) 1.79 + return NULL; 1.80 + 1.81 + const struct dirent* dirent = 1.82 + reinterpret_cast<const struct dirent*>(&buf_[offset_]); 1.83 + return dirent->d_name; 1.84 + } 1.85 + 1.86 + int fd() const { 1.87 + return fd_; 1.88 + } 1.89 + 1.90 + static bool IsFallback() { 1.91 + return false; 1.92 + } 1.93 + 1.94 + private: 1.95 + const int fd_; 1.96 + char buf_[512]; 1.97 + size_t offset_, size_; 1.98 + 1.99 + DISALLOW_COPY_AND_ASSIGN(DirReaderBSD); 1.100 +}; 1.101 + 1.102 +} // namespace base 1.103 + 1.104 +#endif // BASE_DIR_READER_BSD_H_