diff options
author | Raghavendra Bhat <raghavendrabhat@gluster.com> | 2012-02-24 12:53:39 +0530 |
---|---|---|
committer | Raghavendra Bhat <raghavendrabhat@gluster.com> | 2012-02-24 12:57:46 +0530 |
commit | 007dcec23ece3dd66cd6079e43e6f0b44e7da80b (patch) | |
tree | 06b55a5dad46b6677b395215e415fa3db8927c9d /c_pgms/sparse/sparse_file_alt.c | |
parent | a53faa6b9d92be769fad0d47cd7e6b7bc023fc3f (diff) |
sparse: add sparse file creation tools to the git
sparse_file_write.c: This program takes the filename and the size as
the argument from the user (default size 5GB) and
creates the sparse file with the name and the size
provided.
sparse_file_alt.c: This program will create a sparse file with alternate
empty blocks and data blocks. Takes the path as the
argument
Change-Id: I8dfc95c933167fefc53fb0b665f234997aa0c68f
Signed-off-by: Raghavendra Bhat <raghavendrabhat@gluster.com>
Diffstat (limited to 'c_pgms/sparse/sparse_file_alt.c')
-rw-r--r-- | c_pgms/sparse/sparse_file_alt.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/c_pgms/sparse/sparse_file_alt.c b/c_pgms/sparse/sparse_file_alt.c new file mode 100644 index 0000000..4afaa37 --- /dev/null +++ b/c_pgms/sparse/sparse_file_alt.c @@ -0,0 +1,46 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <errno.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/types.h> + +#define BLOCK_SIZE 4096 + +void *buf; + +int main(int argc, char **argv) +{ + buf = malloc (512); + memset (buf,"a",512); + int fd = 0; + int i; + int sector_per_block = BLOCK_SIZE/512; + int block_count = 4; + + if(argc != 2) { + printf ("Wrong usage\n USAGE: program absolute_path_to_write\n"); + _exit (-1); + } + + fd = open (argv[1],O_RDWR | O_CREAT,0666); + if (fd <= 0) { + printf ("file open failed\n"); + _exit (0); + } + + while (block_count > 0) { + lseek (fd, BLOCK_SIZE, SEEK_CUR); + block_count--; + + for(i = 0; i < sector_per_block; i++) + write (fd, buf, 512); + + block_count--; + } + + close(fd); + + return 0; +} |