1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-11-09 14:51:30 +01:00

fix: handle filesystem read and write errors

This commit is contained in:
Théo LUDWIG 2023-06-24 21:13:00 +02:00
parent d42ec38e36
commit b665e3629d
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
2 changed files with 10 additions and 6 deletions

View File

@ -8,7 +8,9 @@ int filesystem_read(char *path, char **file_content, off_t *file_size) {
(*file_size) = lseek(file_descriptor, 0, SEEK_END); (*file_size) = lseek(file_descriptor, 0, SEEK_END);
lseek(file_descriptor, 0, SEEK_SET); lseek(file_descriptor, 0, SEEK_SET);
(*file_content) = malloc(*file_size); (*file_content) = malloc(*file_size);
read(file_descriptor, *file_content, *file_size); if (read(file_descriptor, *file_content, *file_size) == -1) {
return -1;
}
close(file_descriptor); close(file_descriptor);
return 0; return 0;
} }
@ -18,7 +20,9 @@ int filesystem_write(char *path, char *file_content, off_t file_size) {
if (file_descriptor == -1) { if (file_descriptor == -1) {
return -1; return -1;
} }
write(file_descriptor, file_content, file_size); if (write(file_descriptor, file_content, file_size) == -1) {
return -1;
}
close(file_descriptor); close(file_descriptor);
return 0; return 0;
} }

View File

@ -19,8 +19,8 @@
* @param file_content * @param file_content
* @param file_size * @param file_size
* @return int * @return int
* @retval -1 if the file does not exist * @retval -1 if the file does not exist or if there is an error.
* @retval 0 for success * @retval 0 for success.
* @since v1.0.0 * @since v1.0.0
*/ */
int filesystem_read(char *path, char **file_content, off_t *file_size); int filesystem_read(char *path, char **file_content, off_t *file_size);
@ -32,8 +32,8 @@ int filesystem_read(char *path, char **file_content, off_t *file_size);
* @param file_content * @param file_content
* @param file_size * @param file_size
* @return int * @return int
* @retval -1 if errors * @retval -1 if there is an error.
* @retval 0 for success * @retval 0 for success.
* @since v1.0.0 * @since v1.0.0
*/ */
int filesystem_write(char *path, char *file_content, off_t file_size); int filesystem_write(char *path, char *file_content, off_t file_size);