Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: sdk/lib/io/directory_impl.dart

Issue 11759007: Fix Directory.create(recursive: true) for non-existing paths. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/standalone/io/regress_7679_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 class _Directory implements Directory { 7 class _Directory implements Directory {
8 static const CREATE_REQUEST = 0; 8 static const CREATE_REQUEST = 0;
9 static const DELETE_REQUEST = 1; 9 static const DELETE_REQUEST = 1;
10 static const EXISTS_REQUEST = 2; 10 static const EXISTS_REQUEST = 2;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 if (_path is !String) { 45 if (_path is !String) {
46 throw new ArgumentError(); 46 throw new ArgumentError();
47 } 47 }
48 var result = _exists(_path); 48 var result = _exists(_path);
49 if (result is OSError) { 49 if (result is OSError) {
50 throw new DirectoryIOException("Exists failed", _path, result); 50 throw new DirectoryIOException("Exists failed", _path, result);
51 } 51 }
52 return (result == 1); 52 return (result == 1);
53 } 53 }
54 54
55 // Compute the index of the first directory in the list that exists. If
56 // none of the directories exist dirsToCreate.length is returned.
55 Future<int> _computeExistingIndex(List dirsToCreate) { 57 Future<int> _computeExistingIndex(List dirsToCreate) {
56 var future; 58 var future;
59 var notFound = dirsToCreate.length;
57 for (var i = 0; i < dirsToCreate.length; i++) { 60 for (var i = 0; i < dirsToCreate.length; i++) {
58 if (future == null) { 61 if (future == null) {
59 future = dirsToCreate[i].exists().transform((e) => e ? i : -1); 62 future = dirsToCreate[i].exists().transform((e) => e ? i : notFound);
60 } else { 63 } else {
61 future = future.chain((index) { 64 future = future.chain((index) {
62 if (index != -1) { 65 if (index != notFound) {
63 return new Future.immediate(index); 66 return new Future.immediate(index);
64 } 67 }
65 return dirsToCreate[i].exists().transform((e) => e ? i : -1); 68 return dirsToCreate[i].exists().transform((e) => e ? i : notFound);
66 }); 69 });
67 } 70 }
68 } 71 }
69 if (future == null) { 72 if (future == null) {
70 return new Future.immediate(-1); 73 return new Future.immediate(notFound);
71 } else { 74 } else {
72 return future; 75 return future;
73 } 76 }
74 } 77 }
75 78
76 Future<Directory> createRecursively() { 79 Future<Directory> createRecursively() {
77 if (_path is !String) { 80 if (_path is !String) {
78 throw new ArgumentError(); 81 throw new ArgumentError();
79 } 82 }
80 var path = new Path.fromNative(_path); 83 var path = new Path.fromNative(_path);
81 var dirsToCreate = []; 84 var dirsToCreate = [];
82 var terminator = path.isAbsolute ? '/' : ''; 85 var terminator = path.isAbsolute ? '/' : '';
83 while (path.toString() != terminator) { 86 while (path.toString() != terminator) {
84 dirsToCreate.add(new Directory.fromPath(path)); 87 dirsToCreate.add(new Directory.fromPath(path));
85 path = path.directoryPath; 88 path = path.directoryPath;
86 } 89 }
87 return _computeExistingIndex(dirsToCreate).chain((index) { 90 return _computeExistingIndex(dirsToCreate).chain((index) {
91 // If none of the directories already exist we need to create them all.
92 if (index == -1) index = dirsToCreate.length;
Søren Gjesse 2013/01/03 15:11:13 I can't figure out how the _computeExistingIndex c
Mads Ager (google) 2013/01/03 16:04:16 D'oh! That was what it used to do, so this was my
88 var future; 93 var future;
89 for (var i = index - 1; i >= 0 ; i--) { 94 for (var i = index - 1; i >= 0 ; i--) {
90 if (future == null) { 95 if (future == null) {
91 future = dirsToCreate[i].create(); 96 future = dirsToCreate[i].create();
92 } else { 97 } else {
93 future = future.chain((_) { 98 future = future.chain((_) {
94 return dirsToCreate[i].create(); 99 return dirsToCreate[i].create();
95 }); 100 });
96 } 101 }
97 } 102 }
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 } else { 345 } else {
341 throw e; 346 throw e;
342 } 347 }
343 } 348 }
344 349
345 Function _onDir; 350 Function _onDir;
346 Function _onFile; 351 Function _onFile;
347 Function _onDone; 352 Function _onDone;
348 Function _onError; 353 Function _onError;
349 } 354 }
OLDNEW
« no previous file with comments | « no previous file | tests/standalone/io/regress_7679_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698