How cgsecurity/testdisk works
Purpose and Deliverables
TestDisk is a native C/C++ disk-recovery suite: TestDisk diagnoses and restores partition/boot-sector structures; PhotoRec carves lost files by format signatures. It produces console programs, a Qt PhotoRec GUI, and a file-identifier utility. Core sources are under src/; Autotools/Make build packaging spans Unix, Windows, DOS, and Android.
Entry Points and Front Ends
• TestDisk’s interactive recovery program starts at src/testdisk.c, with disk operations and menu flow in src/tdiskop.c and src/tdisksel.c.
• Console PhotoRec starts at src/photorec.c; recovery workflow is divided across src/phmain.c and src/phcli.c.
• Qt PhotoRec enters through src/qmainrec.cpp and presents recovery controls in src/qphotorec.cpp.
• src/fidentify.c is a focused signature-identification executable, useful independently of recovery.
Disk Access and Media Boundary
The storage abstraction separates recovery logic from OS/device access. src/diskacc.c and src/hdaccess.c manage raw disk/image I/O, while src/geometry.c represents media geometry. Platform-specific implementations include src/hdwin32.c, src/win32.c, and src/nodisk.c. This is the main privileged/destructive boundary: higher layers inspect or modify partition structures through these abstractions.
Partition Discovery and Repair
Partition-table handlers encode platform and layout-specific formats: MBR/i386 in src/parti386.c, GPT in src/partgpt.c, Apple maps in src/partmac.c, plus RAID/LVM support in src/md.c and src/lvm.c. src/analyse.c performs partition search/analysis; src/addpart.c and src/tpartwr.c support editing and writing selected partition metadata. TestDisk control flow is: select disk → identify layout → analyze candidate partitions/filesystems → preview/modify → explicitly write changes.
Filesystem and Boot-Sector Analysis
Filesystem modules validate structures, expose partitions, and provide repair/navigation behavior. Key examples are FAT (src/fat.c, src/fat_adv.c), NTFS (src/ntfs.c, src/ntfs_fix.c), ext (src/ext2.c, src/ext2_sb.c), and HFS+ (src/hfsp.c). Directory readers such as src/ntfs_dir.c and src/ext2_dir.c allow filesystem-aware browsing, distinct from raw PhotoRec carving.
PhotoRec Carving Pipeline
PhotoRec scans selected disk regions/sectors, detects headers, validates candidate content, and writes recovered files. src/filegen.c defines the common file-recovery model; src/file_all.c registers the large format detector set; per-family modules such as src/file_jpg.c, src/file_pdf.c, and src/file_zip.c provide signatures/checks. src/phcfg.c carries recovery selection/configuration, src/psearchn.c drives search, and src/file_found.c manages discovered-file state. Sessions can be persisted/resumed through src/sessionp.c.
Output, Logging, and Runtime Dependencies
Recovery output is written to user-selected destinations rather than back into scanned media. Operational logs and optional structured records are handled by src/log.c, src/log_part.c, and src/json_log.c; DFXML support is in src/dfxml.c. The principal runtime is native C, with Qt/C++ only for QPhotoRec (src/qphotorec.cpp). Build definitions are centered on configure.ac, src/Makefile.am, and Android.mk; Qt translations/resources live in src/lang/ and src/qphotorec.qrc.
Main entry points: diskacc.c Raw disk and image I/O · analyse.c Partition analysis · phmain.c PhotoRec workflow · tdiskop.c Disk selection and menus
Components 21
Programs
- TestDisk console
src/testdisk.c - PhotoRec console
src/photorec.c - QPhotoRec GUI
src/qmainrec.cpp - File identifier · Independent signature-identification utility.
src/fidentify.c
TestDisk repair
- Disk selection and menus
src/tdiskop.c - Partition analysis
src/analyse.c - Partition editing and write · Writes selected partition metadata only after explicit confirmation.
src/tpartwr.c - Partition layout handlers · MBR, GPT, Apple maps, RAID, and LVM layouts.
src/parti386.c
Media and filesystem
- Filesystem and boot analysis · FAT, NTFS, ext, and HFS+ validation and repair behavior.
src/fat.c - Filesystem directory readers
src/ntfs_dir.c - Raw disk and image I/O · Privileged boundary between recovery logic and OS/device access.
src/diskacc.c - Media geometry
src/geometry.c - Platform disk backends · Windows and no-disk implementations.
src/hdwin32.c
PhotoRec recovery
- PhotoRec workflow
src/phmain.c - Recovery selection
src/phcfg.c - Sector carving search
src/psearchn.c - Format signatures and checks · Common model, detector registry, and per-format validators.
src/filegen.c - Recovered-file state
src/file_found.c - Session persistence
src/sessionp.c - Recovery output and logs · Writes recovered files to a user-selected destination and records operational output.
src/log.c
Other components
- Cross-platform build · Builds Unix, Windows, DOS, Android, and Qt deliverables.
configure.ac
Connections 24
- TestDisk console → Disk selection and menus
- Disk selection and menus → Raw disk and image I/O: selects media
- Disk selection and menus → Partition analysis
- Partition analysis → Partition layout handlers: identifies layout
- Partition analysis → Filesystem and boot analysis: examines candidates
- Filesystem and boot analysis → Filesystem directory readers: browses filesystems
- Partition analysis → Partition editing and write: selected repairs
- Partition editing and write → Raw disk and image I/O: explicit write
- Partition layout handlers → Raw disk and image I/O: reads metadata
- Filesystem and boot analysis → Raw disk and image I/O: reads structures
- Raw disk and image I/O → Media geometry
- Raw disk and image I/O → Platform disk backends: uses
- PhotoRec console → PhotoRec workflow
- QPhotoRec GUI → PhotoRec workflow: GUI controls
- PhotoRec workflow → Recovery selection
- Recovery selection → Sector carving search: selected regions
- Sector carving search → Raw disk and image I/O: scans sectors
- Sector carving search → Format signatures and checks: detects and validates
- Format signatures and checks → Recovered-file state: candidate files
- Recovered-file state → Recovery output and logs: writes recovered files
- PhotoRec workflow → Session persistence: persists session
- File identifier → Format signatures and checks: identifies signatures
- Cross-platform build → TestDisk console: builds
- Cross-platform build → QPhotoRec GUI: builds