16
16
from typing import Any , Dict , Optional , Tuple , cast
17
17
18
18
import nncf
19
+ from nncf .common .utils .os import fail_if_symlink
20
+ from nncf .common .utils .os import safe_open
19
21
20
22
METADATA_FILE = "statistics_metadata.json"
21
23
@@ -35,7 +37,7 @@ def load_metadata(dir_path: Path) -> Dict[str, Any]:
35
37
"""
36
38
metadata_file = dir_path / METADATA_FILE
37
39
if metadata_file .exists ():
38
- with open (metadata_file , "r" ) as f :
40
+ with safe_open (metadata_file , "r" ) as f :
39
41
return cast (Dict [str , Any ], json .load (f ))
40
42
return {"mapping" : {}, "metadata" : {}}
41
43
@@ -47,7 +49,7 @@ def save_metadata(metadata: Dict[str, Any], dir_path: Path) -> None:
47
49
:param dir_path: The directory where the metadata file will be stored.
48
50
"""
49
51
metadata_file = dir_path / METADATA_FILE
50
- with open (metadata_file , "w" ) as f :
52
+ with safe_open (metadata_file , "w" ) as f :
51
53
json .dump (metadata , f , indent = 4 )
52
54
53
55
@@ -70,6 +72,7 @@ def load_from_dir(dir_path: str) -> Tuple[Dict[str, Any], Dict[str, str]]:
70
72
continue # Skip the metadata file
71
73
72
74
try :
75
+ fail_if_symlink (statistics_file )
73
76
with gzip .open (statistics_file , "rb" ) as f :
74
77
sanitized_name = statistics_file .name
75
78
original_name = mapping .get (sanitized_name , sanitized_name )
@@ -101,6 +104,7 @@ def dump_to_dir(
101
104
mapping [sanitized_name ] = original_name
102
105
103
106
try :
107
+ fail_if_symlink (file_path )
104
108
with gzip .open (file_path , "wb" ) as f :
105
109
pickle .dump (statistics_value , f )
106
110
except (IOError , pickle .PicklingError ) as e :
0 commit comments