OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 import 'dart:_mojo_services/mojo/files/types.mojom.dart' as types; |
| 6 |
5 // | 7 // |
6 // Implementation of File and RandomAccessFile for Mojo. | 8 // Implementation of Directory, File, and RandomAccessFile for Mojo. |
7 // | 9 // |
8 | 10 |
9 // TODO(johnmccutchan): Implement. | 11 OSError _OSErrorFromError(types.Error error) { |
| 12 assert(error != null); |
| 13 return new OSError(error.toString(), error.toJson()); |
| 14 } |
| 15 |
| 16 String _ensurePathIsRelative(String path) { |
| 17 while (path.startsWith('/')) { |
| 18 // Trim off the leading '/'. |
| 19 path = path.substring(1); |
| 20 } |
| 21 return path; |
| 22 } |
| 23 |
| 24 patch class _Directory { |
| 25 // We start at the root of the file system. |
| 26 static String _currentDirectoryPath = '/'; |
| 27 |
| 28 /* patch */ Future<Directory> create({bool recursive: false}) async { |
| 29 if (recursive) { |
| 30 return exists().then((exists) { |
| 31 if (exists) return this; |
| 32 if (path != parent.path) { |
| 33 return parent.create(recursive: true).then((_) { |
| 34 return create(); |
| 35 }); |
| 36 } else { |
| 37 return create(); |
| 38 } |
| 39 }); |
| 40 } |
| 41 DirectoryProxy rootDirectory = await _getRootDirectory(); |
| 42 int flags = |
| 43 types.kOpenFlagRead | types.kOpenFlagWrite | types.kOpenFlagCreate; |
| 44 var response = |
| 45 await rootDirectory.responseOrError( |
| 46 rootDirectory.ptr.openDirectory(_ensurePathIsRelative(path), |
| 47 null, |
| 48 flags)); |
| 49 if (response.error != types.Error.ok) { |
| 50 throw _OSErrorFromError(response.error); |
| 51 } |
| 52 return this; |
| 53 } |
| 54 |
| 55 /* patch */ Future<Directory> createTemp([String prefix]) async { |
| 56 DirectoryProxy rootDirectory = await _getRootDirectory(); |
| 57 // Create directory and fail if it already exists. |
| 58 int flags = types.kOpenFlagRead | types.kOpenFlagWrite | |
| 59 types.kOpenFlagCreate | types.kOpenFlagExclusive; |
| 60 String tempPath = '$path/$prefix'; |
| 61 while (true) { |
| 62 var response = |
| 63 await rootDirectory.responseOrError( |
| 64 rootDirectory.ptr.openDirectory(tempPath, null, flags)); |
| 65 if (response.error == types.Error.ok) { |
| 66 // Success. |
| 67 break; |
| 68 } |
| 69 // Alter the path and try again. |
| 70 // TODO(johnmccutchan): Append a randomly generated character. |
| 71 tempPath = tempPath + 'a'; |
| 72 } |
| 73 return new Directory(tempPath); |
| 74 } |
| 75 |
| 76 /* patch */ Future<bool> exists() async { |
| 77 DirectoryProxy rootDirectory = await _getRootDirectory(); |
| 78 int flags = types.kOpenFlagRead | types.kOpenFlagWrite; |
| 79 var response = |
| 80 await await rootDirectory.responseOrError( |
| 81 rootDirectory.ptr.openDirectory(_ensurePathIsRelative(path), |
| 82 null, |
| 83 flags)); |
| 84 // If we can open it, it exists. |
| 85 return response.error == types.Error.ok; |
| 86 } |
| 87 |
| 88 /* patch */ static _current() { |
| 89 return _currentDirectoryPath; |
| 90 } |
| 91 |
| 92 /* patch */ static _setCurrent(path) { |
| 93 _currentDirectoryPath = path; |
| 94 } |
| 95 |
| 96 /* patch */ static _createTemp(String path) { |
| 97 throw new UnimplementedError(); |
| 98 } |
| 99 |
| 100 /* patch */ static String _systemTemp() { |
| 101 return 'tmp'; |
| 102 } |
| 103 |
| 104 /* patch */ static _exists(String path) { |
| 105 throw new UnimplementedError(); |
| 106 } |
| 107 |
| 108 /* patch */ static _create(String path) { |
| 109 throw new UnimplementedError(); |
| 110 } |
| 111 |
| 112 /* patch */ static _deleteNative(String path, bool recursive) { |
| 113 throw new UnimplementedError(); |
| 114 } |
| 115 |
| 116 /* patch */ static _rename(String path, String newPath) { |
| 117 throw new UnimplementedError(); |
| 118 } |
| 119 |
| 120 /* patch */ static List _list(String path, bool recursive, bool followLinks) { |
| 121 throw new UnimplementedError(); |
| 122 } |
| 123 } |
| 124 |
| 125 |
| 126 patch class _File { |
| 127 /* patch */ Future<bool> exists() async { |
| 128 DirectoryProxy rootDirectory = await _getRootDirectory(); |
| 129 int flags = types.kOpenFlagRead; |
| 130 var response = |
| 131 await rootDirectory.responseOrError( |
| 132 rootDirectory.ptr.openFile(_ensurePathIsRelative(path), |
| 133 null, |
| 134 flags)); |
| 135 // If we can open it, it exists. |
| 136 return response.error == types.Error.ok; |
| 137 } |
| 138 |
| 139 Future<File> create({bool recursive: false}) async { |
| 140 if (recursive) { |
| 141 // Create any parent directories. |
| 142 await parent.create(recursive: true); |
| 143 } |
| 144 DirectoryProxy rootDirectory = await _getRootDirectory(); |
| 145 int flags = types.kOpenFlagWrite | types.kOpenFlagCreate; |
| 146 var response = |
| 147 await rootDirectory.responseOrError( |
| 148 rootDirectory.ptr.openFile(_ensurePathIsRelative(path), |
| 149 null, |
| 150 flags)); |
| 151 if (response.error != types.Error.ok) { |
| 152 throw _OSErrorFromError(response.error); |
| 153 } |
| 154 return this; |
| 155 } |
| 156 |
| 157 /* patch */ static _exists(String path) { |
| 158 throw new UnimplementedError(); |
| 159 } |
| 160 |
| 161 /* patch */ static _create(String path) { |
| 162 throw new UnimplementedError(); |
| 163 } |
| 164 |
| 165 /* patch */ static _createLink(String path, String target) { |
| 166 throw new UnimplementedError(); |
| 167 } |
| 168 |
| 169 /* patch */ static _linkTarget(String path) { |
| 170 throw new UnimplementedError(); |
| 171 } |
| 172 |
| 173 /* patch */ static _deleteNative(String path) { |
| 174 throw new UnimplementedError(); |
| 175 } |
| 176 |
| 177 /* patch */ static _deleteLinkNative(String path) { |
| 178 throw new UnimplementedError(); |
| 179 } |
| 180 |
| 181 /* patch */ static _rename(String oldPath, String newPath) { |
| 182 throw new UnimplementedError(); |
| 183 } |
| 184 |
| 185 /* patch */ static _renameLink(String oldPath, String newPath) { |
| 186 throw new UnimplementedError(); |
| 187 } |
| 188 |
| 189 /* patch */ static _copy(String oldPath, String newPath) { |
| 190 throw new UnimplementedError(); |
| 191 } |
| 192 |
| 193 /* patch */ static _lengthFromPath(String path) { |
| 194 throw new UnimplementedError(); |
| 195 } |
| 196 |
| 197 /* patch */ static _lastModified(String path) { |
| 198 throw new UnimplementedError(); |
| 199 } |
| 200 |
| 201 /* patch */ static _open(String path, int mode) { |
| 202 throw new UnimplementedError(); |
| 203 } |
| 204 |
| 205 /* patch */ static int _openStdio(int fd) { |
| 206 throw new UnimplementedError(); |
| 207 } |
| 208 } |
| 209 |
| 210 patch class FileStat { |
| 211 /* patch */ static _statSync(String path) { |
| 212 } |
| 213 } |
| 214 |
| 215 |
| 216 patch class FileSystemEntity { |
| 217 /* patch */ static _getType(String path, bool followLinks) { |
| 218 throw new UnimplementedError(); |
| 219 } |
| 220 /* patch */ static _identical(String path1, String path2) { |
| 221 throw new UnimplementedError(); |
| 222 } |
| 223 /* patch */ static _resolveSymbolicLinks(String path) { |
| 224 throw new UnimplementedError(); |
| 225 } |
| 226 } |
| 227 |
| 228 patch class _Link { |
| 229 } |
| 230 |
| 231 |
| 232 patch class _RandomAccessFile { |
| 233 /* patch */ static int _close(int id) { |
| 234 throw new UnimplementedError(); |
| 235 } |
| 236 |
| 237 /* patch */ static int _getFD(int id) { |
| 238 throw new UnimplementedError(); |
| 239 } |
| 240 |
| 241 /* patch */ static _readByte(int id) { |
| 242 throw new UnimplementedError(); |
| 243 } |
| 244 |
| 245 /* patch */ static _read(int id, int bytes) { |
| 246 throw new UnimplementedError(); |
| 247 } |
| 248 |
| 249 /* patch */ static _readInto(int id, List<int> buffer, int start, int end) { |
| 250 throw new UnimplementedError(); |
| 251 } |
| 252 |
| 253 /* patch */ static _writeByte(int id, int value) { |
| 254 throw new UnimplementedError(); |
| 255 } |
| 256 |
| 257 /* patch */ static _writeFrom(int id, List<int> buffer, int start, int end) { |
| 258 throw new UnimplementedError(); |
| 259 } |
| 260 |
| 261 /* patch */ static _position(int id) { |
| 262 throw new UnimplementedError(); |
| 263 } |
| 264 |
| 265 /* patch */ static _setPosition(int id, int position) { |
| 266 throw new UnimplementedError(); |
| 267 } |
| 268 |
| 269 /* patch */ static _truncate(int id, int length) { |
| 270 throw new UnimplementedError(); |
| 271 } |
| 272 |
| 273 /* patch */ static _length(int id) { |
| 274 throw new UnimplementedError(); |
| 275 } |
| 276 |
| 277 /* patch */ static _flush(int id) { |
| 278 throw new UnimplementedError(); |
| 279 } |
| 280 |
| 281 /* patch */ static _lock(int id, int lock, int start, int end) { |
| 282 throw new UnimplementedError(); |
| 283 } |
| 284 } |
OLD | NEW |