-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_manager.py
More file actions
1077 lines (978 loc) · 40.4 KB
/
Copy pathbackup_manager.py
File metadata and controls
1077 lines (978 loc) · 40.4 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Xbox 360 backup discovery, package installation, verification, and transfer."""
from __future__ import annotations
import ftplib
import hashlib
import json
import os
import re
import shutil
import subprocess
import tempfile
import time
import zipfile
from dataclasses import asdict, dataclass, field
from datetime import datetime, timezone
from pathlib import Path, PurePosixPath
from typing import Any, Callable, Iterable, Iterator, Optional
from unityscraper.domains.packages.errors import (
InvalidPackageError,
PackageError as BackupError,
UnsafeArchiveError,
)
from unityscraper.domains.packages.executables import (
inspect_xbe as _domain_inspect_xbe,
inspect_xex as _domain_inspect_xex,
)
from unityscraper.domains.packages.stfs import (
extract_stfs_files as _domain_extract_stfs_files,
inspect_stfs as _domain_inspect_stfs,
list_stfs_entries as _domain_list_stfs_entries,
)
STFS_MAGICS = {b"CON ", b"LIVE", b"PIRS"}
CONTENT_TYPES = {
0x00000001: ("Saved Game", "00000001"),
0x00000002: ("DLC", "00000002"),
0x00005000: ("Original Xbox Game", "00005000"),
0x00007000: ("Xbox 360 Game", "00007000"),
0x000B0000: ("Title Update", "000B0000"),
0x000D0000: ("Xbox Live Arcade", "000D0000"),
0x00010000: ("Profile", "00010000"),
}
GAME_CONTENT_DIRECTORIES = {"00005000", "00007000", "000D0000"}
SUPPORT_CONTENT_DIRECTORIES = {"00000002", "000B0000"}
HEX8_RE = re.compile(r"^[0-9A-Fa-f]{8}$")
FOLDER_TITLE_ID_RE = re.compile(r"\[([0-9A-Fa-f]{8})\]\s*$")
COPY_CHUNK = 1024 * 1024
MAX_ARCHIVE_FILES = 200_000
MAX_ARCHIVE_EXPANDED_SIZE = 128 * 1024 * 1024 * 1024
FATX_INVALID_RE = re.compile(r'[<>:"/\\|?*]')
class ConflictError(BackupError):
"""Raised when a destination conflict cannot be resolved automatically."""
@dataclass(frozen=True)
class StfsPackage:
path: Path
magic: str
content_type: int
content_label: str
content_directory: str
title_id: str
media_id: str
disc_number: int
disc_count: int
display_name: str
title_name: str
size: int
save_game_id: str
console_id: str
profile_id: str
device_id: str
@dataclass(frozen=True)
class StfsEntry:
index: int
path: str
name: str
is_directory: bool
consecutive: bool
allocated_blocks: int
starting_block: int
parent_index: int
size: int
@dataclass(frozen=True)
class XbePackage:
path: Path
title_id: str
title_name: str
size: int
allowed_media: int
region_flags: int
disc_number: int
version: int
@dataclass(frozen=True)
class XexPackage:
path: Path
title_id: str
media_id: str
version: str
base_version: str
disc_number: int
disc_count: int
module_flags: int
size: int
@dataclass
class BackupItem:
path: Path
title_id: str
name: str
format: str
size: int
media_id: str = ""
content_type: str = ""
status: str = "ready"
notes: list[str] = field(default_factory=list)
disc_number: int = 0
disc_count: int = 0
def to_dict(self) -> dict:
result = asdict(self)
result["path"] = str(self.path)
return result
@dataclass
class ScanResult:
root: Path
items: list[BackupItem]
warnings: list[str]
scanned_at: str
@property
def total_size(self) -> int:
return sum(item.size for item in self.items)
def to_dict(self) -> dict:
return {
"root": str(self.root),
"scanned_at": self.scanned_at,
"total_size": self.total_size,
"warnings": self.warnings,
"items": [item.to_dict() for item in self.items],
}
@dataclass(frozen=True)
class TransferResult:
source: str
destination: str
bytes_copied: int
sha256: str
status: str
@dataclass(frozen=True)
class FtpTarget:
host: str
port: int = 21
username: str = "xbox"
password: str = "xbox"
content_root: str = "/Hdd1/Content/0000000000000000"
games_root: str = "/Hdd1/Games"
timeout: float = 20.0
def _read_utf16be(data: bytes) -> str:
return data.decode("utf-16-be", errors="ignore").split("\x00", 1)[0].strip()
def inspect_stfs(path: str | Path) -> StfsPackage:
"""Read public STFS header fields without extracting package contents."""
package_path = Path(path)
with package_path.open("rb") as handle:
header = handle.read(0x1791)
if len(header) < 0x1791 or header[:4] not in STFS_MAGICS:
raise InvalidPackageError(f"{package_path.name} is not a supported STFS package")
content_type = int.from_bytes(header[0x344:0x348], "big")
content = CONTENT_TYPES.get(content_type)
if content is None:
raise InvalidPackageError(
f"Unsupported STFS content type 0x{content_type:08X}"
)
title_id = header[0x360:0x364].hex().upper()
if not HEX8_RE.fullmatch(title_id) or title_id == "00000000":
raise InvalidPackageError("STFS package does not contain a usable TitleID")
return StfsPackage(
path=package_path,
magic=header[:4].decode("ascii").strip(),
content_type=content_type,
content_label=content[0],
content_directory=content[1],
title_id=title_id,
media_id=header[0x354:0x358].hex().upper(),
disc_number=header[0x366],
disc_count=header[0x367],
display_name=_read_utf16be(header[0x411:0x511]),
title_name=_read_utf16be(header[0x1691:0x1791]),
size=package_path.stat().st_size,
save_game_id=header[0x368:0x36C].hex().upper(),
console_id=header[0x36C:0x371].hex().upper(),
profile_id=header[0x371:0x379].hex().upper(),
device_id=header[0x3FD:0x411].hex().upper(),
)
def _stfs_data_block_number(block: int, magic: bytes, header_size: int,
block_separation: int) -> int:
if block < 0 or block > 0xFFFFFF:
raise InvalidPackageError("STFS block number is outside the supported range")
aligned_header = (header_size + 0xFFF) & 0xFFFFF000
shift = 1 if aligned_header == 0xB000 else (0 if block_separation & 1 else 1)
base = (block + 0xAA) // 0xAA
if magic == b"CON ":
base <<= shift
result = base + block
if block > 0xAA:
base = (block + 0x70E4) // 0x70E4
if magic == b"CON ":
base <<= shift
result += base
if block > 0x70E4:
base = (block + 0x4AF768) // 0x4AF768
if magic == b"CON ":
base <<= shift
result += base
return result
def list_stfs_entries(path: str | Path, max_entries: int = 100_000) -> list[StfsEntry]:
"""Read the bounded STFS file table without extracting or mutating content."""
package_path = Path(path)
package_size = package_path.stat().st_size
with package_path.open("rb") as handle:
header = handle.read(0x3AD)
if len(header) < 0x3AD or header[:4] not in STFS_MAGICS:
raise InvalidPackageError("Not a supported STFS package")
if int.from_bytes(header[0x3A9:0x3AD], "big") != 0:
raise InvalidPackageError("SVOD packages do not contain an STFS file table")
header_size = int.from_bytes(header[0x340:0x344], "big")
descriptor = header[0x379:0x39D]
if descriptor[0] != 0x24:
raise InvalidPackageError("STFS volume descriptor is invalid")
block_separation = descriptor[2]
table_blocks = int.from_bytes(descriptor[3:5], "big")
table_start = int.from_bytes(descriptor[5:8], "big")
if table_blocks <= 0 or table_blocks > 0x1000:
raise InvalidPackageError("STFS file-table size is invalid")
aligned_header = (header_size + 0xFFF) & 0xFFFFF000
raw_entries: list[dict[str, Any]] = []
for table_index in range(table_blocks):
logical_block = table_start + table_index
physical_block = _stfs_data_block_number(
logical_block, header[:4], header_size, block_separation
)
offset = aligned_header + physical_block * 0x1000
if offset + 0x1000 > package_size:
raise InvalidPackageError("STFS file table points outside the package")
handle.seek(offset)
table = handle.read(0x1000)
for entry_offset in range(0, 0x1000, 0x40):
data = table[entry_offset:entry_offset + 0x40]
if not any(data):
continue
flags = data[0x28]
name_length = flags & 0x3F
if name_length == 0 or name_length > 0x28:
continue
name = data[:name_length].decode("utf-8", errors="replace")
raw_entries.append({
"name": name,
"directory": bool(flags & 0x80),
"consecutive": bool(flags & 0x40),
"blocks": int.from_bytes(data[0x29:0x2C], "little"),
"start": int.from_bytes(data[0x2F:0x32], "little"),
"parent": int.from_bytes(data[0x32:0x34], "big"),
"size": int.from_bytes(data[0x34:0x38], "big"),
})
if len(raw_entries) > max_entries:
raise InvalidPackageError("STFS file table exceeds the safety limit")
entries: list[StfsEntry] = []
for index, row in enumerate(raw_entries):
ancestors: list[str] = []
parent = row["parent"]
visited: set[int] = set()
while parent != 0xFFFF:
if parent >= len(raw_entries) or parent in visited:
ancestors = ["[invalid-parent]"]
break
visited.add(parent)
ancestors.append(raw_entries[parent]["name"])
parent = raw_entries[parent]["parent"]
full_path = "/".join(reversed(ancestors))
full_path = f"{full_path}/{row['name']}" if full_path else row["name"]
entries.append(StfsEntry(
index=index, path=full_path, name=row["name"],
is_directory=row["directory"], consecutive=row["consecutive"],
allocated_blocks=row["blocks"], starting_block=row["start"],
parent_index=row["parent"], size=row["size"],
))
return entries
def extract_stfs_files(
path: str | Path,
destination: str | Path,
selected_paths: Iterable[str] | None = None,
*,
max_output_size: int = 32 * 1024 * 1024 * 1024,
) -> dict[str, Any]:
"""Extract consecutive STFS files read-only with path and size validation.
Fragmented files are reported instead of guessed. This keeps extraction useful
while making the unsupported block-chain case explicit and non-destructive.
"""
package = Path(path).expanduser().resolve()
target = Path(destination).expanduser().resolve()
if package == target or target.is_relative_to(package):
raise InvalidPackageError("Extraction destination must be outside the package")
requested = {item.replace("\\", "/") for item in selected_paths or ()}
entries = list_stfs_entries(package)
files = [
entry for entry in entries
if not entry.is_directory and (not requested or entry.path in requested)
]
if requested - {entry.path for entry in files}:
missing = sorted(requested - {entry.path for entry in files})
raise InvalidPackageError(f"STFS entries were not found: {', '.join(missing[:5])}")
total_size = sum(entry.size for entry in files)
if total_size > max_output_size:
raise InvalidPackageError("Selected STFS output exceeds the extraction safety limit")
with package.open("rb") as handle:
header = handle.read(0x3AD)
if len(header) < 0x3AD or header[:4] not in STFS_MAGICS:
raise InvalidPackageError("Not a supported STFS package")
header_size = int.from_bytes(header[0x340:0x344], "big")
descriptor = header[0x379:0x39D]
block_separation = descriptor[2]
allocated = int.from_bytes(descriptor[0x1C:0x20], "big")
aligned_header = (header_size + 0xFFF) & 0xFFFFF000
target.mkdir(parents=True, exist_ok=True)
extracted: list[dict[str, Any]] = []
skipped: list[dict[str, str]] = []
for entry in files:
if not entry.consecutive and entry.allocated_blocks > 1:
skipped.append({"path": entry.path, "reason": "fragmented block chain"})
continue
required_blocks = (entry.size + 0xFFF) // 0x1000
if required_blocks > entry.allocated_blocks or entry.starting_block + required_blocks > allocated:
skipped.append({"path": entry.path, "reason": "invalid block allocation"})
continue
relative = _safe_archive_member(entry.path)
output = (target / relative).resolve()
if not output.is_relative_to(target):
raise UnsafeArchiveError(f"STFS path escapes destination: {entry.path}")
if output.exists():
skipped.append({"path": entry.path, "reason": "destination exists"})
continue
output.parent.mkdir(parents=True, exist_ok=True)
partial = output.with_name(output.name + ".partial")
digest = hashlib.sha256()
remaining = entry.size
try:
with partial.open("xb") as destination_handle:
for block in range(entry.starting_block, entry.starting_block + required_blocks):
physical = _stfs_data_block_number(
block, header[:4], header_size, block_separation
)
offset = aligned_header + physical * 0x1000
if offset + min(remaining, 0x1000) > package.stat().st_size:
raise InvalidPackageError(
f"STFS data points outside the package: {entry.path}"
)
handle.seek(offset)
chunk = handle.read(min(remaining, 0x1000))
if len(chunk) != min(remaining, 0x1000):
raise InvalidPackageError(f"STFS data is truncated: {entry.path}")
destination_handle.write(chunk)
digest.update(chunk)
remaining -= len(chunk)
partial.replace(output)
finally:
partial.unlink(missing_ok=True)
extracted.append({
"path": entry.path,
"output": str(output),
"size": entry.size,
"sha256": digest.hexdigest(),
})
manifest = target / "unityscraper-stfs-extraction.json"
manifest.write_text(json.dumps({
"schema": 1,
"source": str(package),
"source_sha256": sha256_file(package),
"read_only": True,
"extracted": extracted,
"skipped": skipped,
}, indent=2), encoding="utf-8")
return {"manifest": str(manifest), "extracted": extracted, "skipped": skipped}
def _safe_archive_member(value: str) -> Path:
normalized = value.replace("\\", "/")
pure = PurePosixPath(normalized)
if pure.is_absolute() or not pure.parts or any(part in {"", ".", ".."} for part in pure.parts):
raise UnsafeArchiveError(f"Unsafe package path: {value}")
if ":" in pure.parts[0]:
raise UnsafeArchiveError(f"Unsafe package path: {value}")
return Path(*pure.parts)
def inspect_xbe(path: str | Path) -> XbePackage:
"""Read TitleID and title from an original Xbox executable certificate."""
package_path = Path(path)
with package_path.open("rb") as handle:
header = handle.read(0x11C)
if len(header) < 0x11C or header[:4] != b"XBEH":
raise InvalidPackageError(f"{package_path.name} is not an XBE executable")
base_address = int.from_bytes(header[0x104:0x108], "little")
certificate_address = int.from_bytes(header[0x118:0x11C], "little")
certificate_offset = certificate_address - base_address
if certificate_offset < 0:
raise InvalidPackageError("XBE certificate address is invalid")
handle.seek(certificate_offset)
certificate = handle.read(0xD0)
if len(certificate) < 0xD0:
raise InvalidPackageError("XBE certificate is incomplete")
title_id = f"{int.from_bytes(certificate[0x8:0xC], 'little'):08X}"
title_name = certificate[0xC:0x5C].decode("utf-16-le", errors="ignore")
title_name = title_name.split("\x00", 1)[0].strip()
return XbePackage(
package_path, title_id, title_name, package_path.stat().st_size,
int.from_bytes(certificate[0x9C:0xA0], "little"),
int.from_bytes(certificate[0xA0:0xA4], "little"),
int.from_bytes(certificate[0xA8:0xAC], "little"),
int.from_bytes(certificate[0xAC:0xB0], "little"),
)
def inspect_xex(path: str | Path) -> XexPackage:
"""Read the public XEX2 execution-info header without decrypting content."""
package_path = Path(path)
with package_path.open("rb") as handle:
header = handle.read(0x4000)
if len(header) < 0x18 or header[:4] != b"XEX2":
raise InvalidPackageError(f"{package_path.name} is not an XEX2 executable")
module_flags = int.from_bytes(header[4:8], "big")
optional_count = int.from_bytes(header[0x14:0x18], "big")
if optional_count > 4096 or 0x18 + optional_count * 8 > len(header):
raise InvalidPackageError("XEX optional-header table is invalid")
execution_offset = 0
for index in range(optional_count):
entry = 0x18 + index * 8
key = int.from_bytes(header[entry : entry + 4], "big")
value = int.from_bytes(header[entry + 4 : entry + 8], "big")
if key == 0x00040006:
execution_offset = value
break
if not execution_offset or execution_offset + 24 > len(header):
raise InvalidPackageError("XEX execution metadata is unavailable")
info = header[execution_offset : execution_offset + 24]
def format_version(value: int) -> str:
return (
f"{(value >> 28) & 0xF}."
f"{(value >> 24) & 0xF}."
f"{(value >> 8) & 0xFFFF}."
f"{value & 0xFF}"
)
return XexPackage(
path=package_path,
title_id=info[12:16].hex().upper(),
media_id=info[0:4].hex().upper(),
version=format_version(int.from_bytes(info[4:8], "big")),
base_version=format_version(int.from_bytes(info[8:12], "big")),
disc_number=info[18],
disc_count=info[19],
module_flags=module_flags,
size=package_path.stat().st_size,
)
# Compatibility exports. Package-format ownership lives in the packages domain.
inspect_stfs = _domain_inspect_stfs
list_stfs_entries = _domain_list_stfs_entries
extract_stfs_files = _domain_extract_stfs_files
inspect_xbe = _domain_inspect_xbe
inspect_xex = _domain_inspect_xex
def sha256_file(path: str | Path) -> str:
digest = hashlib.sha256()
with Path(path).open("rb") as handle:
for chunk in iter(lambda: handle.read(COPY_CHUNK), b""):
digest.update(chunk)
return digest.hexdigest()
def sha1_file(path: str | Path) -> str:
digest = hashlib.sha1()
with Path(path).open("rb") as handle:
for chunk in iter(lambda: handle.read(COPY_CHUNK), b""):
digest.update(chunk)
return digest.hexdigest()
def directory_size(path: Path) -> int:
total = 0
for child in path.rglob("*"):
try:
if child.is_file():
total += child.stat().st_size
except OSError:
continue
return total
def _content_root(root: Path) -> Path:
nested = root / "Content" / "0000000000000000"
return nested if nested.exists() else root
def scan_local_target(
root: str | Path,
title_lookup: Optional[Callable[[str], Optional[str]]] = None,
) -> ScanResult:
"""Inventory a console content root, USB drive, or archive directory."""
target = Path(root).expanduser().resolve()
if not target.is_dir():
raise BackupError(f"Backup target does not exist: {target}")
items: list[BackupItem] = []
warnings: list[str] = []
content_root = _content_root(target)
if content_root.is_dir():
for title_dir in sorted(content_root.iterdir()):
if not title_dir.is_dir() or not HEX8_RE.fullmatch(title_dir.name):
continue
title_id = title_dir.name.upper()
types = {
child.name.upper()
for child in title_dir.iterdir()
if child.is_dir() and HEX8_RE.fullmatch(child.name)
}
game_types = sorted(types & GAME_CONTENT_DIRECTORIES)
support_types = sorted(types & SUPPORT_CONTENT_DIRECTORIES)
status = "ready" if game_types else "incomplete"
notes = []
package_names: list[str] = []
media_ids: set[str] = set()
disc_numbers: set[int] = set()
disc_count = 0
malformed = 0
for type_name in sorted(types & set(CONTENT_TYPES[value][1] for value in CONTENT_TYPES)):
for package_path in (title_dir / type_name).iterdir():
if not package_path.is_file() or package_path.name.endswith(".partial"):
continue
try:
package = inspect_stfs(package_path)
except (InvalidPackageError, OSError):
malformed += 1
continue
if package.media_id and package.media_id != "00000000":
media_ids.add(package.media_id)
if package.disc_number:
disc_numbers.add(package.disc_number)
disc_count = max(disc_count, package.disc_count)
candidate_name = package.title_name or package.display_name
if candidate_name:
package_names.append(candidate_name)
if support_types and not game_types:
notes.append("Only add-on or update content was found")
warnings.append(f"{title_id} has support content but no base game")
if malformed:
notes.append(f"{malformed} package header(s) could not be identified")
if disc_count:
notes.append(
f"Discs found: {', '.join(str(value) for value in sorted(disc_numbers))} "
f"of {disc_count}"
)
name = title_lookup(title_id) if title_lookup else None
items.append(
BackupItem(
path=title_dir,
title_id=title_id,
name=name or (package_names[0] if package_names else title_id),
format=", ".join(game_types + support_types) or "Content",
content_type=", ".join(
CONTENT_TYPES[int(value, 16)][0]
for value in game_types + support_types
if int(value, 16) in CONTENT_TYPES
),
media_id=", ".join(sorted(media_ids)),
size=directory_size(title_dir),
status=status,
notes=notes,
disc_number=min(disc_numbers) if len(disc_numbers) == 1 else 0,
disc_count=disc_count,
)
)
games_root = target / "Games"
if games_root.is_dir():
for game_dir in sorted(child for child in games_root.iterdir() if child.is_dir()):
title_id = ""
title_name = game_dir.name
media_id = ""
format_name = "Extracted Xbox 360"
extracted_notes: list[str] = []
disc_number = 0
disc_count = 0
xbe = game_dir / "default.xbe"
xex = game_dir / "default.xex"
if xbe.is_file():
try:
xbe_info = inspect_xbe(xbe)
title_id = xbe_info.title_id
title_name = xbe_info.title_name or title_name
format_name = "Extracted Original Xbox"
except BackupError as exc:
extracted_notes.append(str(exc))
else:
if xex.is_file():
try:
xex_info = inspect_xex(xex)
title_id = xex_info.title_id
media_id = xex_info.media_id if xex_info.media_id != "00000000" else ""
disc_number = xex_info.disc_number
disc_count = xex_info.disc_count
extracted_notes.append(f"XEX version {xex_info.version}")
if xex_info.disc_count:
extracted_notes.append(
f"Disc {xex_info.disc_number} of {xex_info.disc_count}"
)
except BackupError as exc:
extracted_notes.append(str(exc))
folder_match = FOLDER_TITLE_ID_RE.search(game_dir.name)
if not title_id and folder_match:
title_id = folder_match.group(1).upper()
if not xex.is_file():
extracted_notes.append("default.xex or default.xbe is missing")
if title_id and title_lookup:
title_name = title_lookup(title_id) or title_name
items.append(
BackupItem(
path=game_dir,
title_id=title_id,
name=title_name,
format=format_name,
media_id=media_id,
size=directory_size(game_dir),
status="ready" if not extracted_notes else "incomplete",
notes=extracted_notes,
disc_number=disc_number,
disc_count=disc_count,
)
)
if not items:
warnings.append("No Xbox 360 content or extracted games were found")
return ScanResult(
root=target,
items=items,
warnings=warnings,
scanned_at=datetime.now(timezone.utc).isoformat(),
)
def _package_filename(package: StfsPackage) -> str:
if package.content_directory == "000B0000":
return sha1_file(package.path).upper()
filename = package.path.name
if len(filename) > 42 or FATX_INVALID_RE.search(filename):
return sha1_file(package.path).upper()
return filename
def package_destination(package: StfsPackage, target_root: str | Path) -> Path:
root = Path(target_root).expanduser().resolve()
content_root = target_content_root(root)
return (
content_root
/ package.title_id
/ package.content_directory
/ _package_filename(package)
)
def target_content_root(root: str | Path) -> Path:
"""Resolve a drive root, Content folder, or direct common-content folder."""
target = Path(root).expanduser().resolve()
if target.name == "0000000000000000":
return target
if target.name.lower() == "content":
return target / "0000000000000000"
try:
if any(child.is_dir() and HEX8_RE.fullmatch(child.name) for child in target.iterdir()):
return target
except OSError:
pass
return target / "Content" / "0000000000000000"
def atomic_copy(
source: str | Path,
destination: str | Path,
conflict: str = "skip",
progress: Optional[Callable[[int, int], None]] = None,
) -> TransferResult:
"""Copy a file through a partial path, verify it, then atomically publish it."""
source_path = Path(source).resolve()
destination_path = Path(destination).resolve()
if conflict not in {"skip", "replace", "error"}:
raise ValueError("conflict must be skip, replace, or error")
if destination_path.exists():
if conflict == "skip":
source_hash = sha256_file(source_path)
destination_hash = sha256_file(destination_path)
return TransferResult(
str(source_path),
str(destination_path),
0,
destination_hash,
"skipped" if source_hash == destination_hash else "conflict",
)
if conflict == "error":
raise ConflictError(f"Destination already exists: {destination_path}")
destination_path.parent.mkdir(parents=True, exist_ok=True)
partial = destination_path.with_name(destination_path.name + ".partial")
source_size = source_path.stat().st_size
copied = 0
try:
with source_path.open("rb") as source_handle, partial.open("wb") as dest_handle:
while True:
chunk = source_handle.read(COPY_CHUNK)
if not chunk:
break
dest_handle.write(chunk)
copied += len(chunk)
if progress:
progress(copied, source_size)
dest_handle.flush()
os.fsync(dest_handle.fileno())
source_hash = sha256_file(source_path)
destination_hash = sha256_file(partial)
if source_hash != destination_hash:
raise BackupError("Copied file failed SHA-256 verification")
os.replace(partial, destination_path)
return TransferResult(
str(source_path), str(destination_path), copied, source_hash, "completed"
)
finally:
if partial.exists():
partial.unlink()
def install_stfs_package(
source: str | Path,
target_root: str | Path,
conflict: str = "skip",
progress: Optional[Callable[[int, int], None]] = None,
) -> TransferResult:
package = inspect_stfs(source)
return atomic_copy(
package.path,
package_destination(package, target_root),
conflict=conflict,
progress=progress,
)
def _safe_zip_members(archive: zipfile.ZipFile, destination: Path) -> Iterator[zipfile.ZipInfo]:
base = destination.resolve()
members = archive.infolist()
if len(members) > MAX_ARCHIVE_FILES:
raise UnsafeArchiveError(
f"Archive contains more than {MAX_ARCHIVE_FILES:,} entries"
)
expanded_size = sum(member.file_size for member in members)
if expanded_size > MAX_ARCHIVE_EXPANDED_SIZE:
raise UnsafeArchiveError("Archive expanded size exceeds the safety limit")
for member in members:
member_path = Path(member.filename.replace("\\", "/"))
if member_path.is_absolute() or ".." in member_path.parts:
raise UnsafeArchiveError(f"Unsafe archive path: {member.filename}")
mode = member.external_attr >> 16
if mode and (mode & 0o170000) == 0o120000:
raise UnsafeArchiveError(f"Archive symlinks are not supported: {member.filename}")
resolved = (base / member_path).resolve()
try:
resolved.relative_to(base)
except ValueError as exc:
raise UnsafeArchiveError(f"Unsafe archive path: {member.filename}") from exc
yield member
def import_stfs_zip(
archive_path: str | Path,
target_root: str | Path,
conflict: str = "skip",
) -> list[TransferResult]:
"""Safely extract and install supported STFS packages from a ZIP archive."""
results: list[TransferResult] = []
with tempfile.TemporaryDirectory(prefix="unityscraper-import-") as temporary:
destination = Path(temporary)
with zipfile.ZipFile(archive_path) as archive:
members = list(_safe_zip_members(archive, destination))
archive.extractall(destination, members=members)
copied_sources: set[Path] = set()
target_content = target_content_root(target_root)
for common_root in _find_common_content_roots(destination):
for title_dir in common_root.iterdir():
if not title_dir.is_dir() or not HEX8_RE.fullmatch(title_dir.name):
continue
for content_dir in title_dir.iterdir():
type_name = content_dir.name.upper()
if (
not content_dir.is_dir()
or not HEX8_RE.fullmatch(type_name)
or int(type_name, 16) not in CONTENT_TYPES
):
continue
for source in sorted(
path for path in content_dir.rglob("*") if path.is_file()
):
relative = source.relative_to(common_root)
results.append(
atomic_copy(
source,
target_content / relative,
conflict=conflict,
)
)
copied_sources.add(source.resolve())
candidates = []
for path in destination.rglob("*"):
if not path.is_file() or path.resolve() in copied_sources:
continue
try:
candidates.append(inspect_stfs(path))
except (InvalidPackageError, OSError):
continue
if not candidates and not results:
raise InvalidPackageError("Archive contains no supported STFS packages")
for package in candidates:
results.append(
install_stfs_package(package.path, target_root, conflict=conflict)
)
return results
def _find_common_content_roots(extracted: Path) -> list[Path]:
roots = []
for candidate in extracted.rglob("0000000000000000"):
if candidate.is_dir() and (
candidate.parent.name.lower() == "content" or candidate.parent == extracted
):
roots.append(candidate)
if extracted.name == "0000000000000000":
roots.append(extracted)
unique = {path.resolve(): path for path in roots}
return list(unique.values())
def export_backup_item(
item: BackupItem,
destination_root: str | Path,
conflict: str = "skip",
) -> Path:
"""Export an inventory item with a preservation manifest and file hashes."""
output_root = Path(destination_root).expanduser().resolve()
safe_name = re.sub(r'[<>:"/\\|?*]', "_", item.name).strip(" .") or item.title_id or "XboxGame"
destination = output_root / safe_name
try:
destination.resolve().relative_to(output_root)
except ValueError as exc:
raise BackupError("Export destination escapes the selected root") from exc
if destination.exists():
if conflict == "skip":
return destination
if conflict == "error":
raise ConflictError(f"Export destination already exists: {destination}")
if destination.is_symlink():
destination.unlink()
elif destination.is_dir():
shutil.rmtree(destination)
else:
raise ConflictError(f"Export destination is not a directory: {destination}")
destination.mkdir(parents=True, exist_ok=True)
files = []
source_root = item.path
for source in sorted(path for path in source_root.rglob("*") if path.is_file()):
relative = source.relative_to(source_root)
result = atomic_copy(source, destination / relative, conflict="replace")
files.append(
{
"path": relative.as_posix(),
"size": source.stat().st_size,
"sha256": result.sha256,
}
)
manifest = {
"schema": 1,
"created_at": datetime.now(timezone.utc).isoformat(),
"item": item.to_dict(),
"files": files,
}
(destination / "unityscraper-manifest.json").write_text(
json.dumps(manifest, indent=2, sort_keys=True),
encoding="utf-8",
)
return destination
def verify_backup_item(item: BackupItem) -> list[str]:
issues = list(item.notes)
if not item.path.exists():
issues.append("Item path is missing")
return issues
for partial in item.path.rglob("*.partial"):
issues.append(f"Abandoned partial file: {partial}")
for data_dir in item.path.rglob("*.data"):
if data_dir.is_dir() and not any(data_dir.iterdir()):
issues.append(f"Empty GOD data directory: {data_dir}")
return issues
class FtpBackupClient:
"""Single-connection FTP client for user-configured console targets."""
def __init__(self, target: FtpTarget):
self.target = target
def _connect(self) -> ftplib.FTP:
ftp = ftplib.FTP()
ftp.connect(self.target.host, self.target.port, timeout=self.target.timeout)
ftp.login(self.target.username, self.target.password)
return ftp
def test_connection(self) -> str:
with self._connect() as ftp:
return ftp.getwelcome()
@staticmethod
def _mkdirs(ftp: ftplib.FTP, remote_directory: str) -> None:
current = PurePosixPath("/")
for part in PurePosixPath(remote_directory).parts:
if part == "/":
continue
current /= part
try:
ftp.mkd(str(current))
except ftplib.error_perm as exc:
if not str(exc).startswith("550"):
raise
def upload_stfs(
self,
source: str | Path,
conflict: str = "skip",
progress: Optional[Callable[[int, int], None]] = None,
) -> TransferResult:
if conflict not in {"skip", "replace", "error"}:
raise ValueError("conflict must be skip, replace, or error")
package = inspect_stfs(source)
remote = (
PurePosixPath(self.target.content_root)
/ package.title_id
/ package.content_directory
/ _package_filename(package)
)
partial = PurePosixPath(str(remote) + ".partial")
source_path = Path(source)
total = source_path.stat().st_size
copied = 0
def callback(chunk: bytes) -> None:
nonlocal copied
copied += len(chunk)
if progress:
progress(copied, total)