Skip to content

Commit

Permalink
Compare temp and saved file hashes
Browse files Browse the repository at this point in the history
Attempt to ensure the file saved correctly (even if it didn't throw any errors) by comparing the temp plist's MD5 hash against the destination plist's MD5 hash - and warn if they are different.
  • Loading branch information
corpnewt authored Aug 7, 2022
1 parent f9a98bd commit b8ab110
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions Scripts/plistwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,21 @@ def check_path_length(self, item):
if not paths_too_long: return [] # Return an empty array to allow .extend()
return [(item,name,paths_too_long)] # Return a list containing a tuple of the original item, and which paths are too long

def get_hash(self,path,block_size=65536):
if not os.path.exists(path):
return ""
hasher = hashlib.md5()
try:
with open(path,"rb") as f:
while True:
buffer = f.read(block_size)
if not buffer: break
hasher.update(buffer)
return hasher.hexdigest()
except:
pass
return "" # Couldn't determine hash :(

def oc_snapshot(self, event = None, clean = False):
target_dir = os.path.dirname(self.current_plist) if self.current_plist and os.path.exists(os.path.dirname(self.current_plist)) else None
oc_folder = fd.askdirectory(title="Select OC Folder:",initialdir=target_dir)
Expand Down Expand Up @@ -1030,13 +1045,7 @@ def oc_snapshot(self, event = None, clean = False):
# Folders are valid - lets work through each section

# Let's get the hash of OpenCore.efi, compare to a known list, and then compare that version to our snapshot_version if found
hasher = hashlib.md5()
try:
with open(oc_efi,"rb") as f:
hasher.update(f.read())
oc_hash = hasher.hexdigest()
except:
oc_hash = "" # Couldn't determine hash :(
oc_hash = self.get_hash(oc_efi)
# Let's get the version of the snapshot that matches our target, and that matches our hash if any
latest_snap = {} # Highest min_version
target_snap = {} # Matches our hash
Expand Down Expand Up @@ -1979,6 +1988,13 @@ def save_plist_as(self, event=None, path=None):
f.write(plist_text)
# Copy the temp over
shutil.copy(temp_file,path)
# Let's ensure the md5 of the temp file and saved file are the same
# There have been some reports of file issues when saving directly to an ESP
temp_hash = self.get_hash(temp_file)
save_hash = self.get_hash(path)
if not temp_hash == save_hash: # Some issue occurred - let's throw an exception
self.bell()
mb.showerror("Saved MD5 Hash Mismatch","The saved and temp file hashes do not match - which suggests that copying from the temp directory to the destination was unsuccessful.\n\nIf the destination volume is an ESP, try first saving to your Desktop, then copying the file over manually.",parent=self)
except Exception as e:
# Had an issue, throw up a display box
self.bell()
Expand Down

0 comments on commit b8ab110

Please sign in to comment.