| 1 | /* |
| 2 | Authors: |
| 3 | Pavel Březina <[email protected]> |
| 4 | |
| 5 | Copyright (C) 2018 Red Hat |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <fcntl.h> |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <unistd.h> |
| 26 | #include <sys/stat.h> |
| 27 | |
| 28 | #include "common/common.h" |
| 29 | #include "lib/util/file.h" |
| 30 | #include "lib/util/textfile.h" |
| 31 | |
| 32 | static errno_t |
| 33 | textfile_read_content(FILE *file, |
| 34 | const char *filename, |
| 35 | unsigned int limit_KiB, |
| 36 | char **_content) |
| 37 | { |
| 38 | size_t read_bytes; |
| 39 | char *buffer; |
| 40 | long filelen; |
| 41 | errno_t ret; |
| 42 | |
| 43 | ret = fseek(file, 0, SEEK_END); |
| 44 | if (ret != 0) { |
| 45 | ret = errno; |
| 46 | goto done; |
| 47 | } |
| 48 | |
| 49 | filelen = ftell(file); |
| 50 | if (filelen == -1) { |
| 51 | ret = errno; |
| 52 | goto done; |
| 53 | } |
| 54 | |
| 55 | if (filelen > limit_KiB * 1024) { |
| 56 | ERROR("File [%s] is bigger than %uKiB!", filename, limit_KiB); |
| 57 | ret = ERANGE; |
| 58 | goto done; |
| 59 | } |
| 60 | |
| 61 | rewind(file); |
| 62 | |
| 63 | buffer = malloc_zero_array(char, filelen + 1); |
| 64 | if (buffer == NULL) { |
| 65 | ret = ENOMEM; |
| 66 | goto done; |
| 67 | } |
| 68 | |
| 69 | read_bytes = fread(buffer, sizeof(char), filelen, file); |
| 70 | if (read_bytes != filelen) { |
| 71 | free(buffer); |
| 72 | ret = EIO; |
| 73 | goto done; |
| 74 | } |
| 75 | |
| 76 | *_content = buffer; |
| 77 | |
| 78 | ret = EOK; |
| 79 | |
| 80 | done: |
| 81 | /* File descriptor is closed when file is closed. */ |
| 82 | fclose(file); |
| 83 | |
| 84 | if (ret != EOK) { |
| 85 | ERROR("Unable to read file [%s] [%d]: %s", |
| 86 | filename, ret, strerror(ret)); |
| 87 | } |
| 88 | |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | errno_t |
| 93 | textfile_read(const char *filepath, |
| 94 | unsigned int limit_KiB, |
| 95 | char **_content) |
| 96 | { |
| 97 | FILE *file; |
| 98 | |
| 99 | file = fopen(filepath, "r"); |
| 100 | if (file == NULL) { |
| 101 | return errno; |
| 102 | } |
| 103 | |
| 104 | return textfile_read_content(file, filepath, limit_KiB, _content); |
| 105 | } |
| 106 | |
| 107 | errno_t |
| 108 | textfile_read_dirfd(int dirfd, |
| 109 | const char *dirpath, |
| 110 | const char *filename, |
| 111 | unsigned int limit_KiB, |
| 112 | char **_content) |
| 113 | { |
| 114 | errno_t ret; |
| 115 | FILE *file; |
| 116 | int fd; |
| 117 | |
| 118 | fd = openat(dirfd, filename, O_RDONLY); |
| 119 | if (fd == -1) { |
| 120 | return errno; |
| 121 | } |
| 122 | |
| 123 | file = fdopen(fd, "r"); |
| 124 | if (file == NULL) { |
| 125 | ret = errno; |
| 126 | close(fd); |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | return textfile_read_content(file, filename, limit_KiB, _content); |
| 131 | } |
| 132 | |
| 133 | errno_t |
| 134 | textfile_write(const char *filepath, |
| 135 | const char *content, |
| 136 | mode_t mode) |
| 137 | { |
| 138 | FILE *file; |
| 139 | mode_t oldmask; |
| 140 | size_t written; |
| 141 | size_t len; |
| 142 | errno_t ret; |
| 143 | |
| 144 | if (filepath == NULL || filepath[0] == '\0') { |
| 145 | return EINVAL; |
| 146 | } |
| 147 | |
| 148 | /* Create an empty file if no content is given. */ |
| 149 | if (content == NULL) { |
| 150 | content = ""; |
| 151 | } |
| 152 | |
| 153 | oldmask = umask(~mode & ALLPERMS); |
| 154 | |
| 155 | file = fopen(filepath, "w"); |
| 156 | if (file == NULL) { |
| 157 | ret = errno; |
| 158 | ERROR("Unable to open file [%s] [%d]: %s", |
| 159 | filepath, ret, strerror(ret)); |
| 160 | goto done; |
| 161 | } |
| 162 | |
| 163 | len = strlen(content); |
| 164 | written = fwrite(content, sizeof(char), len, file); |
| 165 | if (written != len) { |
| 166 | ret = errno; |
| 167 | ERROR("Unable to write data [%s] [%d]: %s", |
| 168 | filepath, ret, strerror(ret)); |
| 169 | goto done; |
| 170 | } |
| 171 | |
| 172 | ret = fchmod(fileno(file), mode); |
| 173 | if (ret != 0) { |
| 174 | ret = errno; |
| 175 | ERROR("Unable to chmod file [%s] [%d]: %s", |
| 176 | filepath, ret, strerror(ret)); |
| 177 | goto done; |
| 178 | } |
| 179 | |
| 180 | ret = EOK; |
| 181 | |
| 182 | done: |
| 183 | umask(oldmask); |
| 184 | if (file != NULL) { |
| 185 | fclose(file); |
| 186 | } |
| 187 | |
| 188 | if (ret != EOK) { |
| 189 | unlink(filepath); |
| 190 | } |
| 191 | |
| 192 | return ret; |
| 193 | } |