[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
diff --git a/packages/path_provider/path_provider_linux/CHANGELOG.md b/packages/path_provider/path_provider_linux/CHANGELOG.md
index 66c11a4..6f18d0d 100644
--- a/packages/path_provider/path_provider_linux/CHANGELOG.md
+++ b/packages/path_provider/path_provider_linux/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 2.1.0
+
+* Now `getTemporaryPath` returns the value of the `TMPDIR` environment variable primarily. If `TMPDIR` is not set, `/tmp` is returned.
+
## 2.0.2
* Updated installation instructions in README.
diff --git a/packages/path_provider/path_provider_linux/lib/path_provider_linux.dart b/packages/path_provider/path_provider_linux/lib/path_provider_linux.dart
index 38800be..ab18db6 100644
--- a/packages/path_provider/path_provider_linux/lib/path_provider_linux.dart
+++ b/packages/path_provider/path_provider_linux/lib/path_provider_linux.dart
@@ -4,6 +4,7 @@
import 'dart:io';
+import 'package:flutter/foundation.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
import 'package:xdg_directories/xdg_directories.dart' as xdg;
@@ -12,6 +13,17 @@
///
/// This class implements the `package:path_provider` functionality for linux
class PathProviderLinux extends PathProviderPlatform {
+ /// Constructs an instance of [PathProviderLinux]
+ PathProviderLinux() : _environment = Platform.environment;
+
+ /// Constructs an instance of [PathProviderLinux] with the given [environment]
+ @visibleForTesting
+ PathProviderLinux.private({
+ required Map<String, String> environment,
+ }) : _environment = environment;
+
+ final Map<String, String> _environment;
+
/// Registers this class as the default instance of [PathProviderPlatform]
static void registerWith() {
PathProviderPlatform.instance = PathProviderLinux();
@@ -19,7 +31,10 @@
@override
Future<String?> getTemporaryPath() {
- return Future<String?>.value('/tmp');
+ final String environmentTmpDir = _environment['TMPDIR'] ?? '';
+ return Future<String?>.value(
+ environmentTmpDir.isEmpty ? '/tmp' : environmentTmpDir,
+ );
}
@override
diff --git a/packages/path_provider/path_provider_linux/pubspec.yaml b/packages/path_provider/path_provider_linux/pubspec.yaml
index 4d43302..f5b7a88 100644
--- a/packages/path_provider/path_provider_linux/pubspec.yaml
+++ b/packages/path_provider/path_provider_linux/pubspec.yaml
@@ -2,7 +2,7 @@
description: Linux implementation of the path_provider plugin
repository: https://github.com/flutter/plugins/tree/master/packages/path_provider/path_provider_linux
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+path_provider%22
-version: 2.0.2
+version: 2.1.0
environment:
sdk: ">=2.12.0 <3.0.0"
diff --git a/packages/path_provider/path_provider_linux/test/path_provider_linux_test.dart b/packages/path_provider/path_provider_linux/test/path_provider_linux_test.dart
index e058d0d..6dd3500 100644
--- a/packages/path_provider/path_provider_linux/test/path_provider_linux_test.dart
+++ b/packages/path_provider/path_provider_linux/test/path_provider_linux_test.dart
@@ -13,8 +13,24 @@
expect(PathProviderPlatform.instance, isA<PathProviderLinux>());
});
- test('getTemporaryPath', () async {
- final PathProviderPlatform plugin = PathProviderPlatform.instance;
+ test('getTemporaryPath defaults to TMPDIR', () async {
+ final PathProviderPlatform plugin = PathProviderLinux.private(
+ environment: <String, String>{'TMPDIR': '/run/user/0/tmp'},
+ );
+ expect(await plugin.getTemporaryPath(), '/run/user/0/tmp');
+ });
+
+ test('getTemporaryPath uses fallback if TMPDIR is empty', () async {
+ final PathProviderPlatform plugin = PathProviderLinux.private(
+ environment: <String, String>{'TMPDIR': ''},
+ );
+ expect(await plugin.getTemporaryPath(), '/tmp');
+ });
+
+ test('getTemporaryPath uses fallback if TMPDIR is unset', () async {
+ final PathProviderPlatform plugin = PathProviderLinux.private(
+ environment: <String, String>{},
+ );
expect(await plugin.getTemporaryPath(), '/tmp');
});