• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Author: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
19  *
20  */
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <fcntl.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 
29 #include "test.h"
30 
tst_fill_file(const char * path,char pattern,size_t bs,size_t bcount)31 int tst_fill_file(const char *path, char pattern, size_t bs, size_t bcount)
32 {
33 	int fd;
34 	size_t counter;
35 	char *buf;
36 
37 	fd = open(path, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
38 	if (fd < 0)
39 		return -1;
40 
41 	/* Filling a memory buffer with provided pattern */
42 	buf = malloc(bs);
43 	if (buf == NULL) {
44 		close(fd);
45 
46 		return -1;
47 	}
48 
49 	for (counter = 0; counter < bs; counter++)
50 		buf[counter] = pattern;
51 
52 	/* Filling the file */
53 	for (counter = 0; counter < bcount; counter++) {
54 		if (write(fd, buf, bs) != (ssize_t)bs) {
55 			free(buf);
56 			close(fd);
57 			unlink(path);
58 
59 			return -1;
60 		}
61 	}
62 
63 	free(buf);
64 
65 	if (close(fd) < 0) {
66 		unlink(path);
67 
68 		return -1;
69 	}
70 
71 	return 0;
72 }
73