Skip to content

Commit cc23409

Browse files
committed
Fix linker creating existing links,
resulting in Clear moving old existing hard link to overwrite
1 parent 50e814f commit cc23409

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

src/rootbuilder/modules/rootbuilder_linker.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,34 @@ def build(self):
2828
linkFileData = self.files.getRootModFiles()
2929
else:
3030
linkFileData = self.files.getLinkableModFiles()
31-
linkOutputData = []
31+
32+
if self.paths.rootLinkDataFilePath().exists():
33+
linkOutputData = json.load(open(self.paths.rootLinkDataFilePath(),"r", encoding="utf-8"))
34+
else:
35+
linkOutputData = {}
36+
3237
for file in linkFileData:
3338
relativePath = self.paths.rootRelativePath(file)
3439
gamePath = self.paths.gamePath() / relativePath
35-
# If the linkable file is already in the game folder, rename it.
40+
file_path = Path(file)
41+
createLink = True
3642
if gamePath.exists():
37-
#qInfo("Renaming for link " + str(gamePath))
38-
self.utilities.moveTo(gamePath, Path(str(gamePath) + ".rbackup"))
39-
# Create the dirs if they don't exist.
40-
if not gamePath.parent.exists():
41-
os.makedirs(gamePath.parent)
42-
# Try and create a link. This will fail if a link is already there.
43-
#qInfo("Creating link for " + str(gamePath))
44-
Path(file).link_to(gamePath)
45-
mapping = {
46-
"Source": str(file),
47-
"Destination": str(gamePath)
48-
}
49-
linkOutputData.append(mapping)
43+
if str(gamePath) not in linkOutputData:
44+
# The linkable file is already in the game folder, rename it.
45+
self.utilities.moveTo(gamePath, Path(str(gamePath) + ".rbackup"))
46+
elif gamePath.samefile(file_path):
47+
# This is already linked.
48+
createLink = False
49+
else:
50+
# Remove old link to update with new one
51+
gamePath.unlink()
52+
if createLink:
53+
# Create the dirs if they don't exist.
54+
if not gamePath.parent.exists():
55+
os.makedirs(gamePath.parent)
56+
# Try and create a link. This will fail if a link is already there.
57+
file_path.link_to(gamePath)
58+
linkOutputData[str(gamePath)] = str(file)
5059
# Save our link data.
5160
if not self.paths.rootLinkDataFilePath().exists():
5261
self.paths.rootLinkDataFilePath().touch()
@@ -59,9 +68,9 @@ def clear(self):
5968
if self.paths.rootLinkDataFilePath().exists():
6069
linkFileData = json.load(open(self.paths.rootLinkDataFilePath(),"r", encoding="utf-8"))
6170
# Loop through our link data and unlink individual files.
62-
for file in linkFileData:
63-
destPath = Path(file["Destination"])
64-
srcPath = Path(file["Source"])
71+
for dest in linkFileData:
72+
destPath = Path(dest)
73+
srcPath = Path(linkFileData[dest])
6574
if destPath.exists():
6675
if os.stat(destPath).st_nlink <= 1:
6776
self.utilities.moveTo(destPath, srcPath)

0 commit comments

Comments
 (0)