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