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

Unified Diff: sdk/lib/io/file_system_entity.dart

Issue 15018011: dart:io | Add FileSystemEntity.stat() and FileStat class. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add implementation Created 7 years, 7 months 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
Index: sdk/lib/io/file_system_entity.dart
diff --git a/sdk/lib/io/file_system_entity.dart b/sdk/lib/io/file_system_entity.dart
index 847b7d12839eff0abbccca09027dbc5b8c2cf3a5..2ed7290ef6b8ce501d2d88ceb876f9e1f7c7439b 100644
--- a/sdk/lib/io/file_system_entity.dart
+++ b/sdk/lib/io/file_system_entity.dart
@@ -21,6 +21,90 @@ class FileSystemEntityType {
final int _type;
}
+
+class FileStat {
+ // These must agree with enum FileStat in file.h.
+ static const TYPE = 0;
+ static const CHANGED_TIME = 1;
+ static const MODIFIED_TIME = 2;
+ static const ACCESSED_TIME = 3;
+ static const MODE = 4;
+ static const SIZE = 5;
+
+ FileStat._internal(this.changed,
+ this.modified,
+ this.accessed,
+ this.type,
+ this.mode,
+ this.size);
+
+ external static List<int> _statSync(String path);
+
+ static FileStat statSync(String path) {
+ var data = _statSync(path);
+ if (data is Error) throw data;
+ return new FileStat._internal(
+ new DateTime.fromMillisecondsSinceEpoch(data[CHANGED_TIME] * 1000),
+ new DateTime.fromMillisecondsSinceEpoch(data[MODIFIED_TIME] * 1000),
+ new DateTime.fromMillisecondsSinceEpoch(data[ACCESSED_TIME] * 1000),
+ FileSystemEntityType._lookup(data[TYPE]),
+ data[MODE],
+ data[SIZE]);
+ }
+
+ static Future<FileStat> stat(String path) {
+ // Get a new file service port for each request. We could also cache one.
+ var service = _FileUtils._newServicePort();
+ List request = new List(2);
+ request[0] = _STAT_REQUEST;
+ request[1] = path;
+ return service.call(request).then((response) {
+ if (_isErrorResponse(response)) {
+ throw _exceptionFromResponse(response,
+ "Error getting stat of '$path'");
+ }
+ // Unwrap the real list from the "I'm not an error" wrapper.
+ List data = response[1];
+ return new FileStat._internal(
+ new DateTime.fromMillisecondsSinceEpoch(data[CHANGED_TIME] * 1000),
+ new DateTime.fromMillisecondsSinceEpoch(data[MODIFIED_TIME] * 1000),
+ new DateTime.fromMillisecondsSinceEpoch(data[ACCESSED_TIME] * 1000),
+ FileSystemEntityType._lookup(data[TYPE]),
+ data[MODE],
+ data[SIZE]);
+ });
+ }
+
+ String toString() => """
+FileStat: type $type
+ changed $changed
+ modified $modified
+ accessed $accessed
+ mode ${modeString()}
+ size $size""";
+
+ String modeString() {
+ var permissions = mode & 0xFFF;
+ var codes = const ['---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx'];
+ var result = [];
+ if (permissions & 0x800) result.add("(suid) ");
+ if (permissions & 0x400) result.add("(guid) ");
+ if (permissions & 0x200) result.add("(sticky) ");
+ result.add(codes[(permissions >> 6) & 0x7]);
+ result.add(codes[(permissions >> 3) & 0x7]);
+ result.add(codes[permissions & 0x7]);
+ return result.join();
+ }
+
+ final DateTime changed;
+ final DateTime modified;
+ final DateTime accessed;
+ final FileSystemEntityType type;
+ final int mode;
+ final int size;
+}
+
+
/**
* A [FileSystemEntity] is a common super class for [File] and
* [Directory] objects.

Powered by Google App Engine
This is Rietveld 408576698