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 +----------------------------------------------------------------------+
19 #include "hphp/runtime/base/file.h"
20 #include "hphp/runtime/base/execution-context.h"
21 #include "hphp/runtime/base/request-event-handler.h"
24 ///////////////////////////////////////////////////////////////////////////////
27 * A file system file that's nothing but ordinary. A simple FILE* wrapper.
29 struct PlainFile
: File
{
30 DECLARE_RESOURCE_ALLOCATION(PlainFile
);
32 explicit PlainFile(FILE *stream
= nullptr,
33 bool nonblocking
= false,
34 const String
& wrapper_type
= null_string
,
35 const String
& stream_type
= null_string
);
36 explicit PlainFile(int fd
,
37 bool nonblocking
= false,
38 const String
& wrapper
= null_string
,
39 const String
& stream_type
= null_string
);
40 ~PlainFile() override
;
42 // overriding ResourceData
43 const String
& o_getClassNameHook() const override
{ return classnameof(); }
46 bool open(const String
& filename
, const String
& mode
) override
;
47 bool close(int* unused
= nullptr) override
;
48 int64_t readImpl(char *buffer
, int64_t length
) override
;
50 String
read() override
;
51 String
read(int64_t length
) override
;
52 int64_t writeImpl(const char *buffer
, int64_t length
) override
;
53 bool seekable() override
{ return true;}
54 bool seek(int64_t offset
, int whence
= SEEK_SET
) override
;
55 int64_t tell() override
;
57 bool rewind() override
;
58 bool flush() override
;
59 bool truncate(int64_t size
) override
;
60 bool stat(struct stat
*sb
) override
;
62 FILE *getStream() { return m_stream
;}
66 char *m_buffer
; // For setbuffer. Needed to reduce mmap
67 // contention due to how glibc allocates memory
72 * This is wrapper for fds that cannot be closed.
74 struct BuiltinFile
: PlainFile
{
75 explicit BuiltinFile(FILE *stream
);
76 explicit BuiltinFile(int fd
);
77 ~BuiltinFile() override
;
78 bool close(int* unused
= nullptr) final
;
79 void sweep() override
;
81 static_assert(sizeof(BuiltinFile
) == sizeof(PlainFile
),
82 "BuiltinFile inherits PlainFile::heapSize()");
85 * A request-local wrapper for the three standard files:
86 * STDIN, STDOUT, and STDERR.
88 struct BuiltinFiles final
: RequestEventHandler
{
89 static const Variant
& getSTDIN();
90 static const Variant
& getSTDOUT();
91 static const Variant
& getSTDERR();
92 static Variant
getSTDIN(const StringData
* name
);
93 static Variant
getSTDOUT(const StringData
* name
);
94 static Variant
getSTDERR(const StringData
* name
);
96 void requestInit() override
;
97 void requestShutdown() override
;
105 void clearThreadLocalIO();
106 void setThreadLocalIO(FILE* in
, FILE* out
, FILE* err
);
108 ///////////////////////////////////////////////////////////////////////////////