Coverage for src/copytrading_engine/backup/files.py: 87%

107 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-16 16:55 +0000

1"""Private file copies, hashing, fsync, and tree walks that backup and restore rely on.""" 

2 

3from __future__ import annotations 

4 

5import hashlib 

6import os 

7import stat 

8from pathlib import Path, PurePosixPath 

9 

10from copytrading_engine.backup.manifest import ( 

11 CANDIDATE_MANIFEST_NAME, 

12 BackupManifest, 

13 BackupManifestError, 

14) 

15 

16 

17def require_regular_file(path: Path, message: str) -> None: 

18 try: 

19 info = path.lstat() 

20 except OSError as exc: 

21 raise BackupManifestError(message) from exc 

22 if not stat.S_ISREG(info.st_mode) or stat.S_ISLNK(info.st_mode): 22 ↛ 23line 22 didn't jump to line 23 because the condition on line 22 was never true

23 raise BackupManifestError(message) 

24 

25 

26def require_directory(path: Path, message: str) -> None: 

27 try: 

28 info = path.lstat() 

29 except OSError as exc: 

30 raise BackupManifestError(message) from exc 

31 if not stat.S_ISDIR(info.st_mode) or stat.S_ISLNK(info.st_mode): 31 ↛ 32line 31 didn't jump to line 32 because the condition on line 31 was never true

32 raise BackupManifestError(message) 

33 

34 

35def copy_private_bytes(path: Path, content: bytes) -> None: 

36 path.parent.mkdir(mode=0o700, parents=True, exist_ok=True) 

37 descriptor = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600) 

38 with os.fdopen(descriptor, "wb") as stream: 

39 stream.write(content) 

40 stream.flush() 

41 os.fsync(stream.fileno()) 

42 

43 

44def sha256_file(path: Path) -> str: 

45 digest = hashlib.sha256() 

46 with path.open("rb") as stream: 

47 while chunk := stream.read(1024 * 1024): 

48 digest.update(chunk) 

49 return digest.hexdigest() 

50 

51 

52def copy_private_file(source: Path, destination: Path, limit: int) -> tuple[str, int]: 

53 source_descriptor = os.open( 

54 source, 

55 os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0), 

56 ) 

57 destination_descriptor = -1 

58 try: 

59 if not stat.S_ISREG(os.fstat(source_descriptor).st_mode): 59 ↛ 60line 59 didn't jump to line 60 because the condition on line 59 was never true

60 raise BackupManifestError("source attachment is invalid") 

61 destination_descriptor = os.open( 

62 destination, 

63 os.O_CREAT | os.O_EXCL | os.O_WRONLY | getattr(os, "O_NOFOLLOW", 0), 

64 0o600, 

65 ) 

66 digest = hashlib.sha256() 

67 size = 0 

68 with os.fdopen(source_descriptor, "rb") as input_stream: 

69 source_descriptor = -1 

70 with os.fdopen(destination_descriptor, "wb") as output_stream: 

71 destination_descriptor = -1 

72 while chunk := input_stream.read(1024 * 1024): 

73 size += len(chunk) 

74 if size > limit: 74 ↛ 75line 74 didn't jump to line 75 because the condition on line 74 was never true

75 raise BackupManifestError("source attachment exceeds the supported size") 

76 digest.update(chunk) 

77 output_stream.write(chunk) 

78 output_stream.flush() 

79 os.fsync(output_stream.fileno()) 

80 os.chmod(destination, 0o600) 

81 return digest.hexdigest(), size 

82 finally: 

83 if source_descriptor >= 0: 83 ↛ 84line 83 didn't jump to line 84 because the condition on line 83 was never true

84 os.close(source_descriptor) 

85 if destination_descriptor >= 0: 

86 os.close(destination_descriptor) 

87 

88 

89def tree_paths( 

90 root: Path, 

91 manifest: BackupManifest, 

92 *, 

93 allow_candidate_manifest: bool = False, 

94) -> tuple[set[str], set[str]]: 

95 require_directory(root, "restore generation directory is unavailable") 

96 expected_files = {member.path for member in manifest.members} 

97 if allow_candidate_manifest: 

98 expected_files.add(CANDIDATE_MANIFEST_NAME) 

99 expected_directories: set[str] = set() 

100 for member in manifest.members: 

101 parts = PurePosixPath(member.path).parts 

102 for index in range(1, len(parts)): 

103 expected_directories.add(PurePosixPath(*parts[:index]).as_posix()) 

104 

105 actual_files: set[str] = set() 

106 actual_directories: set[str] = set() 

107 for current, directories, filenames in os.walk(root, topdown=True, followlinks=False): 

108 current_path = Path(current) 

109 for name in directories: 

110 child = current_path / name 

111 require_directory(child, "restore generation contains an unsafe directory") 

112 actual_directories.add(child.relative_to(root).as_posix()) 

113 for name in filenames: 

114 child = current_path / name 

115 require_regular_file(child, "restore generation contains an unsafe file") 

116 actual_files.add(child.relative_to(root).as_posix()) 

117 if actual_files != expected_files or actual_directories != expected_directories: 117 ↛ 118line 117 didn't jump to line 118 because the condition on line 117 was never true

118 raise BackupManifestError("restore generation members changed after validation") 

119 return actual_files, actual_directories 

120 

121 

122def fsync_directory(path: Path) -> None: 

123 descriptor = os.open( 

124 path, 

125 os.O_RDONLY | getattr(os, "O_DIRECTORY", 0) | getattr(os, "O_NOFOLLOW", 0), 

126 ) 

127 try: 

128 os.fsync(descriptor) 

129 finally: 

130 os.close(descriptor) 

131 

132 

133def fsync_tree(root: Path) -> None: 

134 directories: list[Path] = [] 

135 for current, child_directories, filenames in os.walk(root, topdown=True, followlinks=False): 

136 current_path = Path(current) 

137 directories.append(current_path) 

138 for name in child_directories: 

139 require_directory( 

140 current_path / name, 

141 "restore candidate contains an unsafe directory", 

142 ) 

143 for name in filenames: 

144 path = current_path / name 

145 descriptor = os.open(path, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)) 

146 try: 

147 if not stat.S_ISREG(os.fstat(descriptor).st_mode): 147 ↛ 148line 147 didn't jump to line 148 because the condition on line 147 was never true

148 raise BackupManifestError("restore candidate contains an unsafe file") 

149 os.fsync(descriptor) 

150 finally: 

151 os.close(descriptor) 

152 for directory in reversed(directories): 

153 fsync_directory(directory)