MCPcopy
hub / github.com/ArchiveBox/ArchiveBox / atomic_write

Function atomic_write

archivebox/system.py:81–109  ·  view source on GitHub ↗

Safe atomic write to filesystem by writing to temp file + atomic rename

(path: Union[Path, str], contents: Union[dict, str, bytes], overwrite: bool=True)

Source from the content-addressed store, hash-verified

79
80@enforce_types
81def atomic_write(path: Union[Path, str], contents: Union[dict, str, bytes], overwrite: bool=True) -> None:
82 """Safe atomic write to filesystem by writing to temp file + atomic rename"""
83
84 mode = 'wb+' if isinstance(contents, bytes) else 'w'
85 encoding = None if isinstance(contents, bytes) else 'utf-8' # enforce utf-8 on all text writes
86
87 # print('\n> Atomic Write:', mode, path, len(contents), f'overwrite={overwrite}')
88 try:
89 with lib_atomic_write(path, mode=mode, overwrite=overwrite, encoding=encoding) as f:
90 if isinstance(contents, dict):
91 dump(contents, f, indent=4, sort_keys=True, cls=ExtendedEncoder)
92 elif isinstance(contents, (bytes, str)):
93 f.write(contents)
94 except OSError as e:
95 if ENFORCE_ATOMIC_WRITES:
96 print(f"[X] OSError: Failed to write {path} with fcntl.F_FULLFSYNC. ({e})")
97 print(" You can store the archive/ subfolder on a hard drive or network share that doesn't support support syncronous writes,")
98 print(" but the main folder containing the index.sqlite3 and ArchiveBox.conf files must be on a filesystem that supports FSYNC.")
99 raise SystemExit(1)
100
101 # retry the write without forcing FSYNC (aka atomic mode)
102 with open(path, mode=mode, encoding=encoding) as f:
103 if isinstance(contents, dict):
104 dump(contents, f, indent=4, sort_keys=True, cls=ExtendedEncoder)
105 elif isinstance(contents, (bytes, str)):
106 f.write(contents)
107
108 # set file permissions
109 os.chmod(path, int(OUTPUT_PERMISSIONS, base=8))
110
111@enforce_types
112def chmod_file(path: str, cwd: str='.') -> None:

Callers 15

write_config_fileFunction · 0.85
copy_and_overwriteFunction · 0.85
write_cursorFunction · 0.85
read_cursorFunction · 0.85
save_text_as_sourceFunction · 0.85
save_file_as_sourceFunction · 0.85
write_sinceFunction · 0.85
read_sinceFunction · 0.85
write_html_link_detailsFunction · 0.85
write_json_link_detailsFunction · 0.85
save_headersFunction · 0.85
save_domFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected