1 /******************************************************************************/ 2 /* */ 3 /* Copyright (c) International Business Machines Corp., 2009 */ 4 /* */ 5 /* This program is free software; you can redistribute it and/or modify */ 6 /* it under the terms of the GNU General Public License as published by */ 7 /* the Free Software Foundation; either version 2 of the License, or */ 8 /* (at your option) any later version. */ 9 /* */ 10 /* This program is distributed in the hope that it will be useful, */ 11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */ 13 /* the GNU General Public License for more details. */ 14 /* */ 15 /* You should have received a copy of the GNU General Public License */ 16 /* along with this program; if not, write to the Free Software */ 17 /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ 18 /* */ 19 /******************************************************************************/ 20 21 /******************************************************************************/ 22 /* */ 23 /* File: frag.c */ 24 /* */ 25 /* Description: This piece of code creates two files, and writes 1k data to */ 26 /* each file in a loop from datafile. Loop continues till it */ 27 /* reaches EOF of data file. In a loop fsync, fclose is called, */ 28 /* to create fragmented files. */ 29 /* */ 30 /* Author: Jyoti Vantagodi jyotiv (at) linux.vnet.ibm.com */ 31 /* */ 32 /* History: Created-Jul 22 2009-Jyoti Vantagodi jyotiv (at) linux.vnet.ibm.com */ 33 /* */ 34 /******************************************************************************/ 35 36 #include<stdio.h> 37 #include<fcntl.h> 38 #include<string.h> 39 #include<sys/types.h> 40 #include<unistd.h> 41 42 FILE *fp_data; /* File pointer for data file */ 43 FILE *fp_frag1; /* File pointer for fragmented file 1 */ 44 FILE *fp_frag2; /* File pointer for fragmented file 2 */ 45 46 int main(int argc, char *argv[]) 47 { 48 int bytes_read = 0, bytes_written = 0, fd1 = -1, fd2 = -1; 49 char buff[1024], frag_file1[100], frag_file2[100]; 50 51 if (argc != 3) { 52 printf("Needs to pass two arguments..\n"); 53 return -1; 54 } 55 fp_data = fopen(argv[1], "r"); 56 if (!fp_data) { 57 perror("fopen"); 58 printf("Error opening datafile \n"); 59 return 1; 60 } 61 strcpy(frag_file1, argv[2]); 62 strcat(frag_file1, "/frag1"); 63 64 strcpy(frag_file2, argv[2]); 65 strcat(frag_file2, "/frag2"); 66 do { 67 fp_frag1 = fopen(frag_file1, "a+"); 68 if (!fp_frag1) { 69 printf("Error opening fragfile \n"); 70 return -1; 71 } 72 fp_frag2 = fopen(frag_file2, "a+"); 73 if (!fp_frag2) { 74 perror("fwrite"); 75 printf("Error opening fragfile \n"); 76 return -1; 77 } 78 bytes_read = fread(buff, 1, 1024, fp_data); 79 if (bytes_read < 0) { 80 perror("fread"); 81 printf("Error reading data file\n"); 82 return -1; 83 } 84 bytes_written = fwrite(buff, 1, bytes_read, fp_frag1); 85 if (bytes_read != bytes_written) { 86 perror("fwrite"); 87 printf("Error in writing data\n"); 88 return -1; 89 } 90 bytes_written = fwrite(buff, 1, bytes_read, fp_frag2); 91 if (bytes_read != bytes_written) { 92 perror("fwrite"); 93 printf("Error in writing data\n"); 94 return -1; 95 } 96 fd1 = fileno(fp_frag1); 97 fd2 = fileno(fp_frag2); 98 99 fsync(fd1); 100 fsync(fd2); 101 fclose(fp_frag1); 102 fclose(fp_frag2); 103 104 if (bytes_read < 1024) 105 break; 106 } while (1); 107 fclose(fp_data); 108 return 0; 109 } 110