2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_DIRECTORY_H_
18 #define incl_HPHP_DIRECTORY_H_
20 #include "hphp/runtime/base/array-iterator.h"
21 #include "hphp/runtime/base/resource-data.h"
22 #include "hphp/runtime/base/type-array.h"
23 #include "hphp/runtime/base/type-string.h"
27 #include <folly/portability/Dirent.h>
30 ///////////////////////////////////////////////////////////////////////////////
34 struct Directory
: SweepableResourceData
{
35 virtual void close() = 0;
36 virtual Variant
read() = 0;
37 virtual void rewind() = 0;
38 virtual Array
getMetaData();
39 virtual bool isEof() const {
40 return false; // Most implementations can't tell if they've reached EOF
42 void sweep() override
{ close(); }
44 CLASSNAME_IS("Directory")
45 // overriding ResourceData
46 const String
& o_getClassNameHook() const override
{ return classnameof(); }
48 String
getLastError() {
49 return String(folly::errnoStr(errno
));
53 struct PlainDirectory
: Directory
{
54 DECLARE_RESOURCE_ALLOCATION(PlainDirectory
);
56 explicit PlainDirectory(const String
& path
);
57 explicit PlainDirectory(int fd
);
58 ~PlainDirectory() override
;
60 void close() override
;
61 Variant
read() override
;
62 void rewind() override
;
69 struct ArrayDirectory
: Directory
{
70 DECLARE_RESOURCE_ALLOCATION_NO_SWEEP(ArrayDirectory
);
72 explicit ArrayDirectory(const Array
& a
) : m_it(a
) {}
74 void close() override
{}
75 Variant
read() override
;
76 void rewind() override
;
77 bool isEof() const override
;
79 void sweep() override
{
84 size_t size() const { return m_it
.getArrayData()->size(); }
91 struct CachedDirectory
: Directory
{
92 DECLARE_RESOURCE_ALLOCATION_NO_SWEEP(CachedDirectory
);
94 explicit CachedDirectory(const String
& path
);
96 void close() override
{}
97 Variant
read() override
{
98 if (m_pos
>= m_files
.size()) return false;
99 return String(m_files
[m_pos
++]);
101 void rewind() override
{ m_pos
= 0; }
102 bool isEof() const override
{ return m_pos
>= m_files
.size(); }
106 std::vector
<std::string
> m_files
;
109 ///////////////////////////////////////////////////////////////////////////////
112 #endif // incl_HPHP_DIRECTORY_H_