ipc/chromium/src/base/dir_reader_bsd.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 // derived from dir_reader_linux.h
michael@0 6
michael@0 7 #ifndef BASE_DIR_READER_BSD_H_
michael@0 8 #define BASE_DIR_READER_BSD_H_
michael@0 9 #pragma once
michael@0 10
michael@0 11 #include <dirent.h>
michael@0 12 #include <errno.h>
michael@0 13 #include <fcntl.h>
michael@0 14 #include <stdint.h>
michael@0 15 #include <unistd.h>
michael@0 16
michael@0 17 #include "base/logging.h"
michael@0 18 #include "base/eintr_wrapper.h"
michael@0 19
michael@0 20 // See the comments in dir_reader_posix.h about this.
michael@0 21
michael@0 22 namespace base {
michael@0 23
michael@0 24 class DirReaderBSD {
michael@0 25 public:
michael@0 26 explicit DirReaderBSD(const char* directory_path)
michael@0 27 #ifdef O_DIRECTORY
michael@0 28 : fd_(open(directory_path, O_RDONLY | O_DIRECTORY)),
michael@0 29 #else
michael@0 30 : fd_(open(directory_path, O_RDONLY)),
michael@0 31 #endif
michael@0 32 offset_(0),
michael@0 33 size_(0) {
michael@0 34 memset(buf_, 0, sizeof(buf_));
michael@0 35 }
michael@0 36
michael@0 37 ~DirReaderBSD() {
michael@0 38 if (fd_ >= 0) {
michael@0 39 if (HANDLE_EINTR(close(fd_)))
michael@0 40 DLOG(ERROR) << "Failed to close directory handle";
michael@0 41 }
michael@0 42 }
michael@0 43
michael@0 44 bool IsValid() const {
michael@0 45 return fd_ >= 0;
michael@0 46 }
michael@0 47
michael@0 48 // Move to the next entry returning false if the iteration is complete.
michael@0 49 bool Next() {
michael@0 50 if (size_) {
michael@0 51 struct dirent* dirent = reinterpret_cast<struct dirent*>(&buf_[offset_]);
michael@0 52 #ifdef OS_DRAGONFLY
michael@0 53 offset_ += _DIRENT_DIRSIZ(dirent);
michael@0 54 #else
michael@0 55 offset_ += dirent->d_reclen;
michael@0 56 #endif
michael@0 57 }
michael@0 58
michael@0 59 if (offset_ != size_)
michael@0 60 return true;
michael@0 61
michael@0 62 const int r = getdents(fd_, buf_, sizeof(buf_));
michael@0 63 if (r == 0)
michael@0 64 return false;
michael@0 65 if (r == -1) {
michael@0 66 DLOG(ERROR) << "getdents returned an error: " << errno;
michael@0 67 return false;
michael@0 68 }
michael@0 69 size_ = r;
michael@0 70 offset_ = 0;
michael@0 71 return true;
michael@0 72 }
michael@0 73
michael@0 74 const char* name() const {
michael@0 75 if (!size_)
michael@0 76 return NULL;
michael@0 77
michael@0 78 const struct dirent* dirent =
michael@0 79 reinterpret_cast<const struct dirent*>(&buf_[offset_]);
michael@0 80 return dirent->d_name;
michael@0 81 }
michael@0 82
michael@0 83 int fd() const {
michael@0 84 return fd_;
michael@0 85 }
michael@0 86
michael@0 87 static bool IsFallback() {
michael@0 88 return false;
michael@0 89 }
michael@0 90
michael@0 91 private:
michael@0 92 const int fd_;
michael@0 93 char buf_[512];
michael@0 94 size_t offset_, size_;
michael@0 95
michael@0 96 DISALLOW_COPY_AND_ASSIGN(DirReaderBSD);
michael@0 97 };
michael@0 98
michael@0 99 } // namespace base
michael@0 100
michael@0 101 #endif // BASE_DIR_READER_BSD_H_

mercurial