Skip to content

Commit 98bd922

Browse files
authored
Add FileSystem.systemTempDirectory (flutter#55)
This provides API parity with dart:io's `Directory.systemTemp` getter.
1 parent 571432e commit 98bd922

File tree

7 files changed

+43
-0
lines changed

7 files changed

+43
-0
lines changed

lib/src/backends/chroot/chroot_file_system.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ChrootFileSystem extends FileSystem {
3434
final FileSystem delegate;
3535
final String root;
3636

37+
String _systemTemp;
3738
String _cwd;
3839

3940
/// Creates a new `ChrootFileSystem` backed by the specified [delegate] file
@@ -62,6 +63,15 @@ class ChrootFileSystem extends FileSystem {
6263
@override
6364
Link link(path) => new _ChrootLink(this, common.getPath(path));
6465

66+
/// Gets the system temp directory. This directory will be created on-demand
67+
/// in the local root of the file system. Once created, its location is fixed
68+
/// for the life of the process.
69+
@override
70+
Directory get systemTempDirectory {
71+
_systemTemp ??= directory(_localRoot).createTempSync('.tmp_').path;
72+
return directory(_systemTemp)..createSync();
73+
}
74+
6575
/// Gets the current working directory for this file system. Note that this
6676
/// does *not* proxy to the underlying file system's current directory in
6777
/// any way; the state of this file system's current directory is local to

lib/src/backends/local/local_file_system.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ class LocalFileSystem extends FileSystem {
1717
@override
1818
Link link(path) => new _LocalLink(this, shim.newLink(path));
1919

20+
/// Gets the directory provided by the operating system for creating temporary
21+
/// files and directories in. The location of the system temp directory is
22+
/// platform-dependent, and may be set by an environment variable.
23+
@override
24+
Directory get systemTempDirectory =>
25+
new _LocalDirectory(this, shim.systemTemp());
26+
2027
@override
2128
Directory get currentDirectory => directory(shim.currentDirectory.path);
2229

lib/src/backends/memory/memory_file_system.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ typedef _Node _SegmentVisitor(
4444
/// as [#28078](https://github.com/dart-lang/sdk/issues/28078) is resolved.
4545
class MemoryFileSystem extends FileSystem {
4646
_RootNode _root;
47+
String _systemTemp;
4748
String _cwd = _separator;
4849

4950
MemoryFileSystem() {
@@ -59,6 +60,15 @@ class MemoryFileSystem extends FileSystem {
5960
@override
6061
Link link(path) => new _MemoryLink(this, common.getPath(path));
6162

63+
/// Gets the system temp directory. This directory will be created on-demand
64+
/// in the root of the file system. Once created, its location is fixed for
65+
/// the life of the process.
66+
@override
67+
Directory get systemTempDirectory {
68+
_systemTemp ??= directory(_separator).createTempSync('.tmp_').path;
69+
return directory(_systemTemp)..createSync();
70+
}
71+
6272
@override
6373
Directory get currentDirectory => directory(_cwd);
6474

lib/src/interface/file_system.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ abstract class FileSystem {
2424
/// [path] can be either a [`String`], a [`Uri`], or a [`FileSystemEntity`].
2525
Link link(path);
2626

27+
/// Gets the system temp directory.
28+
///
29+
/// It is left to file system implementations to decide how to define the
30+
/// "system temp directory".
31+
Directory get systemTempDirectory;
32+
2733
/// Creates a directory object pointing to the current working directory.
2834
Directory get currentDirectory;
2935

lib/src/io/shim_dart_io.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:file/src/common.dart' as common;
77
io.Directory newDirectory(path) => new io.Directory(common.getPath(path));
88
io.File newFile(path) => new io.File(common.getPath(path));
99
io.Link newLink(path) => new io.Link(common.getPath(path));
10+
io.Directory systemTemp() => io.Directory.systemTemp;
1011
io.Directory get currentDirectory => io.Directory.current;
1112
set currentDirectory(dynamic path) => io.Directory.current = path;
1213
Future<io.FileStat> stat(String path) => io.FileStat.stat(path);

lib/src/io/shim_internal.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ dynamic _requiresIO() => throw new UnsupportedError(_requiresIOMsg);
88
io.Directory newDirectory(_) => _requiresIO();
99
io.File newFile(_) => _requiresIO();
1010
io.Link newLink(_) => _requiresIO();
11+
io.Directory systemTemp() => _requiresIO();
1112
io.Directory get currentDirectory => _requiresIO();
1213
set currentDirectory(dynamic _) => _requiresIO();
1314
Future<io.FileStat> stat(String _) => _requiresIO();

test/common_tests.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ void runCommonTests(
120120
});
121121
});
122122

123+
group('systemTempDirectory', () {
124+
test('existsAsDirectory', () {
125+
var tmp = fs.systemTempDirectory;
126+
expect(tmp, isDirectory);
127+
expect(tmp.existsSync(), isTrue);
128+
});
129+
});
130+
123131
group('currentDirectory', () {
124132
test('defaultsToRoot', () {
125133
expect(fs.currentDirectory.path, root);

0 commit comments

Comments
 (0)