0

I am doing research on file system performance, and I am stumped on how to create a very large file very quickly in C. Basically, I am trying to re-create a file system's folders and files by taking this metadata and storing it into a file. This is the extraction process. Later, I want to restore those folders and files into an existing freshly-made file system (in this case, ext3) using the metadata I previously extracted.

In the restore process, I have already succeeded in creating all the folders. However, I am a little confused on how to create the files instantly. For every file that I want to create, I have a file size and a file path. I am just confused on how to set the size of the file very quickly.

I used truncate, but this does not seem to affect the disk space usage from the point of view of the file system.

Thanks!

KZcoding
  • 1,417
  • 4
  • 16
  • 26

2 Answers2

2
#include < stdio.h >
#include < stdlib.h >

int main() {
    int i;
    FILE *fp;

    fp=fopen("bigfakefile.txt","w");

    for(i=0;i<(1024*1024);i++) {
        fseek(fp,(1024*1024), SEEK_CUR);
        fprintf(fp,"C");
    }

    fclose(fp);
    return 0;
}
Mingliang Liu
  • 5,477
  • 1
  • 18
  • 13
  • add the fclose(fp) too! ^_^ – Binayaka Chakraborty May 27 '13 at 16:07
  • Added. The return value should be give too. Thanks! – Mingliang Liu May 27 '13 at 16:10
  • Sorry, @liuml07 I am not understanding something. Every time we loop, we move forward the position by (1024*1024), at which point we write a character "C" in that position. What is happening in between those seeks? How does ext3 deal with this? Do the file system blocks actually get allocated without any data? – KZcoding May 27 '13 at 18:36
  • 2
    No file system data area is allocated for the untouched blocks. This program is creating a sparse file. – jlliagre May 27 '13 at 19:31
  • @user1198224 I learned this trick years before. `fseek` by itself, does not extend the size of the file. It seems that we should write at least one byte after `fseek()` beyond the end of the existing data in the file. Other blocks are filled with 0 (I'm not sure). Thus the sparse file is created. Anyway, `fseek()` is not a primary system service, but a library function. I don't know how to answer the ext3 question. – Mingliang Liu May 28 '13 at 07:01
1

There is no way to do it instantly.

You need to have each block of the file written on disk and this is going to take a significant period of time, especially for a large file.

jlliagre
  • 29,783
  • 6
  • 61
  • 72