Skip to content

Commit 78caa7b

Browse files
committed
fix error when old file used by binja
1 parent f5ba78f commit 78caa7b

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

__init__.py

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ def write_data_file(version, hash, file_name):
115115
def delete_data_file():
116116
path = os.path.join(binaryninja.user_plugin_path(), 'native_plugins_data', plugin_name + '.data')
117117
if os.path.isfile(path):
118-
os.remove(path)
118+
try:
119+
os.remove(path)
120+
except Exception as error:
121+
return path
122+
return True
119123

120124
# Function that calculates hash of file
121125
def calculate_hash(file_path):
@@ -149,13 +153,21 @@ def download_file_to_temp(file_url, file_name):
149153
# Function that deletes file
150154
def delete_file(file_path):
151155
if os.path.isfile(file_path):
152-
os.remove(file_path)
156+
try:
157+
os.remove(file_path)
158+
except Exception as error:
159+
return file_path
160+
return True
153161

154162
# Function that deletes file from temp directory
155163
def delete_file_from_temp(file_name):
156164
path = os.path.join(binaryninja.user_plugin_path(), 'temp', file_name)
157165
if os.path.isfile(path):
158-
os.remove(path)
166+
try:
167+
os.remove(path)
168+
except Exception as error:
169+
return path
170+
return True
159171

160172
# Function that determines whether plugin is installed (for current Binary Ninja version)
161173
def is_plugin_installed(file_name):
@@ -214,14 +226,20 @@ def check_for_updates(repo_owner, repo_name, file_url, win_files, linux_files, d
214226
else:
215227
# Delete all files in temp folder
216228
for file in os.listdir(os.path.join(binaryninja.user_plugin_path(), 'temp')):
217-
delete_file_from_temp(file)
229+
ret = delete_file_from_temp(file)
230+
if ret != True:
231+
alert_user('Failed to delete {}, please close Binary Ninja and delete the file/folder manually'.format(ret))
232+
return
218233

219234
# Verify we have correct file
220235
if (is_system_supported(file) and latest_version != None):
221236
plugin_data = (read_data_file() if data_file_exists() else None) if data_folder_exists() else None
222237
# Check if we have all required data (version, hash, file name)
223238
if plugin_data == None or len(plugin_data) != 3 or plugin_data[0] == None or plugin_data[1] == None or plugin_data[2] == None:
224-
delete_data_file() if data_file_exists() else None
239+
ret = delete_data_file() if data_file_exists() else True
240+
if ret != True:
241+
alert_user('Failed to delete {}, please close Binary Ninja and delete the file/folder manually'.format(ret))
242+
return
225243
plugin_data = None
226244

227245
data_version = plugin_data[0] if plugin_data != None else None
@@ -231,9 +249,15 @@ def check_for_updates(repo_owner, repo_name, file_url, win_files, linux_files, d
231249
# Check if we there is a binary for different Binary Ninja version
232250
if (data_file_name != None and data_file_name != file):
233251
# Delete old file
234-
delete_file(os.path.join(binaryninja.user_plugin_path(), data_file_name))
252+
ret = delete_file(os.path.join(binaryninja.user_plugin_path(), data_file_name))
253+
if ret != True:
254+
alert_user('Failed to delete {}, please close Binary Ninja and delete the file/folder manually'.format(ret))
255+
return
235256
# Delete data file
236-
delete_data_file()
257+
ret = delete_data_file()
258+
if ret != True:
259+
alert_user('Failed to delete {}, please close Binary Ninja and delete the file/folder manually'.format(ret))
260+
return
237261
# Reset data
238262
plugin_data = None
239263
data_version = None
@@ -256,7 +280,10 @@ def check_for_updates(repo_owner, repo_name, file_url, win_files, linux_files, d
256280
if (calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)) == calculate_hash(os.path.join(binaryninja.user_plugin_path(), 'temp', file))):
257281
# We have the latest version, register it in data directory
258282
write_data_file(latest_version, calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)), file)
259-
delete_file_from_temp(file)
283+
ret = delete_file_from_temp(file)
284+
if ret != True:
285+
alert_user('Failed to delete {}, please close Binary Ninja and delete the file/folder manually'.format(ret))
286+
return
260287
else:
261288
# We don't have the latest version, alert user
262289
alert_user('You are using outdated version of this plugin and it must be updated manually\n1. download the latest version from {}\n2. close Binary Ninja\n3. replace the outdated plugin with the newly downloaded file in {}'.format(file_url, binaryninja.user_plugin_path()))
@@ -268,7 +295,10 @@ def check_for_updates(repo_owner, repo_name, file_url, win_files, linux_files, d
268295
alert_user('You are using outdated version of this plugin and it must be updated manually\n1. download the latest version from {}\n2. close Binary Ninja\n3. replace the outdated plugin with the newly downloaded file in {}'.format(file_url, binaryninja.user_plugin_path()))
269296
else:
270297
# Nope, version noted in data doesn't correspond to the hash of currently installed plugin (user probably replaced the plugin)
271-
delete_data_file()
298+
ret = delete_data_file()
299+
if ret != True:
300+
alert_user('Failed to delete {}, please close Binary Ninja and delete the file/folder manually'.format(ret))
301+
return
272302
# Download latest version of the plugin and check if we have that version
273303
download_file_to_temp(file_url, file)
274304
if (calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)) == calculate_hash(os.path.join(binaryninja.user_plugin_path(), 'temp', file))):
@@ -283,13 +313,19 @@ def check_for_updates(repo_owner, repo_name, file_url, win_files, linux_files, d
283313
# Make sure the version in the data directory is actually the version we have installed (we compare hashes - hash now and hash when we downloaded the plugin)
284314
if (data_hash != calculate_hash(os.path.join(binaryninja.user_plugin_path(), file))):
285315
# Nope, hash noted in data doesn't correspond to the hash of currently installed plugin (user probably replaced the plugin with different version)
286-
delete_data_file()
316+
ret = delete_data_file()
317+
if ret != True:
318+
alert_user('Failed to delete {}, please close Binary Ninja and delete the file/folder manually'.format(ret))
319+
return
287320
# Let's check if our plugin matches the hash in the latest github release (developer could have replaced file in the github release and user updated to it)
288321
download_file_to_temp(file_url, file)
289322
if (calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)) == calculate_hash(os.path.join(binaryninja.user_plugin_path(), 'temp', file))):
290323
# Yep, hash of the plugin in the github release corresponds to the hash of currently installed plugin so we have the latest one
291324
write_data_file(latest_version, calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)), file)
292-
delete_file_from_temp(file)
325+
ret = delete_file_from_temp(file)
326+
if ret != True:
327+
alert_user('Failed to delete {}, please close Binary Ninja and delete the file/folder manually'.format(ret))
328+
return
293329
else:
294330
# Not the latest one (according to the hash in the github release), but user might be intending to test different version of the plugin, add ignore option
295331
alert_user('You are using outdated version of this plugin and it must be updated manually\n1. download the latest version from {}\n2. close Binary Ninja\n3. replace the outdated plugin with the newly downloaded file in {}'.format(file_url, binaryninja.user_plugin_path()))

0 commit comments

Comments
 (0)