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 committed by Arkadiusz Hiler
parent 7cb4c4dc93
commit 90a11aca8e

93
proton
View File

@ -36,6 +36,13 @@ CURRENT_PREFIX_VERSION="7.0-100"
PFX="Proton: "
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):
return len(s) > 0 and s != "0"
@ -70,7 +77,7 @@ def file_is_wine_builtin_dll(path):
)):
# This may be a broken link to a dll in a removed Proton install
return True
if not os.path.exists(path):
if not file_exists(path, follow_symlinks=True):
return False
try:
sfile = open(path, "rb")
@ -103,17 +110,17 @@ def merge_user_dir(src, dst):
#we only want to copy into directories which don't already exist. games
#may not react well to two save directory instances being merged.
if not os.path.exists(dst_dir) or os.path.samefile(dst_dir, dst):
if not file_exists(dst_dir, follow_symlinks=True) or os.path.samefile(dst_dir, dst):
makedirs(dst_dir)
for dir_ in dirs:
src_file = os.path.join(src_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):
try_copy(src_file, dst_file, copy_metadata=True, follow_symlinks=False)
for file_ in files:
src_file = os.path.join(src_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):
try_copy(src_file, dst_file, copy_metadata=True, follow_symlinks=False)
else:
extant_dirs += dst_dir
@ -127,7 +134,7 @@ def try_copy(src, dst, prefix=None, add_write_perm=True, copy_metadata=False, op
if os.path.isdir(dst):
dst = os.path.join(dst, os.path.basename(src))
if os.path.lexists(dst):
if file_exists(dst, follow_symlinks=False):
os.remove(dst)
elif track_file and prefix is not None:
track_file.write(os.path.relpath(dst, prefix) + '\n')
@ -141,10 +148,10 @@ def try_copy(src, dst, prefix=None, add_write_perm=True, copy_metadata=False, op
new_mode = os.lstat(dst).st_mode | stat.S_IWUSR | stat.S_IWGRP
os.chmod(dst, new_mode)
if not os.path.exists(src + '.debug'):
if not file_exists(src + '.debug', follow_symlinks=True):
link_debug = False
if os.path.lexists(dst + '.debug'):
if file_exists(dst + '.debug', follow_symlinks=False):
os.remove(dst + '.debug')
elif link_debug:
track_file.write(os.path.relpath(dst + '.debug', prefix) + '\n')
@ -169,9 +176,9 @@ def try_copyfile(src, dst):
try:
if os.path.isdir(dst):
dstfile = dst + "/" + os.path.basename(src)
if os.path.lexists(dstfile):
if file_exists(dstfile, follow_symlinks=False):
os.remove(dstfile)
elif os.path.lexists(dst):
elif file_exists(dst, follow_symlinks=False):
os.remove(dst)
shutil.copyfile(src, dst)
except PermissionError as e:
@ -206,17 +213,17 @@ def setup_game_dir_drive():
if "gamedrive" in g_session.compat_config:
library_dir = try_get_game_library_dir()
if not library_dir:
if os.path.lexists(gamedrive_path):
if file_exists(gamedrive_path, follow_symlinks=False):
os.remove(gamedrive_path)
else:
if os.path.lexists(gamedrive_path):
if file_exists(gamedrive_path, follow_symlinks=False):
cur_tgt = os.readlink(gamedrive_path)
if cur_tgt != library_dir:
os.remove(gamedrive_path)
os.symlink(library_dir, gamedrive_path)
else:
os.symlink(library_dir, gamedrive_path)
elif os.path.lexists(gamedrive_path):
elif file_exists(gamedrive_path, follow_symlinks=False):
os.remove(gamedrive_path)
# Function to find the installed location of DLL files for use by Wine/Proton
@ -295,7 +302,7 @@ def find_nvidia_wine_dll_dir():
nvidia_wine_dir = os.path.join(os.path.dirname(libglx_nvidia_realpath), "nvidia", "wine")
# Check that nvngx.dll exists here, or fail
if os.path.exists(os.path.join(nvidia_wine_dir, "nvngx.dll")):
if file_exists(os.path.join(nvidia_wine_dir, "nvngx.dll"), follow_symlinks=True):
return nvidia_wine_dir
return None
@ -342,18 +349,18 @@ class Proton:
def need_tarball_extraction(self):
'''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 \
not os.path.exists(self.path("dist/version")) or \
return not file_exists(self.dist_dir, follow_symlinks=True) or \
not file_exists(self.path("dist/version"), follow_symlinks=True) or \
not filecmp.cmp(self.version_file, self.path("dist/version"))
def extract_tarball(self):
with self.dist_lock:
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)
tar = None
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:*")
break
if not tar:
@ -390,7 +397,7 @@ class CompatData:
return self.base_dir + d
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??")
return
@ -398,7 +405,7 @@ class CompatData:
dirs = []
for f in tracked_files:
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):
os.remove(path)
else:
@ -438,21 +445,21 @@ class CompatData:
(int(new_proton_maj) == int(old_proton_maj) and \
int(new_proton_min) < int(old_proton_min)):
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
try_copy(g_proton.path("proton_3.7_tracked_files"), self.tracked_files_file)
self.remove_tracked_files()
return
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
log("Detected broken 64-bit-only installation, re-creating prefix.")
shutil.rmtree(self.prefix_dir)
return
#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"):
log("Broken .NET installation detected, switching to wine-mono.")
#deleting this directory allows wine-mono to work
@ -533,7 +540,7 @@ class CompatData:
stale_builtins = [self.prefix_dir + "/drive_c/windows/system32/amd_ags_x64.dll",
self.prefix_dir + "/drive_c/windows/syswow64/amd_ags_x64.dll" ]
for builtin in stale_builtins:
if os.path.lexists(builtin) and file_is_wine_builtin_dll(builtin):
if file_exists(builtin, follow_symlinks=False) and file_is_wine_builtin_dll(builtin):
log("Removing stale builtin " + builtin)
os.remove(builtin)
@ -563,18 +570,18 @@ class CompatData:
if len(rel_dir) > 0:
rel_dir = rel_dir + "/"
dst_dir = src_dir.replace(g_proton.default_pfx_dir, self.prefix_dir, 1)
if not os.path.lexists(dst_dir):
if not file_exists(dst_dir, follow_symlinks=False):
os.makedirs(dst_dir)
tracked_files.write(rel_dir + "\n")
for dir_ in dirs:
src_file = os.path.join(src_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)
for file_ in files:
src_file = os.path.join(src_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)
tracked_files.write(rel_dir + file_ + "\n")
# Set .update-timestamp so Wine doesn't try to update the prefix.
@ -595,7 +602,7 @@ class CompatData:
if len(rel_dir) > 0:
rel_dir = rel_dir + "/"
dst_dir = src_dir.replace(g_proton.default_pfx_dir, self.prefix_dir, 1)
if not os.path.lexists(dst_dir):
if not file_exists(dst_dir, follow_symlinks=False):
os.makedirs(dst_dir)
tracked_files.write(rel_dir + "\n")
for file_ in files:
@ -606,7 +613,7 @@ class CompatData:
continue
if file_is_wine_builtin_dll(dst_file):
os.unlink(dst_file)
elif os.path.lexists(dst_file):
elif file_exists(dst_file, follow_symlinks=False):
# builtin library was replaced
continue
else:
@ -639,7 +646,7 @@ class CompatData:
for p in fontsmap:
lname = os.path.join(windowsfonts, p[2])
fname = os.path.join(p[0], p[1])
if os.path.lexists(lname):
if file_exists(lname, follow_symlinks=False):
if os.path.islink(lname):
os.remove(lname)
os.symlink(fname, lname)
@ -664,15 +671,15 @@ class CompatData:
#running unofficial Proton/Wine builds against a Proton prefix could
#create an infinite symlink loop. detect this and clean it up.
if os.path.lexists(new) and os.path.islink(new) and os.readlink(new).endswith(old):
if file_exists(new, follow_symlinks=False) and os.path.islink(new) and os.readlink(new).endswith(old):
os.remove(new)
old = self.prefix_dir + old
if os.path.lexists(old) and not os.path.islink(old):
if file_exists(old, follow_symlinks=False) and not os.path.islink(old):
merge_user_dir(src=old, dst=new)
os.rename(old, old + " BACKUP")
if not os.path.lexists(old):
if not file_exists(old, follow_symlinks=False):
makedirs(os.path.dirname(old))
os.symlink(src=link, dst=old)
elif os.path.islink(old) and not (os.readlink(old) == link):
@ -687,7 +694,7 @@ class CompatData:
config_dir = self.prefix_dir + "drive_c/users/steamuser/My Documents/My Games/FINAL FANTASY XIV - A Realm Reborn/"
config_file = config_dir + "FFXIV_BOOT.cfg"
if os.path.exists(config_file):
if file_exists(config_file, follow_symlinks=True):
with open(config_file, "r") as file:
config_content = file.read()
config_content = config_content.replace("Browser\t2", "Browser\t1")
@ -701,7 +708,7 @@ class CompatData:
def setup_prefix(self):
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:
old_ver = f.readline().strip()
else:
@ -709,11 +716,11 @@ class CompatData:
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")
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.migrate_user_paths()
@ -876,13 +883,13 @@ class CompatData:
else:
nvapi64_dll = self.prefix_dir + "drive_c/windows/system32/nvapi64.dll"
nvapi32_dll = self.prefix_dir + "drive_c/windows/syswow64/nvapi.dll"
if os.path.exists(nvapi64_dll):
if file_exists(nvapi64_dll, follow_symlinks=True):
os.unlink(nvapi64_dll)
if os.path.exists(nvapi64_dll + '.debug'):
if file_exists(nvapi64_dll + '.debug', follow_symlinks=True):
os.unlink(nvapi64_dll + '.debug')
if os.path.exists(nvapi32_dll):
if file_exists(nvapi32_dll, follow_symlinks=True):
os.unlink(nvapi32_dll)
if os.path.exists(nvapi32_dll + '.debug'):
if file_exists(nvapi32_dll + '.debug', follow_symlinks=True):
os.unlink(nvapi32_dll + '.debug')
# Try to detect known DLLs that ship with the NVIDIA Linux Driver
@ -980,7 +987,7 @@ class Session:
if "STEAM_COMPAT_MEDIA_PATH" in os.environ:
old_audiofoz_path = os.environ["STEAM_COMPAT_MEDIA_PATH"] + "/audio.foz"
if os.path.lexists(old_audiofoz_path):
if file_exists(old_audiofoz_path, follow_symlinks=False):
os.remove(old_audiofoz_path)
self.env["MEDIACONV_AUDIO_DUMP_FILE"] = os.environ["STEAM_COMPAT_MEDIA_PATH"] + "/audiov2.foz"
self.env["MEDIACONV_VIDEO_DUMP_FILE"] = os.environ["STEAM_COMPAT_MEDIA_PATH"] + "/video.foz"
@ -1025,7 +1032,7 @@ class Session:
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)
makedirs(basedir)
@ -1037,7 +1044,7 @@ class Session:
#load environment overrides
used_user_settings = {}
if os.path.exists(g_proton.user_settings_file):
if file_exists(g_proton.user_settings_file, follow_symlinks=True):
try:
import user_settings
for key, value in user_settings.user_settings.items():