Created
April 19, 2012 15:44
-
-
Save rnelson/2421842 to your computer and use it in GitHub Desktop.
Makes a big file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'optparse' | |
require 'ostruct' | |
require 'pp' | |
class MkBigFile | |
attr_reader :name, :version, :options | |
TYPES = %w[zero sparse] | |
def initialize() | |
@name = 'mkbigfile' | |
@version = [0, 0, 1] | |
end | |
def parse(args) | |
@options = OpenStruct.new | |
@options.verbose = false | |
@options.type = "sparse" | |
type_list = TYPES.join(", ") | |
opts = OptionParser.new do |opts| | |
opts.banner = @name + " " + @version.join('.') + "\nusage: mkbigfile.rb [options] -f <filename> -s <size>" | |
opts.separator "" | |
opts.separator "Specific options:" | |
opts.on("-v" , "--[no-]verbose", "Run in verbose mode") do |v| | |
@options.verbose = v | |
end | |
opts.on("-f", "--filename FILENAME", "Specify output filename") do |f| | |
@options.filename = f | |
end | |
opts.on("-s", "--size SIZE", Integer, "Specify file size (GB)") do |s| | |
@options.size = s | |
@options.sizeBytes = s * 1024 * 1024 * 1024 | |
end | |
opts.on("-t", "--type TYPE", TYPES, "Set creation type (#{type_list})") do |type| | |
@options.type = type | |
end | |
opts.on("-h", "--help", "Show this message") do | |
puts opts | |
exit | |
end | |
opts.on_tail("--version", "Show version") do | |
puts @name + " " + @version.join('.') | |
exit | |
end | |
end | |
opts.parse!(args) | |
opts | |
end | |
def info(message) | |
if @options.verbose | |
puts message | |
end | |
end | |
end | |
# Get command line arguments | |
mbf = MkBigFile.new | |
mbf.parse(ARGV) | |
# Write a whole lotta 0s | |
File.open(mbf.options.filename, "wb+") do |file| | |
mbf.info "Creating file" | |
mbf.info " Filename: " + mbf.options.filename | |
mbf.info " Size: " + mbf.options.size.to_s + " GB" | |
mbf.info " Size: " + mbf.options.sizeBytes.to_s + " B" | |
if mbf.options.type == "sparse" | |
mbf.info " Type: Sparse" | |
file.truncate(mbf.options.sizeBytes-1) | |
file.putc 00 | |
elsif mbf.options.type == "zero" | |
mbf.info " Type: Zeros" | |
mbf.options.sizeBytes.times do | |
file.putc 00 | |
end | |
else | |
puts "error: unknown file type \"" + mbf.options.type + "\"" | |
exit | |
end | |
mbf.info "Done" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment