Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 2076d1d

Browse files
[path_provider_linux] Using TMPDIR env as a primary temporary path (#4218)
TMPDIR is a standard variable on UNIX/Linux systems, and is often used in containers such as Flatpak to redirect to a temporary folder inside a sandbox. This allows not to make hard bindings to the /tmp directory Fixes flutter/flutter#87742
1 parent 4a98e23 commit 2076d1d

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

packages/path_provider/path_provider_linux/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.1.0
2+
3+
* Now `getTemporaryPath` returns the value of the `TMPDIR` environment variable primarily. If `TMPDIR` is not set, `/tmp` is returned.
4+
15
## 2.0.2
26

37
* Updated installation instructions in README.

packages/path_provider/path_provider_linux/lib/path_provider_linux.dart

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:io';
66

7+
import 'package:flutter/foundation.dart';
78
import 'package:path/path.dart' as path;
89
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
910
import 'package:xdg_directories/xdg_directories.dart' as xdg;
@@ -12,14 +13,28 @@ import 'package:xdg_directories/xdg_directories.dart' as xdg;
1213
///
1314
/// This class implements the `package:path_provider` functionality for linux
1415
class PathProviderLinux extends PathProviderPlatform {
16+
/// Constructs an instance of [PathProviderLinux]
17+
PathProviderLinux() : _environment = Platform.environment;
18+
19+
/// Constructs an instance of [PathProviderLinux] with the given [environment]
20+
@visibleForTesting
21+
PathProviderLinux.private({
22+
required Map<String, String> environment,
23+
}) : _environment = environment;
24+
25+
final Map<String, String> _environment;
26+
1527
/// Registers this class as the default instance of [PathProviderPlatform]
1628
static void registerWith() {
1729
PathProviderPlatform.instance = PathProviderLinux();
1830
}
1931

2032
@override
2133
Future<String?> getTemporaryPath() {
22-
return Future<String?>.value('/tmp');
34+
final String environmentTmpDir = _environment['TMPDIR'] ?? '';
35+
return Future<String?>.value(
36+
environmentTmpDir.isEmpty ? '/tmp' : environmentTmpDir,
37+
);
2338
}
2439

2540
@override

packages/path_provider/path_provider_linux/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: path_provider_linux
22
description: Linux implementation of the path_provider plugin
33
repository: https://github.com/flutter/plugins/tree/master/packages/path_provider/path_provider_linux
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+path_provider%22
5-
version: 2.0.2
5+
version: 2.1.0
66

77
environment:
88
sdk: ">=2.12.0 <3.0.0"

packages/path_provider/path_provider_linux/test/path_provider_linux_test.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,24 @@ void main() {
1313
expect(PathProviderPlatform.instance, isA<PathProviderLinux>());
1414
});
1515

16-
test('getTemporaryPath', () async {
17-
final PathProviderPlatform plugin = PathProviderPlatform.instance;
16+
test('getTemporaryPath defaults to TMPDIR', () async {
17+
final PathProviderPlatform plugin = PathProviderLinux.private(
18+
environment: <String, String>{'TMPDIR': '/run/user/0/tmp'},
19+
);
20+
expect(await plugin.getTemporaryPath(), '/run/user/0/tmp');
21+
});
22+
23+
test('getTemporaryPath uses fallback if TMPDIR is empty', () async {
24+
final PathProviderPlatform plugin = PathProviderLinux.private(
25+
environment: <String, String>{'TMPDIR': ''},
26+
);
27+
expect(await plugin.getTemporaryPath(), '/tmp');
28+
});
29+
30+
test('getTemporaryPath uses fallback if TMPDIR is unset', () async {
31+
final PathProviderPlatform plugin = PathProviderLinux.private(
32+
environment: <String, String>{},
33+
);
1834
expect(await plugin.getTemporaryPath(), '/tmp');
1935
});
2036

0 commit comments

Comments
 (0)