Build native fnOS disk-copy application with APFS and NTFS support

这个提交包含在:
mac 于 2026-11-11 23:07:17 +08:00 提交
1 父节点 1f598007d1
当前提交 58769c431f
21 个文件已更改
+734 -8

没有与搜索条件相匹配的文件

+17 -7
查看文件
@@ -22,7 +22,9 @@ def main():
mount = Path(config['mount'])
source = mount / config['source_root']
destination = Path(config['destination']).resolve(strict=True)
device = (Path('/dev/disk/by-partuuid') / config['partuuid']).resolve(strict=True)
device = Path(config['device_path']).resolve(strict=True) if config.get('device_path') else (Path('/dev/disk/by-partuuid') / config['partuuid']).resolve(strict=True)
managed = config.get('managed_mount', True)
os.environ['LC_ALL'] = 'C'
lock = (job.parent.parent / 'transfer.lock').open('a')
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
state = {'pid': os.getpid(), 'started': time.time(), 'phase': 'preflight',
@@ -48,9 +50,9 @@ def main():
raise RuntimeError('Source is not mounted.')
mounted = json.loads(subprocess.check_output(
['findmnt', '-J', '-M', str(mount), '-o', 'SOURCE,OPTIONS'], text=True))['filesystems'][0]
if Path(mounted['source']).resolve() != device or 'ro' not in mounted['options'].split(','):
if Path(mounted['source']).resolve() != device or (managed and 'ro' not in mounted['options'].split(',')):
raise RuntimeError('Unexpected source mount or source is not read-only.')
if subprocess.check_output(['blockdev', '--getro', str(device)], text=True).strip() != '1':
if managed and subprocess.check_output(['blockdev', '--getro', str(device)], text=True).strip() != '1':
raise RuntimeError('Block device is not read-only.')
writes_before = (Path('/sys/class/block') / device.name / 'stat').read_text().split()[4:8:2]
state['source_write_counters_before'] = writes_before
@@ -72,7 +74,7 @@ def main():
(job / 'existing-files.list').write_bytes(b'\0'.join(existing) + (b'\0' if existing else b''))
save('copying', existing_files_to_verify=len(existing), copy_started=time.time())
source_args = [str(source / name) for name in config['directories']]
command = ['rsync', '-rltH', '--chown=1000:1001', '--ignore-existing', '--partial',
command = ['rsync', '-rltH', '--chown=' + str(config.get('uid', 1000)) + ':' + str(config.get('gid', 1001)), '--ignore-existing', '--partial', '--no-whole-file',
'--partial-dir=.dayu-apfs-partial', '--info=progress2,stats2', '--outbuf=L',
'--stats', '--no-inc-recursive', '--protect-args', *source_args, str(destination) + '/']
result = run_to_log(command, 'copy.log')
@@ -96,6 +98,14 @@ def main():
*source_args, str(destination) + '/'], 'tree-check.log')
if result or (job / 'tree-check.log').read_text().strip():
raise RuntimeError('Final directory comparison found differences; see tree-check.log.')
subprocess.run(['sync', '-f', str(destination)], check=True)
metadata_helper = Path(__file__).with_name('save_xattrs.py')
if config.get('filesystem') == 'apfs' and metadata_helper.exists():
save('saving_metadata')
metadata_target = destination / ('.dayu-diskcopy-metadata-' + job.name)
result = run_to_log(['/usr/bin/python3', str(metadata_helper), str(config_path), str(metadata_target)], 'metadata-summary.json')
if result:
raise RuntimeError('File copy passed but extended attribute archive failed.')
save('complete', finished=time.time(), existing_files_checksum_match=True,
new_files_verified_by_rsync_transfer_checksums=True)
except BaseException as error:
@@ -103,12 +113,12 @@ def main():
traceback.print_exc()
finally:
try:
if os.path.ismount(mount):
if managed and os.path.ismount(mount):
subprocess.run(['umount', str(mount)], check=True, timeout=60)
if config['original_ro'] == '0':
if managed and config['original_ro'] == '0':
subprocess.run(['blockdev', '--setrw', str(device)], check=True)
writes_after = (Path('/sys/class/block') / device.name / 'stat').read_text().split()[4:8:2]
save(state['phase'], unmounted=True, source_write_counters_after=writes_after,
save(state['phase'], unmounted=managed and not os.path.ismount(mount), source_write_counters_after=writes_after,
source_writes_unchanged=writes_after == state.get('source_write_counters_before'))
except BaseException as error:
save('cleanup_failed', prior_phase=state['phase'], cleanup_error=str(error))