Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit d6795a9

Browse files
authored
Update code to use errno values (#44)
This updates all code that throws FileSystemException to properly set the corresponding osError.errorCode, which allows us to update the tests to expect the error code rather than the error message. This in turn allows the tests to be more robust across locales and platforms. To deal with the fact that different platforms have different errno values (which will be exposed and tested via LocalFileSystem), this change creates an ErrorCodes class with static getters for the set of errno values that are common to all platforms. This code can be removed once dart-lang/sdk#28860 is resolved and live. Fixes #43
1 parent d9dad57 commit d6795a9

24 files changed

+925
-256
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#### 2.2.0
2+
3+
* Added `ErrorCodes` class, which holds errno values.
4+
15
#### 2.1.0
26

37
* Add support for new `dart:io` API methods added in Dart SDK 1.23

lib/src/backends/chroot/chroot_directory.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ class _ChrootDirectory extends _ChrootFileSystemEntity<Directory, io.Directory>
3030
Future<Directory> rename(String newPath) async {
3131
if (_isLink) {
3232
if (await fileSystem.type(path) != expectedType) {
33-
throw new FileSystemException('Not a directory', path);
33+
throw common.notADirectory(path);
3434
}
3535
FileSystemEntityType type = await fileSystem.type(newPath);
3636
if (type != FileSystemEntityType.NOT_FOUND) {
3737
if (type != expectedType) {
38-
throw new FileSystemException('Not a directory', newPath);
38+
throw common.notADirectory(newPath);
3939
}
4040
if (!(await fileSystem
4141
.directory(newPath)
4242
.list(followLinks: false)
4343
.isEmpty)) {
44-
throw new FileSystemException('Directory not empty', newPath);
44+
throw common.directoryNotEmpty(newPath);
4545
}
4646
}
4747
String target = await fileSystem.link(path).target();
@@ -58,18 +58,18 @@ class _ChrootDirectory extends _ChrootFileSystemEntity<Directory, io.Directory>
5858
Directory renameSync(String newPath) {
5959
if (_isLink) {
6060
if (fileSystem.typeSync(path) != expectedType) {
61-
throw new FileSystemException('Not a directory', path);
61+
throw common.notADirectory(path);
6262
}
6363
FileSystemEntityType type = fileSystem.typeSync(newPath);
6464
if (type != FileSystemEntityType.NOT_FOUND) {
6565
if (type != expectedType) {
66-
throw new FileSystemException('Not a directory', newPath);
66+
throw common.notADirectory(newPath);
6767
}
6868
if (fileSystem
6969
.directory(newPath)
7070
.listSync(followLinks: false)
7171
.isNotEmpty) {
72-
throw new FileSystemException('Directory not empty', newPath);
72+
throw common.directoryNotEmpty(newPath);
7373
}
7474
}
7575
String target = fileSystem.link(path).targetSync();
@@ -99,9 +99,9 @@ class _ChrootDirectory extends _ChrootFileSystemEntity<Directory, io.Directory>
9999
if (_isLink) {
100100
switch (await fileSystem.type(path)) {
101101
case FileSystemEntityType.NOT_FOUND:
102-
throw new FileSystemException('No such file or directory', path);
102+
throw common.noSuchFileOrDirectory(path);
103103
case FileSystemEntityType.FILE:
104-
throw new FileSystemException('File exists', path);
104+
throw common.fileExists(path);
105105
case FileSystemEntityType.DIRECTORY:
106106
// Nothing to do.
107107
return this;
@@ -118,9 +118,9 @@ class _ChrootDirectory extends _ChrootFileSystemEntity<Directory, io.Directory>
118118
if (_isLink) {
119119
switch (fileSystem.typeSync(path)) {
120120
case FileSystemEntityType.NOT_FOUND:
121-
throw new FileSystemException('No such file or directory', path);
121+
throw common.noSuchFileOrDirectory(path);
122122
case FileSystemEntityType.FILE:
123-
throw new FileSystemException('File exists', path);
123+
throw common.fileExists(path);
124124
case FileSystemEntityType.DIRECTORY:
125125
// Nothing to do.
126126
return;

lib/src/backends/chroot/chroot_file.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class _ChrootFile extends _ChrootFileSystemEntity<File, io.File>
4545
};
4646
break;
4747
case FileSystemEntityType.DIRECTORY:
48-
throw new FileSystemException('Is a directory', newPath);
48+
throw common.isADirectory(newPath);
4949
default:
5050
// Should never happen.
5151
throw new AssertionError();
@@ -55,9 +55,9 @@ class _ChrootFile extends _ChrootFileSystemEntity<File, io.File>
5555
if (_isLink) {
5656
switch (await fileSystem.type(path)) {
5757
case FileSystemEntityType.NOT_FOUND:
58-
throw new FileSystemException('No such file or directory', path);
58+
throw common.noSuchFileOrDirectory(path);
5959
case FileSystemEntityType.DIRECTORY:
60-
throw new FileSystemException('Is a directory', path);
60+
throw common.isADirectory(path);
6161
case FileSystemEntityType.FILE:
6262
await setUp();
6363
await fileSystem.delegate
@@ -94,7 +94,7 @@ class _ChrootFile extends _ChrootFileSystemEntity<File, io.File>
9494
};
9595
break;
9696
case FileSystemEntityType.DIRECTORY:
97-
throw new FileSystemException('Is a directory', newPath);
97+
throw common.isADirectory(newPath);
9898
default:
9999
// Should never happen.
100100
throw new AssertionError();
@@ -104,9 +104,9 @@ class _ChrootFile extends _ChrootFileSystemEntity<File, io.File>
104104
if (_isLink) {
105105
switch (fileSystem.typeSync(path)) {
106106
case FileSystemEntityType.NOT_FOUND:
107-
throw new FileSystemException('No such file or directory', path);
107+
throw common.noSuchFileOrDirectory(path);
108108
case FileSystemEntityType.DIRECTORY:
109-
throw new FileSystemException('Is a directory', path);
109+
throw common.isADirectory(path);
110110
case FileSystemEntityType.FILE:
111111
setUp();
112112
fileSystem.delegate
@@ -149,7 +149,7 @@ class _ChrootFile extends _ChrootFileSystemEntity<File, io.File>
149149
// Nothing to do.
150150
return this;
151151
case FileSystemEntityType.DIRECTORY:
152-
throw new FileSystemException('Is a directory', path);
152+
throw common.isADirectory(path);
153153
default:
154154
throw new AssertionError();
155155
}
@@ -181,7 +181,7 @@ class _ChrootFile extends _ChrootFileSystemEntity<File, io.File>
181181
// Nothing to do.
182182
return;
183183
case FileSystemEntityType.DIRECTORY:
184-
throw new FileSystemException('Is a directory', path);
184+
throw common.isADirectory(path);
185185
default:
186186
throw new AssertionError();
187187
}

lib/src/backends/chroot/chroot_file_system.dart

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ class ChrootFileSystem extends FileSystem {
114114
case FileSystemEntityType.DIRECTORY:
115115
break;
116116
case FileSystemEntityType.NOT_FOUND:
117-
throw new FileSystemException('No such file or directory');
117+
throw common.noSuchFileOrDirectory(path);
118118
default:
119-
throw new FileSystemException('Not a directory');
119+
throw common.notADirectory(path);
120120
}
121121
assert(() {
122122
p.Context ctx = delegate.path;
@@ -299,7 +299,7 @@ class ChrootFileSystem extends FileSystem {
299299
case FileSystemEntityType.FILE:
300300
breadcrumbs.clear();
301301
if (parts.isNotEmpty) {
302-
throw new FileSystemException('Not a directory', currentPath);
302+
throw common.notADirectory(currentPath);
303303
}
304304
break;
305305
case FileSystemEntityType.NOT_FOUND:
@@ -308,10 +308,6 @@ class ChrootFileSystem extends FileSystem {
308308
return getCurrentPath();
309309
}
310310

311-
FileSystemException notFoundException() {
312-
return new FileSystemException('No such file or directory', path);
313-
}
314-
315311
switch (notFound) {
316312
case _NotFoundBehavior.mkdir:
317313
if (parts.isNotEmpty) {
@@ -324,18 +320,17 @@ class ChrootFileSystem extends FileSystem {
324320
if (parts.isEmpty) {
325321
return returnEarly();
326322
}
327-
throw notFoundException();
323+
throw common.noSuchFileOrDirectory(path);
328324
case _NotFoundBehavior.throwError:
329-
throw notFoundException();
325+
throw common.noSuchFileOrDirectory(path);
330326
}
331327
break;
332328
case FileSystemEntityType.LINK:
333329
if (parts.isEmpty && !followLinks) {
334330
break;
335331
}
336332
if (!breadcrumbs.add(currentPath)) {
337-
throw new FileSystemException(
338-
'Too many levels of symbolic links', path);
333+
throw common.tooManyLevelsOfSymbolicLinks(path);
339334
}
340335
String target = delegate.link(realPath).targetSync();
341336
if (ctx.isAbsolute(target)) {

lib/src/backends/chroot/chroot_file_system_entity.dart

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,9 @@ abstract class _ChrootFileSystemEntity<T extends FileSystemEntity,
116116
String resolvedPath = fileSystem._resolve(p.basename(path),
117117
from: p.dirname(path), notFound: _NotFoundBehavior.allowAtTail);
118118
if (!recursive && await type(resolvedPath) != expectedType) {
119-
String msg = expectedType == FileSystemEntityType.FILE
120-
? 'Is a directory'
121-
: 'Not a directory';
122-
throw new FileSystemException(msg, path);
119+
throw expectedType == FileSystemEntityType.FILE
120+
? common.isADirectory(path)
121+
: common.notADirectory(path);
123122
}
124123
await fileSystem.delegate.link(real(path)).delete();
125124
}
@@ -146,10 +145,9 @@ abstract class _ChrootFileSystemEntity<T extends FileSystemEntity,
146145
String resolvedPath = fileSystem._resolve(p.basename(path),
147146
from: p.dirname(path), notFound: _NotFoundBehavior.allowAtTail);
148147
if (!recursive && type(resolvedPath) != expectedType) {
149-
String msg = expectedType == FileSystemEntityType.FILE
150-
? 'Is a directory'
151-
: 'Not a directory';
152-
throw new FileSystemException(msg, path);
148+
throw expectedType == FileSystemEntityType.FILE
149+
? common.isADirectory(path)
150+
: common.notADirectory(path);
153151
}
154152
fileSystem.delegate.link(real(path)).deleteSync();
155153
}

lib/src/backends/memory/memory_directory.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class _MemoryDirectory extends _MemoryFileSystemEntity implements Directory {
3939
);
4040
if (node.type != expectedType) {
4141
// There was an existing non-directory node at this object's path
42-
throw new io.FileSystemException('File exists', path);
42+
throw common.notADirectory(path);
4343
}
4444
}
4545

@@ -73,7 +73,7 @@ class _MemoryDirectory extends _MemoryFileSystemEntity implements Directory {
7373
newPath,
7474
validateOverwriteExistingEntity: (_DirectoryNode existingNode) {
7575
if (existingNode.children.isNotEmpty) {
76-
throw new io.FileSystemException('Directory not empty', newPath);
76+
throw common.directoryNotEmpty(newPath);
7777
}
7878
},
7979
);

lib/src/backends/memory/memory_file.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class _MemoryFile extends _MemoryFileSystemEntity implements File {
5151
if (node.type != expectedType) {
5252
// There was an existing non-file entity at this object's path
5353
assert(node.type == FileSystemEntityType.DIRECTORY);
54-
throw new io.FileSystemException('Is a directory', path);
54+
throw common.isADirectory(path);
5555
}
5656
return node;
5757
}
@@ -66,10 +66,9 @@ class _MemoryFile extends _MemoryFileSystemEntity implements File {
6666
checkType: (_Node node) {
6767
FileSystemEntityType actualType = node.stat.type;
6868
if (actualType != expectedType) {
69-
String msg = actualType == FileSystemEntityType.NOT_FOUND
70-
? 'No such file or directory'
71-
: 'Is a directory';
72-
throw new FileSystemException(msg, path);
69+
throw actualType == FileSystemEntityType.NOT_FOUND
70+
? common.noSuchFileOrDirectory(path)
71+
: common.isADirectory(path);
7372
}
7473
},
7574
);
@@ -231,7 +230,7 @@ class _MemoryFile extends _MemoryFileSystemEntity implements File {
231230
bool flush: false,
232231
}) {
233232
if (!_isWriteMode(mode)) {
234-
throw new FileSystemException('Bad file descriptor', path);
233+
throw common.badFileDescriptor(path);
235234
}
236235
_FileNode node = _resolvedBackingOrCreate;
237236
_truncateIfNecessary(node, mode);

lib/src/backends/memory/memory_file_system_entity.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ abstract class _MemoryFileSystemEntity implements FileSystemEntity {
261261
_Node node = _backing;
262262
if (!recursive) {
263263
if (node is _DirectoryNode && node.children.isNotEmpty) {
264-
throw new io.FileSystemException('Directory not empty', path);
264+
throw common.directoryNotEmpty(path);
265265
}
266266
(checkType ?? _defaultCheckType)(node);
267267
}

lib/src/backends/memory/memory_link.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ class _MemoryLink extends _MemoryFileSystemEntity implements Link {
2222
newPath,
2323
checkType: (_Node node) {
2424
if (node.type != expectedType) {
25-
throw new FileSystemException(
26-
node.type == FileSystemEntityType.DIRECTORY
27-
? 'Is a directory'
28-
: 'Invalid argument');
25+
throw node.type == FileSystemEntityType.DIRECTORY
26+
? common.isADirectory(newPath)
27+
: common.invalidArgument(newPath);
2928
}
3029
},
3130
);
@@ -50,7 +49,7 @@ class _MemoryLink extends _MemoryFileSystemEntity implements Link {
5049
});
5150
if (preexisting) {
5251
// Per the spec, this is an error.
53-
throw new io.FileSystemException('File exists', path);
52+
throw common.fileExists(path);
5453
}
5554
}
5655

@@ -82,7 +81,7 @@ class _MemoryLink extends _MemoryFileSystemEntity implements Link {
8281
_Node node = _backing;
8382
if (node.type != expectedType) {
8483
// Note: this may change; https://github.com/dart-lang/sdk/issues/28204
85-
throw new FileSystemException('No such file or directory', path);
84+
throw common.noSuchFileOrDirectory(path);
8685
}
8786
return (node as _LinkNode).target;
8887
}

lib/src/backends/memory/utils.dart

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ typedef void _TypeChecker(_Node node);
2727
/// Throws a [io.FileSystemException] if [node] is null.
2828
void _checkExists(_Node node, _PathGenerator path) {
2929
if (node == null) {
30-
throw new io.FileSystemException('No such file or directory', path());
30+
throw common.noSuchFileOrDirectory(path());
3131
}
3232
}
3333

3434
/// Throws a [io.FileSystemException] if [node] is not a directory.
3535
void _checkIsDir(_Node node, _PathGenerator path) {
3636
if (!_isDirectory(node)) {
37-
throw new io.FileSystemException('Not a directory', path());
37+
throw common.notADirectory(path());
3838
}
3939
}
4040

@@ -46,23 +46,18 @@ void _checkType(
4646
_PathGenerator path,
4747
) {
4848
if (expectedType != actualType) {
49-
String msg;
5049
switch (expectedType) {
5150
case FileSystemEntityType.DIRECTORY:
52-
msg = 'Not a directory';
53-
break;
51+
throw common.notADirectory(path());
5452
case FileSystemEntityType.FILE:
5553
assert(actualType == FileSystemEntityType.DIRECTORY);
56-
msg = 'Is a directory';
57-
break;
54+
throw common.isADirectory(path());
5855
case FileSystemEntityType.LINK:
59-
msg = 'Invalid argument';
60-
break;
56+
throw common.invalidArgument(path());
6157
default:
6258
// Should not happen
6359
throw new AssertionError();
6460
}
65-
throw new io.FileSystemException(msg, path());
6661
}
6762
}
6863

@@ -111,8 +106,7 @@ _Node _resolveLinks(
111106
while (_isLink(node)) {
112107
link = node;
113108
if (!breadcrumbs.add(node)) {
114-
throw new io.FileSystemException(
115-
'Too many levels of symbolic links', path());
109+
throw common.tooManyLevelsOfSymbolicLinks(path());
116110
}
117111
if (ledger != null) {
118112
if (_isAbsolute(link.target)) {

0 commit comments

Comments
 (0)