BLI_fileops: Harmonize 'rename' behaviors accross platforms. #115096

Merged
Bastien Montagne merged 7 commits from mont29/blender:tmp-file-rename into main 2023-11-30 22:35:10 +01:00

This commit aim at making the behaviors of BLI_rename and
BLI_rename_overwrite more consistent and coherent across all
supported platforms.

  • BLI_rename now only succeeds in case the target to path does not
    exists (similar to Windows rename behavior).
  • BLI_rename_overwrite allows to replace an existing target to file
    or (empty) directory (similar to Unix rename behavior).

NOTE: In case the target is open by some process on the system, trying
to overwrite it will still fail on Windows, while it should succeed on
Unix-like systems.

The main change for Windows is the usage of MoveFileExW
instead of _wrename, which allows for 'native support' of file
overwrite (using the MOVEFILE_REPLACE_EXISTING flag). Directories
still need to be explicitly removed though.

The main change for *nix systems is the use of renamex_np (OSX) or
renameat2 (most Linux systems) to allow forbidding renaming to an
already existing target in an 'atomic' way.

NOTE: While this commit aims at avoiding the TOC/TOU problem as
much as possible by using available system's primitives for most
common cases, there are some situations where race conditions
(filesystem changes between checks on FS state, and actual rename
operation) remain possible.

This commit aim at making the behaviors of `BLI_rename` and `BLI_rename_overwrite` more consistent and coherent across all supported platforms. * `BLI_rename` now only succeeds in case the target `to` path does not exists (similar to Windows `rename` behavior). * `BLI_rename_overwrite` allows to replace an existing target `to` file or (empty) directory (similar to Unix `rename` behavior). NOTE: In case the target is open by some process on the system, trying to overwrite it will still fail on Windows, while it should succeed on Unix-like systems. The main change for Windows is the usage of `MoveFileExW` instead of `_wrename`, which allows for 'native support' of file overwrite (using the `MOVEFILE_REPLACE_EXISTING` flag). Directories still need to be explicitly removed though. The main change for *nix systems is the use of `renamex_np` (OSX) or `renameat2` (most Linux systems) to allow forbidding renaming to an already existing target in an 'atomic' way. NOTE: While this commit aims at avoiding the TOC/TOU problem as much as possible by using available system's primitives for most common cases, there are some situations where race conditions (filesystem changes between checks on FS state, and actual rename operation) remain possible.
Bastien Montagne added 1 commit 2023-11-18 15:03:06 +01:00
buildbot/vexp-code-patch-coordinator Build done. Details
6d7ac7c48f
BLI_fileops: Harmonize 'rename' behaviors accross platforms.
This commit tries to make the behaviors of `BLI_rename` and
`BLI_rename_overwrite` more consistent and coherent accross all
supported platforms.

* `BLI_rename` now only succeeds in case the target `to` path does not
  exists.
* `BLI_rename_overwrite` allows to replace an existing target `to` file
  or (empty) directory.

NOTE: In case the target is open by some process on the system, trying
to overwrite it will still fail on Windows, while it should succeed on
Unix-like systems.

The main actual change is for Windows, with the usage of `MoveFileExW`
instead of `_wrename`, which allows for 'native support' of file
overwrite (using the `MOVEFILE_REPLACE_EXISTING` flag). Directories
still need to be explicitely removed though.
Author
Owner

@blender-bot build

@blender-bot build
Bastien Montagne requested review from Brecht Van Lommel 2023-11-18 17:48:12 +01:00
Bastien Montagne requested review from Campbell Barton 2023-11-18 17:48:12 +01:00
Bastien Montagne requested review from Sergey Sharybin 2023-11-18 17:48:12 +01:00
Bastien Montagne added the
Module
Core
Interest
Platforms, Builds & Tests
labels 2023-11-18 17:48:29 +01:00
Author
Owner

Note that since this is (mostly) changing things on Windows, and unittests can only do so much,, would be nice to also have some more experienced Windows devs to check on it. @LazyDodo maybe?

Note that since this is (mostly) changing things on Windows, and unittests can only do so much,, would be nice to also have some more experienced Windows devs to check on it. @LazyDodo maybe?
Sergey Sharybin reviewed 2023-11-20 18:32:09 +01:00
@ -449,3 +440,4 @@
return 1;
}
if (BLI_exists(to)) {

Did you look into having non-overwrite behavior which does not suffer from TOC-TOU?

Did you look into having non-overwrite behavior which does not suffer from TOC-TOU?
Author
Owner

Done, although we cannot fully eliminate the TOC/TOU case, in particular for BSD OSs.

Done, although we cannot fully eliminate the TOC/TOU case, in particular for BSD OSs.
mont29 marked this conversation as resolved
Bastien Montagne added 2 commits 2023-11-21 13:04:31 +01:00
buildbot/vexp-code-patch-coordinator Build done. Details
23e98b03de
Attempt to make `rename without overwrite` also atomic.
This is tricky on Unix systems, since there is no standard for this
behavior, BSD does not have it, linux and OSX do with different
functions, and some requirements from the filesystem itself.
Author
Owner

@blender-bot build

@blender-bot build
Bastien Montagne added 1 commit 2023-11-21 13:56:28 +01:00
buildbot/vexp-code-patch-coordinator Build done. Details
0e7239a2e3
Fix missing return in `BLI_rename` in OSX case.
Author
Owner

@blender-bot build

@blender-bot build
Brecht Van Lommel requested changes 2023-11-21 13:59:39 +01:00
@ -467,0 +482,4 @@
}
return rename(from, to);
#else /* __linux__ */
return renameat2(AT_FDCWD, from, AT_FDCWD, to, RENAME_NOREPLACE);

There are also Linuxes without glibc, and other Unixes that don't support this. I think we should check if renameat2 is supported, maybe like this:

#elif defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 28)
  return renameat2(AT_FDCWD, from, AT_FDCWD, to, RENAME_NOREPLACE);
#else
  if (!BLI_exists(to)) {
    return 1;
  }
  return rename(from, to); 
#endif
There are also Linuxes without glibc, and other Unixes that don't support this. I think we should check if `renameat2` is supported, maybe like this: ``` #elif defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 28) return renameat2(AT_FDCWD, from, AT_FDCWD, to, RENAME_NOREPLACE); #else if (!BLI_exists(to)) { return 1; } return rename(from, to); #endif ```
mont29 marked this conversation as resolved
Bastien Montagne added 1 commit 2023-11-21 14:13:09 +01:00
buildbot/vexp-code-patch-coordinator Build done. Details
8eafdebb1e
Re-organize `BLI_rename` code to check on glibc min version.
Do not assume all linuxes have the `renameat2` function.
Author
Owner

@blender-bot build

@blender-bot build
Sergey Sharybin reviewed 2023-11-21 14:19:00 +01:00
@ -467,0 +481,4 @@
return renameat2(AT_FDCWD, from, AT_FDCWD, to, RENAME_NOREPLACE);
#else
/* At least all BSD's currently. */
if (!BLI_exists(to)) {

Shouldn't this be if (BLI_exists(to)) { ?

Shouldn't this be `if (BLI_exists(to)) {` ?
mont29 marked this conversation as resolved
Bastien Montagne added 2 commits 2023-11-21 14:51:35 +01:00
Bastien Montagne requested review from Brecht Van Lommel 2023-11-21 14:51:39 +01:00
Sergey Sharybin approved these changes 2023-11-21 17:41:27 +01:00
Sergey Sharybin left a comment
Owner

From me reading the code seems fine.

From me reading the code seems fine.
Brecht Van Lommel approved these changes 2023-11-21 17:47:54 +01:00
Brecht Van Lommel left a comment
Owner

Works fine on macOS.

Works fine on macOS.
Bastien Montagne merged commit 050d48edfc into main 2023-11-30 22:35:10 +01:00
Bastien Montagne deleted branch tmp-file-rename 2023-11-30 22:35:12 +01:00
Contributor

Hi @mont29,

This commit breaks build for me on FreeBSD using clang 15.

I get error: function-like macro '__GLIBC_PREREQ' is not defined

While it seems reasonable having defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 28) in the same #if it doesn't seem to work with clang.

Breaking it into two lines does work for me.

diff --git a/source/blender/blenlib/intern/fileops_c.cc b/source/blender/blenlib/intern/fileops_c.cc
index 91eb3dec0a35fd9..2839fef3f143c6f 100644
--- a/source/blender/blenlib/intern/fileops_c.cc
+++ b/source/blender/blenlib/intern/fileops_c.cc
@@ -476,9 +476,11 @@ int BLI_rename(const char *from, const char *to)
   return urename(from, to, false);
 #elif defined(__APPLE__)
   return renamex_np(from, to, RENAME_EXCL);
-#elif defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 28)
+#elif defined(__GLIBC_PREREQ)
+#if __GLIBC_PREREQ(2, 28)
   /* Most common Linux cases. */
   return renameat2(AT_FDCWD, from, AT_FDCWD, to, RENAME_NOREPLACE);
+#endif
 #else
   /* At least all BSD's currently. */
   if (BLI_exists(to)) {

Hi @mont29, This commit breaks build for me on FreeBSD using clang 15. I get `error: function-like macro '__GLIBC_PREREQ' is not defined` While it seems reasonable having `defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 28)` in the same `#if` it doesn't seem to work with clang. Breaking it into two lines does work for me. ``` diff --git a/source/blender/blenlib/intern/fileops_c.cc b/source/blender/blenlib/intern/fileops_c.cc index 91eb3dec0a35fd9..2839fef3f143c6f 100644 --- a/source/blender/blenlib/intern/fileops_c.cc +++ b/source/blender/blenlib/intern/fileops_c.cc @@ -476,9 +476,11 @@ int BLI_rename(const char *from, const char *to) return urename(from, to, false); #elif defined(__APPLE__) return renamex_np(from, to, RENAME_EXCL); -#elif defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 28) +#elif defined(__GLIBC_PREREQ) +#if __GLIBC_PREREQ(2, 28) /* Most common Linux cases. */ return renameat2(AT_FDCWD, from, AT_FDCWD, to, RENAME_NOREPLACE); +#endif #else /* At least all BSD's currently. */ if (BLI_exists(to)) { ```
Author
Owner

@ShaneAmbler Hi, thanks for the report, suggested fix seems reasonable! Do you want to submit a PR for it (then you can be officially listed as contributor), or should I go on and commit the change?

@ShaneAmbler Hi, thanks for the report, suggested fix seems reasonable! Do you want to submit a PR for it (then you can be officially listed as contributor), or should I go on and commit the change?
Contributor

I'm fine with you just adjusting it

I'm fine with you just adjusting it
Author
Owner

@ShaneAmbler committed as 1592aaf01a, thanks!

@ShaneAmbler committed as 1592aaf01a, thanks!
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#115096
No description provided.