ipc/chromium/src/base/dir_reader_linux.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 #ifndef BASE_DIR_READER_LINUX_H_
michael@0 6 #define BASE_DIR_READER_LINUX_H_
michael@0 7 #pragma once
michael@0 8
michael@0 9 #include <errno.h>
michael@0 10 #include <fcntl.h>
michael@0 11 #include <stdint.h>
michael@0 12 #include <sys/syscall.h>
michael@0 13 #include <unistd.h>
michael@0 14
michael@0 15 #include "base/logging.h"
michael@0 16 #include "base/eintr_wrapper.h"
michael@0 17
michael@0 18 // See the comments in dir_reader_posix.h about this.
michael@0 19
michael@0 20 namespace base {
michael@0 21
michael@0 22 struct linux_dirent {
michael@0 23 uint64_t d_ino;
michael@0 24 int64_t d_off;
michael@0 25 unsigned short d_reclen;
michael@0 26 unsigned char d_type;
michael@0 27 char d_name[0];
michael@0 28 };
michael@0 29
michael@0 30 class DirReaderLinux {
michael@0 31 public:
michael@0 32 explicit DirReaderLinux(const char* directory_path)
michael@0 33 : fd_(open(directory_path, O_RDONLY | O_DIRECTORY)),
michael@0 34 offset_(0),
michael@0 35 size_(0) {
michael@0 36 memset(buf_, 0, sizeof(buf_));
michael@0 37 }
michael@0 38
michael@0 39 ~DirReaderLinux() {
michael@0 40 if (fd_ >= 0) {
michael@0 41 if (HANDLE_EINTR(close(fd_)))
michael@0 42 DLOG(ERROR) << "Failed to close directory handle";
michael@0 43 }
michael@0 44 }
michael@0 45
michael@0 46 bool IsValid() const {
michael@0 47 return fd_ >= 0;
michael@0 48 }
michael@0 49
michael@0 50 // Move to the next entry returning false if the iteration is complete.
michael@0 51 bool Next() {
michael@0 52 if (size_) {
michael@0 53 linux_dirent* dirent = reinterpret_cast<linux_dirent*>(&buf_[offset_]);
michael@0 54 offset_ += dirent->d_reclen;
michael@0 55 }
michael@0 56
michael@0 57 if (offset_ != size_)
michael@0 58 return true;
michael@0 59
michael@0 60 const int r = syscall(__NR_getdents64, fd_, buf_, sizeof(buf_));
michael@0 61 if (r == 0)
michael@0 62 return false;
michael@0 63 if (r == -1) {
michael@0 64 DLOG(ERROR) << "getdents64 returned an error: " << errno;
michael@0 65 return false;
michael@0 66 }
michael@0 67 size_ = r;
michael@0 68 offset_ = 0;
michael@0 69 return true;
michael@0 70 }
michael@0 71
michael@0 72 const char* name() const {
michael@0 73 if (!size_)
michael@0 74 return NULL;
michael@0 75
michael@0 76 const linux_dirent* dirent =
michael@0 77 reinterpret_cast<const linux_dirent*>(&buf_[offset_]);
michael@0 78 return dirent->d_name;
michael@0 79 }
michael@0 80
michael@0 81 int fd() const {
michael@0 82 return fd_;
michael@0 83 }
michael@0 84
michael@0 85 static bool IsFallback() {
michael@0 86 return false;
michael@0 87 }
michael@0 88
michael@0 89 private:
michael@0 90 const int fd_;
michael@0 91 unsigned char buf_[512];
michael@0 92 size_t offset_, size_;
michael@0 93
michael@0 94 DISALLOW_COPY_AND_ASSIGN(DirReaderLinux);
michael@0 95 };
michael@0 96
michael@0 97 } // namespace base
michael@0 98
michael@0 99 #endif // BASE_DIR_READER_LINUX_H_

mercurial