mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Make remove_dir_if_empty a bit more robust against other programs simultaneously deleting the dir or its contents. See #2060340 (Unhandled Exception - FileNotFoundError)
This commit is contained in:
parent
b5c688c94d
commit
29dbdcdb69
@ -536,16 +536,21 @@ def remove_dir_if_empty(path, ignore_metadata_caches=False):
|
||||
try:
|
||||
os.rmdir(path)
|
||||
except OSError as e:
|
||||
if e.errno == errno.ENOTEMPTY or len(os.listdir(path)) > 0:
|
||||
try:
|
||||
entries = os.listdir(path)
|
||||
except FileNotFoundError: # something deleted path out from under us
|
||||
return
|
||||
if e.errno == errno.ENOTEMPTY or len(entries) > 0:
|
||||
# Some linux systems appear to raise an EPERM instead of an
|
||||
# ENOTEMPTY, see https://bugs.launchpad.net/bugs/1240797
|
||||
if ignore_metadata_caches:
|
||||
try:
|
||||
found = False
|
||||
for x in os.listdir(path):
|
||||
for x in entries:
|
||||
if x.lower() in {'.ds_store', 'thumbs.db'}:
|
||||
found = True
|
||||
x = os.path.join(path, x)
|
||||
with suppress(FileNotFoundError):
|
||||
if os.path.isdir(x):
|
||||
import shutil
|
||||
shutil.rmtree(x)
|
||||
|
Loading…
x
Reference in New Issue
Block a user