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,21 +536,26 @@ def remove_dir_if_empty(path, ignore_metadata_caches=False):
|
|||||||
try:
|
try:
|
||||||
os.rmdir(path)
|
os.rmdir(path)
|
||||||
except OSError as e:
|
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
|
# Some linux systems appear to raise an EPERM instead of an
|
||||||
# ENOTEMPTY, see https://bugs.launchpad.net/bugs/1240797
|
# ENOTEMPTY, see https://bugs.launchpad.net/bugs/1240797
|
||||||
if ignore_metadata_caches:
|
if ignore_metadata_caches:
|
||||||
try:
|
try:
|
||||||
found = False
|
found = False
|
||||||
for x in os.listdir(path):
|
for x in entries:
|
||||||
if x.lower() in {'.ds_store', 'thumbs.db'}:
|
if x.lower() in {'.ds_store', 'thumbs.db'}:
|
||||||
found = True
|
found = True
|
||||||
x = os.path.join(path, x)
|
x = os.path.join(path, x)
|
||||||
if os.path.isdir(x):
|
with suppress(FileNotFoundError):
|
||||||
import shutil
|
if os.path.isdir(x):
|
||||||
shutil.rmtree(x)
|
import shutil
|
||||||
else:
|
shutil.rmtree(x)
|
||||||
os.remove(x)
|
else:
|
||||||
|
os.remove(x)
|
||||||
except Exception: # We could get an error, if, for example, windows has locked Thumbs.db
|
except Exception: # We could get an error, if, for example, windows has locked Thumbs.db
|
||||||
found = False
|
found = False
|
||||||
if found:
|
if found:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user