OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library memory_file_system; | 5 library memory_file_system; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 import 'dart:core' hide Resource; | 9 import 'dart:core' hide Resource; |
10 | 10 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 newFolder(posix.dirname(path)); | 101 newFolder(posix.dirname(path)); |
102 _MemoryDummyLink link = new _MemoryDummyLink(this, path); | 102 _MemoryDummyLink link = new _MemoryDummyLink(this, path); |
103 _pathToResource[path] = link; | 103 _pathToResource[path] = link; |
104 _pathToTimestamp[path] = nextStamp++; | 104 _pathToTimestamp[path] = nextStamp++; |
105 _notifyWatchers(path, ChangeType.ADD); | 105 _notifyWatchers(path, ChangeType.ADD); |
106 return link; | 106 return link; |
107 } | 107 } |
108 | 108 |
109 File newFile(String path, String content, [int stamp]) { | 109 File newFile(String path, String content, [int stamp]) { |
110 path = posix.normalize(path); | 110 path = posix.normalize(path); |
111 newFolder(posix.dirname(path)); | 111 _MemoryResource folder = _pathToResource[posix.dirname(path)]; |
| 112 if (folder == null) { |
| 113 newFolder(posix.dirname(path)); |
| 114 } else if (folder is! Folder) { |
| 115 throw new ArgumentError('Cannot create file ($path) as child of file'); |
| 116 } |
112 _MemoryFile file = new _MemoryFile(this, path); | 117 _MemoryFile file = new _MemoryFile(this, path); |
113 _pathToResource[path] = file; | 118 _pathToResource[path] = file; |
114 _pathToContent[path] = content; | 119 _pathToContent[path] = content; |
115 _pathToTimestamp[path] = stamp != null ? stamp : nextStamp++; | 120 _pathToTimestamp[path] = stamp != null ? stamp : nextStamp++; |
116 _notifyWatchers(path, ChangeType.ADD); | 121 _notifyWatchers(path, ChangeType.ADD); |
117 return file; | 122 return file; |
118 } | 123 } |
119 | 124 |
120 Folder newFolder(String path) { | 125 Folder newFolder(String path) { |
121 path = posix.normalize(path); | 126 path = posix.normalize(path); |
122 if (!path.startsWith('/')) { | 127 if (!path.startsWith('/')) { |
123 throw new ArgumentError("Path must start with '/'"); | 128 throw new ArgumentError("Path must start with '/'"); |
124 } | 129 } |
125 _MemoryResource resource = _pathToResource[path]; | 130 _MemoryResource resource = _pathToResource[path]; |
126 if (resource == null) { | 131 if (resource == null) { |
127 String parentPath = posix.dirname(path); | 132 String parentPath = posix.dirname(path); |
128 if (parentPath != path) { | 133 if (parentPath != path) { |
129 newFolder(parentPath); | 134 newFolder(parentPath); |
130 } | 135 } |
131 _MemoryFolder folder = new _MemoryFolder(this, path); | 136 _MemoryFolder folder = new _MemoryFolder(this, path); |
132 _pathToResource[path] = folder; | 137 _pathToResource[path] = folder; |
133 _pathToTimestamp[path] = nextStamp++; | 138 _pathToTimestamp[path] = nextStamp++; |
| 139 _notifyWatchers(path, ChangeType.ADD); |
134 return folder; | 140 return folder; |
135 } else if (resource is _MemoryFolder) { | 141 } else if (resource is _MemoryFolder) { |
| 142 _notifyWatchers(path, ChangeType.ADD); |
136 return resource; | 143 return resource; |
137 } else { | 144 } else { |
138 String message = | 145 String message = |
139 'Folder expected at ' "'$path'" 'but ${resource.runtimeType} found'; | 146 'Folder expected at ' "'$path'" 'but ${resource.runtimeType} found'; |
140 throw new ArgumentError(message); | 147 throw new ArgumentError(message); |
141 } | 148 } |
142 } | 149 } |
143 | 150 |
144 File updateFile(String path, String content, [int stamp]) { | 151 File updateFile(String path, String content, [int stamp]) { |
145 path = posix.normalize(path); | 152 path = posix.normalize(path); |
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 bool operator ==(other) { | 479 bool operator ==(other) { |
473 if (runtimeType != other.runtimeType) { | 480 if (runtimeType != other.runtimeType) { |
474 return false; | 481 return false; |
475 } | 482 } |
476 return path == other.path; | 483 return path == other.path; |
477 } | 484 } |
478 | 485 |
479 @override | 486 @override |
480 String toString() => path; | 487 String toString() => path; |
481 } | 488 } |
OLD | NEW |