• 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_fd(int fd,char pattern,size_t bs,size_t bcount)31 int tst_fill_fd(int fd, char pattern, size_t bs, size_t bcount)
32 {
33 	size_t i;
34 	char *buf;
35 
36 	/* Filling a memory buffer with provided pattern */
37 	buf = malloc(bs);
38 	if (buf == NULL)
39 		return -1;
40 
41 	for (i = 0; i < bs; i++)
42 		buf[i] = pattern;
43 
44 	/* Filling the file */
45 	for (i = 0; i < bcount; i++) {
46 		if (write(fd, buf, bs) != (ssize_t)bs) {
47 			free(buf);
48 			return -1;
49 		}
50 	}
51 
52 	free(buf);
53 
54 	return 0;
55 }
56 
tst_fill_file(const char * path,char pattern,size_t bs,size_t bcount)57 int tst_fill_file(const char *path, char pattern, size_t bs, size_t bcount)
58 {
59 	int fd;
60 
61 	fd = open(path, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
62 	if (fd < 0)
63 		return -1;
64 
65 	if (tst_fill_fd(fd, pattern, bs, bcount)) {
66 		close(fd);
67 		unlink(path);
68 		return -1;
69 	}
70 
71 	if (close(fd) < 0) {
72 		unlink(path);
73 
74 		return -1;
75 	}
76 
77 	return 0;
78 }
79