Commit 416aad7
committed
project: use os.rename() instead of shutil.move() for cloned manifest
As reported in zephyrproject-rtos#963 (thanks!) and seen in the source of
https://github.com/python/cpython/blob/main/Lib/shutil.py,
`shutil.move()` tries an (atomic) `os.rename()` first, then falls back
on a copy+delete in case of an OSError raised by `os.rename()`. The
source and destination being on different filesystems is one of the (too
many?) conditions caught by this OSError and how `shutil.move()` claims
to "optimize" moves on the same filesystem thanks to
`os.rename()`. Maybe there isn't another, simple and reliable way to
check whether source and destionation are on the same filesystem and
perform this optimization?
In this particular `project.py` use case however, we _know_ that both
are on the same filesystem because we purposely located them in the same
directory so we can benefit from very fast and atomic renames guaranteed
by practically all filesystems in the universe. So, let's simplify
`shutil.move()` down to `os.rename()` in this particular case. Quoting
the same `Lib/shutil.py#move()` source:
os.rename() is preferably used if the source and destination are on the
same filesystem. [...]
It's recommended to use os.rename() if atomic move is strictly required.
But wait... if we know we are always on the same filesystem, then why
does this even matter? os.rename() should always succeed inside
shutil.move() and this minor cleanup should never make any visible
difference, right? To answer that we need to go back to zephyrproject-rtos#963, NTFS and
antivirus scanners which when combined together can break even the
simplest things like renaming a directory. While this simplification is
far from making west compatible with such utterly broken systems, it
does remove a lot of complexity and headaches when such a failure
happens and it recommends the existing --rename-delay workaround.
Signed-off-by: Marc Herbert <Marc.Herbert@gmail.com>1 parent 1152ac2 commit 416aad7
1 file changed
Lines changed: 5 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
516 | 516 | | |
517 | 517 | | |
518 | 518 | | |
519 | | - | |
520 | | - | |
521 | | - | |
522 | | - | |
523 | | - | |
| 519 | + | |
| 520 | + | |
524 | 521 | | |
525 | 522 | | |
526 | 523 | | |
527 | 524 | | |
528 | 525 | | |
529 | | - | |
530 | | - | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
531 | 529 | | |
532 | 530 | | |
533 | 531 | | |
| |||
0 commit comments