OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 // Read the file in blocks of size 64k. | 7 // Read the file in blocks of size 64k. |
8 const int _BLOCK_SIZE = 64 * 1024; | 8 const int _BLOCK_SIZE = 64 * 1024; |
9 | 9 |
10 | 10 |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 | 225 |
226 // Constructor for file. | 226 // Constructor for file. |
227 _File(this.path) { | 227 _File(this.path) { |
228 if (path is! String) { | 228 if (path is! String) { |
229 throw new ArgumentError('${Error.safeToString(path)} ' | 229 throw new ArgumentError('${Error.safeToString(path)} ' |
230 'is not a String'); | 230 'is not a String'); |
231 } | 231 } |
232 } | 232 } |
233 | 233 |
234 Future<bool> exists() { | 234 Future<bool> exists() { |
235 return _IOService.dispatch(_FILE_EXISTS, [path]).then((response) { | 235 return _IOService._dispatch(_FILE_EXISTS, [path]).then((response) { |
236 if (_isErrorResponse(response)) { | 236 if (_isErrorResponse(response)) { |
237 throw _exceptionFromResponse(response, "Cannot check existence", path); | 237 throw _exceptionFromResponse(response, "Cannot check existence", path); |
238 } | 238 } |
239 return response; | 239 return response; |
240 }); | 240 }); |
241 } | 241 } |
242 | 242 |
243 external static _exists(String path); | 243 external static _exists(String path); |
244 | 244 |
245 bool existsSync() { | 245 bool existsSync() { |
246 var result = _exists(path); | 246 var result = _exists(path); |
247 throwIfError(result, "Cannot check existence of file", path); | 247 throwIfError(result, "Cannot check existence of file", path); |
248 return result; | 248 return result; |
249 } | 249 } |
250 | 250 |
251 File get absolute => new File(_absolutePath); | 251 File get absolute => new File(_absolutePath); |
252 | 252 |
253 Future<FileStat> stat() => FileStat.stat(path); | 253 Future<FileStat> stat() => FileStat.stat(path); |
254 | 254 |
255 FileStat statSync() => FileStat.statSync(path); | 255 FileStat statSync() => FileStat.statSync(path); |
256 | 256 |
257 Future<File> create({bool recursive: false}) { | 257 Future<File> create({bool recursive: false}) { |
258 var result = recursive ? parent.create(recursive: true) | 258 var result = recursive ? parent.create(recursive: true) |
259 : new Future.value(null); | 259 : new Future.value(null); |
260 return result | 260 return result |
261 .then((_) => _IOService.dispatch(_FILE_CREATE, [path])) | 261 .then((_) => _IOService._dispatch(_FILE_CREATE, [path])) |
262 .then((response) { | 262 .then((response) { |
263 if (_isErrorResponse(response)) { | 263 if (_isErrorResponse(response)) { |
264 throw _exceptionFromResponse(response, "Cannot create file", path); | 264 throw _exceptionFromResponse(response, "Cannot create file", path); |
265 } | 265 } |
266 return this; | 266 return this; |
267 }); | 267 }); |
268 } | 268 } |
269 | 269 |
270 external static _create(String path); | 270 external static _create(String path); |
271 | 271 |
272 external static _createLink(String path, String target); | 272 external static _createLink(String path, String target); |
273 | 273 |
274 external static _linkTarget(String path); | 274 external static _linkTarget(String path); |
275 | 275 |
276 void createSync({bool recursive: false}) { | 276 void createSync({bool recursive: false}) { |
277 if (recursive) { | 277 if (recursive) { |
278 parent.createSync(recursive: true); | 278 parent.createSync(recursive: true); |
279 } | 279 } |
280 var result = _create(path); | 280 var result = _create(path); |
281 throwIfError(result, "Cannot create file", path); | 281 throwIfError(result, "Cannot create file", path); |
282 } | 282 } |
283 | 283 |
284 Future<File> _delete({bool recursive: false}) { | 284 Future<File> _delete({bool recursive: false}) { |
285 if (recursive) { | 285 if (recursive) { |
286 return new Directory(path).delete(recursive: true).then((_) => this); | 286 return new Directory(path).delete(recursive: true).then((_) => this); |
287 } | 287 } |
288 return _IOService.dispatch(_FILE_DELETE, [path]).then((response) { | 288 return _IOService._dispatch(_FILE_DELETE, [path]).then((response) { |
289 if (_isErrorResponse(response)) { | 289 if (_isErrorResponse(response)) { |
290 throw _exceptionFromResponse(response, "Cannot delete file", path); | 290 throw _exceptionFromResponse(response, "Cannot delete file", path); |
291 } | 291 } |
292 return this; | 292 return this; |
293 }); | 293 }); |
294 } | 294 } |
295 | 295 |
296 external static _deleteNative(String path); | 296 external static _deleteNative(String path); |
297 | 297 |
298 external static _deleteLinkNative(String path); | 298 external static _deleteLinkNative(String path); |
299 | 299 |
300 void _deleteSync({bool recursive: false}) { | 300 void _deleteSync({bool recursive: false}) { |
301 if (recursive) { | 301 if (recursive) { |
302 return new Directory(path).deleteSync(recursive: true); | 302 return new Directory(path).deleteSync(recursive: true); |
303 } | 303 } |
304 var result = _deleteNative(path); | 304 var result = _deleteNative(path); |
305 throwIfError(result, "Cannot delete file", path); | 305 throwIfError(result, "Cannot delete file", path); |
306 } | 306 } |
307 | 307 |
308 Future<File> rename(String newPath) { | 308 Future<File> rename(String newPath) { |
309 return _IOService.dispatch(_FILE_RENAME, [path, newPath]).then((response) { | 309 return _IOService._dispatch(_FILE_RENAME, [path, newPath]).then((response) { |
310 if (_isErrorResponse(response)) { | 310 if (_isErrorResponse(response)) { |
311 throw _exceptionFromResponse( | 311 throw _exceptionFromResponse( |
312 response, "Cannot rename file to '$newPath'", path); | 312 response, "Cannot rename file to '$newPath'", path); |
313 } | 313 } |
314 return new File(newPath); | 314 return new File(newPath); |
315 }); | 315 }); |
316 } | 316 } |
317 | 317 |
318 external static _rename(String oldPath, String newPath); | 318 external static _rename(String oldPath, String newPath); |
319 | 319 |
320 external static _renameLink(String oldPath, String newPath); | 320 external static _renameLink(String oldPath, String newPath); |
321 | 321 |
322 File renameSync(String newPath) { | 322 File renameSync(String newPath) { |
323 var result = _rename(path, newPath); | 323 var result = _rename(path, newPath); |
324 throwIfError(result, "Cannot rename file to '$newPath'", path); | 324 throwIfError(result, "Cannot rename file to '$newPath'", path); |
325 return new File(newPath); | 325 return new File(newPath); |
326 } | 326 } |
327 | 327 |
328 Future<File> copy(String newPath) { | 328 Future<File> copy(String newPath) { |
329 return _IOService.dispatch(_FILE_COPY, [path, newPath]).then((response) { | 329 return _IOService._dispatch(_FILE_COPY, [path, newPath]).then((response) { |
330 if (_isErrorResponse(response)) { | 330 if (_isErrorResponse(response)) { |
331 throw _exceptionFromResponse( | 331 throw _exceptionFromResponse( |
332 response, "Cannot copy file to '$newPath'", path); | 332 response, "Cannot copy file to '$newPath'", path); |
333 } | 333 } |
334 return new File(newPath); | 334 return new File(newPath); |
335 }); | 335 }); |
336 } | 336 } |
337 | 337 |
338 external static _copy(String oldPath, String newPath); | 338 external static _copy(String oldPath, String newPath); |
339 | 339 |
340 File copySync(String newPath) { | 340 File copySync(String newPath) { |
341 var result = _copy(path, newPath); | 341 var result = _copy(path, newPath); |
342 throwIfError(result, "Cannot copy file to '$newPath'", path); | 342 throwIfError(result, "Cannot copy file to '$newPath'", path); |
343 return new File(newPath); | 343 return new File(newPath); |
344 } | 344 } |
345 | 345 |
346 Future<RandomAccessFile> open({FileMode mode: FileMode.READ}) { | 346 Future<RandomAccessFile> open({FileMode mode: FileMode.READ}) { |
347 if (mode != FileMode.READ && | 347 if (mode != FileMode.READ && |
348 mode != FileMode.WRITE && | 348 mode != FileMode.WRITE && |
349 mode != FileMode.APPEND) { | 349 mode != FileMode.APPEND) { |
350 return new Future.error(new ArgumentError()); | 350 return new Future.error(new ArgumentError()); |
351 } | 351 } |
352 return _IOService.dispatch(_FILE_OPEN, [path, mode._mode]).then((response) { | 352 return _IOService._dispatch(_FILE_OPEN, [path, mode._mode]).then((response)
{ |
353 if (_isErrorResponse(response)) { | 353 if (_isErrorResponse(response)) { |
354 throw _exceptionFromResponse(response, "Cannot open file", path); | 354 throw _exceptionFromResponse(response, "Cannot open file", path); |
355 } | 355 } |
356 return new _RandomAccessFile(response, path); | 356 return new _RandomAccessFile(response, path); |
357 }); | 357 }); |
358 } | 358 } |
359 | 359 |
360 Future<int> length() { | 360 Future<int> length() { |
361 return _IOService.dispatch(_FILE_LENGTH_FROM_PATH, [path]).then((response) { | 361 return _IOService._dispatch(_FILE_LENGTH_FROM_PATH, [path]).then((response)
{ |
362 if (_isErrorResponse(response)) { | 362 if (_isErrorResponse(response)) { |
363 throw _exceptionFromResponse(response, | 363 throw _exceptionFromResponse(response, |
364 "Cannot retrieve length of file", | 364 "Cannot retrieve length of file", |
365 path); | 365 path); |
366 } | 366 } |
367 return response; | 367 return response; |
368 }); | 368 }); |
369 } | 369 } |
370 | 370 |
371 | 371 |
372 external static _lengthFromPath(String path); | 372 external static _lengthFromPath(String path); |
373 | 373 |
374 int lengthSync() { | 374 int lengthSync() { |
375 var result = _lengthFromPath(path); | 375 var result = _lengthFromPath(path); |
376 throwIfError(result, "Cannot retrieve length of file", path); | 376 throwIfError(result, "Cannot retrieve length of file", path); |
377 return result; | 377 return result; |
378 } | 378 } |
379 | 379 |
380 Future<DateTime> lastModified() { | 380 Future<DateTime> lastModified() { |
381 return _IOService.dispatch(_FILE_LAST_MODIFIED, [path]).then((response) { | 381 return _IOService._dispatch(_FILE_LAST_MODIFIED, [path]).then((response) { |
382 if (_isErrorResponse(response)) { | 382 if (_isErrorResponse(response)) { |
383 throw _exceptionFromResponse(response, | 383 throw _exceptionFromResponse(response, |
384 "Cannot retrieve modification time", | 384 "Cannot retrieve modification time", |
385 path); | 385 path); |
386 } | 386 } |
387 return new DateTime.fromMillisecondsSinceEpoch(response); | 387 return new DateTime.fromMillisecondsSinceEpoch(response); |
388 }); | 388 }); |
389 } | 389 } |
390 | 390 |
391 external static _lastModified(String path); | 391 external static _lastModified(String path); |
(...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1028 if (_asyncDispatched) { | 1028 if (_asyncDispatched) { |
1029 var msg = "An async operation is currently pending"; | 1029 var msg = "An async operation is currently pending"; |
1030 return new Future.error(new FileSystemException(msg, path)); | 1030 return new Future.error(new FileSystemException(msg, path)); |
1031 } | 1031 } |
1032 if (markClosed) { | 1032 if (markClosed) { |
1033 // Set the id_ to 0 (NULL) to ensure the no more async requests | 1033 // Set the id_ to 0 (NULL) to ensure the no more async requests |
1034 // can be issued for this file. | 1034 // can be issued for this file. |
1035 _id = 0; | 1035 _id = 0; |
1036 } | 1036 } |
1037 _asyncDispatched = true; | 1037 _asyncDispatched = true; |
1038 return _IOService.dispatch(request, data) | 1038 return _IOService._dispatch(request, data) |
1039 .whenComplete(() { | 1039 .whenComplete(() { |
1040 _asyncDispatched = false; | 1040 _asyncDispatched = false; |
1041 }); | 1041 }); |
1042 } | 1042 } |
1043 | 1043 |
1044 void _checkAvailable() { | 1044 void _checkAvailable() { |
1045 if (_asyncDispatched) { | 1045 if (_asyncDispatched) { |
1046 throw new FileSystemException("An async operation is currently pending", | 1046 throw new FileSystemException("An async operation is currently pending", |
1047 path); | 1047 path); |
1048 } | 1048 } |
1049 if (closed) { | 1049 if (closed) { |
1050 throw new FileSystemException("File closed", path); | 1050 throw new FileSystemException("File closed", path); |
1051 } | 1051 } |
1052 } | 1052 } |
1053 } | 1053 } |
OLD | NEW |