Chromium Code Reviews| Index: mojo/dart/embedder/io/file_patch.dart |
| diff --git a/mojo/dart/embedder/io/file_patch.dart b/mojo/dart/embedder/io/file_patch.dart |
| index 73767a1b01552562c19d0b65ece4f7304d934bb2..3f45dc564f8cc48a4a72ee5e9b6d05e968e190db 100644 |
| --- a/mojo/dart/embedder/io/file_patch.dart |
| +++ b/mojo/dart/embedder/io/file_patch.dart |
| @@ -2,8 +2,244 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +import 'dart:_mojo_services/mojo/files/types.mojom.dart' as types; |
| + |
| // |
| -// Implementation of File and RandomAccessFile for Mojo. |
| +// Implementation of Directory, File, and RandomAccessFile for Mojo. |
| // |
| -// TODO(johnmccutchan): Implement. |
| +OSError _OSErrorFromError(types.Error error) { |
| + assert(error != null); |
| + return new OSError(error.toString(), error.toJson()); |
| +} |
| + |
| +String _ensurePathIsRelative(String path) { |
| + while (path.startsWith('/')) { |
| + // Trim off the leading '/'. |
| + path = path.substring(1); |
| + } |
| + return path; |
| +} |
| + |
| +patch class _Directory { |
| + // We start at the root of the file system. |
| + static String _currentDirectoryPath = '/'; |
| + |
| + /* patch */ Future<Directory> create({bool recursive: false}) async { |
| + if (recursive) { |
| + return exists().then((exists) { |
| + if (exists) return this; |
| + if (path != parent.path) { |
| + return parent.create(recursive: true).then((_) { |
| + return create(); |
| + }); |
| + } else { |
| + return create(); |
| + } |
| + }); |
| + } |
| + DirectoryProxy rootDirectory = await _getRootDirectory(); |
| + int flags = |
| + types.kOpenFlagRead | types.kOpenFlagWrite | types.kOpenFlagCreate; |
| + var response = |
| + await rootDirectory.ptr.openDirectory(_ensurePathIsRelative(path), |
|
zra
2015/12/18 22:51:46
For IPC calls deep in library implementations, it
Cutch
2015/12/18 23:05:20
Done here and elsewhere.
|
| + null, |
| + flags); |
| + if (response.error != types.Error.ok) { |
| + throw _OSErrorFromError(response.error); |
| + } |
| + return this; |
| + } |
| + |
| + /* patch */ Future<Directory> createTemp([String prefix]) async { |
| + DirectoryProxy rootDirectory = await _getRootDirectory(); |
| + // Create directory and fail if it already exists. |
| + int flags = types.kOpenFlagRead | types.kOpenFlagWrite | |
| + types.kOpenFlagCreate | types.kOpenFlagExclusive; |
| + String tempPath = '$path/$prefix'; |
| + while (true) { |
| + print(tempPath); |
|
zra
2015/12/18 22:51:46
debug print?
Cutch
2015/12/18 23:05:20
Done.
|
| + var response = await rootDirectory.ptr.openDirectory(tempPath, null, flags); |
|
zra
2015/12/18 22:51:46
long line
Cutch
2015/12/18 23:05:20
Done.
|
| + if (response.error == types.Error.ok) { |
| + // Success. |
| + break; |
| + } |
| + // Alter the path and try again. |
| + // TODO(johnmccutchan): Append a randomly generated character. |
| + tempPath = tempPath + 'a'; |
| + } |
| + return new Directory(tempPath); |
| + } |
| + |
| + /* patch */ Future<bool> exists() async { |
| + DirectoryProxy rootDirectory = await _getRootDirectory(); |
| + int flags = types.kOpenFlagRead | types.kOpenFlagWrite; |
| + var response = |
| + await rootDirectory.ptr.openDirectory(_ensurePathIsRelative(path), |
| + null, |
| + flags); |
| + // If we can open it, it exists. |
| + return response.error == types.Error.ok; |
| + } |
| + |
| + /* patch */ static _current() { |
| + return _currentDirectoryPath; |
| + } |
| + |
| + /* patch */ static _setCurrent(path) { |
| + _currentDirectoryPath = path; |
| + } |
| + |
| + /* patch */ static _createTemp(String path) { |
|
zra
2015/12/18 22:51:46
What are these empty functions for? Should they th
Cutch
2015/12/18 23:05:20
These are patches that will be necessary. I've add
|
| + } |
| + |
| + /* patch */ static String _systemTemp() { |
| + return 'tmp'; |
| + } |
| + |
| + /* patch */ static _exists(String path) { |
| + } |
| + |
| + /* patch */ static _create(String path) { |
| + } |
| + |
| + /* patch */ static _deleteNative(String path, bool recursive) { |
| + } |
| + |
| + /* patch */ static _rename(String path, String newPath) { |
| + } |
| + |
| + /* patch */ static List _list(String path, bool recursive, bool followLinks) { |
| + } |
| +} |
| + |
| + |
| +patch class _File { |
| + /* patch */ Future<bool> exists() async { |
| + DirectoryProxy rootDirectory = await _getRootDirectory(); |
| + int flags = types.kOpenFlagRead; |
| + var response = |
| + await rootDirectory.ptr.openFile(_ensurePathIsRelative(path), |
| + null, |
| + flags); |
| + // If we can open it, it exists. |
| + return response.error == types.Error.ok; |
| + } |
| + |
| + Future<File> create({bool recursive: false}) async { |
| + if (recursive) { |
| + // Create any parent directories. |
| + await parent.create(recursive: true); |
| + } |
| + DirectoryProxy rootDirectory = await _getRootDirectory(); |
| + int flags = types.kOpenFlagWrite | types.kOpenFlagCreate; |
| + print(_ensurePathIsRelative(path)); |
| + var response = |
| + await rootDirectory.ptr.openFile(_ensurePathIsRelative(path), |
| + null, |
| + flags); |
| + if (response.error != types.Error.ok) { |
| + throw _OSErrorFromError(response.error); |
| + } |
| + return this; |
| + } |
| + |
| + /* patch */ static _exists(String path) { |
| + } |
| + |
| + /* patch */ static _create(String path) { |
| + } |
| + |
| + /* patch */ static _createLink(String path, String target) { |
| + } |
| + |
| + /* patch */ static _linkTarget(String path) { |
| + } |
| + |
| + /* patch */ static _deleteNative(String path) { |
| + } |
| + |
| + /* patch */ static _deleteLinkNative(String path) { |
| + } |
| + |
| + /* patch */ static _rename(String oldPath, String newPath) { |
| + } |
| + |
| + /* patch */ static _renameLink(String oldPath, String newPath) { |
| + } |
| + |
| + /* patch */ static _copy(String oldPath, String newPath) { |
| + } |
| + |
| + /* patch */ static _lengthFromPath(String path) { |
| + } |
| + |
| + /* patch */ static _lastModified(String path) { |
| + } |
| + |
| + /* patch */ static _open(String path, int mode) { |
| + } |
| + |
| + /* patch */ static int _openStdio(int fd) { |
| + } |
| +} |
| + |
| +patch class FileStat { |
| + /* patch */ static _statSync(String path) { |
| + } |
| +} |
| + |
| + |
| +patch class FileSystemEntity { |
| + /* patch */ static _getType(String path, bool followLinks) { |
| + } |
| + /* patch */ static _identical(String path1, String path2) { |
| + } |
| + /* patch */ static _resolveSymbolicLinks(String path) { |
| + } |
| +} |
| + |
| +patch class _Link { |
| +} |
| + |
| + |
| +patch class _RandomAccessFile { |
| + /* patch */ static int _close(int id) { |
| + } |
| + |
| + /* patch */ static int _getFD(int id) { |
| + } |
| + |
| + /* patch */ static _readByte(int id) { |
| + } |
| + |
| + /* patch */ static _read(int id, int bytes) { |
| + } |
| + |
| + /* patch */ static _readInto(int id, List<int> buffer, int start, int end) { |
| + } |
| + |
| + /* patch */ static _writeByte(int id, int value) { |
| + } |
| + |
| + /* patch */ static _writeFrom(int id, List<int> buffer, int start, int end) { |
| + } |
| + |
| + /* patch */ static _position(int id) { |
| + } |
| + |
| + /* patch */ static _setPosition(int id, int position) { |
| + } |
| + |
| + /* patch */ static _truncate(int id, int length) { |
| + } |
| + |
| + /* patch */ static _length(int id) { |
| + } |
| + |
| + /* patch */ static _flush(int id) { |
| + } |
| + |
| + /* patch */ static _lock(int id, int lock, int start, int end) { |
| + } |
| +} |