Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust Executable Copy Utility

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.

Features

  • Automatically uses a sandbox directory by default
  • Generates unique output directories using timestamps and process IDs
  • Copies the currently running executable
  • Supports creating multiple copies with --n
  • Validates that --n is 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

Usage

Default settings

Running the program without arguments creates one copy of the executable in a generated directory under sandbox:

mycopy.exe

Create multiple copies

Use --n to specify the number of unique directories and executable copies to create:

mycopy.exe --n 5

This creates five uniquely named directories under sandbox, with one executable copied into each directory.

Custom destination

Specify a different base directory with --dest:

mycopy.exe --dest C:\Temp\MyCopies

Custom filename

Specify the filename to use for the copied executable:

mycopy.exe --name test_copy.exe

Combine options

Options can be combined:

mycopy.exe --dest C:\Temp\MyCopies --name test_copy.exe --n 5 --verbose

Verbose mode

Use --verbose to display diagnostic information and perform SHA-256 verification:

mycopy.exe --verbose

Example 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

Command-Line Options

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

--n validation

The value supplied to --n must be a positive integer greater than zero.

Valid:

mycopy.exe --n 1
mycopy.exe --n 10

Invalid:

mycopy.exe --n 0
mycopy.exe --n -1
mycopy.exe --n abc
mycopy.exe --n 2.5

Project Structure

The program is divided into several functions:

  • main() — controls execution and creates the requested number of copies
  • parse_arguments() — parses and validates command-line arguments
  • default_destination() — provides the default sandbox destination
  • generate_directory_name() — generates unique directory names
  • copy_self() — locates and copies the running executable
  • calculate_sha256() — calculates the SHA-256 hash of a file
  • verify_copy() — compares the original and copied executable hashes

Integrity Verification

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.

Rust Concepts Demonstrated

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 for loops
  • Path and PathBuf
  • Filesystem operations
  • Environment arguments with std::env
  • Process IDs
  • System timestamps
  • Buffered I/O
  • SHA-256 hashing
  • Command-line validation
  • Method chaining

Dependencies

The project uses the sha2 crate for SHA-256 hashing.

[dependencies]
sha2 = "0.10"

Purpose

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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages