From eb6518f7fffe7315536a440e582a62f1316e3cd1 Mon Sep 17 00:00:00 2001 From: Cyril Bordage Date: Tue, 25 Oct 2022 18:52:30 +0200 Subject: [PATCH] LU-16267 lnet: fix missing error check in LUTF In find_replace_file function, the file is opened with default encoding option. If the file has a different encoding it will fail. The solution is to use a try/except for UnicodeDecodeError and skip bad encoded files. Test-Parameters: @lnet Signed-off-by: Cyril Bordage Change-Id: I9115d39414d31b628d550e8289b3193d13787288 Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/48951 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Amir Shehata Reviewed-by: Chris Horn Reviewed-by: Oleg Drokin --- lustre/tests/lutf/python/infra/lutf_file.py | 47 +++++++++++++++++------------ 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/lustre/tests/lutf/python/infra/lutf_file.py b/lustre/tests/lutf/python/infra/lutf_file.py index 5658f37..a7f9eec 100644 --- a/lustre/tests/lutf/python/infra/lutf_file.py +++ b/lustre/tests/lutf/python/infra/lutf_file.py @@ -73,11 +73,14 @@ class LutfFile(BaseTest): # search string if not replace: with open(fname) as old_file: - for line in old_file: - if search in line: - if not found_line: - found_line = line - count += 1 + try: + for line in old_file: + if search in line: + if not found_line: + found_line = line + count += 1 + except UnicodeDecodeError: + pass if getline: return count, found_line return count @@ -85,20 +88,26 @@ class LutfFile(BaseTest): fh, abs_path = tempfile.mkstemp() with os.fdopen(fh,'w') as new_file: with open(fname) as old_file: - for line in old_file: - if search in line: - if not found_line: - found_line = line - new_file.write(replace) - count += 1 - else: - new_file.write(line) - #Copy the file permissions from the old file to the new file - shutil.copymode(fname, abs_path) - #Remove original file - os.remove(fname) - #Move new file - shutil.move(abs_path, fname) + try: + for line in old_file: + if search in line: + if not found_line: + found_line = line + new_file.write(replace) + count += 1 + else: + new_file.write(line) + except UnicodeDecodeError: + pass + if count: + #Copy the file permissions from the old file to the new file + shutil.copymode(fname, abs_path) + #Remove original file + os.remove(fname) + #Move new file + shutil.move(abs_path, fname) + else: + os.remove(abs_path) if getline: return count, found_line -- 1.8.3.1