Codemod asserts to assertxs in the runtime
[hiphop-php.git] / hphp / runtime / base / directory.cpp
blobed5aae476ebdd7c603048ed19fcdf581dda29749
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_map_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 struct dirent entry;
83 struct dirent *result;
84 int ret = readdir_r(m_dir, &entry, &result);
85 if (ret != 0 || !result) {
86 return false;
88 return String(entry.d_name, CopyString);
91 void PlainDirectory::rewind() {
92 ::rewinddir(m_dir);
95 bool PlainDirectory::isValid() const {
96 return m_dir;
99 Variant ArrayDirectory::read() {
100 if (!m_it) {
101 return false;
104 auto ret = m_it.second();
105 assertx(ret.isString());
106 ++m_it;
107 return Variant(HHVM_FN(basename)(ret.toString()));
110 void ArrayDirectory::rewind() {
111 m_it.rewind();
114 bool ArrayDirectory::isEof() const {
115 return m_it.end();
118 String ArrayDirectory::path() {
119 if (!m_it) {
120 return empty_string();
123 auto entry = m_it.second();
124 assertx(entry.isString());
125 return HHVM_FN(dirname)(entry.toString());
128 CachedDirectory::CachedDirectory(const String& path) {
129 assertx(File::IsVirtualDirectory(path));
130 m_files = StaticContentCache::TheFileCache->readDirectory(path.c_str());
133 ///////////////////////////////////////////////////////////////////////////////