Problem/Motivation
The method \Drupal\Component\FileSystem::getOsTemporaryDirectory() is trying to maintain multi-os compatibility instead of using php defaults.
Proposed resolution
Deprecate the method and replace it with sys_get_temp_dir().
Remaining tasks
None
User interface changes
None
API changes
The method \Drupal\Component\FileSystem::getOsTemporaryDirectory() is deprecated and replaced by
sys_get_temp_dir().
Data model changes
None
Release notes snippet
TBD
Original Issue
In #3039026-67: Deprecate file_directory_temp() and move to FileSystem service which adds "fallback" to configurable temporary dir the last fallback to this component which, according to https://www.php.net/manual/en/wrappers.php.php#wrappers.php.memory could be replaced with default stream wrapper
The whole component trying to maintain multi-os compatibility instead of using php defaults
The logic split into component & service could be simplified
| Comment | File | Size | Author |
|---|---|---|---|
| #43 | 3063704-nr-bot.txt | 90 bytes | needs-review-queue-bot |
| #26 | 3063704-26.patch | 10.59 KB | andypost |
| #17 | 3063704-17.patch | 9.71 KB | kim.pepper |
Issue fork drupal-3063704
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
berdir> The whole component trying to maintain multi-os compatibility instead of using php defaults
That depends on whether the php defaults are actually sufficient and we'll need to test that.
All those low-level functions we have in the FileSystem class are to work around and expand on incomplete/missing features in the corresponding php functions.
Comment #3
andypost@Berdir basically this function trying to guess where temp dir lives but starts with
upload_tmp_dirinstead ofsys_get_temp_dir()and environment variablesMeantime php itself going different https://github.com/php/php-src/blob/master/main/php_open_temporary_file....
I started to review places where core require temp dir and no longer see the reason why
upload_tmp_dirused here.When most of temp file/dir usage are in tests and upload is delegated to
\Symfony\Component\HttpFoundation\FileBag.Now it looks core need to reconsider temporary file/dir usage
Comment #4
zviryatko commented@Berdir I've done a little research and found that this code was added 13 years ago (!) because there was an error on Windows 2000 and PHP 5.0.5-dev. Initial issue #26249 and it wasn't changed but only moved around.
Right now I think that there's no reason to keep it there. Seems we need to start getting rid of legacy in the core.
Comment #5
catchComment #6
catchWould definitely be great to remove this if possible. I guess the first step is a patch and then try to get it tested on windows?
Comment #7
andypostGood point, will do stub
Comment #11
longwaveDiscovered this issue while helping @mglaman and @mixologic figure out why the wrong tmp dir was being used in upgrade_status even though
sys_temp_dirwas being explicitly set.This is because \Drupal\Component\FileSystem\FileSystem::getOsTemporaryDirectory() looks at these directories in order of preference:
This feels like a bug to me, because if
sys_temp_diris set in php.ini I would expect any application to follow that - but if /tmp exists (and it almost always will on a Unix system) then it will always be used in preference.Comment #12
andypostIt looks a blocker now, but I'm afk
Comment #14
kim.pepperInitial patch that just swaps usage with 'php://temp'. Let's see what breaks!
Comment #15
andypostLooks tests won't run
Comment #16
kim.pepperFrom what I've read, 'php://temp' is not a location, but will create a new one-time-use temp dir each time the stream is opened, and cannot be re-used.
Instead we want to use sys_get_temp_dir().
Here's a patch that does that.
Comment #17
kim.pepperFix coding standards and adds a deprecation test.
Comment #18
berdirI think I'm pretty confused now.
Are we certain that we no longer need all those existing checks?
The trailing / inconsistency for example, and according to some comments on https://www.php.net/manual/en/function.sys-get-temp-dir.php, it might return a per-user path in some cases.
Also, https://www.php.net/manual/en/ini.core.php#ini.upload-tmp-dir does mention open_basedir, what about hosting setups that use that (is that still a thing these days?) and have a customized upload dir.
Feels like that might be a lot of work to verify all those things on all kinds of operationg systems to deprecate a handful lines of code?
Comment #19
kim.pepperI guess we are testing those assumptions? Some of the comments are quite old. For example, trailing slash seems to always be removed https://github.com/php/php-src/blob/af6c11c5f060870d052a2b765dc634d9e47d...
sys_temp_dircan be set in php.ini since PHP 5.5 and we check an empty value in\Drupal\Core\File\FileSystem::getTempDirectory().You might be right. 🤔
Comment #20
andypostGreat point about temporary file (within request) and it's make me to check current usage of
sys_get_temp_dir()in core.The file system service just allows to decorate PHP and then OS-settings (if defined and what the deprecated method doing)
Maybe it needs to file another issue to review current usage of temp-dir and
tempnam()to make all usages to use service instead ofsys_get_temp_dir()likecore/lib/Drupal/Core/Database/Driver/sqlite/Install/Tasks.php:79: $connection_info['default']['database'] = \Drupal::service('file_system')->tempnam(sys_get_temp_dir(), 'sqlite');and
core/tests/Drupal/KernelTests/Core/Theme/FrontMatterTest.php:63: $file = tempnam(sys_get_temp_dir(), 'twig') . ".html.twig";I wonder why OS temp dir (sys_get_temp_dir) used in tests, I think all of them should be site-specific so should use temp-dir from file system service.
Moreover checksing current usage of sys_get_temp_dir() in core codebase it mostly used in tests and few places for install
here the else branch could be removed as system should not care about OS-temp because at runtime it supposed to use service to determine actually used dir (less code in install file)
Comment #21
daffie commented+1 for just deprecating the method.
I think that changing what the method does might result in a BC break. Just doing a deprecation is enough.
Comment #22
andypostFix for #21
I see no much difference if
sys_get_temp_dir()will return trailing slash but I think about follow-up to use file-system service defined temp-dir for tests consistentlyComment #23
andypostClean-up one more mention
Comment #24
andypostFiled follow-up #3225473: Use \Drupal\Core\File\FileSystemInterface::getTempDirectory() instead of sys_get_temp_dir() where possible
Comment #26
andypostFix test and one more usage
Comment #27
daffie commentedThe method has been deprecated.
Deprecation message test has been added.
All use of the code has been replaced.
It is not used in documentaton.
The testbot is happy.
The Is and the CR are in order.
For meit is RTBC.
Comment #28
berdirI'm still uncertain about this, see #18. Just not removing the code in the existing method doesn't really address my concerns IMHO.
Are we _certain_ that those checks for the ini setting and windows aren't required anymore? Did we test the impact of this patch on Windows?
What exactly do we gain from removing and deprecating this code? Is it worth the risk of possibly breaking someones hosting setup?
Comment #29
catchLet's get this manually tested on windows.
Comment #33
smustgrave commentedUnfortunately missed the D10 window
And still needs windows testing. (Can't do as I'm on a mac)
Comment #34
Akhil Yadav commentedAdded patch against #26 in 10.1 version
Comment #35
andypostmessage should be changed to 10.1.0 and 11.0.0
Comment #36
ravi.shankar commentedMade changes as per comment #35.
Comment #37
andypostSee my patch in #26
this test is missing in last reroll
Comment #40
bhanu951 commentedRaised a MR against 10.x branch from the patch in #26, addressed #35 and #37.
Comment #41
bramdriesenCame here for something else, decided to do a code review instead. 😇
Merge request and changes looks good! @Bhanu951
Still needs review for the manual Windows testing mentioned in #33 also on a Mac/Linux here.
Comment #42
smustgrave commentedAsked around and can't find anyone on a windows to do testing.
Comment #43
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #44
bramdriesenI have a Windows VM on which I could test it. Although it's not the "real deal" of course.
Comment #47
alexpottGiven the issue summary of #3225473: Use \Drupal\Core\File\FileSystemInterface::getTempDirectory() instead of sys_get_temp_dir() where possible I'm closing this in favour of that.