A Go program to extract files from a FAT32 .bin disk image (e.g., piusb.bin), optionally verifying checksums and cleaning up files afterward. Primarily tested with the go-diskfs library, it can open a partition from the image, list directories, and selectively extract files based on provided CLI arguments.
This script:
- Opens a FAT32
.bindisk image (e.g.,piusb.bin) using go-diskfs. - Lists files and directories (and can recurse if desired).
- Sorts the files by modification time, optionally verifies checksums, and extracts them to a local target directory.
- Cleans up (removes) extracted files if desired.
Primary Goal: Automate extraction of the latest file(s) from a disk image, such as those created by certain instruments or devices.
- Opens the first partition of a
.binimage and lists files/folders (FAT32). - Sorts the files by modification time (descending).
- Optional Checksum comparison for duplicates.
- Selective file extraction based on arguments:
--latest,--all,--latestSame. - Cleanup option to remove files after successful extraction, either from:
- The host OS (
os.Remove) - The FAT32 filesystem (
filesystem.FileSystem.Remove)
- The host OS (
-
Argument 1:
<device>– If empty, works on the root directory (/).- If
SMP50or another name is provided, it will target/<device>/results. - If
all, it will recurse from/.
- If
-
Argument 2:
<rangeArgument>– Usually--latest,--latestSame, or--all.--latest: Extract only the single newest file.--latestSame: Attempt to extract additional files with the same basename.--all: Extract all files in the sorted list.
-
Argument 3:
<shouldTidyUp>– A boolean (true/false). Iftrue, files in the FAT32 image are removed after extraction, or the local file system path is removed (depending on usage).
# 1) Running without arguments:
# - Targets root directory
# - Sorts by modification time
# - Extracts the newest file only
./fat32_reader
# 2) Running with device SMP50 (no second argument -> default is "--latest"):
# - Moves into /SMP50/results
# - Extracts the newest file only
./fat32_reader SMP50
# 3) Extract all files in "/SMP50/results" and do not tidy up:
./fat32_reader SMP50 --all false
# 4) Extract only the newest file,
# then remove it from the .bin FS once done:
./fat32_reader SMP50 --latest truego mod tidy
go build -o fat32_readerThis produces a binary named fat32_reader (or fat32_reader.exe on Windows).
A Raspberry Pi Zero W uses ARMv6. So you need to cross-compile with:
# For 32-bit ARMv6 (Pi Zero / Zero W):
GOOS=linux GOARCH=arm GOARM=6 go build -o fat32_readerThen transfer fat32_reader to your Pi Zero W (e.g., via scp). Once on the Pi, you can run:
chmod +x fat32_reader
./fat32_reader- Parses
os.Argsfor optional arguments:[1]: Device (e.g.,SMP50).[2]:--latest,--latestSame,--all(default--latest).[3]: Boolean forshouldTidyUp.
- Opens the disk image with
diskfs.Open(imagePath). - Reads the first partition with
disk.GetFilesystem(0). - Lists files using
readPath(), optionally recurses depending on arguments. - Sorts files by modification time, ignoring “System Volume Information”.
- Calls
loopThroughFilesToExtract()to handle extraction based on your rules.
- Reads a path
mypathin themyFSfilesystem. - Ignores special directories like
".","..","System Volume Information". - If
recursive == true, it calls itself on subdirectories. - Returns a slice of
FileWithPath, which includes thefs.FileInfoand theItsPath.
- Iterates through sorted files.
- Checks arguments (
--latest,--latestSame,--all) and callsreadAndExtractFile(). - If argument
[3]is set totrue, callstidyUp()on the extracted file(s). - Returns
trueif an error condition arises, otherwisefalse.
- file: A
FileWithPathcontainingfs.FileInfoand a path. - Opens the file with
myFS.OpenFile(absolutePath, 0). - Optionally calculates checksums if
check[0] == true. - Creates the local output directory (under
targetDir). - Copies the data from the
filesystem.Fileto an output file. - Returns
trueif successful,falseif any error.
- Can handle either a
filesystem.Fileor a local file path (string). - Uses a SHA-256 hash, reading with
io.Copy. - Returns the hex-encoded result via
hex.EncodeToString.
- Switches on type:
- FSFileWithPath: Calls
fs.Remove(filePath). - string: Calls
os.Remove(path).
- FSFileWithPath: Calls
- Lets you remove a file from the disk image’s FS or from the local system, depending on what is passed.
This project is licensed under the MIT License. See the header in fat32_reader.go for the full text:
MIT License
Copyright (c) 2024 David Herrmann
...
You’re free to use, modify, and distribute this software, provided the license text remains intact.
Feel free to open issues or pull requests to improve or extend functionality. This docu was initial created by ChatGPT AI.