Try to automatically fix temp folder permissions on windows

This commit is contained in:
Kovid Goyal 2013-07-08 12:24:39 +05:30
parent fae8aa1405
commit 3a9fa00032
4 changed files with 27 additions and 4 deletions

View File

@ -790,7 +790,9 @@ folder.
Some users have reported that running the following command in an Administrator Some users have reported that running the following command in an Administrator
Command Prompt fixed their permissions. To get an Administrator Command Prompt 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 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 icacls "%appdata%\..\Local\Temp" /reset /T
Alternately, you can run calibre as Administrator, but doing so will cause Alternately, you can run calibre as Administrator, but doing so will cause

View File

@ -436,12 +436,20 @@ def fit_image(width, height, pwidth, pheight):
class CurrentDir(object): class CurrentDir(object):
def __init__(self, path): def __init__(self, path, workaround_temp_folder_permissions=False):
self.path = path self.path = path
self.cwd = None self.cwd = None
self.workaround_temp_folder_permissions = workaround_temp_folder_permissions
def __enter__(self, *args): def __enter__(self, *args):
self.cwd = os.getcwdu() self.cwd = os.getcwdu()
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) os.chdir(self.path)
return self.cwd return self.cwd

View File

@ -233,7 +233,7 @@ class InputFormatPlugin(Plugin):
# In case stdout is broken # In case stdout is broken
pass pass
with CurrentDir(output_dir): with CurrentDir(output_dir, workaround_temp_folder_permissions=True):
for x in os.listdir('.'): for x in os.listdir('.'):
shutil.rmtree(x) if os.path.isdir(x) else os.remove(x) shutil.rmtree(x) if os.path.isdir(x) else os.remove(x)

View File

@ -34,6 +34,19 @@ def app_prefix(prefix):
return '%s_'%__appname__ return '%s_'%__appname__
return '%s_%s_%s'%(__appname__, __version__, prefix) 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(): def base_dir():
global _base_dir global _base_dir
if _base_dir is not None and not os.path.exists(_base_dir): if _base_dir is not None and not os.path.exists(_base_dir):