ipc/chromium/src/base/dir_reader_linux.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/dir_reader_linux.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,99 @@
     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 +#ifndef BASE_DIR_READER_LINUX_H_
     1.9 +#define BASE_DIR_READER_LINUX_H_
    1.10 +#pragma once
    1.11 +
    1.12 +#include <errno.h>
    1.13 +#include <fcntl.h>
    1.14 +#include <stdint.h>
    1.15 +#include <sys/syscall.h>
    1.16 +#include <unistd.h>
    1.17 +
    1.18 +#include "base/logging.h"
    1.19 +#include "base/eintr_wrapper.h"
    1.20 +
    1.21 +// See the comments in dir_reader_posix.h about this.
    1.22 +
    1.23 +namespace base {
    1.24 +
    1.25 +struct linux_dirent {
    1.26 +  uint64_t        d_ino;
    1.27 +  int64_t         d_off;
    1.28 +  unsigned short  d_reclen;
    1.29 +  unsigned char   d_type;
    1.30 +  char            d_name[0];
    1.31 +};
    1.32 +
    1.33 +class DirReaderLinux {
    1.34 + public:
    1.35 +  explicit DirReaderLinux(const char* directory_path)
    1.36 +      : fd_(open(directory_path, O_RDONLY | O_DIRECTORY)),
    1.37 +        offset_(0),
    1.38 +        size_(0) {
    1.39 +    memset(buf_, 0, sizeof(buf_));
    1.40 +  }
    1.41 +
    1.42 +  ~DirReaderLinux() {
    1.43 +    if (fd_ >= 0) {
    1.44 +      if (HANDLE_EINTR(close(fd_)))
    1.45 +        DLOG(ERROR) << "Failed to close directory handle";
    1.46 +    }
    1.47 +  }
    1.48 +
    1.49 +  bool IsValid() const {
    1.50 +    return fd_ >= 0;
    1.51 +  }
    1.52 +
    1.53 +  // Move to the next entry returning false if the iteration is complete.
    1.54 +  bool Next() {
    1.55 +    if (size_) {
    1.56 +      linux_dirent* dirent = reinterpret_cast<linux_dirent*>(&buf_[offset_]);
    1.57 +      offset_ += dirent->d_reclen;
    1.58 +    }
    1.59 +
    1.60 +    if (offset_ != size_)
    1.61 +      return true;
    1.62 +
    1.63 +    const int r = syscall(__NR_getdents64, fd_, buf_, sizeof(buf_));
    1.64 +    if (r == 0)
    1.65 +      return false;
    1.66 +    if (r == -1) {
    1.67 +      DLOG(ERROR) << "getdents64 returned an error: " << errno;
    1.68 +      return false;
    1.69 +    }
    1.70 +    size_ = r;
    1.71 +    offset_ = 0;
    1.72 +    return true;
    1.73 +  }
    1.74 +
    1.75 +  const char* name() const {
    1.76 +    if (!size_)
    1.77 +      return NULL;
    1.78 +
    1.79 +    const linux_dirent* dirent =
    1.80 +        reinterpret_cast<const linux_dirent*>(&buf_[offset_]);
    1.81 +    return dirent->d_name;
    1.82 +  }
    1.83 +
    1.84 +  int fd() const {
    1.85 +    return fd_;
    1.86 +  }
    1.87 +
    1.88 +  static bool IsFallback() {
    1.89 +    return false;
    1.90 +  }
    1.91 +
    1.92 + private:
    1.93 +  const int fd_;
    1.94 +  unsigned char buf_[512];
    1.95 +  size_t offset_, size_;
    1.96 +
    1.97 +  DISALLOW_COPY_AND_ASSIGN(DirReaderLinux);
    1.98 +};
    1.99 +
   1.100 +}  // namespace base
   1.101 +
   1.102 +#endif // BASE_DIR_READER_LINUX_H_

mercurial