Skip to content
Permalink
Browse files

Add Uri.directory constructor.

Make Directory.uri getter use the new constructor.
This also fixes that the uri getter doesn't work correctly on Windows.

Add uri getter on file system entity.

BUG= http://dartbug.com/17065
R=sgjesse@google.com

Review URL: https://codereview.chromium.org//1089183004

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@45227 260f80e4-7a28-3924-810f-c04153c831b5
  • Loading branch information...
lrhn committed Apr 17, 2015
1 parent 8cf32dc commit d8dbb4a60f5e8a7f874c2a4fbf59eaf1a39f4776
@@ -705,7 +705,21 @@ class Uri {
*/
factory Uri.file(String path, {bool windows}) {
windows = (windows == null) ? Uri._isWindows : windows;
return windows ? _makeWindowsFileUrl(path) : _makeFileUri(path);
return windows ? _makeWindowsFileUrl(path, false)
: _makeFileUri(path, false);
}

/**
* Like [Uri.file] except that a non-empty URI path ends in a slash.
*
* If [path] is not empty, and it doesn't end in a directory separator,
* then a slash is added to the returned URI's path.
* In all other cases, the result is the same as returned by `Uri.file`.
*/
factory Uri.directory(String path, {bool windows}) {
windows = (windows == null) ? Uri._isWindows : windows;
return windows ? _makeWindowsFileUrl(path, true)
: _makeFileUri(path, true);
}

/**
@@ -762,18 +776,24 @@ class Uri {
}
}

static _makeFileUri(String path) {
static _makeFileUri(String path, bool slashTerminated) {
const String sep = "/";
var segments = path.split(sep);
if (slashTerminated &&
segments.isNotEmpty &&
segments.last.isNotEmpty) {
segments.add(""); // Extra separator at end.
}
if (path.startsWith(sep)) {
// Absolute file:// URI.
return new Uri(scheme: "file", pathSegments: path.split(sep));
return new Uri(scheme: "file", pathSegments: segments);
} else {
// Relative URI.
return new Uri(pathSegments: path.split(sep));
return new Uri(pathSegments: segments);
}
}

static _makeWindowsFileUrl(String path) {
static _makeWindowsFileUrl(String path, bool slashTerminated) {
if (path.startsWith(r"\\?\")) {
if (path.startsWith(r"UNC\", 4)) {
path = path.replaceRange(0, 7, r'\');
@@ -798,6 +818,10 @@ class Uri {
}
// Absolute file://C:/ URI.
var pathSegments = path.split(sep);
if (slashTerminated &&
pathSegments.last.isNotEmpty) {
pathSegments.add(""); // Extra separator at end.
}
_checkWindowsPathReservedCharacters(pathSegments, true, 1);
return new Uri(scheme: "file", pathSegments: pathSegments);
}
@@ -812,18 +836,31 @@ class Uri {
(pathStart < 0) ? "" : path.substring(pathStart + 1);
var pathSegments = pathPart.split(sep);
_checkWindowsPathReservedCharacters(pathSegments, true);
if (slashTerminated &&
pathSegments.last.isNotEmpty) {
pathSegments.add(""); // Extra separator at end.
}
return new Uri(
scheme: "file", host: hostPart, pathSegments: pathSegments);
} else {
// Absolute file:// URI.
var pathSegments = path.split(sep);
if (slashTerminated &&
pathSegments.last.isNotEmpty) {
pathSegments.add(""); // Extra separator at end.
}
_checkWindowsPathReservedCharacters(pathSegments, true);
return new Uri(scheme: "file", pathSegments: pathSegments);
}
} else {
// Relative URI.
var pathSegments = path.split(sep);
_checkWindowsPathReservedCharacters(pathSegments, true);
if (slashTerminated &&
pathSegments.isNotEmpty &&
pathSegments.last.isNotEmpty) {
pathSegments.add(""); // Extra separator at end.
}
return new Uri(pathSegments: pathSegments);
}
}
@@ -44,11 +44,7 @@ class _Directory extends FileSystemEntity implements Directory {
}

Uri get uri {
String path = this.path;
if (!path.endsWith("/")) {
path = path + "/";
}
return new Uri.file(path);
return new Uri.directory(path);
}

Future<bool> exists() {
@@ -91,10 +91,15 @@ testFileUriWindowsSlash() {
];

for (var test in tests) {
Uri uri;
uri = new Uri.file(test[1], windows: true);
Uri uri = new Uri.file(test[1], windows: true);
Expect.equals(test[0], uri.toString());
Expect.equals(test[2], uri.toFilePath(windows: true));
bool couldBeDir = uri.path.isEmpty || uri.path.endsWith('\\');
Uri dirUri = new Uri.directory(test[1], windows: true);
Expect.isTrue(dirUri.path.isEmpty || dirUri.path.endsWith('/'));
if (couldBeDir) {
Expect.equals(uri, dirUri);
}
}
}

@@ -119,6 +124,12 @@ testFileUriWindowsWin32Namespace() {
Expect.throws(
() => new Uri.file("\\\\?\\UNX\\server\\share\\file", windows: true),
(e) => e is ArgumentError);
Expect.throws(
() => new Uri.directory("\\\\?\\file", windows: true),
(e) => e is ArgumentError);
Expect.throws(
() => new Uri.directory("\\\\?\\UNX\\server\\share\\file", windows: true),
(e) => e is ArgumentError);
}

testFileUriDriveLetter() {
@@ -145,6 +156,12 @@ testFileUriDriveLetter() {
(e) => e is ArgumentError);
Expect.throws(() => new Uri.file("C:a\b", windows: true),
(e) => e is ArgumentError);
Expect.throws(() => new Uri.directory("C:", windows: true),
(e) => e is ArgumentError);
Expect.throws(() => new Uri.directory("C:a", windows: true),
(e) => e is ArgumentError);
Expect.throws(() => new Uri.directory("C:a\b", windows: true),
(e) => e is ArgumentError);
}

testFileUriResolve() {
@@ -197,19 +214,31 @@ testFileUriIllegalCharacters() {
(e) => e is ArgumentError);
Expect.throws(() => new Uri.file("\\$test", windows: true),
(e) => e is ArgumentError);
Expect.throws(() => new Uri.directory(test, windows: true),
(e) => e is ArgumentError);
Expect.throws(() => new Uri.directory("\\$test", windows: true),
(e) => e is ArgumentError);

// It is possible to create non-Windows URIs, but not Windows URIs.
Uri uri = new Uri.file(test, windows: false);
Uri absoluteUri = new Uri.file("/$test", windows: false);
Uri dirUri = new Uri.directory(test, windows: false);
Uri dirAbsoluteUri = new Uri.directory("/$test", windows: false);
Expect.throws(() => new Uri.file(test, windows: true));
Expect.throws(() => new Uri.file("\\$test", windows: true));
Expect.throws(() => new Uri.direcory(test, windows: true));
Expect.throws(() => new Uri.directory("\\$test", windows: true));

// It is possible to extract non-Windows file path, but not
// Windows file path.
Expect.equals(test, uri.toFilePath(windows: false));
Expect.equals("/$test", absoluteUri.toFilePath(windows: false));
Expect.equals("$test/", dirUri.toFilePath(windows: false));
Expect.equals("/$test/", dirAbsoluteUri.toFilePath(windows: false));
Expect.throws(() => uri.toFilePath(windows: true));
Expect.throws(() => absoluteUri.toFilePath(windows: true));
Expect.throws(() => dirUri.toFilePath(windows: true));
Expect.throws(() => dirAbsoluteUri.toFilePath(windows: true));
}

// Backslash
@@ -218,6 +247,8 @@ testFileUriIllegalCharacters() {
// It is possible to create both non-Windows URIs, and Windows URIs.
Uri uri = new Uri.file(test, windows: false);
Uri absoluteUri = new Uri.file("/$test", windows: false);
Uri dirUri = new Uri.directory(test, windows: false);
Uri dirAbsoluteUri = new Uri.directory("/$test", windows: false);
new Uri.file(test, windows: true);
new Uri.file("\\$test", windows: true);

@@ -226,19 +257,30 @@ testFileUriIllegalCharacters() {
// in a path segment).
Expect.equals(test, uri.toFilePath(windows: false));
Expect.equals("/$test", absoluteUri.toFilePath(windows: false));
Expect.equals("$test/", dirUri.toFilePath(windows: false));
Expect.equals("/$test/", dirAbsoluteUri.toFilePath(windows: false));
Expect.throws(() => uri.toFilePath(windows: true),
(e) => e is UnsupportedError);
Expect.throws(() => absoluteUri.toFilePath(windows: true));
Expect.throws(() => dirUri.toFilePath(windows: true),
(e) => e is UnsupportedError);
Expect.throws(() => dirAbsoluteUri.toFilePath(windows: true));
}
}

testFileUriIllegalDriveLetter() {
Expect.throws(() => new Uri.file("1:\\", windows: true),
(e) => e is ArgumentError);
Expect.throws(() => new Uri.directory("1:\\", windows: true),
(e) => e is ArgumentError);
Uri uri = new Uri.file("1:\\", windows: false);
Uri dirUri = new Uri.directory("1:\\", windows: false);
Expect.equals("1:\\", uri.toFilePath(windows: false));
Expect.equals("1:\\/", dirUri.toFilePath(windows: false));
Expect.throws(() => uri.toFilePath(windows: true),
(e) => e is UnsupportedError);
Expect.throws(() => dirUri.toFilePath(windows: true),
(e) => e is UnsupportedError);
}

testAdditionalComponents() {
@@ -34,7 +34,7 @@ void main() {
testFile("/foo/bar");
testFile("/foo/bar/");

testDirectory("", "/");
testDirectory("");
testDirectory("/");
testDirectory("foo/bar", "foo/bar/");
testDirectory("/foo/bar", "/foo/bar/");

0 comments on commit d8dbb4a

Please sign in to comment.
You can’t perform that action at this time.