proton: Add file_exists helper function

To make desired handling of symlinks more clear at the callsite.
This commit is contained in:
Andrew Eikum 2022-02-09 09:10:04 -06:00
parent 8f5a92e619
commit bc57d557cf

63
proton
View File

@ -25,6 +25,13 @@ CURRENT_PREFIX_VERSION="5.13-1"
PFX="Proton: " PFX="Proton: "
ld_path_var = "LD_LIBRARY_PATH" ld_path_var = "LD_LIBRARY_PATH"
def file_exists(s, *, follow_symlinks):
if follow_symlinks:
#'exists' returns False on broken symlinks
return os.path.exists(s)
#'lexists' returns True on broken symlinks
return os.path.lexists(s)
def nonzero(s): def nonzero(s):
return len(s) > 0 and s != "0" return len(s) > 0 and s != "0"
@ -50,7 +57,7 @@ def file_is_wine_builtin_dll(path):
if os.path.dirname(contents).endswith(('/lib/wine', '/lib/wine/fakedlls', '/lib64/wine', '/lib64/wine/fakedlls')): if os.path.dirname(contents).endswith(('/lib/wine', '/lib/wine/fakedlls', '/lib64/wine', '/lib64/wine/fakedlls')):
# This may be a broken link to a dll in a removed Proton install # This may be a broken link to a dll in a removed Proton install
return True return True
if not os.path.exists(path): if not file_exists(path, follow_symlinks=True):
return False return False
try: try:
sfile = open(path, "rb") sfile = open(path, "rb")
@ -71,11 +78,11 @@ def try_copy(src, dst, add_write_perm=True):
try: try:
if os.path.isdir(dst): if os.path.isdir(dst):
dstfile = dst + "/" + os.path.basename(src) dstfile = dst + "/" + os.path.basename(src)
if os.path.lexists(dstfile): if file_exists(dstfile, follow_symlinks=False):
os.remove(dstfile) os.remove(dstfile)
else: else:
dstfile = dst dstfile = dst
if os.path.lexists(dst): if file_exists(dst, follow_symlinks=False):
os.remove(dst) os.remove(dst)
shutil.copy(src, dst) shutil.copy(src, dst)
@ -95,9 +102,9 @@ def try_copyfile(src, dst):
try: try:
if os.path.isdir(dst): if os.path.isdir(dst):
dstfile = dst + "/" + os.path.basename(src) dstfile = dst + "/" + os.path.basename(src)
if os.path.lexists(dstfile): if file_exists(dstfile, follow_symlinks=False):
os.remove(dstfile) os.remove(dstfile)
elif os.path.lexists(dst): elif file_exists(dst, follow_symlinks=False):
os.remove(dst) os.remove(dst)
shutil.copyfile(src, dst) shutil.copyfile(src, dst)
except PermissionError as e: except PermissionError as e:
@ -166,18 +173,18 @@ class Proton:
def need_tarball_extraction(self): def need_tarball_extraction(self):
'''Checks if the proton_dist tarball archive must be extracted. Returns true if extraction is needed, false otherwise''' '''Checks if the proton_dist tarball archive must be extracted. Returns true if extraction is needed, false otherwise'''
return not os.path.exists(self.dist_dir) or \ return not file_exists(self.dist_dir, follow_symlinks=True) or \
not os.path.exists(self.path("dist/version")) or \ not file_exists(self.path("dist/version"), follow_symlinks=True) or \
not filecmp.cmp(self.version_file, self.path("dist/version")) not filecmp.cmp(self.version_file, self.path("dist/version"))
def extract_tarball(self): def extract_tarball(self):
with self.dist_lock: with self.dist_lock:
if self.need_tarball_extraction(): if self.need_tarball_extraction():
if os.path.exists(self.dist_dir): if file_exists(self.dist_dir, follow_symlinks=True):
shutil.rmtree(self.dist_dir) shutil.rmtree(self.dist_dir)
tar = None tar = None
for sf in ["", ".xz", ".bz2", ".gz"]: for sf in ["", ".xz", ".bz2", ".gz"]:
if os.path.exists(self.path("proton_dist.tar" + sf)): if file_exists(self.path("proton_dist.tar" + sf), follow_symlinks=True):
tar = tarfile.open(self.path("proton_dist.tar" + sf), mode="r:*") tar = tarfile.open(self.path("proton_dist.tar" + sf), mode="r:*")
break break
if not tar: if not tar:
@ -214,7 +221,7 @@ class CompatData:
return self.base_dir + d return self.base_dir + d
def remove_tracked_files(self): def remove_tracked_files(self):
if not os.path.exists(self.tracked_files_file): if not file_exists(self.tracked_files_file, follow_symlinks=True):
log("Prefix has no tracked_files??") log("Prefix has no tracked_files??")
return return
@ -222,7 +229,7 @@ class CompatData:
dirs = [] dirs = []
for f in tracked_files: for f in tracked_files:
path = self.prefix_dir + f.strip() path = self.prefix_dir + f.strip()
if os.path.exists(path): if file_exists(path, follow_symlinks=True):
if os.path.isfile(path) or os.path.islink(path): if os.path.isfile(path) or os.path.islink(path):
os.remove(path) os.remove(path)
else: else:
@ -262,21 +269,21 @@ class CompatData:
(int(new_proton_maj) == int(old_proton_maj) and \ (int(new_proton_maj) == int(old_proton_maj) and \
int(new_proton_min) < int(old_proton_min)): int(new_proton_min) < int(old_proton_min)):
log("Removing newer prefix") log("Removing newer prefix")
if old_proton_ver == "3.7" and not os.path.exists(self.tracked_files_file): if old_proton_ver == "3.7" and not file_exists(self.tracked_files_file, follow_symlinks=True):
#proton 3.7 did not generate tracked_files, so copy it into place first #proton 3.7 did not generate tracked_files, so copy it into place first
try_copy(g_proton.path("proton_3.7_tracked_files"), self.tracked_files_file) try_copy(g_proton.path("proton_3.7_tracked_files"), self.tracked_files_file)
self.remove_tracked_files() self.remove_tracked_files()
return return
if old_proton_ver == "3.7" and old_prefix_ver == "1": if old_proton_ver == "3.7" and old_prefix_ver == "1":
if not os.path.exists(self.prefix_dir + "/drive_c/windows/syswow64/kernel32.dll"): if not file_exists(self.prefix_dir + "/drive_c/windows/syswow64/kernel32.dll", follow_symlinks=True):
#shipped a busted 64-bit-only installation on 20180822. detect and wipe clean #shipped a busted 64-bit-only installation on 20180822. detect and wipe clean
log("Detected broken 64-bit-only installation, re-creating prefix.") log("Detected broken 64-bit-only installation, re-creating prefix.")
shutil.rmtree(self.prefix_dir) shutil.rmtree(self.prefix_dir)
return return
#replace broken .NET installations with wine-mono support #replace broken .NET installations with wine-mono support
if os.path.exists(self.prefix_dir + "/drive_c/windows/Microsoft.NET/NETFXRepair.exe") and \ if file_exists(self.prefix_dir + "/drive_c/windows/Microsoft.NET/NETFXRepair.exe", follow_symlinks=True) and \
file_is_wine_builtin_dll(self.prefix_dir + "/drive_c/windows/system32/mscoree.dll"): file_is_wine_builtin_dll(self.prefix_dir + "/drive_c/windows/system32/mscoree.dll"):
log("Broken .NET installation detected, switching to wine-mono.") log("Broken .NET installation detected, switching to wine-mono.")
#deleting this directory allows wine-mono to work #deleting this directory allows wine-mono to work
@ -334,18 +341,18 @@ class CompatData:
if len(rel_dir) > 0: if len(rel_dir) > 0:
rel_dir = rel_dir + "/" rel_dir = rel_dir + "/"
dst_dir = src_dir.replace(g_proton.default_pfx_dir, self.prefix_dir, 1) dst_dir = src_dir.replace(g_proton.default_pfx_dir, self.prefix_dir, 1)
if not os.path.exists(dst_dir): if not file_exists(dst_dir, follow_symlinks=True):
os.makedirs(dst_dir) os.makedirs(dst_dir)
tracked_files.write(rel_dir + "\n") tracked_files.write(rel_dir + "\n")
for dir_ in dirs: for dir_ in dirs:
src_file = os.path.join(src_dir, dir_) src_file = os.path.join(src_dir, dir_)
dst_file = os.path.join(dst_dir, dir_) dst_file = os.path.join(dst_dir, dir_)
if os.path.islink(src_file) and not os.path.exists(dst_file): if os.path.islink(src_file) and not file_exists(dst_file, follow_symlinks=True):
self.pfx_copy(src_file, dst_file) self.pfx_copy(src_file, dst_file)
for file_ in files: for file_ in files:
src_file = os.path.join(src_dir, file_) src_file = os.path.join(src_dir, file_)
dst_file = os.path.join(dst_dir, file_) dst_file = os.path.join(dst_dir, file_)
if not os.path.exists(dst_file): if not file_exists(dst_file, follow_symlinks=True):
self.pfx_copy(src_file, dst_file) self.pfx_copy(src_file, dst_file)
tracked_files.write(rel_dir + file_ + "\n") tracked_files.write(rel_dir + file_ + "\n")
@ -361,7 +368,7 @@ class CompatData:
if len(rel_dir) > 0: if len(rel_dir) > 0:
rel_dir = rel_dir + "/" rel_dir = rel_dir + "/"
dst_dir = src_dir.replace(g_proton.default_pfx_dir, self.prefix_dir, 1) dst_dir = src_dir.replace(g_proton.default_pfx_dir, self.prefix_dir, 1)
if not os.path.exists(dst_dir): if not file_exists(dst_dir, follow_symlinks=True):
os.makedirs(dst_dir) os.makedirs(dst_dir)
tracked_files.write(rel_dir + "\n") tracked_files.write(rel_dir + "\n")
for file_ in files: for file_ in files:
@ -372,7 +379,7 @@ class CompatData:
continue continue
if file_is_wine_builtin_dll(dst_file): if file_is_wine_builtin_dll(dst_file):
os.unlink(dst_file) os.unlink(dst_file)
elif os.path.lexists(dst_file): elif file_exists(dst_file, follow_symlinks=False):
# builtin library was replaced # builtin library was replaced
continue continue
else: else:
@ -397,7 +404,7 @@ class CompatData:
for p in fontsmap: for p in fontsmap:
lname = os.path.join(windowsfonts, p[1]) lname = os.path.join(windowsfonts, p[1])
fname = os.path.join(g_proton.fonts_dir, p[0]) fname = os.path.join(g_proton.fonts_dir, p[0])
if os.path.lexists(lname): if file_exists(lname, follow_symlinks=False):
if os.path.islink(lname): if os.path.islink(lname):
os.remove(lname) os.remove(lname)
os.symlink(fname, lname) os.symlink(fname, lname)
@ -406,7 +413,7 @@ class CompatData:
def setup_prefix(self): def setup_prefix(self):
with self.prefix_lock: with self.prefix_lock:
if os.path.exists(self.version_file): if file_exists(self.version_file, follow_symlinks=True):
with open(self.version_file, "r") as f: with open(self.version_file, "r") as f:
old_ver = f.readline().strip() old_ver = f.readline().strip()
else: else:
@ -414,11 +421,11 @@ class CompatData:
self.upgrade_pfx(old_ver) self.upgrade_pfx(old_ver)
if not os.path.exists(self.prefix_dir): if not file_exists(self.prefix_dir, follow_symlinks=True):
makedirs(self.prefix_dir + "/drive_c") makedirs(self.prefix_dir + "/drive_c")
set_dir_casefold_bit(self.prefix_dir + "/drive_c") set_dir_casefold_bit(self.prefix_dir + "/drive_c")
if not os.path.exists(self.prefix_dir + "/user.reg"): if not file_exists(self.prefix_dir + "/user.reg", follow_symlinks=True):
self.copy_pfx() self.copy_pfx()
# collect configuration info # collect configuration info
@ -577,17 +584,17 @@ class CompatData:
if "gamedrive" in g_session.compat_config: if "gamedrive" in g_session.compat_config:
library_dir = try_get_game_library_dir() library_dir = try_get_game_library_dir()
if not library_dir: if not library_dir:
if os.path.lexists(gamedrive_path): if file_exists(gamedrive_path, follow_symlinks=False):
os.remove(gamedrive_path) os.remove(gamedrive_path)
else: else:
if os.path.lexists(gamedrive_path): if file_exists(gamedrive_path, follow_symlinks=False):
cur_tgt = os.readlink(gamedrive_path) cur_tgt = os.readlink(gamedrive_path)
if cur_tgt != library_dir: if cur_tgt != library_dir:
os.remove(gamedrive_path) os.remove(gamedrive_path)
os.symlink(library_dir, gamedrive_path) os.symlink(library_dir, gamedrive_path)
else: else:
os.symlink(library_dir, gamedrive_path) os.symlink(library_dir, gamedrive_path)
elif os.path.lexists(gamedrive_path): elif file_exists(gamedrive_path, follow_symlinks=False):
os.remove(gamedrive_path) os.remove(gamedrive_path)
def comma_escaped(s): def comma_escaped(s):
@ -687,7 +694,7 @@ class Session:
self.env["WINEPREFIX"] = g_compatdata.prefix_dir self.env["WINEPREFIX"] = g_compatdata.prefix_dir
#load environment overrides #load environment overrides
if os.path.exists(g_proton.user_settings_file): if file_exists(g_proton.user_settings_file, follow_symlinks=True):
try: try:
import user_settings import user_settings
for key, value in user_settings.user_settings.items(): for key, value in user_settings.user_settings.items():
@ -764,7 +771,7 @@ class Session:
basedir = self.env.get("PROTON_LOG_DIR", os.environ["HOME"]) basedir = self.env.get("PROTON_LOG_DIR", os.environ["HOME"])
makedirs(basedir) makedirs(basedir)
lfile_path = basedir + "/steam-" + os.environ["SteamGameId"] + ".log" lfile_path = basedir + "/steam-" + os.environ["SteamGameId"] + ".log"
if os.path.exists(lfile_path): if file_exists(lfile_path, follow_symlinks=True):
os.remove(lfile_path) os.remove(lfile_path)
self.log_file = open(lfile_path, "w+") self.log_file = open(lfile_path, "w+")
self.log_file.write("======================\n") self.log_file.write("======================\n")