9
9
use Amp \Parallel \Worker \TaskException ;
10
10
use Amp \Parallel \Worker \WorkerException ;
11
11
use Amp \Promise ;
12
+ use Amp \Success ;
13
+ use function Amp \call ;
12
14
13
15
class ParallelDriver implements Driver {
14
16
/**
@@ -67,14 +69,28 @@ private function runFileTask(Internal\FileTask $task): \Generator {
67
69
* {@inheritdoc}
68
70
*/
69
71
public function unlink (string $ path ): Promise {
70
- return new Coroutine ($ this ->runFileTask (new Internal \FileTask ("unlink " , [$ path ])));
72
+ return call (function () use ($ path ) {
73
+ $ result = yield from $ this ->runFileTask (new Internal \FileTask ("unlink " , [$ path ]));
74
+ StatCache::clear ($ path );
75
+ return $ result ;
76
+ });
71
77
}
72
78
73
79
/**
74
80
* {@inheritdoc}
75
81
*/
76
82
public function stat (string $ path ): Promise {
77
- return new Coroutine ($ this ->runFileTask (new Internal \FileTask ("stat " , [$ path ])));
83
+ if ($ stat = StatCache::get ($ path )) {
84
+ return new Success ($ stat );
85
+ }
86
+
87
+ return call (function () use ($ path ) {
88
+ $ stat = yield from $ this ->runFileTask (new Internal \FileTask ("stat " , [$ path ]));
89
+ if (!empty ($ stat )) {
90
+ StatCache::set ($ path , $ stat );
91
+ }
92
+ return $ stat ;
93
+ });
78
94
}
79
95
80
96
/**
@@ -88,14 +104,32 @@ public function rename(string $from, string $to): Promise {
88
104
* {@inheritdoc}
89
105
*/
90
106
public function isfile (string $ path ): Promise {
91
- return new Coroutine ($ this ->runFileTask (new Internal \FileTask ("isfile " , [$ path ])));
107
+ return call (function () use ($ path ) {
108
+ $ stat = yield $ this ->stat ($ path );
109
+ if (empty ($ stat )) {
110
+ return false ;
111
+ }
112
+ if ($ stat ["mode " ] & 0100000 ) {
113
+ return true ;
114
+ }
115
+ return false ;
116
+ });
92
117
}
93
118
94
119
/**
95
120
* {@inheritdoc}
96
121
*/
97
122
public function isdir (string $ path ): Promise {
98
- return new Coroutine ($ this ->runFileTask (new Internal \FileTask ("isdir " , [$ path ])));
123
+ return call (function () use ($ path ) {
124
+ $ stat = yield $ this ->stat ($ path );
125
+ if (empty ($ stat )) {
126
+ return false ;
127
+ }
128
+ if ($ stat ["mode " ] & 0040000 ) {
129
+ return true ;
130
+ }
131
+ return false ;
132
+ });
99
133
}
100
134
101
135
/**
@@ -137,7 +171,11 @@ public function scandir(string $path): Promise {
137
171
* {@inheritdoc}
138
172
*/
139
173
public function rmdir (string $ path ): Promise {
140
- return new Coroutine ($ this ->runFileTask (new Internal \FileTask ("rmdir " , [$ path ])));
174
+ return call (function () use ($ path ) {
175
+ $ result = yield from $ this ->runFileTask (new Internal \FileTask ("rmdir " , [$ path ]));
176
+ StatCache::clear ($ path );
177
+ return $ result ;
178
+ });
141
179
}
142
180
143
181
/**
@@ -165,28 +203,55 @@ public function exists(string $path): Promise {
165
203
* {@inheritdoc}
166
204
*/
167
205
public function size (string $ path ): Promise {
168
- return new Coroutine ($ this ->runFileTask (new Internal \FileTask ("size " , [$ path ])));
206
+ return call (function () use ($ path ) {
207
+ $ stat = yield $ this ->stat ($ path );
208
+ if (empty ($ stat )) {
209
+ throw new FilesystemException ("Specified path does not exist " );
210
+ }
211
+ if ($ stat ["mode " ] & 0100000 ) {
212
+ return $ stat ["size " ];
213
+ }
214
+ throw new FilesystemException ("Specified path is not a regular file " );
215
+ });
169
216
}
170
217
171
218
/**
172
219
* {@inheritdoc}
173
220
*/
174
221
public function mtime (string $ path ): Promise {
175
- return new Coroutine ($ this ->runFileTask (new Internal \FileTask ("mtime " , [$ path ])));
222
+ return call (function () use ($ path ) {
223
+ $ stat = yield $ this ->stat ($ path );
224
+ if (empty ($ stat )) {
225
+ throw new FilesystemException ("Specified path does not exist " );
226
+ }
227
+ return $ stat ["mtime " ];
228
+ });
176
229
}
177
230
178
231
/**
179
232
* {@inheritdoc}
180
233
*/
181
234
public function atime (string $ path ): Promise {
182
- return new Coroutine ($ this ->runFileTask (new Internal \FileTask ("atime " , [$ path ])));
235
+ return call (function () use ($ path ) {
236
+ $ stat = yield $ this ->stat ($ path );
237
+ if (empty ($ stat )) {
238
+ throw new FilesystemException ("Specified path does not exist " );
239
+ }
240
+ return $ stat ["atime " ];
241
+ });
183
242
}
184
243
185
244
/**
186
245
* {@inheritdoc}
187
246
*/
188
247
public function ctime (string $ path ): Promise {
189
- return new Coroutine ($ this ->runFileTask (new Internal \FileTask ("ctime " , [$ path ])));
248
+ return call (function () use ($ path ) {
249
+ $ stat = yield $ this ->stat ($ path );
250
+ if (empty ($ stat )) {
251
+ throw new FilesystemException ("Specified path does not exist " );
252
+ }
253
+ return $ stat ["ctime " ];
254
+ });
190
255
}
191
256
192
257
/**
0 commit comments