A Rust command-line utility that copies the currently running executable into one or more uniquely generated directories and optionally verifies each copied file using SHA-256 integrity checking.
The project demonstrates Rust concepts including filesystem manipulation, command-line argument parsing, Result-based error handling, iterators, buffered file processing, and cryptographic hashing.
- Automatically uses a
sandboxdirectory by default - Generates unique output directories using timestamps and process IDs
- Copies the currently running executable
- Supports creating multiple copies with
--n - Validates that
--nis a positive integer greater than zero - Supports custom destination directories with
--dest - Supports custom output filenames with
--name - Optional verbose diagnostics with
--verbose - SHA-256 integrity verification when verbose mode is enabled
- Stream-based hashing for efficient processing of large files
- Returns descriptive errors for invalid command-line arguments
Running the program without arguments creates one copy of the executable in a generated directory under sandbox:
mycopy.exeUse --n to specify the number of unique directories and executable copies to create:
mycopy.exe --n 5This creates five uniquely named directories under sandbox, with one executable copied into each directory.
Specify a different base directory with --dest:
mycopy.exe --dest C:\Temp\MyCopiesSpecify the filename to use for the copied executable:
mycopy.exe --name test_copy.exeOptions can be combined:
mycopy.exe --dest C:\Temp\MyCopies --name test_copy.exe --n 5 --verboseUse --verbose to display diagnostic information and perform SHA-256 verification:
mycopy.exe --verboseExample output:
Created directory 1: sandbox\18a42c1_1234
Source : C:\path\to\mycopy.exe
Destination : sandbox\18a42c1_1234\mycopy.exe
Copied executable to: sandbox\18a42c1_1234\mycopy.exe
SHA-256 Verification:
Original : <sha256 hash>
Copied : <sha256 hash>
Verification successful
| Option | Description |
|---|---|
--dest <path> |
Sets the base destination directory |
--name <filename> |
Sets the filename of the copied executable |
--n <number> |
Creates the specified number of unique directories and copies |
--verbose |
Enables diagnostic output and SHA-256 verification |
The value supplied to --n must be a positive integer greater than zero.
Valid:
mycopy.exe --n 1
mycopy.exe --n 10Invalid:
mycopy.exe --n 0
mycopy.exe --n -1
mycopy.exe --n abc
mycopy.exe --n 2.5The program is divided into several functions:
main()— controls execution and creates the requested number of copiesparse_arguments()— parses and validates command-line argumentsdefault_destination()— provides the defaultsandboxdestinationgenerate_directory_name()— generates unique directory namescopy_self()— locates and copies the running executablecalculate_sha256()— calculates the SHA-256 hash of a fileverify_copy()— compares the original and copied executable hashes
When verbose mode is enabled, the program calculates a SHA-256 hash for both the original executable and each copied executable.
The executable is processed using a buffered reader and streamed directly into the SHA-256 hasher rather than loading the entire file into memory.
Matching hashes indicate that the copied file has the same contents as the original.
This project provides practice with:
- Structs and
Option<T> Result<T, E>and error propagation with?- Generic parsing with
.parse::<i32>() - Closures and
.map_err() - Iteration with
forloops PathandPathBuf- Filesystem operations
- Environment arguments with
std::env - Process IDs
- System timestamps
- Buffered I/O
- SHA-256 hashing
- Command-line validation
- Method chaining
The project uses the sha2 crate for SHA-256 hashing.
[dependencies]
sha2 = "0.10"This project is intended as a Rust systems-programming exercise focused on executable file handling, filesystem operations, argument parsing, and file-integrity verification in a controlled environment.