44import os
55import sys
66from re import sub
7+ from re import escape
78
89fix_permission = {
910 "system/app/*/.apk" : "u:object_r:system_file:s0" ,
1819 "/vendor/bin/hw/android.hardware.wifi@1.0" : "u:object_r:hal_wifi_default_exec:s0"
1920}
2021
22+ def str_to_selinux (s : str ) -> str :
23+ return escape (s ).replace ('\\ -' , '-' )
2124
2225def scan_context (file ) -> dict :
2326 context = {}
2427 with open (file , "r" , encoding = 'utf-8' ) as file_ :
2528 for i in file_ .readlines ():
2629 line = i .strip ().replace ('\\ ' , '' )
2730 if not line :
28- continue # Bỏ qua dòng trống
31+ continue
2932 if line .startswith ('#' ):
3033 continue
3134 parts = line .split ()
3235 if not parts :
33- continue # An toàn thêm 1 lần
36+ continue
3437 filepath , * other = parts
38+ filepath = filepath .replace (r'\@' , '@' )
3539 context [filepath ] = other
3640 return context
3741
@@ -54,32 +58,33 @@ def scan_dir(folder) -> list:
5458
5559def context_patch (fs_file , filename ) -> dict :
5660 new_fs = {}
57- permission_d = fs_file .get (list (fs_file )[0 ])
61+ # Giữ logic cũ: lấy permission mặc định từ entry đầu
62+ permission_d = fs_file .get (next (iter (fs_file )))
5863 if not permission_d :
5964 permission_d = ['u:object_r:system_file:s0' ]
6065 for i in filename :
66+ selinux_path = str_to_selinux (i )
6167 if fs_file .get (i ):
62- new_fs [sub (r'([^-_/a-zA-Z0-9])' , r'\\\1' , i )] = fs_file [i ]
63- else :
64- permission = permission_d
65- if i :
66- if not i .isprintable ():
67- tmp = ''
68- for c in i :
69- tmp += c if c .isprintable () else '*'
70- i = tmp
71- if i in fix_permission .keys ():
72- permission = fix_permission [i ]
73- else :
74- # Tìm context từ thư mục gần nhất có trong fs_file
75- tmp_path = os .path .dirname (i )
76- while tmp_path != "/" and tmp_path :
77- if tmp_path in fs_file :
78- permission = fs_file [tmp_path ]
79- break
80- tmp_path = os .path .dirname (tmp_path )
81- print (f"ADD [{ i } :{ permission } ]" )
82- new_fs [sub (r'([^-_/a-zA-Z0-9])' , r'\\\1' , i )] = permission
68+ new_fs [selinux_path ] = fs_file [i ]
69+ continue
70+ permission = permission_d
71+ if i :
72+ # giữ logic cũ: thay ký tự không printable bằng '*'
73+ if not i .isprintable ():
74+ i = '' .join (c if c .isprintable () else '*' for c in i )
75+ # giữ logic cũ: fix_permission
76+ if i in fix_permission :
77+ permission = fix_permission [i ]
78+ else :
79+ # giữ nguyên logic tìm thư mục cha
80+ tmp_path = os .path .dirname (i )
81+ while tmp_path and tmp_path != "/" :
82+ if tmp_path in fs_file :
83+ permission = fs_file [tmp_path ]
84+ break
85+ tmp_path = os .path .dirname (tmp_path )
86+ print (f"ADD [{ i } :{ permission } ]" )
87+ new_fs [selinux_path ] = permission
8388 return new_fs
8489
8590
0 commit comments