• 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 #define _GNU_SOURCE
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <fcntl.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include "lapi/fallocate.h"
30 
31 #include "test.h"
32 
tst_fill_fd(int fd,char pattern,size_t bs,size_t bcount)33 int tst_fill_fd(int fd, char pattern, size_t bs, size_t bcount)
34 {
35 	size_t i;
36 	char *buf;
37 
38 	/* Filling a memory buffer with provided pattern */
39 	buf = malloc(bs);
40 	if (buf == NULL)
41 		return -1;
42 
43 	for (i = 0; i < bs; i++)
44 		buf[i] = pattern;
45 
46 	/* Filling the file */
47 	for (i = 0; i < bcount; i++) {
48 		if (write(fd, buf, bs) != (ssize_t)bs) {
49 			free(buf);
50 			return -1;
51 		}
52 	}
53 
54 	free(buf);
55 
56 	return 0;
57 }
58 
tst_prealloc_size_fd(int fd,size_t bs,size_t bcount)59 int tst_prealloc_size_fd(int fd, size_t bs, size_t bcount)
60 {
61 	int ret;
62 
63 	errno = 0;
64 	ret = fallocate(fd, 0, 0, bs * bcount);
65 
66 	if (ret && errno == ENOSPC)
67 		return ret;
68 
69 	if (ret)
70 		ret = tst_fill_fd(fd, 0, bs, bcount);
71 
72 	return ret;
73 }
74 
tst_fill_file(const char * path,char pattern,size_t bs,size_t bcount)75 int tst_fill_file(const char *path, char pattern, size_t bs, size_t bcount)
76 {
77 	int fd;
78 
79 	fd = open(path, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
80 	if (fd < 0)
81 		return -1;
82 
83 	if (tst_fill_fd(fd, pattern, bs, bcount)) {
84 		close(fd);
85 		unlink(path);
86 		return -1;
87 	}
88 
89 	if (close(fd) < 0) {
90 		unlink(path);
91 
92 		return -1;
93 	}
94 
95 	return 0;
96 }
97 
tst_prealloc_file(const char * path,size_t bs,size_t bcount)98 int tst_prealloc_file(const char *path, size_t bs, size_t bcount)
99 {
100 	int fd;
101 
102 	fd = open(path, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
103 	if (fd < 0)
104 		return -1;
105 
106 	if (tst_prealloc_size_fd(fd, bs, bcount)) {
107 		close(fd);
108 		unlink(path);
109 		return -1;
110 	}
111 
112 	if (close(fd) < 0) {
113 		unlink(path);
114 		return -1;
115 	}
116 
117 	return 0;
118 }
119