Search code examples
perlfileddfallocate

dd/fallocate-smiliar function in perl


I have a line like dd if=/dev/zero of=myImg.img bs=1KB count=1024k , which I have to do several times in my script. Therefore i wanted to integrate this line in my perl script, not by doing a system('...'); call, but by creating a funktion. Is there a good way to do so?

I've tried it with MakeMaker, but i didn't find a proper way to set my filelength/offset the right way, or anything similar to the -o -l from fallocate. And of course, the obvious way open(FH,">/myFolder/MyImg.img"); didn't work, either.


Solution

  • fallocate(1) has the advantage of being much faster than the pure-Perl way of writing out a gigabyte of zero bytes. It uses a Linux-specific syscall that does have a POSIX equivalent but unfortunately that's not implemented by the POSIX module. If you can use sparse files (the only application I can think of right now that really doesn't like them is using the files as swap space), the easiest way is to call truncate($fh, $size). But of course you do need to open the file for writing first -- why didn't this work?