sync the repo
[hiphop-php.git] / hphp / runtime / base / directory.cpp
blob78189a7bcd65eb9342a0198f8ef053cc34e52314
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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 #include "hphp/runtime/base/directory.h"
19 #include "hphp/runtime/base/array-init.h"
21 #include "hphp/runtime/ext/std/ext_std_file.h"
22 #include "hphp/runtime/base/file.h"
23 #include "hphp/runtime/server/static-content-cache.h"
25 #include <sys/types.h>
26 #include <tuple>
28 namespace HPHP {
30 IMPLEMENT_RESOURCE_ALLOCATION(PlainDirectory)
32 ///////////////////////////////////////////////////////////////////////////////
34 const StaticString
35 s_wrapper_type("wrapper_type"),
36 s_stream_type("stream_type"),
37 s_mode("mode"),
38 s_unread_bytes("unread_bytes"),
39 s_seekable("seekable"),
40 s_timed_out("timed_out"),
41 s_blocked("blocked"),
42 s_eof("eof"),
43 s_plainfile("plainfile"),
44 s_dir("dir"),
45 s_r("r");
47 Array Directory::getMetaData() {
48 return make_dict_array(
49 s_wrapper_type, s_plainfile, // PHP5 compatibility
50 s_stream_type, s_dir,
51 s_mode, s_r,
52 s_unread_bytes, 0,
53 s_seekable, false,
54 s_timed_out, false,
55 s_blocked, true,
56 s_eof, isEof()
60 ///////////////////////////////////////////////////////////////////////////////
62 PlainDirectory::PlainDirectory(int fd) {
63 m_dir = ::fdopendir(fd);
66 PlainDirectory::PlainDirectory(const String& path) {
67 m_dir = ::opendir(path.data());
70 PlainDirectory::~PlainDirectory() {
71 close();
74 void PlainDirectory::close() {
75 if (m_dir) {
76 ::closedir(m_dir);
77 m_dir = nullptr;
81 Variant PlainDirectory::read() {
82 if (!m_dir) return false;
84 struct dirent entry;
85 struct dirent *result;
86 int ret = readdir_r(m_dir, &entry, &result);
87 if (ret != 0 || !result) {
88 return false;
90 return String(entry.d_name, CopyString);
93 void PlainDirectory::rewind() {
94 if (!m_dir) return;
96 ::rewinddir(m_dir);
99 bool PlainDirectory::isValid() const {
100 return m_dir;
103 Variant ArrayDirectory::read() {
104 if (!m_it) {
105 return false;
108 auto ret = m_it.second();
109 assertx(ret.isString());
110 ++m_it;
111 return Variant(HHVM_FN(basename)(ret.toString()));
114 void ArrayDirectory::rewind() {
115 m_it.rewind();
118 bool ArrayDirectory::isEof() const {
119 return m_it.end();
122 String ArrayDirectory::path() {
123 if (!m_it) {
124 return empty_string();
127 auto entry = m_it.second();
128 assertx(entry.isString());
129 return HHVM_FN(dirname)(entry.toString());
132 CachedDirectory::CachedDirectory(const String& path) {
133 assertx(File::IsVirtualDirectory(path));
134 m_files = StaticContentCache::TheFileCache->listDirectory(path.toCppString());
137 ///////////////////////////////////////////////////////////////////////////////