| | 128 | =head2 $path = make_dummy_file($size_in_bytes, $binary); |
|---|
| | 129 | |
|---|
| | 130 | Makes a file filled with random numbers. Returns the absolute path to the file. |
|---|
| | 131 | |
|---|
| | 132 | =cut |
|---|
| | 133 | |
|---|
| | 134 | push @EXPORT, 'make_dummy_file'; |
|---|
| | 135 | sub make_dummy_file { |
|---|
| | 136 | my $desired_size = shift; |
|---|
| | 137 | my $binary = shift || 0; |
|---|
| | 138 | my $handle = File::Temp->new(UNLINK => 0); # Set to 0 to leave the file hanging around |
|---|
| | 139 | #my $handle = File::Temp->new(UNLINK => 1); |
|---|
| | 140 | my $name = $handle->filename(); |
|---|
| | 141 | |
|---|
| | 142 | unless ($desired_size) { |
|---|
| | 143 | close $handle; |
|---|
| | 144 | return $name; |
|---|
| | 145 | } |
|---|
| | 146 | my $begin = "BEGIN MARKER\n"; |
|---|
| | 147 | my $begin_length = length($begin); |
|---|
| | 148 | my $end = "END MARKER\n"; |
|---|
| | 149 | $desired_size = $desired_size - length($begin) - length($end); |
|---|
| | 150 | print $handle $begin; |
|---|
| | 151 | if ($binary) { |
|---|
| | 152 | $desired_size--; # Needed because echo will add a newline before and after |
|---|
| | 153 | close $handle; |
|---|
| | 154 | system("/bin/dd if=/dev/urandom of=$name count=$desired_size bs=1 seek=$begin_length conv=fsync status=noxfer 2> /dev/null"); |
|---|
| | 155 | system("/bin/echo '$end' >> $name"); |
|---|
| | 156 | } else { |
|---|
| | 157 | my $remaining = $desired_size; |
|---|
| | 158 | while ($remaining >= 10240) { |
|---|
| | 159 | print $handle ('X' x 10239) . "\n"; |
|---|
| | 160 | $remaining -= 10240; |
|---|
| | 161 | } |
|---|
| | 162 | print $handle 'X' x $remaining; |
|---|
| | 163 | print $handle $end; |
|---|
| | 164 | close $handle; |
|---|
| | 165 | } |
|---|
| | 166 | |
|---|
| | 167 | return $name; |
|---|
| | 168 | } |
|---|