| 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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 } | 65 } |
| 66 | 66 |
| 67 @override | 67 @override |
| 68 File getFile(String path) => new _MemoryFile(this, path); | 68 File getFile(String path) => new _MemoryFile(this, path); |
| 69 | 69 |
| 70 @override | 70 @override |
| 71 Folder getFolder(String path) => newFolder(path); | 71 Folder getFolder(String path) => newFolder(path); |
| 72 | 72 |
| 73 @override | 73 @override |
| 74 Resource getResource(String path) { | 74 Resource getResource(String path) { |
| 75 path = posix.normalize(path); | 75 path = pathContext.normalize(path); |
| 76 Resource resource = _pathToResource[path]; | 76 Resource resource = _pathToResource[path]; |
| 77 if (resource == null) { | 77 if (resource == null) { |
| 78 resource = new _MemoryFile(this, path); | 78 resource = new _MemoryFile(this, path); |
| 79 } | 79 } |
| 80 return resource; | 80 return resource; |
| 81 } | 81 } |
| 82 | 82 |
| 83 @override | 83 @override |
| 84 Folder getStateLocation(String pluginId) { | 84 Folder getStateLocation(String pluginId) { |
| 85 return newFolder('/user/home/$pluginId'); | 85 return newFolder('/user/home/$pluginId'); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void modifyFile(String path, String content) { | 88 void modifyFile(String path, String content) { |
| 89 _checkFileAtPath(path); | 89 _checkFileAtPath(path); |
| 90 _pathToContent[path] = content; | 90 _pathToContent[path] = content; |
| 91 _pathToTimestamp[path] = nextStamp++; | 91 _pathToTimestamp[path] = nextStamp++; |
| 92 _notifyWatchers(path, ChangeType.MODIFY); | 92 _notifyWatchers(path, ChangeType.MODIFY); |
| 93 } | 93 } |
| 94 | 94 |
| 95 /** | 95 /** |
| 96 * Create a resource representing a dummy link (that is, a File object which | 96 * Create a resource representing a dummy link (that is, a File object which |
| 97 * appears in its parent directory, but whose `exists` property is false) | 97 * appears in its parent directory, but whose `exists` property is false) |
| 98 */ | 98 */ |
| 99 File newDummyLink(String path) { | 99 File newDummyLink(String path) { |
| 100 path = posix.normalize(path); | 100 path = pathContext.normalize(path); |
| 101 newFolder(posix.dirname(path)); | 101 newFolder(pathContext.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 = pathContext.normalize(path); |
| 111 newFolder(posix.dirname(path)); | 111 _MemoryResource folder = _pathToResource[pathContext.dirname(path)]; |
| 112 if (folder == null) { |
| 113 newFolder(pathContext.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 = pathContext.normalize(path); |
| 122 if (!path.startsWith('/')) { | 127 if (!path.startsWith(pathContext.separator)) { |
| 123 throw new ArgumentError("Path must start with '/'"); | 128 throw new ArgumentError( |
| 129 "Path must start with '${pathContext.separator}' : $path"); |
| 124 } | 130 } |
| 125 _MemoryResource resource = _pathToResource[path]; | 131 _MemoryResource resource = _pathToResource[path]; |
| 126 if (resource == null) { | 132 if (resource == null) { |
| 127 String parentPath = posix.dirname(path); | 133 String parentPath = pathContext.dirname(path); |
| 128 if (parentPath != path) { | 134 if (parentPath != path) { |
| 129 newFolder(parentPath); | 135 newFolder(parentPath); |
| 130 } | 136 } |
| 131 _MemoryFolder folder = new _MemoryFolder(this, path); | 137 _MemoryFolder folder = new _MemoryFolder(this, path); |
| 132 _pathToResource[path] = folder; | 138 _pathToResource[path] = folder; |
| 133 _pathToTimestamp[path] = nextStamp++; | 139 _pathToTimestamp[path] = nextStamp++; |
| 140 _notifyWatchers(path, ChangeType.ADD); |
| 134 return folder; | 141 return folder; |
| 135 } else if (resource is _MemoryFolder) { | 142 } else if (resource is _MemoryFolder) { |
| 143 _notifyWatchers(path, ChangeType.ADD); |
| 136 return resource; | 144 return resource; |
| 137 } else { | 145 } else { |
| 138 String message = | 146 String message = |
| 139 'Folder expected at ' "'$path'" 'but ${resource.runtimeType} found'; | 147 'Folder expected at ' "'$path'" 'but ${resource.runtimeType} found'; |
| 140 throw new ArgumentError(message); | 148 throw new ArgumentError(message); |
| 141 } | 149 } |
| 142 } | 150 } |
| 143 | 151 |
| 144 File updateFile(String path, String content, [int stamp]) { | 152 File updateFile(String path, String content, [int stamp]) { |
| 145 path = posix.normalize(path); | 153 path = pathContext.normalize(path); |
| 146 newFolder(posix.dirname(path)); | 154 newFolder(pathContext.dirname(path)); |
| 147 _MemoryFile file = new _MemoryFile(this, path); | 155 _MemoryFile file = new _MemoryFile(this, path); |
| 148 _pathToResource[path] = file; | 156 _pathToResource[path] = file; |
| 149 _pathToContent[path] = content; | 157 _pathToContent[path] = content; |
| 150 _pathToTimestamp[path] = stamp != null ? stamp : nextStamp++; | 158 _pathToTimestamp[path] = stamp != null ? stamp : nextStamp++; |
| 151 _notifyWatchers(path, ChangeType.MODIFY); | 159 _notifyWatchers(path, ChangeType.MODIFY); |
| 152 return file; | 160 return file; |
| 153 } | 161 } |
| 154 | 162 |
| 155 void _checkFileAtPath(String path) { | 163 void _checkFileAtPath(String path) { |
| 156 _MemoryResource resource = _pathToResource[path]; | 164 _MemoryResource resource = _pathToResource[path]; |
| 157 if (resource is! _MemoryFile) { | 165 if (resource is! _MemoryFile) { |
| 158 throw new ArgumentError( | 166 throw new ArgumentError( |
| 159 'File expected at "$path" but ${resource.runtimeType} found'); | 167 'File expected at "$path" but ${resource.runtimeType} found'); |
| 160 } | 168 } |
| 161 } | 169 } |
| 162 | 170 |
| 163 void _checkFolderAtPath(String path) { | 171 void _checkFolderAtPath(String path) { |
| 164 _MemoryResource resource = _pathToResource[path]; | 172 _MemoryResource resource = _pathToResource[path]; |
| 165 if (resource is! _MemoryFolder) { | 173 if (resource is! _MemoryFolder) { |
| 166 throw new ArgumentError( | 174 throw new ArgumentError( |
| 167 'Folder expected at "$path" but ${resource.runtimeType} found'); | 175 'Folder expected at "$path" but ${resource.runtimeType} found'); |
| 168 } | 176 } |
| 169 } | 177 } |
| 170 | 178 |
| 171 void _notifyWatchers(String path, ChangeType changeType) { | 179 void _notifyWatchers(String path, ChangeType changeType) { |
| 172 _pathToWatchers.forEach((String watcherPath, | 180 _pathToWatchers.forEach((String watcherPath, |
| 173 List<StreamController<WatchEvent>> streamControllers) { | 181 List<StreamController<WatchEvent>> streamControllers) { |
| 174 if (watcherPath == path || posix.isWithin(watcherPath, path)) { | 182 if (watcherPath == path || pathContext.isWithin(watcherPath, path)) { |
| 175 for (StreamController<WatchEvent> streamController | 183 for (StreamController<WatchEvent> streamController |
| 176 in streamControllers) { | 184 in streamControllers) { |
| 177 streamController.add(new WatchEvent(changeType, path)); | 185 streamController.add(new WatchEvent(changeType, path)); |
| 178 } | 186 } |
| 179 } | 187 } |
| 180 }); | 188 }); |
| 181 } | 189 } |
| 182 } | 190 } |
| 183 | 191 |
| 184 /** | 192 /** |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 String content = _provider._pathToContent[path]; | 255 String content = _provider._pathToContent[path]; |
| 248 if (content == null) { | 256 if (content == null) { |
| 249 throw new FileSystemException(path, 'File "$path" does not exist.'); | 257 throw new FileSystemException(path, 'File "$path" does not exist.'); |
| 250 } | 258 } |
| 251 return content; | 259 return content; |
| 252 } | 260 } |
| 253 | 261 |
| 254 @override | 262 @override |
| 255 Source createSource([Uri uri]) { | 263 Source createSource([Uri uri]) { |
| 256 if (uri == null) { | 264 if (uri == null) { |
| 257 uri = posix.toUri(path); | 265 uri = _provider.pathContext.toUri(path); |
| 258 } | 266 } |
| 259 return new _MemoryFileSource(this, uri); | 267 return new _MemoryFileSource(this, uri); |
| 260 } | 268 } |
| 261 | 269 |
| 262 @override | 270 @override |
| 263 bool isOrContains(String path) { | 271 bool isOrContains(String path) { |
| 264 return path == this.path; | 272 return path == this.path; |
| 265 } | 273 } |
| 266 | 274 |
| 267 @override | 275 @override |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 */ | 375 */ |
| 368 class _MemoryFolder extends _MemoryResource implements Folder { | 376 class _MemoryFolder extends _MemoryResource implements Folder { |
| 369 _MemoryFolder(MemoryResourceProvider provider, String path) | 377 _MemoryFolder(MemoryResourceProvider provider, String path) |
| 370 : super(provider, path); | 378 : super(provider, path); |
| 371 | 379 |
| 372 @override | 380 @override |
| 373 bool get exists => _provider._pathToResource[path] is _MemoryFolder; | 381 bool get exists => _provider._pathToResource[path] is _MemoryFolder; |
| 374 | 382 |
| 375 @override | 383 @override |
| 376 String canonicalizePath(String relPath) { | 384 String canonicalizePath(String relPath) { |
| 377 relPath = posix.normalize(relPath); | 385 relPath = _provider.pathContext.normalize(relPath); |
| 378 String childPath = posix.join(path, relPath); | 386 String childPath = _provider.pathContext.join(path, relPath); |
| 379 childPath = posix.normalize(childPath); | 387 childPath = _provider.pathContext.normalize(childPath); |
| 380 return childPath; | 388 return childPath; |
| 381 } | 389 } |
| 382 | 390 |
| 383 @override | 391 @override |
| 384 bool contains(String path) { | 392 bool contains(String path) { |
| 385 return posix.isWithin(this.path, path); | 393 return _provider.pathContext.isWithin(this.path, path); |
| 386 } | 394 } |
| 387 | 395 |
| 388 @override | 396 @override |
| 389 Resource getChild(String relPath) { | 397 Resource getChild(String relPath) { |
| 390 String childPath = canonicalizePath(relPath); | 398 String childPath = canonicalizePath(relPath); |
| 391 _MemoryResource resource = _provider._pathToResource[childPath]; | 399 _MemoryResource resource = _provider._pathToResource[childPath]; |
| 392 if (resource == null) { | 400 if (resource == null) { |
| 393 resource = new _MemoryFile(_provider, childPath); | 401 resource = new _MemoryFile(_provider, childPath); |
| 394 } | 402 } |
| 395 return resource; | 403 return resource; |
| 396 } | 404 } |
| 397 | 405 |
| 398 @override | 406 @override |
| 399 _MemoryFolder getChildAssumingFolder(String relPath) { | 407 _MemoryFolder getChildAssumingFolder(String relPath) { |
| 400 String childPath = canonicalizePath(relPath); | 408 String childPath = canonicalizePath(relPath); |
| 401 _MemoryResource resource = _provider._pathToResource[childPath]; | 409 _MemoryResource resource = _provider._pathToResource[childPath]; |
| 402 if (resource is _MemoryFolder) { | 410 if (resource is _MemoryFolder) { |
| 403 return resource; | 411 return resource; |
| 404 } | 412 } |
| 405 return new _MemoryFolder(_provider, childPath); | 413 return new _MemoryFolder(_provider, childPath); |
| 406 } | 414 } |
| 407 | 415 |
| 408 @override | 416 @override |
| 409 List<Resource> getChildren() { | 417 List<Resource> getChildren() { |
| 410 if (!exists) { | 418 if (!exists) { |
| 411 throw new FileSystemException(path, 'Folder does not exist.'); | 419 throw new FileSystemException(path, 'Folder does not exist.'); |
| 412 } | 420 } |
| 413 List<Resource> children = <Resource>[]; | 421 List<Resource> children = <Resource>[]; |
| 414 _provider._pathToResource.forEach((resourcePath, resource) { | 422 _provider._pathToResource.forEach((resourcePath, resource) { |
| 415 if (posix.dirname(resourcePath) == path) { | 423 if (_provider.pathContext.dirname(resourcePath) == path) { |
| 416 children.add(resource); | 424 children.add(resource); |
| 417 } | 425 } |
| 418 }); | 426 }); |
| 419 return children; | 427 return children; |
| 420 } | 428 } |
| 421 | 429 |
| 422 @override | 430 @override |
| 423 bool isOrContains(String path) { | 431 bool isOrContains(String path) { |
| 424 if (path == this.path) { | 432 if (path == this.path) { |
| 425 return true; | 433 return true; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 451 } | 459 } |
| 452 }); | 460 }); |
| 453 return streamController.stream; | 461 return streamController.stream; |
| 454 } | 462 } |
| 455 | 463 |
| 456 @override | 464 @override |
| 457 get hashCode => path.hashCode; | 465 get hashCode => path.hashCode; |
| 458 | 466 |
| 459 @override | 467 @override |
| 460 Folder get parent { | 468 Folder get parent { |
| 461 String parentPath = posix.dirname(path); | 469 String parentPath = _provider.pathContext.dirname(path); |
| 462 if (parentPath == path) { | 470 if (parentPath == path) { |
| 463 return null; | 471 return null; |
| 464 } | 472 } |
| 465 return _provider.getResource(parentPath); | 473 return _provider.getResource(parentPath); |
| 466 } | 474 } |
| 467 | 475 |
| 468 @override | 476 @override |
| 469 String get shortName => posix.basename(path); | 477 String get shortName => _provider.pathContext.basename(path); |
| 470 | 478 |
| 471 @override | 479 @override |
| 472 bool operator ==(other) { | 480 bool operator ==(other) { |
| 473 if (runtimeType != other.runtimeType) { | 481 if (runtimeType != other.runtimeType) { |
| 474 return false; | 482 return false; |
| 475 } | 483 } |
| 476 return path == other.path; | 484 return path == other.path; |
| 477 } | 485 } |
| 478 | 486 |
| 479 @override | 487 @override |
| 480 String toString() => path; | 488 String toString() => path; |
| 481 } | 489 } |
| OLD | NEW |