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

Unified Diff: mojo/dart/embedder/io/file_patch.dart

Issue 1539843004: Support some file operations in dart:io under mojo (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | mojo/dart/embedder/io/mojo_patch.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..3de76e3bde092c9eab8cee90c83457f5d44f08cf 100644
--- a/mojo/dart/embedder/io/file_patch.dart
+++ b/mojo/dart/embedder/io/file_patch.dart
@@ -2,8 +2,283 @@
// 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.responseOrError(
+ rootDirectory.ptr.openDirectory(_ensurePathIsRelative(path),
+ 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) {
+ var response =
+ await rootDirectory.responseOrError(
+ rootDirectory.ptr.openDirectory(tempPath, null, flags));
+ 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 await rootDirectory.responseOrError(
+ 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) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static String _systemTemp() {
+ return 'tmp';
+ }
+
+ /* patch */ static _exists(String path) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _create(String path) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _deleteNative(String path, bool recursive) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _rename(String path, String newPath) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static List _list(String path, bool recursive, bool followLinks) {
+ throw new UnimplementedError();
+ }
+}
+
+
+patch class _File {
+ /* patch */ Future<bool> exists() async {
+ DirectoryProxy rootDirectory = await _getRootDirectory();
+ int flags = types.kOpenFlagRead;
+ var response =
+ await rootDirectory.responseOrError(
+ 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;
+ var response =
+ await rootDirectory.responseOrError(
+ rootDirectory.ptr.openFile(_ensurePathIsRelative(path),
+ null,
+ flags));
+ if (response.error != types.Error.ok) {
+ throw _OSErrorFromError(response.error);
+ }
+ return this;
+ }
+
+ /* patch */ static _exists(String path) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _create(String path) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _createLink(String path, String target) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _linkTarget(String path) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _deleteNative(String path) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _deleteLinkNative(String path) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _rename(String oldPath, String newPath) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _renameLink(String oldPath, String newPath) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _copy(String oldPath, String newPath) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _lengthFromPath(String path) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _lastModified(String path) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _open(String path, int mode) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static int _openStdio(int fd) {
+ throw new UnimplementedError();
+ }
+}
+
+patch class FileStat {
+ /* patch */ static _statSync(String path) {
+ }
+}
+
+
+patch class FileSystemEntity {
+ /* patch */ static _getType(String path, bool followLinks) {
+ throw new UnimplementedError();
+ }
+ /* patch */ static _identical(String path1, String path2) {
+ throw new UnimplementedError();
+ }
+ /* patch */ static _resolveSymbolicLinks(String path) {
+ throw new UnimplementedError();
+ }
+}
+
+patch class _Link {
+}
+
+
+patch class _RandomAccessFile {
+ /* patch */ static int _close(int id) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static int _getFD(int id) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _readByte(int id) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _read(int id, int bytes) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _readInto(int id, List<int> buffer, int start, int end) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _writeByte(int id, int value) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _writeFrom(int id, List<int> buffer, int start, int end) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _position(int id) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _setPosition(int id, int position) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _truncate(int id, int length) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _length(int id) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _flush(int id) {
+ throw new UnimplementedError();
+ }
+
+ /* patch */ static _lock(int id, int lock, int start, int end) {
+ throw new UnimplementedError();
+ }
+}
« no previous file with comments | « no previous file | mojo/dart/embedder/io/mojo_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698