blob: f2bc52d42e709800b19a73ddc6950f96df7680fc [file] [log] [blame]
Stanislav Kholmanskikh1d5d4842013-08-14 10:01:46 +04001/*
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
Sumit Garg0efad132019-02-21 14:30:09 +053031int tst_fill_fd(int fd, char pattern, size_t bs, size_t bcount)
Stanislav Kholmanskikh1d5d4842013-08-14 10:01:46 +040032{
Sumit Garg0efad132019-02-21 14:30:09 +053033 size_t i;
Stanislav Kholmanskikh1d5d4842013-08-14 10:01:46 +040034 char *buf;
35
Stanislav Kholmanskikh1d5d4842013-08-14 10:01:46 +040036 /* Filling a memory buffer with provided pattern */
37 buf = malloc(bs);
Sumit Garg0efad132019-02-21 14:30:09 +053038 if (buf == NULL)
Stanislav Kholmanskikh1d5d4842013-08-14 10:01:46 +040039 return -1;
Stanislav Kholmanskikh1d5d4842013-08-14 10:01:46 +040040
Sumit Garg0efad132019-02-21 14:30:09 +053041 for (i = 0; i < bs; i++)
42 buf[i] = pattern;
Stanislav Kholmanskikh1d5d4842013-08-14 10:01:46 +040043
44 /* Filling the file */
Sumit Garg0efad132019-02-21 14:30:09 +053045 for (i = 0; i < bcount; i++) {
Cyril Hrubisee75ce32014-02-05 14:05:06 +010046 if (write(fd, buf, bs) != (ssize_t)bs) {
Stanislav Kholmanskikh1d5d4842013-08-14 10:01:46 +040047 free(buf);
Stanislav Kholmanskikh1d5d4842013-08-14 10:01:46 +040048 return -1;
49 }
50 }
51
52 free(buf);
53
Sumit Garg0efad132019-02-21 14:30:09 +053054 return 0;
55}
56
57int 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
Stanislav Kholmanskikh1d5d4842013-08-14 10:01:46 +040071 if (close(fd) < 0) {
72 unlink(path);
73
74 return -1;
75 }
76
77 return 0;
78}