| 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 | #ifndef _TEXTFILE_H_ |
| 22 | #define _TEXTFILE_H_ |
| 23 | |
| 24 | #include <sys/stat.h> |
| 25 | |
| 26 | #include "common/errno_t.h" |
| 27 | |
| 28 | /** |
| 29 | * Read file contents. |
| 30 | * |
| 31 | * If the file is larger then @limit_KiB an error is returned. |
| 32 | * |
| 33 | * @param filepath Path to the file. |
| 34 | * @param limit_KiB File size limit in KiB. |
| 35 | * |
| 36 | * @param _content Output variable where file content is stored. |
| 37 | * |
| 38 | * @return On success, file content is stored in @_content and EOK is returned. |
| 39 | * If the file is larger then @limit_KiB ERANGE is returned. Other |
| 40 | * errno code is returned on other error. |
| 41 | */ |
| 42 | errno_t |
| 43 | textfile_read(const char *filepath, |
| 44 | unsigned int limit_KiB, |
| 45 | char **_content); |
| 46 | |
| 47 | /** |
| 48 | * Read file contents of a file in directory opened at @dirfd descriptor. |
| 49 | * |
| 50 | * If the file is larger then @limit_KiB an error is returned. |
| 51 | * |
| 52 | * @param dirfd File descriptor of an opened directory. |
| 53 | * @param dirpath Path to the directory. |
| 54 | * @param filename Name of the file. |
| 55 | * @param limit_KiB File size limit in KiB. |
| 56 | * |
| 57 | * @param _content Output variable where file content is stored. |
| 58 | * |
| 59 | * @return On success, file content is stored in @_content and EOK is returned. |
| 60 | * If the file is larger then @limit_KiB ERANGE is returned. Other |
| 61 | * errno code is returned on other error. |
| 62 | */ |
| 63 | errno_t |
| 64 | textfile_read_dirfd(int dirfd, |
| 65 | const char *dirpath, |
| 66 | const char *filename, |
| 67 | unsigned int limit_KiB, |
| 68 | char **_content); |
| 69 | |
| 70 | /** |
| 71 | * Write file contents to a file. If the file does not exist, it is created, |
| 72 | * otherwise its content is truncated. The file mode is set to @mode. |
| 73 | * |
| 74 | * |
| 75 | * @param filepath Path to the file. |
| 76 | * @param content Content to write. |
| 77 | * @param mode Mode to create the file with. |
| 78 | * |
| 79 | * @return EOK on success, other errno code on error. |
| 80 | */ |
| 81 | errno_t |
| 82 | textfile_write(const char *filepath, |
| 83 | const char *content, |
| 84 | mode_t mode); |
| 85 | |
| 86 | #endif /* _TEXTFILE_H_ */ |