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