1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-11-08 22:31:31 +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);
lseek(file_descriptor, 0, SEEK_SET);
(*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);
return 0;
}
@ -18,7 +20,9 @@ int filesystem_write(char *path, char *file_content, off_t file_size) {
if (file_descriptor == -1) {
return -1;
}
write(file_descriptor, file_content, file_size);
if (write(file_descriptor, file_content, file_size) == -1) {
return -1;
}
close(file_descriptor);
return 0;
}

View File

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