Stanislav Kholmanskikh | 1d5d484 | 2013-08-14 10:01:46 +0400 | [diff] [blame] | 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 | |
Sumit Garg | 0efad13 | 2019-02-21 14:30:09 +0530 | [diff] [blame] | 31 | int tst_fill_fd(int fd, char pattern, size_t bs, size_t bcount) |
Stanislav Kholmanskikh | 1d5d484 | 2013-08-14 10:01:46 +0400 | [diff] [blame] | 32 | { |
Sumit Garg | 0efad13 | 2019-02-21 14:30:09 +0530 | [diff] [blame] | 33 | size_t i; |
Stanislav Kholmanskikh | 1d5d484 | 2013-08-14 10:01:46 +0400 | [diff] [blame] | 34 | char *buf; |
| 35 | |
Stanislav Kholmanskikh | 1d5d484 | 2013-08-14 10:01:46 +0400 | [diff] [blame] | 36 | /* Filling a memory buffer with provided pattern */ |
| 37 | buf = malloc(bs); |
Sumit Garg | 0efad13 | 2019-02-21 14:30:09 +0530 | [diff] [blame] | 38 | if (buf == NULL) |
Stanislav Kholmanskikh | 1d5d484 | 2013-08-14 10:01:46 +0400 | [diff] [blame] | 39 | return -1; |
Stanislav Kholmanskikh | 1d5d484 | 2013-08-14 10:01:46 +0400 | [diff] [blame] | 40 | |
Sumit Garg | 0efad13 | 2019-02-21 14:30:09 +0530 | [diff] [blame] | 41 | for (i = 0; i < bs; i++) |
| 42 | buf[i] = pattern; |
Stanislav Kholmanskikh | 1d5d484 | 2013-08-14 10:01:46 +0400 | [diff] [blame] | 43 | |
| 44 | /* Filling the file */ |
Sumit Garg | 0efad13 | 2019-02-21 14:30:09 +0530 | [diff] [blame] | 45 | for (i = 0; i < bcount; i++) { |
Cyril Hrubis | ee75ce3 | 2014-02-05 14:05:06 +0100 | [diff] [blame] | 46 | if (write(fd, buf, bs) != (ssize_t)bs) { |
Stanislav Kholmanskikh | 1d5d484 | 2013-08-14 10:01:46 +0400 | [diff] [blame] | 47 | free(buf); |
Stanislav Kholmanskikh | 1d5d484 | 2013-08-14 10:01:46 +0400 | [diff] [blame] | 48 | return -1; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | free(buf); |
| 53 | |
Sumit Garg | 0efad13 | 2019-02-21 14:30:09 +0530 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
| 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 | |
Stanislav Kholmanskikh | 1d5d484 | 2013-08-14 10:01:46 +0400 | [diff] [blame] | 71 | if (close(fd) < 0) { |
| 72 | unlink(path); |
| 73 | |
| 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |