1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
7 #ifndef BASE_DIR_READER_LINUX_H_
8 #define BASE_DIR_READER_LINUX_H_
14 #include <sys/syscall.h>
17 #include "base/logging.h"
18 #include "base/eintr_wrapper.h"
20 // See the comments in dir_reader_posix.h about this.
27 unsigned short d_reclen
;
32 class DirReaderLinux
{
34 explicit DirReaderLinux(const char* directory_path
)
35 : fd_(open(directory_path
, O_RDONLY
| O_DIRECTORY
)),
38 memset(buf_
, 0, sizeof(buf_
));
43 if (IGNORE_EINTR(close(fd_
)))
44 DLOG(ERROR
) << "Failed to close directory handle";
48 bool IsValid() const { return fd_
>= 0; }
50 // Move to the next entry returning false if the iteration is complete.
53 linux_dirent
* dirent
= reinterpret_cast<linux_dirent
*>(&buf_
[offset_
]);
54 offset_
+= dirent
->d_reclen
;
57 if (offset_
!= size_
) return true;
59 const int r
= syscall(__NR_getdents64
, fd_
, buf_
, sizeof(buf_
));
60 if (r
== 0) return false;
62 DLOG(ERROR
) << "getdents64 returned an error: " << errno
;
70 const char* name() const {
71 if (!size_
) return NULL
;
73 const linux_dirent
* dirent
=
74 reinterpret_cast<const linux_dirent
*>(&buf_
[offset_
]);
75 return dirent
->d_name
;
78 int fd() const { return fd_
; }
80 static bool IsFallback() { return false; }
86 unsigned char buf_
[512];
88 size_t offset_
, size_
;
90 DISALLOW_COPY_AND_ASSIGN(DirReaderLinux
);
95 #endif // BASE_DIR_READER_LINUX_H_