1414
1515
1616def str_to_selinux (s : str ) -> str :
17- # Escape dấu . cho đúng định dạng regex file_contexts, giữ nguyên / và -
17+ # Escape dấu . đúng chuẩn regex file_contexts, giữ nguyên / và -
1818 escaped = re .escape (s ).replace (r"\-" , "-" ).replace (r"\/" , "/" )
1919 return escaped .replace (r"\@" , "@" )
2020
2121
22+ def clean_permission (perm : List [str ]) -> List [str ]:
23+ """Lọc bỏ các cờ file type modifier như --, -d, -l, -p,... để tránh gán nhầm cờ của file cho thư mục"""
24+ modifiers = {"--" , "-d" , "-c" , "-b" , "-l" , "-p" , "-s" }
25+ return [p for p in perm if p not in modifiers ]
26+
27+
2228def scan_context (file_path : str ) -> List [ContextEntry ]:
23- """Đọc file gốc và GIỮ NGUYÊN thứ tự xuất hiện của từng dòng."""
2429 entries : List [ContextEntry ] = []
2530 with open (file_path , "r" , encoding = "utf-8" , errors = "ignore" ) as fp :
2631 for line in fp :
@@ -60,15 +65,11 @@ def scan_dir(folder: str) -> list:
6065
6166def _default_permission (entries : List [ContextEntry ]) -> list :
6267 if entries :
63- return entries [0 ][1 ]
68+ return clean_permission ( entries [0 ][1 ])
6469 return ["u:object_r:system_file:s0" ]
6570
6671
6772def find_insert_index (entries : List [ContextEntry ], raw_path : str ) -> Tuple [int , list ]:
68- """
69- Tìm vị trí xuất hiện cuối cùng của thư mục cha trong file gốc
70- để chèn item mới vào ngay bên cạnh/dưới nó. Đồng thời trả về permission kế thừa.
71- """
7273 tmp_path = os .path .dirname (raw_path )
7374
7475 while tmp_path and tmp_path != "/" :
@@ -77,24 +78,24 @@ def find_insert_index(entries: List[ContextEntry], raw_path: str) -> Tuple[int,
7778 found_perm = None
7879
7980 for idx , (path , perm ) in enumerate (entries ):
80- # Khớp với đường dẫn gốc hoặc dạng đã escape
81- if path == tmp_path or path == tmp_selinux or path .startswith (tmp_selinux + "/" ):
81+ # So sánh linh hoạt cả dạng raw lẫn dạng đã escape regex
82+ clean_entry_path = path .replace ("\\ ." , "." ).replace ("\\ " , "" )
83+ if path in (tmp_path , tmp_selinux ) or clean_entry_path == tmp_path or path .startswith (tmp_selinux + "/" ):
8284 last_idx = idx
83- found_perm = perm
85+ found_perm = clean_permission ( perm )
8486
8587 if last_idx is not None :
8688 return last_idx + 1 , found_perm
8789
8890 tmp_path = os .path .dirname (tmp_path )
8991
90- # Nếu không tìm thấy cha, chèn vào cuối file
9192 return len (entries ), None
9293
9394
9495def context_patch (fs_entries : List [ContextEntry ], filename : list ) -> Tuple [List [ContextEntry ], int ]:
9596 entries = list (fs_entries )
9697
97- # Tập hợp các đường dẫn đã tồn tại để tránh chèn trùng
98+ # Chuẩn hóa tập hợp kiểm tra trùng lặp
9899 existing_paths = {path for path , _ in entries }
99100 permission_d = _default_permission (entries )
100101
@@ -111,23 +112,19 @@ def context_patch(fs_entries: List[ContextEntry], filename: list) -> Tuple[List[
111112
112113 selinux_path = str_to_selinux (raw_path )
113114
114- # Bỏ qua nếu dòng đã tồn tại trong file gốc hoặc đã được thêm trước đó
115115 if raw_path in existing_paths or selinux_path in existing_paths or selinux_path in seen_new :
116116 continue
117117
118- # Xử lý quy tắc đè thủ công trong fix_permission
119118 if raw_path in fix_permission :
120119 permission = fix_permission [raw_path ]
121120 insert_at , _ = find_insert_index (entries , raw_path )
122121 elif selinux_path in fix_permission :
123122 permission = fix_permission [selinux_path ]
124123 insert_at , _ = find_insert_index (entries , raw_path )
125124 else :
126- # Tìm vị trí thích hợp (sau thư mục cha) và lấy permission kế thừa
127125 insert_at , parent_perm = find_insert_index (entries , raw_path )
128126 permission = parent_perm if parent_perm else permission_d
129127
130- # Chèn trực tiếp vào vị trí tìm được mà KHÔNG làm xáo trộn các dòng khác
131128 entries .insert (insert_at , (selinux_path , permission ))
132129 seen_new .add (selinux_path )
133130 existing_paths .add (selinux_path )
0 commit comments