From 3a9fa00032fd8dca84848fcc979a5f97f8def534 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 8 Jul 2013 12:24:39 +0530 Subject: [PATCH] Try to automatically fix temp folder permissions on windows --- manual/faq.rst | 4 +++- src/calibre/__init__.py | 12 ++++++++++-- src/calibre/customize/conversion.py | 2 +- src/calibre/ptempfile.py | 13 +++++++++++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/manual/faq.rst b/manual/faq.rst index 64da7cd7ef..e5a6342cf8 100644 --- a/manual/faq.rst +++ b/manual/faq.rst @@ -790,7 +790,9 @@ folder. Some users have reported that running the following command in an Administrator Command Prompt fixed their permissions. To get an Administrator Command Prompt search for cmd.exe in the start menu, then right click on the command prompt -entry and select Run as Administrator:: +entry and select Run as Administrator. At the command prompt type the following +command and press Enter:: + icacls "%appdata%\..\Local\Temp" /reset /T Alternately, you can run calibre as Administrator, but doing so will cause diff --git a/src/calibre/__init__.py b/src/calibre/__init__.py index 07ad906247..5d938ecc55 100644 --- a/src/calibre/__init__.py +++ b/src/calibre/__init__.py @@ -436,13 +436,21 @@ def fit_image(width, height, pwidth, pheight): class CurrentDir(object): - def __init__(self, path): + def __init__(self, path, workaround_temp_folder_permissions=False): self.path = path self.cwd = None + self.workaround_temp_folder_permissions = workaround_temp_folder_permissions def __enter__(self, *args): self.cwd = os.getcwdu() - os.chdir(self.path) + try: + os.chdir(self.path) + except OSError: + if not self.workaround_temp_folder_permissions: + raise + from calibre.ptempfile import reset_temp_folder_permissions + reset_temp_folder_permissions() + os.chdir(self.path) return self.cwd def __exit__(self, *args): diff --git a/src/calibre/customize/conversion.py b/src/calibre/customize/conversion.py index 38ffcef71f..9a7ed0d24c 100644 --- a/src/calibre/customize/conversion.py +++ b/src/calibre/customize/conversion.py @@ -233,7 +233,7 @@ class InputFormatPlugin(Plugin): # In case stdout is broken pass - with CurrentDir(output_dir): + with CurrentDir(output_dir, workaround_temp_folder_permissions=True): for x in os.listdir('.'): shutil.rmtree(x) if os.path.isdir(x) else os.remove(x) diff --git a/src/calibre/ptempfile.py b/src/calibre/ptempfile.py index 96271fbeaf..f3816f766b 100644 --- a/src/calibre/ptempfile.py +++ b/src/calibre/ptempfile.py @@ -34,6 +34,19 @@ def app_prefix(prefix): return '%s_'%__appname__ return '%s_%s_%s'%(__appname__, __version__, prefix) +def reset_temp_folder_permissions(): + # There are some broken windows installs where the permissions for the temp + # folder are set to not be executable, which means chdir() into temp + # folders fails. Try to fix that by resetting the permissions on the temp + # folder. + global _base_dir + if iswindows and _base_dir: + import subprocess + from calibre import prints + parent = os.path.dirname(_base_dir) + retcode = subprocess.Popen(['icacls.exe', parent, '/reset', '/Q', '/T']).wait() + prints('Trying to reset permissions of temp folder', parent, 'return code:', retcode) + def base_dir(): global _base_dir if _base_dir is not None and not os.path.exists(_base_dir):