Skip to content

Commit cf62e79

Browse files
committed
Update
1 parent d83e9ad commit cf62e79

2 files changed

Lines changed: 37 additions & 50 deletions

File tree

.github/module/termux/py/contextpatch.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import os
55
import sys
66

7-
# Đồng bộ kiểu dữ liệu mảng [list] để không bị lỗi rã chữ cái khi dùng join()
8-
# Đã điều chỉnh key không có dấu "/" ở đầu để khớp hoàn chỉnh với cấu trúc sinh ra từ scan_dir
7+
# Key không có dấu "/" ở đầu để khớp hoàn chỉnh với cấu trúc sinh ra từ scan_dir
98
fix_permission = {
109
"vendor/bin/hw/android.hardware.wifi@1.0": ["u:object_r:hal_wifi_default_exec:s0"],
1110
"system/bin/init": ["u:object_r:init_exec:s0"],
@@ -41,33 +40,27 @@ def scan_context(file) -> tuple:
4140
def scan_dir(folder) -> list:
4241
"""
4342
Quét thư mục thực tế và chuyển đổi sang định dạng path chuẩn của Android.
44-
HỖ TRỢ DYNAMIC PARTITIONS: Tự động đưa tên thư mục tạm về tên phân vùng chuẩn.
43+
HỖ TRỢ DYNAMIC PARTITIONS.
4544
"""
46-
# Lấy tên thư mục gốc (giữ nguyên hoa/thường để tránh lỗi phân vùng tùy biến)
47-
folder_name_raw = os.path.basename(os.path.normpath(folder))
48-
folder_name = folder_name_raw.lower()
49-
50-
# Mặc định là tên thư mục gốc ban đầu nếu không nhận diện được phân vùng chuẩn
51-
part_name = folder_name_raw
52-
for partition in ['vendor', 'product', 'system_ext', 'odm', 'system']:
53-
if partition in folder_name:
54-
part_name = partition
55-
break
45+
folder_abs = os.path.abspath(folder).replace('\\', '/')
46+
part_name = os.path.basename(os.path.normpath(folder))
5647

57-
# Xây dựng các root path chuẩn (SELinux của Android thường viết dạng /vendor hay /vendor/.*)
48+
# Xây dựng các root path chuẩn
5849
allfiles = ['/', f'/{part_name}/lost+found', f'/{part_name}', f'/{part_name}/']
5950

6051
for root, dirs, files in os.walk(folder, topdown=True):
52+
# Chuẩn hóa root path hiện tại về dạng gạch chéo xuôi
53+
root_clean = os.path.abspath(root).replace('\\', '/')
54+
6155
for dir_ in dirs:
62-
if os.name == 'nt':
63-
allfiles.append(os.path.join(root, dir_).replace(folder, '/' + part_name).replace('\\', '/'))
64-
elif os.name == 'posix':
65-
allfiles.append(os.path.join(root, dir_).replace(folder, '/' + part_name))
56+
full_path = f"{root_clean}/{dir_}"
57+
target_path = full_path.replace(folder_abs, '/' + part_name)
58+
allfiles.append(target_path)
59+
6660
for file in files:
67-
if os.name == 'nt':
68-
allfiles.append(os.path.join(root, file).replace(folder, '/' + part_name).replace('\\', '/'))
69-
elif os.name == 'posix':
70-
allfiles.append(os.path.join(root, file).replace(folder, '/' + part_name))
61+
full_path = f"{root_clean}/{file}"
62+
target_path = full_path.replace(folder_abs, '/' + part_name)
63+
allfiles.append(target_path)
7164

7265
return sorted(set(allfiles), key=allfiles.index)
7366

@@ -91,22 +84,23 @@ def context_patch(fs_file, filename) -> tuple:
9184
continue
9285

9386
permission = permission_d
94-
95-
# Đồng bộ hóa việc check luật cứng (loại bỏ dấu "/" ở đầu actual_i nếu có để khớp fix_permission)
9687
clean_i = actual_i.lstrip('/')
9788

9889
if clean_i in fix_permission:
9990
permission = fix_permission[clean_i]
10091
elif actual_i in fix_permission:
10192
permission = fix_permission[actual_i]
10293
else:
103-
# Thuật toán tìm ngược lên thư mục cha để lấy quyền kế thừa
94+
# Thuật toán tìm ngược lên thư mục cha có thêm điều kiện chặn lặp vô hạn
10495
tmp_path = os.path.dirname(actual_i)
10596
while tmp_path and tmp_path != "/":
10697
if tmp_path in fs_file:
10798
permission = fs_file[tmp_path]
10899
break
100+
prev_path = tmp_path
109101
tmp_path = os.path.dirname(tmp_path)
102+
if tmp_path == prev_path: # Chặn lỗi lặp cấu trúc lạ
103+
break
110104

111105
print(f"ADD [{actual_i}:{permission}]")
112106
new_fs[actual_i] = permission
@@ -121,11 +115,13 @@ def main(dir_path, fs_config) -> None:
121115
new_fs, added_keys = context_patch(origin, allfiles)
122116

123117
with open(fs_config, "w", encoding='utf-8', newline='\n') as f:
118+
# Ghi lại các key cũ theo đúng thứ tự ban đầu
124119
for key in orig_order:
125120
if key in new_fs:
126121
f.write(f"{key} {' '.join(new_fs[key])}\n")
127122
del new_fs[key]
128123

124+
# Ghi các key mới được thêm vào cuối file
129125
for key in sorted(added_keys):
130126
if key in new_fs:
131127
f.write(f"{key} {' '.join(new_fs[key])}\n")

.github/module/termux/py/fspatch.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,7 @@ def scan_dir(folder):
3030
Quét thư mục thực tế cấu trúc POSIX Android.
3131
Hỗ trợ Dynamic Partitions bằng cách ép đường dẫn gốc về chuẩn Android.
3232
"""
33-
# Lấy tên thư mục thực tế
34-
# Lấy tên thư mục gốc (giữ nguyên hoa/thường ban đầu)
35-
folder_name_raw = os.path.basename(os.path.normpath(folder))
36-
folder_name = folder_name_raw.lower()
37-
38-
# Mặc định là tên thư mục gốc nếu không nhận diện được phân vùng chuẩn
39-
base = folder_name_raw
40-
for partition in ['vendor', 'product', 'system_ext', 'odm', 'system']:
41-
if partition in folder_name:
42-
base = partition
43-
break
33+
base = os.path.basename(os.path.normpath(folder))
4434

4535
yield base
4636
yield '/'
@@ -62,9 +52,11 @@ def islink(file) -> str or None:
6252
if os.path.exists(file) and not os.path.isdir(file):
6353
try:
6454
with open(file, 'rb') as f:
65-
if f.read(12) == b'!<symlink>\xff\xfe':
66-
# Sửa lỗi giải mã: Phải dùng utf-16 cho file có BOM \xff\xfe
67-
return f.read().decode("utf-16").replace('\x00', '').strip()
55+
content = f.read()
56+
# Kiểm tra header của file symlink kiểu cũ (Cygwin/Msys2/Giả lập)
57+
if content.startswith(b'!<symlink>\xff\xfe'):
58+
link_bytes = content[12:]
59+
return link_bytes.decode("utf-16", errors="ignore").replace('\x00', '').strip()
6860
except IOError:
6961
pass
7062
elif os.name == 'posix':
@@ -81,12 +73,15 @@ def fs_patch(fs_file, dir_path) -> tuple:
8173

8274
print("FsPatcher: Load origin %d entries" % len(fs_file.keys()))
8375

84-
# Tập hợp các file nhị phân đặc biệt cần quyền thực thi cao 0755
76+
# Đã sửa: Bỏ dấu gạch chéo ở đầu để khớp chính xác với kết quả sinh ra từ scan_dir
8577
special_binaries = {
86-
"/bin/su", "/xbin/su", "disable_selinux.sh", "daemon", "ext/.su",
78+
"bin/su", "xbin/su", "disable_selinux.sh", "daemon", "ext/.su",
8779
"install-recovery", 'installed_su', 'bin/rw-system.sh', 'bin/getSPL'
8880
}
8981

82+
# Lấy thư mục cha trực tiếp của dir_path một cách an toàn
83+
parent_dir = os.path.dirname(os.path.abspath(dir_path))
84+
9085
for i in scan_dir(os.path.abspath(dir_path)):
9186
actual_i = i
9287
if not i.isprintable():
@@ -99,14 +94,11 @@ def fs_patch(fs_file, dir_path) -> tuple:
9994
if actual_i in r_fs:
10095
continue
10196

102-
# Định dạng đường dẫn kiểm tra file tùy theo OS
103-
if os.name == 'nt':
104-
filepath = os.path.abspath(dir_path + os.sep + ".." + os.sep + i.replace('/', '\\'))
105-
else:
106-
filepath = os.path.abspath(dir_path + os.sep + ".." + os.sep + i)
97+
# Sửa lỗi tính toán sai filepath tương đối khi gộp hệ thống
98+
filepath = os.path.join(parent_dir, actual_i.replace('/', os.sep))
10799

108100
is_dir = os.path.isdir(filepath)
109-
exists = os.path.exists(filepath)
101+
exists = os.path.exists(filepath) or os.path.islink(filepath)
110102
link_target = islink(filepath)
111103

112104
# Thiết lập GID phân vùng nhị phân
@@ -119,14 +111,14 @@ def fs_patch(fs_file, dir_path) -> tuple:
119111
elif not exists:
120112
config = ['0', '0', '0755']
121113
elif link_target is not None:
122-
if ("/bin" in actual_i) or ("/xbin" in actual_i):
114+
if ("bin/" in actual_i) or ("xbin/" in actual_i):
123115
mode = '0755'
124116
elif ".sh" in actual_i:
125117
mode = "0750"
126118
else:
127119
mode = "0644"
128120
config = [uid, gid, mode, link_target]
129-
elif ("/bin" in actual_i) or ("/xbin" in actual_i):
121+
elif ("bin/" in actual_i) or ("xbin/" in actual_i):
130122
if ".sh" in actual_i:
131123
mode = "0750"
132124
elif any(s in actual_i for s in special_binaries):
@@ -146,7 +138,6 @@ def fs_patch(fs_file, dir_path) -> tuple:
146138
return new_fs, added_keys, new_add
147139

148140
def main(dir_path, fs_config) -> None:
149-
# Đọc dữ liệu cũ kèm thứ tự dòng để bảo toàn cấu trúc ROM gốc
150141
origin_fs, orig_order = scanfs(os.path.abspath(fs_config))
151142
new_fs, added_keys, new_add = fs_patch(origin_fs, dir_path)
152143

0 commit comments

Comments
 (0)