-
Notifications
You must be signed in to change notification settings - Fork 2
/
ngx_files.php
194 lines (150 loc) · 4.17 KB
/
ngx_files.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
define('ngx_stdout',STDOUT);
define('ngx_stderr',STDERR);
/* Standard file descriptors. */
define('STDIN_FILENO', STDIN);/* Standard input. */
define('STDOUT_FILENO', STDOUT); /* Standard output. */
define('STDERR_FILENO', STDERR); /* Standard error output. */
define('NGX_LINEFEED',"\x0a");
define('NGX_FILE_RDONLY', 'r');
define('NGX_FILE_WRONLY', 'w');
define('NGX_FILE_RDWR', 'w+');
define('NGX_FILE_CREATE_OR_OPEN', 'c');
define('NGX_FILE_OPEN', 'r');
define('NGX_FILE_TRUNCATE', 'w');
define('NGX_FILE_APPEND', 'a+');
define('NGX_FILE_DEFAULT_ACCESS', 0644);
define('NGX_FILE_OWNER_ACCESS', 0600);
//define('NGX_FILE_NONBLOCK', O_NONBLOCK);
define('NGX_INVALID_FILE',false);
define('NGX_FILE_ERROR',false);
define('ngx_open_file_n',"open()");
define('ngx_fd_info_n',"fstat()");
define('ngx_close_file_n',"close()");
define('ngx_read_file_n',"read()");
define('ngx_delete_file_n',"unlink()");
define('ngx_create_dir_n',"mkdir()");
define('ngx_file_info_n',"stat()");
define('ngx_set_stderr_n',"set_stderr(STDERR_FILENO)");
define('ngx_rename_file_n', "rename()");
define('ngx_open_glob_n',"glob()");
include_once 'ngx_core.php';
/**
* Created by PhpStorm.
* User: [email protected]
* Date: 2015/11/4
* Time: 12:30
* @param $p
* @return string
*/
function ngx_linefeed(&$p)
{
return $p.LF;
}
function ngx_open_file($file,$mode,$access){
$fp = fopen($file,$mode);
chmod($file, $access);
return $fp;
}
function ngx_path_separator($c){
return (($c) == '/');
}
//function ngx_write_fd($fd, $p)
//{
// return fwrite($fd, $p);
//}
function ngx_set_stderr(ngx_log $log){
$log->file->fd = ngx_stderr;
}
define('ngx_getcwd_n', 'getcwd()');
function ngx_fd_info($fd){
return fstat($fd);
}
function ngx_file_size($sb){
return $sb->size;
}
function ngx_close_file($fd){
return fclose($fd);
}
function ngx_read_file(ngx_file_t $file, $buf, $size, $offset)
{
ngx_log_debug4(NGX_LOG_DEBUG_CORE, $file->log, 0,
"read: %d, %p, %uz, %O", $file->fd, $buf, $size, $offset);
if ($file->sys_offset != $offset) {
if (fseek($file->fd, $offset, SEEK_SET) == -1) {
ngx_log_error(NGX_LOG_CRIT, $file->log, NGX_FERROR,
"fseek() \"%s\" failed", (array)$file->name);
return NGX_ERROR;
}
$file->sys_offset = $offset;
}
$o = fread($file->fd, $size);
if ($o == false) {
ngx_log_error(NGX_LOG_CRIT, $file->log, NGX_FERROR,
"read() \"%s\" failed", $file->name);
return NGX_ERROR;
}
$buf .= $o;
$n = strlen($o);
$file->sys_offset += $n;
$file->offset += $n;
return $n;
}
function ngx_write_file(ngx_file_t $file, $buf, $size, $offset)
{
// ssize_t n, written;
ngx_log_debug4(NGX_LOG_DEBUG_CORE, $file->log, 0,
"write: %d, %p, %uz, %O", $file->fd, $buf, $size, $offset);
$written = 0;
if ($file->sys_offset != $offset) {
if (fseek($file->fd, $offset, SEEK_SET) == -1) {
ngx_log_error(NGX_LOG_CRIT, $file->log, NGX_FLERROR,
"lseek() \"%s\" failed", $file->name);
return NGX_ERROR;
}
$file->sys_offset = $offset;
}
for ( ;; ) {
$buf = substr($buf,$written);
$n = fwrite($file->fd, $buf, $size);
if ($n == false) {
ngx_log_error(NGX_LOG_CRIT, $file->log, NGX_FERROR,
"write() \"%s\" failed", $file->name);
return NGX_ERROR;
}
$file->offset += $n;
$written += $n;
if ( $n == $size) {
return $written;
}
$size -= $n;
}
}
function ngx_file_info($file){
return stat($file);
}
function ngx_delete_file($name)
{
return unlink($name);
}
function ngx_create_dir($name, $access)
{
if(empty($name)){
return false;
}
return mkdir($name, $access);
}
function ngx_rename_file($o, $n){
return rename($o, $n);
}
function ngx_getcwd(){
return getcwd();
}
function ngx_open_glob($pattern)
{
$n = glob($pattern);
if (empty($n)) {
return NGX_ERROR;
}
return $n;
}