-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsync.py
85 lines (73 loc) · 2.63 KB
/
sync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
'''
Name: Hamdy Abou El Anein
Email: [email protected]
Date of creation: 18-11-2024
Last update: 18-11-2024
Version: 1.0
Description: The sync command from GNU coreutils in Python3.
Example of use: python3 sync.py
'''
import os
import argparse
import sys
def sync_files(files=None, data_only=False, file_system=False):
"""
Synchronize cached writes to persistent storage.
Args:
files (list, optional): List of specific files to sync
data_only (bool, optional): Sync only file data
file_system (bool, optional): Sync entire file systems
"""
try:
if files:
# Validate file existence
for file in files:
if not os.path.exists(file):
print(f"Error: File {file} does not exist", file=sys.stderr)
return False
if file_system:
# Sync file systems containing specified files
file_systems = set(os.path.realpath(os.path.dirname(f)) for f in files)
for fs in file_systems:
os.sync()
else:
# Sync specific files
for file in files:
fd = os.open(file, os.O_RDWR)
if data_only:
os.fsync(fd)
else:
os.sync()
os.close(fd)
else:
# Sync entire system if no files specified
os.sync()
return True
except PermissionError:
print("Error: Insufficient permissions to sync", file=sys.stderr)
return False
except Exception as e:
print(f"Sync error: {e}", file=sys.stderr)
return False
def main():
"""
Main function to parse arguments and call sync functionality
"""
parser = argparse.ArgumentParser(description='Synchronize cached writes to persistent storage')
parser.add_argument('files', nargs='*', help='Files to synchronize')
parser.add_argument('-d', '--data', action='store_true',
help='Sync only file data, no unneeded metadata')
parser.add_argument('-f', '--file-system', action='store_true',
help='Sync the file systems that contain the files')
parser.add_argument('--version', action='version',
version='sync utility 1.0')
args = parser.parse_args()
result = sync_files(
files=args.files,
data_only=args.data,
file_system=args.file_system
)
sys.exit(0 if result else 1)
if __name__ == '__main__':
main()