From: Kevin Day Date: Thu, 11 Aug 2022 03:50:26 +0000 (-0500) Subject: Update: Major changes regarding file functions. X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=47a2881fd86c15a062cb22c37ff2f88af4f5af42;p=fll Update: Major changes regarding file functions. I like having support for "standard" debug and "standard" warning. The problem is that there is no such standard. These are made available in case there ever is. I want to simplify the code, so get rid of this non-standard "standard" code. The low-level file functions inconsistently used id, stream, and file structure. Switch everything to use the file structure. Relax function errors by having invalid stream or invalid file descriptor return without error bit set. In these cases use special status codes F_stream_not and F_file_descriptor_not as appropriate. Simplify the functions to be simpler. Some of the functions, namely the close functions, also support conditionally flushing before close. Remove the flushing and the passed boolean. Let the caller use two functions directly rather than building this in. Add missing function for flushing via the file descriptor. The private_f_file_flush() function is missing several disable macro checks. (This change needs to be backported.) This change does not address the tests. This change does not address the programs that use or might use the changed, new, or deleted code. --- diff --git a/level_0/f_file/c/file.c b/level_0/f_file/c/file.c index 1fccfbb..7d24c96 100644 --- a/level_0/f_file/c/file.c +++ b/level_0/f_file/c/file.c @@ -36,11 +36,12 @@ extern "C" { #endif // _di_f_file_access_ #ifndef _di_f_file_access_at_ - f_status_t f_file_access_at(const int at_id, const f_string_static_t path, const int mode, const int flag) { + f_status_t f_file_access_at(const f_file_t directory, const f_string_static_t path, const int mode, const int flag) { + if (directory.id == -1) return F_file_descriptor_not; if (!path.used) return F_data_not; - if (faccessat(at_id, path.string, mode, flag) == -1) { + if (faccessat(directory.id, path.string, mode, flag) == -1) { if (mode == F_file_access_mode_exist_d) { if (errno == ENOENT) return F_false; @@ -120,24 +121,16 @@ extern "C" { #endif // _di_f_file_clone_ #ifndef _di_f_file_close_ - f_status_t f_file_close(int * const id) { + f_status_t f_file_close(f_file_t * const file) { #ifndef _di_level_0_parameter_checking_ - if (!id) return F_status_set_error(F_parameter); + if (!file) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - return private_f_file_close(F_false, id); - } -#endif // _di_f_file_close_ - -#ifndef _di_f_file_close_flush_ - f_status_t f_file_close_flush(int * const id) { - #ifndef _di_level_0_parameter_checking_ - if (!id) return F_status_set_error(F_parameter); - #endif // _di_level_0_parameter_checking_ + if (file->id == -1) return F_file_descriptor_not; - return private_f_file_close(F_true, id); + return private_f_file_close(file); } -#endif // _di_f_file_close_flush_ +#endif // _di_f_file_close_ #ifndef _di_f_file_copy_ f_status_t f_file_copy(const f_string_static_t source, const f_string_static_t destination, const f_mode_t mode, const f_number_unsigned_t size_block, const uint8_t flag) { @@ -260,11 +253,12 @@ extern "C" { #endif // _di_f_file_create_ #ifndef _di_f_file_create_at_ - f_status_t f_file_create_at(const int at_id, const f_string_static_t path, const mode_t mode, const bool exclusive) { + f_status_t f_file_create_at(const f_file_t directory, const f_string_static_t path, const mode_t mode, const bool exclusive) { + if (directory.id == -1) return F_file_descriptor_not; if (!path.used) return F_data_not; - return private_f_file_create_at(at_id, path, mode, exclusive); + return private_f_file_create_at(directory, path, mode, exclusive); } #endif // _di_f_file_create_at_ @@ -284,8 +278,9 @@ extern "C" { #endif // _di_f_file_create_device_ #ifndef _di_f_file_create_device_at_ - f_status_t f_file_create_device_at(const int at_id, const f_string_static_t path, const mode_t mode, const unsigned int major, const unsigned int minor) { + f_status_t f_file_create_device_at(const f_file_t directory, const f_string_static_t path, const mode_t mode, const unsigned int major, const unsigned int minor) { + if (directory.id == -1) return F_file_descriptor_not; if (!path.used) return F_data_not; if (!macro_f_file_type_is_fifo(mode) && !macro_f_file_type_is_character(mode) && !macro_f_file_type_is_block(mode)) { @@ -294,7 +289,7 @@ extern "C" { const dev_t device = makedev(major, minor); - return private_f_file_create_node_at(at_id, path, mode, device); + return private_f_file_create_node_at(directory, path, mode, device); } #endif // _di_f_file_create_device_at_ @@ -308,11 +303,12 @@ extern "C" { #endif // _di_f_file_create_fifo_ #ifndef _di_f_file_create_fifo_at_ - f_status_t f_file_create_fifo_at(const int at_id, const f_string_static_t path, const mode_t mode) { + f_status_t f_file_create_fifo_at(const f_file_t directory, const f_string_static_t path, const mode_t mode) { + if (directory.id == -1) return F_file_descriptor_not; if (!path.used) return F_data_not; - return private_f_file_create_fifo_at(at_id, path, mode); + return private_f_file_create_fifo_at(directory, path, mode); } #endif // _di_f_file_create_fifo_at_ @@ -330,27 +326,30 @@ extern "C" { #endif // _di_f_file_create_node_ #ifndef _di_f_file_create_node_at_ - f_status_t f_file_create_node_at(const int at_id, const f_string_static_t path, const mode_t mode, const dev_t device) { + f_status_t f_file_create_node_at(const f_file_t directory, const f_string_static_t path, const mode_t mode, const dev_t device) { + if (directory.id == -1) return F_file_descriptor_not; if (!path.used) return F_data_not; if (!macro_f_file_type_is_fifo(mode) && !macro_f_file_type_is_character(mode) && !macro_f_file_type_is_block(mode)) { return F_status_set_error(F_supported_not); } - return private_f_file_create_node_at(at_id, path, mode, device); + return private_f_file_create_node_at(directory, path, mode, device); } #endif // _di_f_file_create_node_at_ #ifndef _di_f_file_descriptor_ - f_status_t f_file_descriptor(f_file_t *file) { + f_status_t f_file_descriptor(f_file_t * const file) { #ifndef _di_level_0_parameter_checking_ if (!file) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ + if (!file->stream) return F_stream_not; + file->id = fileno(file->stream); - if (file->id == -1) return F_status_set_error(F_stream_not); + if (file->id == -1) return F_status_set_error(F_file_descriptor_not); return F_none; } @@ -378,15 +377,16 @@ extern "C" { #endif // _di_f_file_exists_ #ifndef _di_f_file_exists_at_ - f_status_t f_file_exists_at(const int at_id, const f_string_static_t path, const int flag) { + f_status_t f_file_exists_at(const f_file_t directory, const f_string_static_t path, const int flag) { + if (directory.id == -1) return F_file_descriptor_not; if (!path.used) return F_data_not; struct stat stat_file; memset(&stat_file, 0, sizeof(struct stat)); - const f_status_t status = private_f_file_stat_at(at_id, path, flag, &stat_file); + const f_status_t status = private_f_file_stat_at(directory, path, flag, &stat_file); if (F_status_is_error(status)) { if (F_status_set_fine(status) == F_file_found_not) return F_false; @@ -399,9 +399,11 @@ extern "C" { #endif // _di_f_file_exists_at_ #ifndef _di_f_file_flush_ - f_status_t f_file_flush(const int id) { + f_status_t f_file_flush(const f_file_t file) { - return private_f_file_flush(id); + if (file.id == -1) return F_file_descriptor_not; + + return private_f_file_flush(file); } #endif // _di_f_file_flush_ @@ -449,15 +451,16 @@ extern "C" { #endif // _di_f_file_is_ #ifndef _di_f_file_is_at_ - f_status_t f_file_is_at(const int at_id, const f_string_static_t path, const int type, const int flag) { + f_status_t f_file_is_at(const f_file_t directory, const f_string_static_t path, const int type, const int flag) { + if (directory.id == -1) return F_file_descriptor_not; if (!path.used) return F_data_not; struct stat stat_file; memset(&stat_file, 0, sizeof(struct stat)); - if (fstatat(at_id, path.string, &stat_file, flag) < 0) { + if (fstatat(directory.id, path.string, &stat_file, flag) < 0) { if (errno == EACCES) return F_status_set_error(F_access_denied); if (errno == EBADF) return F_status_set_error(F_directory_descriptor); if (errno == EFAULT) return F_status_set_error(F_buffer); @@ -487,11 +490,12 @@ extern "C" { #endif // _di_f_file_link_ #ifndef _di_f_file_link_at_ - f_status_t f_file_link_at(const int at_id, const f_string_static_t target, const f_string_static_t point) { + f_status_t f_file_link_at(const f_file_t directory, const f_string_static_t target, const f_string_static_t point) { + if (directory.id == -1) return F_file_descriptor_not; if (!target.used || !point.used) return F_data_not; - return private_f_file_link_at(at_id, target, point); + return private_f_file_link_at(directory, target, point); } #endif // _di_f_file_link_at_ @@ -526,11 +530,12 @@ extern "C" { #endif // _di_f_file_link_hard_ #ifndef _di_f_file_link_hard_at_ - f_status_t f_file_link_hard_at(const int at_id_target, const int at_id_point, const f_string_static_t target, const f_string_static_t point, const int flag) { + f_status_t f_file_link_hard_at(const f_file_t file_target, const f_file_t file_point, const f_string_static_t target, const f_string_static_t point, const int flag) { + if (file_target.id == -1 || file_point.id == -1) return F_file_descriptor_not; if (!target.used || !point.used) return F_data_not; - if (linkat(at_id_target, target.string, at_id_point, point.string, flag) < 0) { + if (linkat(file_target.id, target.string, file_point.id, point.string, flag) < 0) { if (errno == EACCES) return F_status_set_error(F_access_denied); if (errno == EBADF) return F_status_set_error(F_file_descriptor); if (errno == EDQUOT) return F_status_set_error(F_filesystem_quota_block); @@ -578,11 +583,12 @@ extern "C" { #endif // _di_f_file_link_read_ #ifndef _di_f_file_link_read_at_ - f_status_t f_file_link_read_at(const int at_id, const f_string_static_t path, const int flag, f_string_dynamic_t * const target) { + f_status_t f_file_link_read_at(const f_file_t directory, const f_string_static_t path, const int flag, f_string_dynamic_t * const target) { #ifndef _di_level_0_parameter_checking_ if (!target) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ + if (directory.id == -1) return F_file_descriptor_not; if (!path.used) return F_data_not; struct stat stat_file; @@ -590,11 +596,11 @@ extern "C" { memset(&stat_file, 0, sizeof(struct stat)); { - const f_status_t status = private_f_file_stat_at(at_id, path, flag, &stat_file); + const f_status_t status = private_f_file_stat_at(directory, path, flag, &stat_file); if (F_status_is_error(status)) return status; } - return private_f_file_link_read_at(at_id, path, stat_file.st_size, target); + return private_f_file_link_read_at(directory, path, stat_file.st_size, target); } #endif // _di_f_file_link_read_at_ @@ -1287,11 +1293,12 @@ extern "C" { #endif // _di_f_file_mode_read_ #ifndef _di_f_file_mode_read_at_ - f_status_t f_file_mode_read_at(const int at_id, const f_string_static_t path, const int flag, mode_t * const mode) { + f_status_t f_file_mode_read_at(const f_file_t file, const f_string_static_t path, const int flag, mode_t * const mode) { #ifndef _di_level_0_parameter_checking_ if (!mode) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ + if (file.id == -1) return F_file_descriptor_not; if (!path.used) return F_data_not; struct stat stat_file; @@ -1299,7 +1306,7 @@ extern "C" { memset(&stat_file, 0, sizeof(struct stat)); { - const f_status_t status = private_f_file_stat_at(at_id, path, flag, &stat_file); + const f_status_t status = private_f_file_stat_at(file, path, flag, &stat_file); if (F_status_is_error(status)) return status; } @@ -1319,11 +1326,12 @@ extern "C" { #endif // _di_f_file_mode_set_ #ifndef _di_f_file_mode_set_at_ - f_status_t f_file_mode_set_at(const int at_id, const f_string_static_t path, const mode_t mode) { + f_status_t f_file_mode_set_at(const f_file_t directory, const f_string_static_t path, const mode_t mode) { + if (directory.id == -1) return F_file_descriptor_not; if (!path.used) return F_data_not; - return private_f_file_mode_set_at(at_id, path, mode); + return private_f_file_mode_set_at(directory, path, mode); } #endif // _di_f_file_mode_set_at_ @@ -1467,14 +1475,15 @@ extern "C" { #endif // _di_f_file_open_ #ifndef _di_f_file_open_at_ - f_status_t f_file_open_at(const int at_id, const f_string_static_t path, const mode_t mode, f_file_t * const file) { + f_status_t f_file_open_at(const f_file_t directory, const f_string_static_t path, const mode_t mode, f_file_t * const file) { #ifndef _di_level_0_parameter_checking_ if (!file) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ + if (directory.id == -1) return F_file_descriptor_not; if (!path.used) return F_data_not; - return private_f_file_open_at(at_id, path, mode, file); + return private_f_file_open_at(directory, path, mode, file); } #endif // _di_f_file_open_at_ @@ -1508,7 +1517,7 @@ extern "C" { if (!buffer) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - if (file.id == -1) return F_status_set_error(F_file_closed); + if (file.id == -1) return F_file_descriptor_not; f_status_t status = F_none; ssize_t size_read = 0; @@ -1546,7 +1555,7 @@ extern "C" { if (!buffer) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - if (file.id == -1) return F_status_set_error(F_file_closed); + if (file.id == -1) return F_file_descriptor_not; ssize_t size_read = 0; @@ -1586,8 +1595,7 @@ extern "C" { if (!buffer) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - if (file.id == -1) return F_status_set_error(F_file_closed); - + if (file.id == -1) return F_file_descriptor_not; if (!total) return F_data_not; f_array_length_t buffer_size = file.size_read; @@ -1659,11 +1667,12 @@ extern "C" { #endif // _di_f_file_remove_ #ifndef _di_f_file_remove_at_ - f_status_t f_file_remove_at(const int at_id, const f_string_static_t path, const int flag) { + f_status_t f_file_remove_at(const f_file_t directory, const f_string_static_t path, const int flag) { + if (directory.id == -1) return F_file_descriptor_not; if (!path.used) return F_data_not; - if (unlinkat(at_id, path.string, flag) < 0) { + if (unlinkat(directory.id, path.string, flag) < 0) { if (errno == EACCES) return F_status_set_error(F_access_denied); if (errno == EBADF) return F_status_set_error(F_directory_descriptor); if (errno == EBUSY) return F_status_set_error(F_busy); @@ -1718,14 +1727,15 @@ extern "C" { #endif // _di_f_file_rename_ #ifndef _di_f_file_rename_at_ - f_status_t f_file_rename_at(const int at_id, const int to_id, const f_string_static_t source, const f_string_static_t destination, const unsigned int flag) { + f_status_t f_file_rename_at(const f_file_t directory_source, const f_file_t directory_destination, const f_string_static_t source, const f_string_static_t destination, const unsigned int flag) { + if (directory_source.id == -1 || directory_destination.id == -1) return F_file_descriptor_not; if (!source.used || !destination.used) return F_data_not; #ifdef _f_file_rename_use_renameat2_ - if (renameat2(at_id, source.string, to_id, destination.string, flag) < 0) { + if (renameat2(directory_source.id, source.string, directory_destination.id, destination.string, flag) < 0) { #else - if (renameat(at_id, source.string, to_id, destination.string) < 0) { + if (renameat(directory_source.id, source.string, directory_destination.id, destination.string) < 0) { #endif // _f_file_rename_use_renameat2_ if (errno == EACCES) return F_status_set_error(F_access_denied); if (errno == EBADF) return F_status_set_error(F_directory_descriptor); @@ -1764,22 +1774,25 @@ extern "C" { #endif // _di_f_file_role_change_ #ifndef _di_f_file_role_change_at_ - f_status_t f_file_role_change_at(const int at_id, const f_string_static_t path, const uid_t uid, const gid_t gid, const int flag) { + f_status_t f_file_role_change_at(const f_file_t directory, const f_string_static_t path, const uid_t uid, const gid_t gid, const int flag) { + if (directory.id == -1) return F_file_descriptor_not; if ((uid == -1 && gid == -1) || !path.used) return F_data_not; - return private_f_file_role_change_at(at_id, path, uid, gid, flag); + return private_f_file_role_change_at(directory, path, uid, gid, flag); } #endif // _di_f_file_role_change_at_ #ifndef _di_f_file_seek_ - f_status_t f_file_seek(const int id, const int whence, const off_t offset, off_t * const seeked) { + f_status_t f_file_seek(const f_file_t file, const int whence, const off_t offset, off_t * const seeked) { #ifndef _di_level_0_parameter_checking_ if (whence < 0) return F_status_set_error(F_parameter); if (!seeked) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - *seeked = lseek(id, offset, whence); + if (file.id == -1) return F_file_descriptor_not; + + *seeked = lseek(file.id, offset, whence); if (*seeked < 0) { if (errno == EBADF) return F_status_set_error(F_file_descriptor); @@ -1819,11 +1832,12 @@ extern "C" { #endif // _di_f_file_size_ #ifndef _di_f_file_size_at_ - f_status_t f_file_size_at(const int at_id, const f_string_static_t path, const bool dereference, off_t * const size) { + f_status_t f_file_size_at(const f_file_t directory, const f_string_static_t path, const bool dereference, off_t * const size) { #ifndef _di_level_0_parameter_checking_ if (!size) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ + if (directory.id == -1) return F_file_descriptor_not; if (!path.used) return F_data_not; struct stat stat_file; @@ -1831,7 +1845,7 @@ extern "C" { memset(&stat_file, 0, sizeof(struct stat)); { - const f_status_t status = private_f_file_stat_at(at_id, path, dereference, &stat_file); + const f_status_t status = private_f_file_stat_at(directory, path, dereference, &stat_file); if (F_status_is_error(status)) return status; } @@ -1842,19 +1856,19 @@ extern "C" { #endif // _di_f_file_size_at_ #ifndef _di_f_file_size_by_id_ - f_status_t f_file_size_by_id(const int id, off_t * const size) { + f_status_t f_file_size_by_id(const f_file_t file, off_t * const size) { #ifndef _di_level_0_parameter_checking_ if (!size) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - if (id == -1) return F_status_set_error(F_file_closed); + if (file.id == -1) return F_file_descriptor_not; struct stat stat_file; memset(&stat_file, 0, sizeof(struct stat)); { - const f_status_t status = private_f_file_stat_by_id(id, &stat_file); + const f_status_t status = private_f_file_stat_by_id(file, &stat_file); if (F_status_is_error(status)) return status; } @@ -1877,84 +1891,95 @@ extern "C" { #endif // _di_f_file_stat_ #ifndef _di_f_file_stat_at_ - f_status_t f_file_stat_at(const int at_id, const f_string_static_t path, const int flag, struct stat * const stat_file) { + f_status_t f_file_stat_at(const f_file_t directory, const f_string_static_t path, const int flag, struct stat * const stat_file) { #ifndef _di_level_0_parameter_checking_ if (!stat_file) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ + if (directory.id == -1) return F_file_descriptor_not; if (!path.used) return F_data_not; - return private_f_file_stat_at(at_id, path, flag, stat_file); + return private_f_file_stat_at(directory, path, flag, stat_file); } #endif // _di_f_file_stat_at_ #ifndef _di_f_file_stat_by_id_ - f_status_t f_file_stat_by_id(const int id, struct stat * const stat_file) { + f_status_t f_file_stat_by_id(const f_file_t file, struct stat * const stat_file) { #ifndef _di_level_0_parameter_checking_ if (!stat_file) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - if (id == -1) { - return F_status_set_error(F_file_closed); - } + if (file.id == -1) return F_file_descriptor_not; - return private_f_file_stat_by_id(id, stat_file); + return private_f_file_stat_by_id(file, stat_file); } #endif // _di_f_file_stat_by_id_ #ifndef _di_f_file_stream_close_ - f_status_t f_file_stream_close(const bool flush, f_file_t * const file) { + f_status_t f_file_stream_close(f_file_t * const file) { #ifndef _di_level_0_parameter_checking_ if (!file) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - if (file->stream) { - if (flush) { - fflush(file->stream); - } + if (!file->stream) return F_stream_not; - if (fclose(file->stream) == EOF) { - - // According to man pages, further access to a stream on error results in undefined behavior. - file->stream = 0; - file->id = -1; - - if (errno == EACCES) return F_status_set_error(F_access_denied); - if (errno == EAGAIN || errno == EWOULDBLOCK) return F_status_set_error(F_block); - if (errno == EBADF) return F_status_set_error(F_file_descriptor); - if (errno == EFBIG) return F_status_set_error(F_file_overflow); - if (errno == EDEADLK) return F_status_set_error(F_deadlock); - if (errno == EDESTADDRREQ) return F_status_set_error(F_socket_not); - if (errno == EDQUOT) return F_status_set_error(F_space_not); - if (errno == EFAULT) return F_status_set_error(F_buffer); - if (errno == EINTR) return F_status_set_error(F_interrupt); - if (errno == EINVAL) return F_status_set_error(F_parameter); - if (errno == EIO) return F_status_set_error(F_input_output); - if (errno == EMFILE) return F_status_set_error(F_file_descriptor_max); - if (errno == ENOLCK) return F_status_set_error(F_lock); - if (errno == ENOSPC) return F_status_set_error(F_space_not); - if (errno == ENOTDIR) return F_status_set_error(F_file_type_not_directory); - if (errno == EPERM) return F_status_set_error(F_prohibited); - if (errno == EPIPE) return F_status_set_error(F_pipe_not); - - return F_status_set_error(F_file_close); - } + if (fclose(file->stream) == EOF) { + // According to man pages, further access to a stream on error results in undefined behavior. file->stream = 0; - file->id = -1; - return F_none; - } + if (errno == EACCES) return F_status_set_error(F_access_denied); + if (errno == EAGAIN || errno == EWOULDBLOCK) return F_status_set_error(F_block); + if (errno == EBADF) return F_status_set_error(F_file_descriptor); + if (errno == EFBIG) return F_status_set_error(F_file_overflow); + if (errno == EDEADLK) return F_status_set_error(F_deadlock); + if (errno == EDESTADDRREQ) return F_status_set_error(F_socket_not); + if (errno == EDQUOT) return F_status_set_error(F_space_not); + if (errno == EFAULT) return F_status_set_error(F_buffer); + if (errno == EINTR) return F_status_set_error(F_interrupt); + if (errno == EINVAL) return F_status_set_error(F_parameter); + if (errno == EIO) return F_status_set_error(F_input_output); + if (errno == EMFILE) return F_status_set_error(F_file_descriptor_max); + if (errno == ENOLCK) return F_status_set_error(F_lock); + if (errno == ENOSPC) return F_status_set_error(F_space_not); + if (errno == ENOTDIR) return F_status_set_error(F_file_type_not_directory); + if (errno == EPERM) return F_status_set_error(F_prohibited); + if (errno == EPIPE) return F_status_set_error(F_pipe_not); - const f_status_t status = private_f_file_close(flush, &file->id); + return F_status_set_error(F_file_close); + } file->stream = 0; - file->id = -1; - return status; + return F_none; } #endif // _di_f_file_stream_close_ +#ifndef _di_f_file_stream_flush_ + f_status_t f_file_stream_flush(f_file_t * const file) { + #ifndef _di_level_0_parameter_checking_ + if (!file) return F_status_set_error(F_parameter); + #endif // _di_level_0_parameter_checking_ + + if (!file->stream) return F_stream_not; + + // Only 0 is considered a success and so any non-zero value could be an error. + if (fflush(file->stream) != 0) { + if (errno == EAGAIN || errno == EWOULDBLOCK) return F_status_set_error(F_block); + if (errno == EBADF) return F_status_set_error(F_file_descriptor); + if (errno == EFAULT) return F_status_set_error(F_buffer); + if (errno == EINTR) return F_status_set_error(F_interrupt); + if (errno == EINVAL) return F_status_set_error(F_parameter); + if (errno == EIO) return F_status_set_error(F_input_output); + if (errno == EISDIR) return F_status_set_error(F_file_type_directory); + + return F_status_set_error(F_file_synchronize); + } + + return F_none; + } +#endif // _di_f_file_stream_flush_ + #ifndef _di_f_file_stream_open_ f_status_t f_file_stream_open(const f_string_static_t path, const f_string_static_t mode, f_file_t * const file) { #ifndef _di_level_0_parameter_checking_ @@ -1994,10 +2019,6 @@ extern "C" { return F_status_set_error(F_failure); } - file->id = fileno(file->stream); - - if (file->id == -1) return F_status_set_error(F_file_descriptor); - return F_none; } #endif // _di_f_file_stream_open_ @@ -2008,7 +2029,7 @@ extern "C" { if (!file) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - if (file->id == -1) return F_status_set_error(F_file_descriptor); + if (file->id == -1) return F_file_descriptor_not; if (mode.used) { file->stream = fdopen(file->id, private_f_file_stream_open_mode_determine(file->flag)); @@ -2052,7 +2073,7 @@ extern "C" { if (!buffer) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - if (!file.stream) return F_status_set_error(F_file_closed); + if (!file.stream) return F_stream_not; flockfile(file.stream); @@ -2105,7 +2126,7 @@ extern "C" { if (!buffer) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - if (!file.stream) return F_status_set_error(F_file_closed); + if (!file.stream) return F_stream_not; flockfile(file.stream); @@ -2162,8 +2183,8 @@ extern "C" { if (!buffer) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - if (!file.stream) return F_status_set_error(F_file_closed); - if (!total) return F_none_stop; + if (!file.stream) return F_stream_not; + if (!total) return F_data_not; flockfile(file.stream); @@ -2267,9 +2288,6 @@ extern "C" { } file->stream = result; - file->id = fileno(file->stream); - - if (file->id == -1) return F_status_set_error(F_file_descriptor); return F_none; } @@ -2277,65 +2295,47 @@ extern "C" { #ifndef _di_f_file_stream_write_ f_status_t f_file_stream_write(const f_file_t file, const f_string_static_t buffer, f_array_length_t * const written) { - #ifndef _di_level_0_parameter_checking_ - if (!file.size_write) return F_status_set_error(F_parameter); - #endif // _di_level_0_parameter_checking_ - if (!file.stream) return F_status_set_error(F_file_closed); - - if (!buffer.used) { + if (!file.stream || !buffer.used || !file.size_write) { if (written) { *written = 0; } - return F_data_not; + return file.stream ? F_data_not : F_stream_not; } - f_status_t status = F_none; - if (written) { - status = private_f_file_stream_write_until(file, buffer, buffer.used, written); + const f_status_t status = private_f_file_stream_write_until(file, buffer, buffer.used, written); if (status == F_none && *written == buffer.used) return F_none_eos; } else { f_array_length_t written_local = 0; - status = private_f_file_stream_write_until(file, buffer, buffer.used, &written_local); + const f_status_t status = private_f_file_stream_write_until(file, buffer, buffer.used, &written_local); if (status == F_none && written_local == buffer.used) return F_none_eos; } - return status; + return F_none; } #endif // _di_f_file_stream_write_ #ifndef _di_f_file_stream_write_block_ f_status_t f_file_stream_write_block(const f_file_t file, const f_string_static_t buffer, f_array_length_t * const written) { - #ifndef _di_level_0_parameter_checking_ - if (!file.size_write) return F_status_set_error(F_parameter); - #endif // _di_level_0_parameter_checking_ - - if (!file.stream) return F_status_set_error(F_file_closed); - if (!buffer.used) { + if (!file.stream || !buffer.used || !file.size_write) { if (written) { *written = 0; } - return F_data_not; - } - - f_array_length_t write_max = file.size_write; - - if (write_max > buffer.used) { - write_max = buffer.used; + return file.stream ? F_data_not : F_stream_not; } - f_status_t status = F_none; + const f_array_length_t write_max = file.size_write > buffer.used ? buffer.used : file.size_write; if (written) { - status = private_f_file_stream_write_until(file, buffer, write_max, written); + const f_status_t status = private_f_file_stream_write_until(file, buffer, write_max, written); if (status == F_none) { if (*written == buffer.used) return F_none_eos; @@ -2345,7 +2345,7 @@ extern "C" { else { f_array_length_t written_local = 0; - status = private_f_file_stream_write_until(file, buffer, write_max, &written_local); + const f_status_t status = private_f_file_stream_write_until(file, buffer, write_max, &written_local); if (status == F_none) { if (written_local == buffer.used) return F_none_eos; @@ -2353,36 +2353,25 @@ extern "C" { } } - return status; + return F_none; } #endif // _di_f_file_stream_write_block_ #ifndef _di_f_file_stream_write_until_ f_status_t f_file_stream_write_until(const f_file_t file, const f_string_static_t buffer, const f_array_length_t total, f_array_length_t * const written) { - #ifndef _di_level_0_parameter_checking_ - if (!file.size_write) return F_status_set_error(F_parameter); - #endif // _di_level_0_parameter_checking_ - if (!file.stream) return F_status_set_error(F_file_closed); - - if (!buffer.used || !total) { + if (!file.stream || !buffer.used || !total || !file.size_write) { if (written) { *written = 0; } - return F_data_not; + return file.stream ? F_data_not : F_stream_not; } - f_array_length_t write_max = total; - - if (write_max > buffer.used) { - write_max = buffer.used; - } - - f_status_t status = F_none; + const f_array_length_t write_max = total > buffer.used ? buffer.used : total; if (written) { - status = private_f_file_stream_write_until(file, buffer, write_max, written); + const f_status_t status = private_f_file_stream_write_until(file, buffer, write_max, written); if (status == F_none) { if (*written == buffer.used) return F_none_eos; @@ -2392,7 +2381,7 @@ extern "C" { else { f_array_length_t written_local = 0; - status = private_f_file_stream_write_until(file, buffer, buffer.used, &written_local); + const f_status_t status = private_f_file_stream_write_until(file, buffer, buffer.used, &written_local); if (status == F_none) { if (written_local == buffer.used) return F_none_eos; @@ -2400,58 +2389,46 @@ extern "C" { } } - return status; + return F_none; } #endif // _di_f_file_stream_write_until_ #ifndef _di_f_file_stream_write_range_ f_status_t f_file_stream_write_range(const f_file_t file, const f_string_static_t buffer, const f_string_range_t range, f_array_length_t * const written) { - #ifndef _di_level_0_parameter_checking_ - if (!file.size_write) return F_status_set_error(F_parameter); - #endif // _di_level_0_parameter_checking_ - if (!file.stream) return F_status_set_error(F_file_closed); - - if (!buffer.used || range.start > range.stop || range.start >= buffer.used) { + if (!file.stream || !buffer.used || range.start > range.stop || range.start >= buffer.used || !file.size_write) { if (written) { *written = 0; } - return F_data_not; + return file.stream ? F_data_not : F_stream_not; } - const f_array_length_t total = (range.stop - range.start) + 1; - f_array_length_t write_max = total; - - if (write_max > buffer.used) { - write_max = buffer.used; - } - - f_status_t status = F_none; + const f_array_length_t write_max = (range.stop - range.start) + 1 > buffer.used ? buffer.used : (range.stop - range.start) + 1; if (written) { const f_string_static_t buffer_adjusted = macro_f_string_static_t_initialize(buffer.string + range.start, 0, buffer.used - range.start); - status = private_f_file_stream_write_until(file, buffer_adjusted, write_max, written); + const f_status_t status = private_f_file_stream_write_until(file, buffer_adjusted, write_max, written); if (status == F_none) { - if (range.start + *written == buffer.used) return F_none_stop; - if (range.start + *written == total) return F_none_eos; + if (range.start + *written == buffer.used) return F_none_eos; + if (range.start + *written == write_max) return F_none_stop; } } else { const f_string_static_t buffer_adjusted = macro_f_string_static_t_initialize(buffer.string + range.start, 0, buffer.used - range.start); f_array_length_t written_local = 0; - status = private_f_file_stream_write_until(file, buffer_adjusted, write_max, &written_local); + const f_status_t status = private_f_file_stream_write_until(file, buffer_adjusted, write_max, &written_local); if (status == F_none) { if (range.start + written_local == buffer.used) return F_none_eos; - if (range.start + written_local == total) return F_none_stop; + if (range.start + written_local == write_max) return F_none_stop; } } - return status; + return F_none; } #endif // _di_f_file_stream_write_range_ @@ -2460,15 +2437,19 @@ extern "C" { if (!path.used) return F_data_not; - f_status_t status = F_none; struct stat stat_file; memset(&stat_file, 0, sizeof(struct stat)); - status = private_f_file_stat(path, dereference, &stat_file); + { + const f_status_t status = private_f_file_stat(path, dereference, &stat_file); - if (F_status_set_fine(status) == F_file_found_not) return private_f_file_create(path, mode, dereference); - if (F_status_is_error(status)) return status; + if (F_status_set_fine(status) == F_file_found_not) { + return private_f_file_create(path, mode, dereference); + } + + if (F_status_is_error(status)) return status; + } if (utimensat(F_file_at_current_working_d, path.string, 0, dereference ? 0 : F_file_at_symlink_follow_no_d) < 0) { if (errno == EACCES) return F_status_set_error(F_access_denied); @@ -2491,21 +2472,26 @@ extern "C" { #endif // _di_f_file_touch_ #ifndef _di_f_file_touch_at_ - f_status_t f_file_touch_at(const int at_id, const f_string_static_t path, const mode_t mode, const int flag) { + f_status_t f_file_touch_at(const f_file_t directory, const f_string_static_t path, const mode_t mode, const int flag) { + if (directory.id == -1) return F_stream_not; if (!path.used) return F_data_not; - f_status_t status = F_none; struct stat stat_file; memset(&stat_file, 0, sizeof(struct stat)); - status = private_f_file_stat_at(at_id, path, flag, &stat_file); + { + const f_status_t status = private_f_file_stat_at(directory, path, flag, &stat_file); - if (F_status_set_fine(status) == F_file_found_not) return private_f_file_create_at(at_id, path, mode, F_false); - if (F_status_is_error(status)) return status; + if (F_status_set_fine(status) == F_file_found_not) { + return private_f_file_create_at(directory, path, mode, F_false); + } + + if (F_status_is_error(status)) return status; + } - if (utimensat(at_id, path.string, 0, flag) < 0) { + if (utimensat(directory.id, path.string, 0, flag) < 0) { if (errno == EACCES) return F_status_set_error(F_access_denied); if (errno == EBADF) return F_status_set_error(F_directory_descriptor); if (errno == EFAULT) return F_status_set_error(F_buffer); @@ -2549,11 +2535,12 @@ extern "C" { #endif // _di_f_file_type_ #ifndef _di_f_file_type_at_ - f_status_t f_file_type_at(const int at_id, const f_string_static_t path, const int flag, int * const type) { + f_status_t f_file_type_at(const f_file_t directory, const f_string_static_t path, const int flag, int * const type) { #ifndef _di_level_0_parameter_checking_ if (!type) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ + if (directory.id == -1) return F_stream_not; if (!path.used) return F_data_not; struct stat stat_file; @@ -2561,7 +2548,7 @@ extern "C" { memset(&stat_file, 0, sizeof(struct stat)); { - const f_status_t status = private_f_file_stat_at(at_id, path, flag, &stat_file); + const f_status_t status = private_f_file_stat_at(directory, path, flag, &stat_file); if (F_status_is_error(status)) return status; } @@ -2598,61 +2585,46 @@ extern "C" { #ifndef _di_f_file_write_ f_status_t f_file_write(const f_file_t file, const f_string_static_t buffer, f_array_length_t * const written) { - #ifndef _di_level_0_parameter_checking_ - if (!file.size_write) return F_status_set_error(F_parameter); - #endif // _di_level_0_parameter_checking_ - - if (file.id == -1) return F_status_set_error(F_file_closed); - if (!buffer.used) { + if (file.id == -1 || !buffer.used || !file.size_write) { if (written) *written = 0; - return F_data_not; + return file.id == -1 ? F_stream_not : F_data_not; } - f_status_t status = F_none; - if (written) { - status = private_f_file_write_until(file, buffer, buffer.used, written); + const f_status_t status = private_f_file_write_until(file, buffer, buffer.used, written); + if (F_status_is_error(status)) return status; if (status == F_none && *written == buffer.used) return F_none_eos; } else { f_array_length_t written_local = 0; - status = private_f_file_write_until(file, buffer, buffer.used, &written_local); + const f_status_t status = private_f_file_write_until(file, buffer, buffer.used, &written_local); + if (F_status_is_error(status)) return status; if (status == F_none && written_local == buffer.used) return F_none_eos; } - return status; + return F_none; } #endif // _di_f_file_write_ #ifndef _di_f_file_write_block_ f_status_t f_file_write_block(const f_file_t file, const f_string_static_t buffer, f_array_length_t * const written) { - #ifndef _di_level_0_parameter_checking_ - if (!file.size_write) return F_status_set_error(F_parameter); - #endif // _di_level_0_parameter_checking_ - if (file.id == -1) return F_status_set_error(F_file_closed); - - if (!buffer.used) { + if (file.id == -1 || !buffer.used || !file.size_write) { if (written) *written = 0; - return F_data_not; + return file.id == -1 ? F_stream_not : F_data_not; } - f_array_length_t write_max = file.size_write; - - if (write_max > buffer.used) { - write_max = buffer.used; - } - - f_status_t status = F_none; + const f_array_length_t write_max = file.size_write > buffer.used ? buffer.used : file.size_write; if (written) { - status = private_f_file_write_until(file, buffer, write_max, written); + const f_status_t status = private_f_file_write_until(file, buffer, write_max, written); + if (F_status_is_error(status)) return status; if (status == F_none) { if (*written == buffer.used) return F_none_eos; @@ -2662,7 +2634,8 @@ extern "C" { else { f_array_length_t written_local = 0; - status = private_f_file_write_until(file, buffer, write_max, &written_local); + const f_status_t status = private_f_file_write_until(file, buffer, write_max, &written_local); + if (F_status_is_error(status)) return status; if (status == F_none) { if (written_local == buffer.used) return F_none_eos; @@ -2670,34 +2643,24 @@ extern "C" { } } - return status; + return F_none; } #endif // _di_f_file_write_block_ #ifndef _di_f_file_write_until_ f_status_t f_file_write_until(const f_file_t file, const f_string_static_t buffer, const f_array_length_t total, f_array_length_t * const written) { - #ifndef _di_level_0_parameter_checking_ - if (!file.size_write) return F_status_set_error(F_parameter); - #endif // _di_level_0_parameter_checking_ - - if (file.id == -1) return F_status_set_error(F_file_closed); - if (!buffer.used || !total) { + if (file.id == -1 || !buffer.used || !total || !file.size_write) { if (written) *written = 0; - return F_data_not; - } - - f_array_length_t write_max = total; - - if (write_max > buffer.used) { - write_max = buffer.used; + return file.id == -1 ? F_stream_not : F_data_not; } - f_status_t status = F_none; + const f_array_length_t write_max = total > buffer.used ? buffer.used : total; if (written) { - status = private_f_file_write_until(file, buffer, write_max, written); + const f_status_t status = private_f_file_write_until(file, buffer, write_max, written); + if (F_status_is_error(status)) return status; if (status == F_none) { if (*written == buffer.used) return F_none_eos; @@ -2707,7 +2670,8 @@ extern "C" { else { f_array_length_t written_local = 0; - status = private_f_file_write_until(file, buffer, buffer.used, &written_local); + const f_status_t status = private_f_file_write_until(file, buffer, buffer.used, &written_local); + if (F_status_is_error(status)) return status; if (status == F_none) { if (written_local == buffer.used) return F_none_eos; @@ -2715,58 +2679,48 @@ extern "C" { } } - return status; + return F_none; } #endif // _di_f_file_write_until_ #ifndef _di_f_file_write_range_ f_status_t f_file_write_range(const f_file_t file, const f_string_static_t buffer, const f_string_range_t range, f_array_length_t * const written) { - #ifndef _di_level_0_parameter_checking_ - if (!file.size_write) return F_status_set_error(F_parameter); - #endif // _di_level_0_parameter_checking_ - - if (file.id == -1) return F_status_set_error(F_file_closed); - if (!buffer.used || range.start > range.stop || range.start >= buffer.used) { + if (file.id == -1 || !buffer.used || range.start > range.stop || range.start >= buffer.used || !file.size_write) { if (written) { *written = 0; } - return F_data_not; + return file.id == -1 ? F_stream_not : F_data_not; } - const f_array_length_t total = (range.stop - range.start) + 1; - f_array_length_t write_max = total; - - if (write_max > buffer.used) { - write_max = buffer.used; - } - - f_status_t status = F_none; + const f_array_length_t write_max = (range.stop - range.start) + 1 > buffer.used ? buffer.used : (range.stop - range.start) + 1; if (written) { const f_string_static_t buffer_adjusted = macro_f_string_static_t_initialize(buffer.string + range.start, 0, buffer.used - range.start); - status = private_f_file_write_until(file, buffer_adjusted, write_max, written); + const f_status_t status = private_f_file_write_until(file, buffer_adjusted, write_max, written); + if (F_status_is_error(status)) return status; if (status == F_none) { - if (range.start + *written == buffer.used) return F_none_stop; - if (range.start + *written == total) return F_none_eos; + if (range.start + *written == buffer.used) return F_none_eos; + if (range.start + *written == write_max) return F_none_stop; } } else { const f_string_static_t buffer_adjusted = macro_f_string_static_t_initialize(buffer.string + range.start, 0, buffer.used - range.start); f_array_length_t written_local = 0; - status = private_f_file_write_until(file, buffer_adjusted, write_max, &written_local); + const f_status_t status = private_f_file_write_until(file, buffer_adjusted, write_max, &written_local); + if (F_status_is_error(status)) return status; if (status == F_none) { if (range.start + written_local == buffer.used) return F_none_eos; - if (range.start + written_local == total) return F_none_stop; + if (range.start + written_local == write_max) return F_none_stop; } } - return status; + return F_none; } #endif // _di_f_file_write_range_ diff --git a/level_0/f_file/c/file.h b/level_0/f_file/c/file.h index 9fe8948..fffd301 100644 --- a/level_0/f_file/c/file.h +++ b/level_0/f_file/c/file.h @@ -85,8 +85,8 @@ extern "C" { * Do not use this to check file access before immediately attempting to open a file due to the possibility that the permissions change between this call and the open call. * Instead, use the f_file_open() directly. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param mode @@ -98,6 +98,7 @@ extern "C" { * F_true if requested access is allowed. * F_false if requested access is denied. * F_data_not if path.used is 0. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) on access denied. * F_directory_descriptor (with error bit) when at_id is not a valid file descriptor (at_id must point to a directory). @@ -114,7 +115,7 @@ extern "C" { * @see faccessat() */ #ifndef _di_f_file_access_at_ - extern f_status_t f_file_access_at(const int at_id, const f_string_static_t path, const int mode, const int flag); + extern f_status_t f_file_access_at(const f_file_t directory, const f_string_static_t path, const int mode, const int flag); #endif // _di_f_file_access_at_ /** @@ -180,11 +181,12 @@ extern "C" { * * Will not flush before closing. * - * @param id - * The file descriptor. + * @param file + * The file. * * @return * F_none on success. + * F_file_descriptor_not if file.id is -1. * * F_file_close (with error bit) if fclose() failed for any other reason. * F_file_descriptor (with error bit) if file descriptor is invalid. @@ -197,35 +199,10 @@ extern "C" { * @see fclose() */ #ifndef _di_f_file_close_ - extern f_status_t f_file_close(int * const id); + extern f_status_t f_file_close(f_file_t * const file); #endif // _di_f_file_close_ /** - * Close an open file. - * - * Will flush before closing. - * - * @param id - * The file descriptor. - * - * @return - * F_none on success. - * - * F_file_close (with error bit) if fclose() failed for any other reason. - * F_file_descriptor (with error bit) if file descriptor is invalid. - * F_file_synchronize (with error bit) on flush failure. - * F_filesystem_quota_block (with error bit) if file system's disk blocks or inodes are exhausted. - * F_input_output (with error bit) on I/O error. - * F_interrupt (with error bit) when program received an interrupt signal, halting operation. - * F_space_not (with error bit) if file system is out of space (or file system quota is reached). - * - * @see fclose() - */ -#ifndef _di_f_file_close_flush_ - extern f_status_t f_file_close_flush(int * const id); -#endif // _di_f_file_close_flush_ - -/** * Copy a file. * * The paths must not contain NULL except for the terminating NULL. @@ -327,8 +304,8 @@ extern "C" { * * The file will not be open after calling this. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param mode @@ -340,6 +317,7 @@ extern "C" { * @return * F_none on success. * F_data_not if path.used is 0. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) on access denied. * F_busy (with error bit) if file system is too busy to perform write. @@ -362,7 +340,7 @@ extern "C" { * @see openat() */ #ifndef _di_f_file_create_at_ - extern f_status_t f_file_create_at(const int at_id, const f_string_static_t path, const mode_t mode, const bool exclusive); + extern f_status_t f_file_create_at(const f_file_t directory, const f_string_static_t path, const mode_t mode, const bool exclusive); #endif // _di_f_file_create_at_ /** @@ -410,8 +388,8 @@ extern "C" { * * Warning: Due to the current status of POSIX and LINUX in regards to major and minor devices, this utilizes the non-POSI makedev() function. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param mode @@ -426,6 +404,7 @@ extern "C" { * @return * F_none on success. * F_data_not if path.used is 0. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) on access denied. * F_directory_descriptor (with error bit) when at_id is not a valid file descriptor (at_id must point to a directory). @@ -445,7 +424,7 @@ extern "C" { * @see mknodat() */ #ifndef _di_f_file_create_device_at_ - extern f_status_t f_file_create_device_at(const int at_id, const f_string_static_t path, const mode_t mode, const unsigned int major, const unsigned int minor); + extern f_status_t f_file_create_device_at(const f_file_t directory, const f_string_static_t path, const mode_t mode, const unsigned int major, const unsigned int minor); #endif // _di_f_file_create_device_at_ /** @@ -482,8 +461,8 @@ extern "C" { /** * Create a fifo based on the given path and file mode. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param mode @@ -492,6 +471,7 @@ extern "C" { * @return * F_none on success. * F_data_not if path.used is 0. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) on access denied. * F_directory_descriptor (with error bit) when at_id is not a valid file descriptor (at_id must point to a directory). @@ -510,7 +490,7 @@ extern "C" { * @see mkfifoat() */ #ifndef _di_f_file_create_fifo_at_ - extern f_status_t f_file_create_fifo_at(const int at_id, const f_string_static_t path, const mode_t mode); + extern f_status_t f_file_create_fifo_at(const f_file_t directory, const f_string_static_t path, const mode_t mode); #endif // _di_f_file_create_fifo_at_ /** @@ -550,8 +530,8 @@ extern "C" { /** * Create a node based on the given path and file mode. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param mode @@ -563,6 +543,7 @@ extern "C" { * @return * F_none on success. * F_data_not if path.used is 0. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) on access denied. * F_directory_descriptor (with error bit) when at_id is not a valid file descriptor (at_id must point to a directory). @@ -581,17 +562,18 @@ extern "C" { * @see mknodat() */ #ifndef _di_f_file_create_node_at_ - extern f_status_t f_file_create_node_at(const int at_id, const f_string_static_t path, const mode_t mode, const dev_t device); + extern f_status_t f_file_create_node_at(const f_file_t directory, const f_string_static_t path, const mode_t mode, const dev_t device); #endif // _di_f_file_create_node_at_ /** * Identify the file descriptor of a valid file stream. * * @param file - * The file stream to get descriptor of. + * The file. * * @return * F_none is returned on success. + * F_stream_not if file.stream is NULL. * * F_parameter (with error bit) if a parameter is invalid. * F_stream_not (with error bit) if file is not a valid stream. @@ -640,8 +622,8 @@ extern "C" { * This does not require access on the file itself. * This only requires access via the parent directories in the path. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param flag @@ -651,6 +633,7 @@ extern "C" { * F_true if file exists. * F_false if file does not exist. * F_data_not if path.used is 0. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) on access denied. * F_directory_descriptor (with error bit) when at_id is not a valid file descriptor (at_id must point to a directory). @@ -665,17 +648,18 @@ extern "C" { * @see fstatat() */ #ifndef _di_f_file_exists_at_ - extern f_status_t f_file_exists_at(const int at_id, const f_string_static_t path, const int flag); + extern f_status_t f_file_exists_at(const f_file_t directory, const f_string_static_t path, const int flag); #endif // _di_f_file_exists_at_ /** * Flush the file. * - * @param id - * The file descriptor. + * @param file + * The file. * * @return * F_none is returned on success. + * F_file_descriptor_not if file.id is -1. * * F_file_descriptor (with error bit) if file descriptor is invalid. * F_filesystem_quota_block (with error bit) if file system's disk blocks or inodes are exhausted. @@ -688,7 +672,7 @@ extern "C" { * @see fsync() */ #ifndef _di_f_file_flush_ - extern f_status_t f_file_flush(const int id); + extern f_status_t f_file_flush(const f_file_t id); #endif // _di_f_file_flush_ /** @@ -738,8 +722,8 @@ extern "C" { * @return * F_true if path was found and path is type. * F_false if path was found and path is not type. - * F_file_found_not if the path was not found. * F_data_not if path.used is 0. + * F_file_found_not if the path was not found. * * F_access_denied (with error bit) if access to the file was denied. * F_directory_not (with error bit) on invalid directory. @@ -758,8 +742,8 @@ extern "C" { /** * Identify whether or not a file exists at the given path within the parent directory and if that file is a specific type. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param type @@ -770,8 +754,9 @@ extern "C" { * @return * F_true if path was found and path is type. * F_false if path was found and path is not type. - * F_file_found_not if the path was not found. * F_data_not if path.used is 0. + * F_file_descriptor_not if file.id is -1. + * F_file_found_not if the path was not found. * * F_access_denied (with error bit) if access to the file was denied. * F_directory_descriptor (with error bit) when at_id is not a valid file descriptor (at_id must point to a directory). @@ -785,7 +770,7 @@ extern "C" { * @see fstatat() */ #ifndef _di_f_file_is_at_ - extern f_status_t f_file_is_at(const int at_id, const f_string_static_t path, const int type, const int flag); + extern f_status_t f_file_is_at(const f_file_t directory, const f_string_static_t path, const int type, const int flag); #endif // _di_f_file_is_at_ /** @@ -832,8 +817,8 @@ extern "C" { * This will not replace existing files/links. * This does not validate the existence of target. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which point path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param target * A path that the link points to. * @param point @@ -842,6 +827,7 @@ extern "C" { * @return * F_none on success. * F_data_not if target.used or point.used is 0. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) on access denied. * F_busy (with error bit) if file system is too busy to perform write. @@ -864,7 +850,7 @@ extern "C" { * @see symlinkat() */ #ifndef _di_f_file_link_at_ - extern f_status_t f_file_link_at(const int at_id, const f_string_static_t target, const f_string_static_t point); + extern f_status_t f_file_link_at(const f_file_t directory, const f_string_static_t target, const f_string_static_t point); #endif // _di_f_file_link_at_ /** @@ -909,10 +895,10 @@ extern "C" { * * This will not replace existing files/links. * - * @param at_id_target - * The parent directory, as an open directory file descriptor, in which target path is relative to. - * @param at_id_point - * The parent directory, as an open directory file descriptor, in which point path is relative to. + * @param file_target + * The parent directory, via an open directory file descriptor, in which target path is relative to. + * @param file_point + * The parent directory, via an open directory file descriptor, in which point path is relative to. * @param target * A path that the link points to. * @param point @@ -923,6 +909,7 @@ extern "C" { * @return * F_none on success. * F_data_not if target.used or point.used is 0. + * F_file_descriptor_not if either file.id is -1. * * F_access_denied (with error bit) on access denied. * F_buffer (with error bit) if the buffer is invalid. @@ -946,7 +933,7 @@ extern "C" { * @see linkat() */ #ifndef _di_f_file_link_hard_at_ - extern f_status_t f_file_link_hard_at(const int at_id_target, const int at_id_point, const f_string_static_t target, const f_string_static_t point, const int flag); + extern f_status_t f_file_link_hard_at(const f_file_t file_target, const f_file_t file_point, const f_string_static_t target, const f_string_static_t point, const int flag); #endif // _di_f_file_link_hard_at_ /** @@ -1000,8 +987,8 @@ extern "C" { * This does not require access on the file itself. * This only requires access via the parent directories in the path. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param flag @@ -1013,6 +1000,7 @@ extern "C" { * @return * F_none on success. * F_data_not if path.used is 0. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) on access denied. * F_buffer (with error bit) if the buffer is invalid. @@ -1037,7 +1025,7 @@ extern "C" { * @see f_string_dynamic_terminate_after() */ #ifndef _di_f_file_link_read_at_ - extern f_status_t f_file_link_read_at(const int at_id, const f_string_static_t path, const int flag, f_string_dynamic_t * const target); + extern f_status_t f_file_link_read_at(const f_file_t directory, const f_string_static_t path, const int flag, f_string_dynamic_t * const target); #endif // _di_f_file_link_read_at_ /** @@ -1204,8 +1192,8 @@ extern "C" { /** * Get the current file mode as an f_file_mode_t. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param flag @@ -1216,6 +1204,7 @@ extern "C" { * @return * F_none on success. * F_data_not if path.used is 0. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) if access to the file was denied. * F_directory_descriptor (with error bit) when at_id is not a valid file descriptor (at_id must point to a directory). @@ -1230,7 +1219,7 @@ extern "C" { * @see fstatat() */ #ifndef _di_f_file_mode_read_at_ - extern f_status_t f_file_mode_read_at(const int at_id, const f_string_static_t path, const int flag, mode_t * const mode); + extern f_status_t f_file_mode_read_at(const f_file_t directory, const f_string_static_t path, const int flag, mode_t * const mode); #endif // _di_f_file_mode_read_at_ /** @@ -1272,8 +1261,8 @@ extern "C" { * This does not set mode based on umask(), be sure to apply umask if so desired. * (such as: mode & ~mask). * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param mode @@ -1282,6 +1271,7 @@ extern "C" { * @return * F_none on success. * F_data_not if path.used is 0. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) on access denied. * F_access_mode (with error bit) if the current user does not have access to assign the file mode. @@ -1299,7 +1289,7 @@ extern "C" { * @see fchmodat() */ #ifndef _di_f_file_mode_set_at_ - extern f_status_t f_file_mode_set_at(const int at_id, const f_string_static_t path, const mode_t mode); + extern f_status_t f_file_mode_set_at(const f_file_t directory, const f_string_static_t path, const mode_t mode); #endif // _di_f_file_mode_set_at_ /** @@ -1435,8 +1425,8 @@ extern "C" { * This will open the file as a file descriptor. * This does not open a file stream. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param mode @@ -1449,6 +1439,7 @@ extern "C" { * @return * F_none on success. * F_data_not if path.used is 0. + * F_file_descriptor_not if file.id is -1. * * F_directory_descriptor (with error bit) for bad directory descriptor for at_id. * F_file_found_not (with error bit) if the file was not found. @@ -1458,7 +1449,7 @@ extern "C" { * @see openat() */ #ifndef _di_f_file_open_at_ - extern f_status_t f_file_open_at(const int at_id, const f_string_static_t path, const mode_t mode, f_file_t * const file); + extern f_status_t f_file_open_at(const f_file_t directory, const f_string_static_t path, const mode_t mode, f_file_t * const file); #endif // _di_f_file_open_at_ /** @@ -1498,13 +1489,13 @@ extern "C" { * * @param file * The file to read. - * The file must already be open. * @param buffer * The buffer the file is being read into. * The contents of the file is appended into this buffer. * * @return * F_none_eof on success and EOF was reached. + * F_file_descriptor_not if file.id is -1. * * F_block (with error bit) if file descriptor is set to non-block and the read would result in a blocking operation. * F_buffer (with error bit) if the buffer is invalid. @@ -1532,7 +1523,6 @@ extern "C" { * * @param file * The file to read. - * The file must already be open. * @param buffer * The buffer the file is being read into. * The contents of the file is appended into this buffer. @@ -1540,6 +1530,7 @@ extern "C" { * @return * F_none on success. * F_none_eof on success and EOF was reached. + * F_file_descriptor_not if file.id is -1. * * F_block (with error bit) if file descriptor is set to non-block and the read would result in a blocking operation. * F_buffer (with error bit) if the buffer is invalid. @@ -1568,7 +1559,6 @@ extern "C" { * * @param file * The file to read. - * The file must already be open. * @param total * The total bytes to read, unless EOF is reached first. * @param buffer @@ -1578,6 +1568,7 @@ extern "C" { * F_none_eof on success and EOF was reached. * F_none_stop on success and total was reached. * F_data_not if total is 0. + * F_file_descriptor_not if file.id is -1. * * F_block (with error bit) if file descriptor is set to non-block and the read would result in a blocking operation. * F_buffer (with error bit) if the buffer is invalid. @@ -1626,8 +1617,8 @@ extern "C" { /** * Remove a file. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param flag @@ -1635,6 +1626,7 @@ extern "C" { * * @return * F_none on success. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) on access denied. * F_busy (with error bit) if file is busy. @@ -1654,7 +1646,7 @@ extern "C" { * @see unlinkat() */ #ifndef _di_f_file_remove_at_ - extern f_status_t f_file_remove_at(const int at_id, const f_string_static_t path, const int flag); + extern f_status_t f_file_remove_at(const f_file_t directory, const f_string_static_t path, const int flag); #endif // _di_f_file_remove_at_ /** @@ -1714,10 +1706,10 @@ extern "C" { * If destination already exists, then according to rename(), destination will be atomically replaced. * Which, if destination is a directory, then that directory must either not exist or be empty. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which the source is relative to. - * @param to_id - * The parent directory, as an open directory file descriptor, in which the destination is relative to. + * @param directory_source + * The parent directory, via an open directory file descriptor, in which the source path is relative to. + * @param directory_destination + * The parent directory, via an open directory file descriptor, in which the destination path is relative to. * @param source * The path to the file to copy from. * @param destination @@ -1729,6 +1721,7 @@ extern "C" { * * @return * F_none on success. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) on access denied. * F_buffer (with error bit) if the buffer is invalid. @@ -1753,7 +1746,7 @@ extern "C" { * @see renameat() */ #ifndef _di_f_file_rename_at_ - extern f_status_t f_file_rename_at(const int at_id, const int to_id, const f_string_static_t source, const f_string_static_t destination, const unsigned int flag); + extern f_status_t f_file_rename_at(const f_file_t directory_source, const f_file_t directory_destination, const f_string_static_t source, const f_string_static_t destination, const unsigned int flag); #endif // _di_f_file_rename_at_ /** @@ -1799,8 +1792,8 @@ extern "C" { /** * Change owner and/or group of a given file at the specified path. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param uid @@ -1815,6 +1808,7 @@ extern "C" { * @return * F_none on success. * F_data_not if either both uid and gid are -1 or path.used is 0. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) on access denied. * F_access_group (with error bit) if the current user does not have access to assign the specified group. @@ -1834,14 +1828,14 @@ extern "C" { * @see fchownat() */ #ifndef _di_f_file_role_change_at_ - extern f_status_t f_file_role_change_at(const int at_id, const f_string_static_t path, const uid_t uid, const gid_t gid, const int flag); + extern f_status_t f_file_role_change_at(const f_file_t directory, const f_string_static_t path, const uid_t uid, const gid_t gid, const int flag); #endif // _di_f_file_role_change_at_ /** * Given an open file descriptor, seek to a given location. * - * @param id - * The file descriptor. + * @param file + * The file. * @param whence * One of: SEEK_SET, SEEK_CUR, SEEK_END, SEEK_DATA, SEEK_HOLE. * @param offset @@ -1852,6 +1846,7 @@ extern "C" { * * @return * F_none on success. + * F_file_descriptor_not if file.id is -1. * * F_bound_not (with error bit) if SEEK_DATA or SEEK_HOLE is specified as whence and offset is beyond the end of file. * F_file_descriptor (with error bit) if the file descriptor is invalid. @@ -1863,7 +1858,7 @@ extern "C" { * @see lseek */ #ifndef _di_f_file_seek_ - extern f_status_t f_file_seek(const int id, const int whence, const off_t offset, off_t * const seeked); + extern f_status_t f_file_seek(const f_file_t file, const int whence, const off_t offset, off_t * const seeked); #endif // _di_f_file_seek_ /** @@ -1898,8 +1893,8 @@ extern "C" { /** * Read the size of file. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path to the file. * @param dereference @@ -1910,6 +1905,7 @@ extern "C" { * * @return * F_none on success. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) if access to the file was denied. * F_directory_descriptor (with error bit) when at_id is not a valid file descriptor (at_id must point to a directory). @@ -1921,22 +1917,25 @@ extern "C" { * F_number_overflow (with error bit) on overflow error. * F_parameter (with error bit) if a parameter is invalid. * + * F_file_stat (with error bit) on any other error. + * * @see f_file_stat_at() */ #ifndef _di_f_file_size_at_ - extern f_status_t f_file_size_at(const int at_id, const f_string_static_t path, const bool dereference, off_t * const size); + extern f_status_t f_file_size_at(const f_file_t directory, const f_string_static_t path, const bool dereference, off_t * const size); #endif // _di_f_file_size_at_ /** * Read size of a file relative to the path represented by the file descriptor id. * - * @param id - * The file descriptor. + * @param file + * The file. * @param size * This gets set to the size of the file. * * @return * F_none on success. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) if access to the file was denied. * F_directory_not (with error bit) on invalid directory. @@ -1949,10 +1948,12 @@ extern "C" { * F_number_overflow (with error bit) on overflow error. * F_parameter (with error bit) if a parameter is invalid. * + * F_file_stat (with error bit) on any other error. + * * @see f_file_stat_by_id() */ #ifndef _di_f_file_size_by_id_ - extern f_status_t f_file_size_by_id(const int id, off_t * const size); + extern f_status_t f_file_size_by_id(const f_file_t file, off_t * const size); #endif // _di_f_file_size_by_id_ /** @@ -1968,6 +1969,7 @@ extern "C" { * * @return * F_none on success. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) if access to the file was denied. * F_directory_not (with error bit) on invalid directory. @@ -1978,6 +1980,8 @@ extern "C" { * F_number_overflow (with error bit) on overflow error. * F_parameter (with error bit) if a parameter is invalid. * + * F_file_stat (with error bit) on any other error. + * * @see stat() */ #ifndef _di_f_file_stat_ @@ -1987,8 +1991,8 @@ extern "C" { /** * Read statistics of a file relative to the path represented by the file descriptor id. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path to the file. * @param flag @@ -1998,6 +2002,7 @@ extern "C" { * * @return * F_none on success. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) if access to the file was denied. * F_directory_descriptor (with error bit) when at_id is not a valid file descriptor (at_id must point to a directory). @@ -2009,22 +2014,25 @@ extern "C" { * F_number_overflow (with error bit) on overflow error. * F_parameter (with error bit) if a parameter is invalid. * + * F_file_stat (with error bit) on any other error. + * * @see fstatat() */ #ifndef _di_f_file_stat_at_ - extern f_status_t f_file_stat_at(const int at_id, const f_string_static_t path, const int flag, struct stat * const stat_file); + extern f_status_t f_file_stat_at(const f_file_t directory, const f_string_static_t path, const int flag, struct stat * const stat_file); #endif // _di_f_file_stat_at_ /** * Read statistics of a file using a file descriptor id. * - * @param id - * The file descriptor. + * @param file + * The file. * @param stat_file * The statistics read. * * @return * F_none on success. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) if access to the file was denied. * F_directory_not (with error bit) on invalid directory. @@ -2035,39 +2043,33 @@ extern "C" { * F_number_overflow (with error bit) on overflow error. * F_parameter (with error bit) if a parameter is invalid. * + * F_file_stat (with error bit) on any other error. + * * @see fstat() */ #ifndef _di_f_file_stat_by_id_ - extern f_status_t f_file_stat_by_id(const int id, struct stat * const stat_file); + extern f_status_t f_file_stat_by_id(const f_file_t file, struct stat * const stat_file); #endif // _di_f_file_stat_by_id_ /** * Close an open file stream. * - * @param flush - * If TRUE, will explicitly flush all unwritten data in any buffers to the file. - * If FALSE, will not explicitly flush unwritten data. - * - * If TRUE and flush fails, then this will still attempt to close the stream. * @param file * The file information. - * The file.stream is set to 0, on success or on failure. - * The file.id is set to 0, on success or on failure. + * The file.stream is set to NULL, on both success or on failure. * * @return - * F_none is returned on success. + * F_none on success. + * F_stream_not if file.stream is NULL. * * F_access_denied (with error bit) on access denied. * F_block (with error bit) if the action would block and non-blocking is set on the stream. * F_buffer (with error bit) if the buffer is invalid. * F_deadlock (with error bit) if operation would cause a deadlock. - * F_file_close (with error bit) if fclose() failed for any other reason. * F_file_descriptor (with error bit) if file descriptor is invalid. * F_file_descriptor_max (with error bit) if max file descriptors is reached. * F_file_overflow (with error bit) if the write exceeds some implementation defined maximum file size. - * F_file_synchronize (with error bit) on flush failure. * F_file_type_not_directory (with error bit) if F_NOTIFY was specified and file.id is not a directory. - * F_filesystem_quota_block (with error bit) if file system's disk blocks or inodes are exhausted. * F_input_output (with error bit) on I/O error. * F_interrupt (with error bit) when program received an interrupt signal, halting operation. * F_lock (with error bit) if failed to lock, such as lock table is full or too many open segments. @@ -2076,15 +2078,43 @@ extern "C" { * F_space_not (with error bit) if file system is out of space (or file system quota is reached). * F_socket_not (with error bit) if the datagram socket in which a peer has not been set (for socket related streams). * - * @see close() + * F_file_close (with error bit) on any other error. + * * @see fclose() - * @see fflush() */ #ifndef _di_f_file_stream_close_ - extern f_status_t f_file_stream_close(const bool flush, f_file_t * const file); + extern f_status_t f_file_stream_close(f_file_t * const file); #endif // _di_f_file_stream_close_ /** + * Flush a file stream. + * + * @param file + * The file information. + * The file.id is updated with the file descriptor, if necessary and able. + * + * @return + * F_none is returned on success. + * F_stream_not if file.stream is NULL. + * F_file_descriptor_not if file.id is -1. + * + * F_block (with error bit) if file descriptor is set to non-block and the write would result in a blocking operation. + * F_buffer (with error bit) if the buffer is invalid. + * F_file_closed (with error bit) if file is not open. + * F_file_descriptor (with error bit) if the file descriptor is invalid. + * F_file_type_directory (with error bit) if file descriptor represents a directory. + * F_input_output (with error bit) on I/O error. + * F_interrupt (with error bit) if interrupt was received. + * + * F_file_synchronize (with error bit) on any other error. + * + * @see fflush() + */ +#ifndef _di_f_file_stream_flush_ + extern f_status_t f_file_stream_flush(f_file_t * const file); +#endif // _di_f_file_stream_flush_ + +/** * Open a file stream. * * The file descriptor is retrieved on success, if necessary and able. @@ -2135,7 +2165,6 @@ extern "C" { * F_supported_not (with error bit) fo unsupported file types. * F_failure (with error bit) for any other error. * - * @see fileno() * @see fopen() */ #ifndef _di_f_file_stream_open_ @@ -2152,10 +2181,10 @@ extern "C" { * This should match the modes used to open the file descriptor as it relates to the stream modes. * @param file * The file with a valid file descriptor (file.id). - * THe file stream (file.stream) is updated on success, but may be set to NULL on error. * * @return * F_none is returned on success. + * F_file_descriptor_not if file.id is -1. * * F_access_denied (with error bit) on access denied. * F_block (with error bit) if the action would block and non-blocking is set on the stream. @@ -2186,7 +2215,6 @@ extern "C" { * * @param file * The file to read. - * The file must already be open. * The file.size_read represents the amount to process at a given time. * @param buffer * The buffer the file is being read into. @@ -2194,6 +2222,7 @@ extern "C" { * * @return * F_none_eof on success and EOF was reached. + * F_stream_not if file.stream is NULL. * * F_block (with error bit) if file descriptor is set to non-block and the read would result in a blocking operation. * F_buffer (with error bit) if the buffer is invalid. @@ -2222,7 +2251,6 @@ extern "C" { * * @param file * The file to read. - * The file must already be open. * The file.size_read represents the amount to process at a given time. * @param buffer * The buffer the file is being read into. @@ -2231,6 +2259,7 @@ extern "C" { * @return * F_none on success. * F_none_eof on success and EOF was reached. + * F_stream_not if file.stream is NULL. * * F_error (with error bit) if the file is already in the error state at the start of this function. * F_file_closed (with error bit) if the file is closed. @@ -2259,7 +2288,6 @@ extern "C" { * * @param file * The file to read. - * The file must already be open. * The file.size_read represents the amount to process at a given time. * @param total * The total bytes to read, unless EOF is reached first. @@ -2269,6 +2297,8 @@ extern "C" { * @return * F_none_eof on success and EOF was reached. * F_none_stop on success and total was reached. + * F_data_not if total is 0. + * F_stream_not if file.stream is NULL. * * F_error (with error bit) if the file is already in the error state at the start of this function. * F_file_closed (with error bit) if the file is closed. @@ -2306,7 +2336,6 @@ extern "C" { * @param file * The file information. * The file.stream is updated, if necessary. - * The file.id is updated with the file descriptor, if necessary and able. * * @return * F_none is returned on success. @@ -2348,14 +2377,15 @@ extern "C" { * @param buffer * The buffer to write to the file. * @param written - * The total bytes written. - * Set pointer to 0 to not use. + * (optional) The total bytes written. + * Set to NULL to not use. * * @return * F_none on success. * F_none_eof when the file stream is at the end of the file. * F_none_stop on success but no data was written (written == 0) (not an error and often happens if file type is not a regular file). * F_data_not on success but buffer.used is 0. + * F_stream_not if file.stream is NULL. * * F_file_write (with error bit) on error during file write. * F_parameter (with error bit) if a parameter is invalid. @@ -2383,14 +2413,15 @@ extern "C" { * @param buffer * The buffer to write to the file. * @param written - * The total bytes written. - * Set pointer to 0 to not use. + * (optional) The total bytes written. + * Set to NULL to not use. * * @return * F_none on success. * F_none_eof when the file stream is at the end of the file. * F_none_stop on success but no data was written (written == 0) (not an error and often happens if file type is not a regular file). * F_data_not on success but buffer.used is 0. + * F_stream_not if file.stream is NULL. * * F_file_write (with error bit) on error during file write. * F_parameter (with error bit) if a parameter is invalid. @@ -2418,8 +2449,8 @@ extern "C" { * @param total * The total bytes to write, unless end of buffer is reached first. * @param written - * The total bytes written. - * Set pointer to 0 to not use. + * (optional) The total bytes written. + * Set to NULL to not use.. * * @return * F_none on success. @@ -2427,6 +2458,7 @@ extern "C" { * F_none_eos on success but range.stop exceeded buffer.used (only wrote up to buffer.used). * F_none_stop on success but no data was written (written == 0) (not an error and often happens if file type is not a regular file). * F_data_not on success but either buffer.used or total is 0. + * F_stream_not if file.stream is NULL. * * F_file_write (with error bit) on error during file write. * F_parameter (with error bit) if a parameter is invalid. @@ -2447,20 +2479,20 @@ extern "C" { * * @param file * The file to write to. - * The file must already be open. * The file.size_write represents the amount to process at a given time. * @param buffer * The buffer to write to the file. * @param range * An inclusive start an stop range within the buffer to read. * @param written - * The total bytes written. - * Set pointer to 0 to not use. + * (optional) The total bytes written. + * Set to NULL to not use. * * @return * F_none on success. * F_none_stop on success but no data was written (written == 0) (not an error and often happens if file type is not a regular file). * F_none_eos on success but range.stop exceeded buffer.used (only wrote up to buffer.used). + * F_stream_not if file.stream is NULL. * * F_file_write (with error bit) on error during file write. * F_parameter (with error bit) if a parameter is invalid. @@ -2519,8 +2551,8 @@ extern "C" { * * When the file is created, it is created as a regular file. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param mode @@ -2532,6 +2564,7 @@ extern "C" { * F_none on success. * F_access_denied (with error bit) on access denied. * F_buffer (with error bit) if the buffer is invalid. + * F_stream_not if file.stream is NULL. * * F_busy (with error bit) if file system is too busy to perform write. * F_directory_descriptor (with error bit) when at_id is not a valid file descriptor (at_id must point to a directory). @@ -2554,7 +2587,7 @@ extern "C" { * @see utimensat() */ #ifndef _di_f_file_touch_at_ - extern f_status_t f_file_touch_at(const int at_id, const f_string_static_t path, const mode_t mode, const int flag); + extern f_status_t f_file_touch_at(const f_file_t directory, const f_string_static_t path, const mode_t mode, const int flag); #endif // _di_f_file_touch_at_ /** @@ -2592,8 +2625,8 @@ extern "C" { /** * Get the file type for the file at the given path within the parent directory. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param flag @@ -2604,6 +2637,7 @@ extern "C" { * @return * F_none if path was found and and the type was loaded in the type parameter. * F_file_found_not if the path was not found. + * F_stream_not if file.stream is NULL. * * F_access_denied (with error bit) if access to the file was denied. * F_directory_descriptor (with error bit) when at_id is not a valid file descriptor (at_id must point to a directory). @@ -2617,7 +2651,7 @@ extern "C" { * @see fstatat() */ #ifndef _di_f_file_type_at_ - extern f_status_t f_file_type_at(const int at_id, const f_string_static_t path, const int flag, int * const type); + extern f_status_t f_file_type_at(const f_file_t directory, const f_string_static_t path, const int flag, int * const type); #endif // _di_f_file_type_at_ /** @@ -2661,17 +2695,17 @@ extern "C" { * * @param file * The file to write to. - * The file must already be open. * @param buffer * The buffer to write to the file. * @param written - * The total bytes written. - * Set pointer to 0 to not use. + * (optional) The total bytes written. + * Set to NULL to not use. * * @return * F_none on success. - * F_none_stop on success but no data was written (written == 0) (not an error and often happens if file type is not a regular file). - * F_data_not if buffer.used is 0. + * F_none_eos on success and wrote up to buffer.used. + * F_data_not if buffer.used is 0 or range.start > range.stop. + * F_stream_not if file.id is -1. * * F_block (with error bit) if file descriptor is set to non-block and the write would result in a blocking operation. * F_buffer (with error bit) if the buffer is invalid. @@ -2695,17 +2729,18 @@ extern "C" { * * @param file * The file to write to. - * The file must already be open. * @param buffer * The buffer to write to the file. * @param written - * The total bytes written. - * Set pointer to 0 to not use. + * (optional) The total bytes written. + * Set to NULL to not use. * * @return * F_none on success. - * F_none_stop on success but no data was written (written == 0) (not an error and often happens if file type is not a regular file). - * F_data_not if buffer.used is 0. + * F_none_stop on success and wrote up to stop point. + * F_none_eos on success and wrote up to buffer.used (buffer.used is reached before stop point). + * F_data_not if buffer.used is 0 or range.start > range.stop. + * F_stream_not if file.id is -1. * * F_block (with error bit) if file descriptor is set to non-block and the write would result in a blocking operation. * F_buffer (with error bit) if the buffer is invalid. @@ -2727,20 +2762,20 @@ extern "C" { * * @param file * The file to write to. - * The file must already be open. * @param buffer * The buffer to write to the file. * @param total * The total bytes to write, unless end of buffer is reached first. * @param written - * The total bytes written. - * Set pointer to 0 to not use. + * (optional) The total bytes written. + * Set to NULL to not use. * * @return * F_none on success. - * F_none_stop on success but no data was written (written == 0) (not an error and often happens if file type is not a regular file). - * F_none_eos on success but range.stop exceeded buffer.used (only wrote up to buffer.used). - * F_data_not if buffer.used is 0 or total is 0. + * F_none_stop on success and wrote up to stop point. + * F_none_eos on success and wrote up to buffer.used (buffer.used is reached before stop point). + * F_data_not if buffer.used is 0 or range.start > range.stop. + * F_stream_not if file.id is -1. * * F_block (with error bit) if file descriptor is set to non-block and the write would result in a blocking operation. * F_buffer (with error bit) if the buffer is invalid. @@ -2762,20 +2797,20 @@ extern "C" { * * @param file * The file to write to. - * The file must already be open. * @param buffer * The buffer to write to the file. * @param range * An inclusive start an stop range within the buffer to read. * @param written - * The total bytes written. - * Set pointer to 0 to not use. + * (optional) The total bytes written. + * Set to NULL to not use. * * @return * F_none on success. - * F_none_stop on success but no data was written (written == 0) (not an error and often happens if file type is not a regular file). - * F_none_eos on success but range.stop exceeded buffer.used (only wrote up to buffer.used). + * F_none_stop on success and wrote up to stop point. + * F_none_eos on success and wrote up to buffer.used (buffer.used is reached before stop point). * F_data_not if buffer.used is 0 or range.start > range.stop. + * F_stream_not if file.id is -1. * * F_block (with error bit) if file descriptor is set to non-block and the write would result in a blocking operation. * F_buffer (with error bit) if the buffer is invalid. diff --git a/level_0/f_file/c/file/common.h b/level_0/f_file/c/file/common.h index efc9681..b7f197f 100644 --- a/level_0/f_file/c/file/common.h +++ b/level_0/f_file/c/file/common.h @@ -189,6 +189,8 @@ extern "C" { #define macro_f_file_t_initialize(stream, id, flag, read_size, write_size) { stream, id, flag, read_size, write_size } #define macro_f_file_t_initialize2(stream, id, flag) { stream, id, flag, F_file_default_read_size_d, F_file_default_write_size_d } + #define macro_f_file_t_initialize_id(id) { 0, id, F_file_flag_read_only_d, F_file_default_read_size_d, F_file_default_write_size_d } + #define macro_f_file_t_initialize_stream(stream) { stream, -1, F_file_flag_read_only_d, F_file_default_read_size_d, F_file_default_write_size_d } #define macro_f_file_t_clear(file) \ file.stream = 0; \ diff --git a/level_0/f_file/c/private-file.c b/level_0/f_file/c/private-file.c index acaa20e..9a6cdd8 100644 --- a/level_0/f_file/c/private-file.c +++ b/level_0/f_file/c/private-file.c @@ -6,21 +6,13 @@ extern "C" { #endif #if !defined(_di_f_file_close_) || !defined(_di_f_file_copy_) || !defined(_di_f_file_stream_close_) - f_status_t private_f_file_close(const bool flush, int * const id) { + f_status_t private_f_file_close(f_file_t * const file) { - if (*id == -1) { - return F_none; - } - - if (flush) { - private_f_file_flush(*id); - } - - if (close(*id) < 0) { + if (close(file->id) < 0) { // According to man pages, retrying close() after another close on error is invalid on Linux because Linux releases the descriptor before stages that cause failures. if (errno != EBADF && errno != EINTR) { - *id = -1; + file->id = -1; } if (errno == EBADF) return F_status_set_error(F_file_descriptor); @@ -32,7 +24,7 @@ extern "C" { return F_status_set_error(F_file_close); } - *id = -1; + file->id = -1; return F_none; } @@ -53,7 +45,8 @@ extern "C" { status = private_f_file_open(destination, 0, &file_destination); if (F_status_is_error(status)) { - private_f_file_close(F_true, &file_source.id); + private_f_file_flush(file_source); + private_f_file_close(&file_source); return status; } @@ -69,26 +62,30 @@ extern "C" { size_write = write(file_destination.id, buffer, size_read); if (size_write < 0 || size_write != size_read) { - private_f_file_close(F_true, &file_destination.id); - private_f_file_close(F_true, &file_source.id); + private_f_file_flush(file_destination); + private_f_file_flush(file_source); + + private_f_file_close(&file_destination); + private_f_file_close(&file_source); return F_status_set_error(F_file_write); } } // while - private_f_file_close(F_true, &file_destination.id); - private_f_file_close(F_true, &file_source.id); + private_f_file_flush(file_destination); + private_f_file_flush(file_source); - if (size_read < 0) { - return F_status_set_error(F_file_read); - } + private_f_file_close(&file_destination); + private_f_file_close(&file_source); + + if (size_read < 0) return F_status_set_error(F_file_read); return F_none; } #endif // !defined(_di_f_file_copy_) || !defined(_di_f_file_clone_) #if !defined(_di_f_file_copy_at_) || !defined(_di_f_file_clone_at_) - f_status_t private_f_file_copy_content_at(const int at_id, const f_string_static_t source, const f_string_static_t destination, const f_number_unsigned_t size_block) { + f_status_t private_f_file_copy_content_at(const f_file_t directory, const f_string_static_t source, const f_string_static_t destination, const f_number_unsigned_t size_block) { f_file_t file_source = f_file_t_initialize; f_file_t file_destination = f_file_t_initialize; @@ -96,13 +93,14 @@ extern "C" { file_source.flag = F_file_flag_read_only_d; file_destination.flag = F_file_flag_write_only_d | F_file_flag_no_follow_d; - f_status_t status = private_f_file_open_at(at_id, source, 0, &file_source); + f_status_t status = private_f_file_open_at(directory, source, 0, &file_source); if (F_status_is_error(status)) return status; - status = private_f_file_open_at(at_id, destination, 0, &file_destination); + status = private_f_file_open_at(directory, destination, 0, &file_destination); if (F_status_is_error(status)) { - private_f_file_close(F_true, &file_source.id); + private_f_file_flush(file_source); + private_f_file_close(&file_source); return status; } @@ -118,19 +116,23 @@ extern "C" { size_write = write(file_destination.id, buffer, size_read); if (size_write < 0 || size_write != size_read) { - private_f_file_close(F_true, &file_destination.id); - private_f_file_close(F_true, &file_source.id); + private_f_file_flush(file_destination); + private_f_file_flush(file_source); + + private_f_file_close(&file_destination); + private_f_file_close(&file_source); return F_status_set_error(F_file_write); } } // while - private_f_file_close(F_true, &file_destination.id); - private_f_file_close(F_true, &file_source.id); + private_f_file_flush(file_destination); + private_f_file_flush(file_source); - if (size_read < 0) { - return F_status_set_error(F_file_read); - } + private_f_file_close(&file_destination); + private_f_file_close(&file_source); + + if (size_read < 0) return F_status_set_error(F_file_read); return F_none; } @@ -147,34 +149,42 @@ extern "C" { file.flag |= F_file_flag_exclusive_d; } - const f_status_t status = private_f_file_open(path, mode, &file); + f_status_t status = private_f_file_open(path, mode, &file); + + if (F_status_is_error_not(status) && file.id != -1) { + private_f_file_flush(file); - if (file.id != -1) { - return private_f_file_close(F_true, &file.id); + status = private_f_file_close(&file); } - return status; + if (F_status_is_error(status)) return status; + + return F_none; } #endif // !defined(_di_f_file_create_) || !defined(_di_f_file_copy_) #if !defined(_di_f_file_create_at_) || !defined(_di_f_file_copy_at_) - f_status_t private_f_file_create_at(const int at_id, const f_string_static_t path, const mode_t mode, const bool exclusive) { + f_status_t private_f_file_create_at(const f_file_t file, const f_string_static_t path, const mode_t mode, const bool exclusive) { - f_file_t file = f_file_t_initialize; + f_file_t file_internal = f_file_t_initialize; - file.flag = F_file_flag_close_execute_d | F_file_flag_create_d | F_file_flag_write_only_d; + file_internal.flag = F_file_flag_close_execute_d | F_file_flag_create_d | F_file_flag_write_only_d; if (exclusive) { - file.flag |= F_file_flag_exclusive_d; + file_internal.flag |= F_file_flag_exclusive_d; } - const f_status_t status = private_f_file_open_at(at_id, path, mode, &file); + f_status_t status = private_f_file_open_at(file, path, mode, &file_internal); + + if (F_status_is_error_not(status) && file_internal.id != -1) { + private_f_file_flush(file_internal); - if (file.id != -1) { - return private_f_file_close(F_true, &file.id); + status = private_f_file_close(&file_internal); } - return status; + if (F_status_is_error(status)) return status; + + return F_none; } #endif // !defined(_di_f_file_create_at_) || !defined(_di_f_file_copy_at_) @@ -205,9 +215,9 @@ extern "C" { #endif // !defined(_di_f_file_copy_) #if !defined(_di_f_file_copy_at_) - f_status_t private_f_file_create_directory_at(const int at_id, const f_string_static_t path, const mode_t mode) { + f_status_t private_f_file_create_directory_at(const f_file_t directory, const f_string_static_t path, const mode_t mode) { - if (mkdirat(at_id, path.string, mode) < 0) { + if (mkdirat(directory.id, path.string, mode) < 0) { if (errno == EACCES) return F_status_set_error(F_access_denied); if (errno == EDQUOT) return F_status_set_error(F_filesystem_quota_block); if (errno == EEXIST) return F_status_set_error(F_file_found); @@ -253,9 +263,9 @@ extern "C" { #endif // !defined(_di_f_file_create_fifo_) || !defined(_di_f_file_copy_) #if !defined(_di_f_file_create_fifo_at_) || !defined(_di_f_file_copy_at_) - f_status_t private_f_file_create_fifo_at(const int at_id, const f_string_static_t path, const mode_t mode) { + f_status_t private_f_file_create_fifo_at(const f_file_t file, const f_string_static_t path, const mode_t mode) { - if (mkfifoat(at_id, path.string, mode) < 0) { + if (mkfifoat(file.id, path.string, mode) < 0) { if (errno == EACCES) return F_status_set_error(F_access_denied); if (errno == EBADF) return F_status_set_error(F_directory_descriptor); if (errno == EDQUOT) return F_status_set_error(F_filesystem_quota_block); @@ -301,9 +311,9 @@ extern "C" { #endif // !defined(_di_f_file_create_device_) || !defined(_di_f_file_create_node_) || !defined(_di_f_file_copy_) #if !defined(_di_f_file_create_device_at_) || !defined(_di_f_file_create_node_at_) || !defined(_di_f_file_copy_at_) - f_status_t private_f_file_create_node_at(const int at_id, const f_string_static_t path, const mode_t mode, const dev_t device) { + f_status_t private_f_file_create_node_at(const f_file_t file, const f_string_static_t path, const mode_t mode, const dev_t device) { - if (mknodat(at_id, path.string, mode, device) < 0) { + if (mknodat(file.id, path.string, mode, device) < 0) { if (errno == EACCES) return F_status_set_error(F_access_denied); if (errno == EBADF) return F_status_set_error(F_directory_descriptor); if (errno == EDQUOT) return F_status_set_error(F_filesystem_quota_block); @@ -327,10 +337,10 @@ extern "C" { } #endif // !defined(_di_f_file_create_device_at_) || !defined(_di_f_file_create_node_at_) || !defined(_di_f_file_copy_at_) -#if !defined(_di_f_file_flush_) || !defined(_di_f_file_close_) || !defined(_di_f_file_copy_) || !defined(_di_f_file_stream_close_) - f_status_t private_f_file_flush(const int id) { +#if !defined(_di_f_file_clone_at_) || !defined(_di_f_file_close_) || !defined(_di_f_file_copy_) || !defined(_di_f_file_copy_at_) || !defined(_di_f_file_create_) || !defined(_di_f_file_create_at_) || !defined(_di_f_file_flush_) || !defined(_di_f_file_stream_close_) + f_status_t private_f_file_flush(const f_file_t file) { - if (fsync(id) < 0) { + if (fsync(file.id) < 0) { if (errno == EBADF) return F_status_set_error(F_file_descriptor); if (errno == EDQUOT) return F_status_set_error(F_filesystem_quota_block); if (errno == EINVAL) return F_status_set_error(F_supported_not); @@ -343,7 +353,7 @@ extern "C" { return F_none; } -#endif // !defined(_di_f_file_flush_) || !defined(_di_f_file_close_) || !defined(_di_f_file_copy_) || !defined(_di_f_file_stream_close_) +#endif // !defined(_di_f_file_clone_at_) || !defined(_di_f_file_close_) || !defined(_di_f_file_copy_) || !defined(_di_f_file_copy_at_) || !defined(_di_f_file_create_) || !defined(_di_f_file_create_at_) || !defined(_di_f_file_flush_) || !defined(_di_f_file_stream_close_) #if !defined(_di_f_file_link_) || !defined(_di_f_file_copy_) f_status_t private_f_file_link(const f_string_static_t target, const f_string_static_t point) { @@ -374,9 +384,9 @@ extern "C" { #endif // !defined(_di_f_file_link_) || !defined(_di_f_file_copy_) #if !defined(_di_f_file_link_at_) || !defined(_di_f_file_copy_at_) - f_status_t private_f_file_link_at(const int at_id, const f_string_static_t target, const f_string_static_t point) { + f_status_t private_f_file_link_at(const f_file_t directory, const f_string_static_t target, const f_string_static_t point) { - if (symlinkat(target.string, at_id, point.string) < 0) { + if (symlinkat(target.string, directory.id, point.string) < 0) { if (errno == EACCES) return F_status_set_error(F_access_denied); if (errno == EBADF) return F_status_set_error(F_directory_descriptor); if (errno == EDQUOT) return F_status_set_error(F_filesystem_quota_block); @@ -436,7 +446,7 @@ extern "C" { #endif // !defined(_di_f_file_link_read_) || !defined(_di_f_file_copy_) #if !defined(_di_f_file_link_read_at_) || !defined(_di_f_file_copy_at_) - f_status_t private_f_file_link_read_at(const int at_id, const f_string_static_t path, const off_t size, f_string_dynamic_t * const target) { + f_status_t private_f_file_link_read_at(const f_file_t file, const f_string_static_t path, const off_t size, f_string_dynamic_t * const target) { target->used = 0; @@ -444,7 +454,7 @@ extern "C" { if (F_status_is_error(status)) return status; if (size) { - if (readlinkat(at_id, path.string, target->string, size) < 0) { + if (readlinkat(file.id, path.string, target->string, size) < 0) { if (errno == EACCES) return F_status_set_error(F_access_denied); if (errno == EBADF) return F_status_set_error(F_directory_descriptor); if (errno == EFAULT) return F_status_set_error(F_buffer); @@ -492,9 +502,9 @@ extern "C" { #endif // !defined(_di_f_file_mode_set_) || !defined(_di_f_file_copy_) #if !defined(_di_f_file_mode_set_at_) || !defined(_di_f_file_copy_at_) - f_status_t private_f_file_mode_set_at(const int at_id, const f_string_static_t path, const mode_t mode) { + f_status_t private_f_file_mode_set_at(const f_file_t file, const f_string_static_t path, const mode_t mode) { - if (fchmodat(at_id, path.string, mode, 0) < 0) { + if (fchmodat(file.id, path.string, mode, 0) < 0) { if (errno == EACCES) return F_status_set_error(F_access_denied); if (errno == EBADF) return F_status_set_error(F_directory_descriptor); if (errno == EFAULT) return F_status_set_error(F_buffer); @@ -548,9 +558,9 @@ extern "C" { #endif // !defined(_di_f_file_open_) || !defined(_di_f_file_copy_) #if !defined(_di_f_file_copy_at_) || !defined(_di_f_file_clone_at_) || !defined(_di_f_file_open_at_) || !defined(_di_f_file_copy_at_) - f_status_t private_f_file_open_at(const int at_id, const f_string_static_t path, const mode_t mode, f_file_t * const file) { + f_status_t private_f_file_open_at(const f_file_t directory, const f_string_static_t path, const mode_t mode, f_file_t * const file) { - file->id = openat(at_id, path.string, file->flag, mode); + file->id = openat(directory.id, path.string, file->flag, mode); if (file->id == -1) { if (errno == EACCES) return F_status_set_error(F_access_denied); @@ -631,12 +641,12 @@ extern "C" { #endif // !defined(_di_f_file_role_change_) || !defined(_di_f_file_copy_) #if !defined(_di_f_file_role_change_at_) || !defined(_di_f_file_copy_at_) - f_status_t private_f_file_role_change_at(const int at_id, const f_string_static_t path, const uid_t uid, const gid_t gid, const int flag) { + f_status_t private_f_file_role_change_at(const f_file_t file, const f_string_static_t path, const uid_t uid, const gid_t gid, const int flag) { int result = 0; if (uid != -1) { - result = fchownat(at_id, path.string, uid, -1, flag); + result = fchownat(file.id, path.string, uid, -1, flag); if (result < 0 && errno == EPERM) { return F_status_set_error(F_access_owner); @@ -644,7 +654,7 @@ extern "C" { } if (result == 0 && gid != -1) { - result = fchownat(at_id, path.string, -1, gid, flag); + result = fchownat(file.id, path.string, -1, gid, flag); if (result < 0 && errno == EPERM) { return F_status_set_error(F_access_group); @@ -692,9 +702,9 @@ extern "C" { #endif // !defined(_di_f_file_stat_) || !defined(_di_f_file_copy_) #if !defined(_di_f_file_stat_at_) || !defined(_di_f_file_copy_at_) - f_status_t private_f_file_stat_at(const int at_id, const f_string_static_t path, const int flag, struct stat * const file_stat) { + f_status_t private_f_file_stat_at(const f_file_t file, const f_string_static_t path, const int flag, struct stat * const file_stat) { - if (fstatat(at_id, path.string, file_stat, flag) < 0) { + if (fstatat(file.id, path.string, file_stat, flag) < 0) { if (errno == EACCES) return F_status_set_error(F_access_denied); if (errno == EBADF) return F_status_set_error(F_directory_descriptor); if (errno == EFAULT) return F_status_set_error(F_buffer); @@ -714,9 +724,9 @@ extern "C" { #endif // !defined(_di_f_file_stat_at_) || !defined(_di_f_file_copy_at_) #if !defined(_di_f_file_stat_by_id_) || !defined(_di_f_file_size_by_id_) - f_status_t private_f_file_stat_by_id(const int id, struct stat * const file_stat) { + f_status_t private_f_file_stat_by_id(const f_file_t file, struct stat * const file_stat) { - if (fstat(id, file_stat) < 0) { + if (fstat(file.id, file_stat) < 0) { if (errno == EACCES) return F_status_set_error(F_access_denied); if (errno == EBADF) return F_status_set_error(F_file_descriptor); if (errno == EFAULT) return F_status_set_error(F_buffer); diff --git a/level_0/f_file/c/private-file.h b/level_0/f_file/c/private-file.h index d7486e2..1be050d 100644 --- a/level_0/f_file/c/private-file.h +++ b/level_0/f_file/c/private-file.h @@ -20,12 +20,9 @@ extern "C" { * * Intended to be shared to each of the different implementation variations. * - * @param flush - * If TRUE, then perform flush before closing. - * If FALSE, then do not perform flush before closing. - * @param id - * The file descriptor. - * The value gets set to -1. + * @param file + * The file to close. + * The file descriptor gets set to -1. * * @return * F_none on success. @@ -40,14 +37,13 @@ extern "C" { * F_space_not (with error bit) if file system is out of space (or file system quota is reached). * * @see close() - * @see fsync() * * @see f_file_close() * @see f_file_copy() * @see f_file_stream_close() */ #if !defined(_di_f_file_close_) || !defined(_di_f_file_copy_) || !defined(_di_f_file_stream_close_) - extern f_status_t private_f_file_close(const bool flush, int * const id) F_attribute_visibility_internal_d; + extern f_status_t private_f_file_close(f_file_t * const file) F_attribute_visibility_internal_d; #endif // !defined(_di_f_file_close_) || !defined(_di_f_file_copy_) || !defined(_di_f_file_stream_close_) /** @@ -152,8 +148,8 @@ extern "C" { * * Intended to be shared to each of the different implementation variations. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param file * The data related to the file being opened. * This will be updated with the file descriptor. @@ -191,7 +187,7 @@ extern "C" { * @see f_file_create_at() */ #if !defined(_di_f_file_create_at_) - extern f_status_t private_f_file_create_at(const int at_id, const f_string_static_t path, const mode_t mode, const bool exclusive) F_attribute_visibility_internal_d; + extern f_status_t private_f_file_create_at(const f_file_t directory, const f_string_static_t path, const mode_t mode, const bool exclusive) F_attribute_visibility_internal_d; #endif // !defined(_di_f_file_create_at_) /** @@ -236,8 +232,8 @@ extern "C" { * * Intended to be shared to each of the different implementation variations. * - * @param at_id - * The file descriptor in which the directory will be created within. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param mode @@ -268,7 +264,7 @@ extern "C" { * @see f_file_copy_at() */ #if !defined(_di_f_file_copy_at_) - extern f_status_t private_f_file_create_directory_at(const int at_id, const f_string_static_t path, const mode_t mode) F_attribute_visibility_internal_d; + extern f_status_t private_f_file_create_directory_at(const f_file_t directory, const f_string_static_t path, const mode_t mode) F_attribute_visibility_internal_d; #endif // !defined(_di_f_file_copy_at_) /** @@ -311,8 +307,8 @@ extern "C" { * * Intended to be shared to each of the different implementation variations. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param mode @@ -341,7 +337,7 @@ extern "C" { * @see f_file_copy_at() */ #if !defined(_di_f_file_create_fifo_at_) || !defined(_di_f_file_copy_at_) - extern f_status_t private_f_file_create_fifo_at(const int at_id, const f_string_static_t path, const mode_t mode) F_attribute_visibility_internal_d; + extern f_status_t private_f_file_create_fifo_at(const f_file_t directory, const f_string_static_t path, const mode_t mode) F_attribute_visibility_internal_d; #endif // !defined(_di_f_file_create_fifo_at_) || !defined(_di_f_file_copy_at_) /** @@ -391,8 +387,8 @@ extern "C" { * * Intended to be shared to each of the different implementation variations. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param mode @@ -428,7 +424,7 @@ extern "C" { * @see f_file_create_node_at() */ #if !defined(_di_f_file_create_device_at_) || !defined(_di_f_file_create_node_at_) || !defined(_di_f_file_copy_at_) - extern f_status_t private_f_file_create_node_at(const int at_id, const f_string_static_t path, const mode_t mode, const dev_t device) F_attribute_visibility_internal_d; + extern f_status_t private_f_file_create_node_at(const f_file_t directory, const f_string_static_t path, const mode_t mode, const dev_t device) F_attribute_visibility_internal_d; #endif // !defined(_di_f_file_create_device_at_) || !defined(_di_f_file_create_node_at_) || !defined(_di_f_file_copy_at_) /** @@ -436,8 +432,8 @@ extern "C" { * * Intended to be shared to each of the different implementation variations. * - * @param id - * The file descriptor. + * @param file + * The file to flush. * * @return * F_none is returned on success. @@ -450,16 +446,18 @@ extern "C" { * F_supported_not (with error bit) if the file system or file type does not support flushing. * F_file_synchronize (with error bit) on any other failure. * - * @see fsync() - * + * @see f_file_clone_at() * @see f_file_close() * @see f_file_copy() + * @see f_file_copy_at() + * @see f_file_create() + * @see f_file_create_at() * @see f_file_flush() * @see f_file_stream_close() */ -#if !defined(_di_f_file_flush_) || !defined(_di_f_file_close_) || !defined(_di_f_file_copy_) || !defined(_di_f_file_stream_close_) - extern f_status_t private_f_file_flush(const int id) F_attribute_visibility_internal_d; -#endif // !defined(_di_f_file_flush_) || !defined(_di_f_file_close_) || !defined(_di_f_file_copy_) || !defined(_di_f_file_stream_close_) +#if !defined(_di_f_file_clone_at_) || !defined(_di_f_file_close_) || !defined(_di_f_file_copy_) || !defined(_di_f_file_copy_at_) || !defined(_di_f_file_create_) || !defined(_di_f_file_create_at_) || !defined(_di_f_file_flush_) || !defined(_di_f_file_stream_close_) + extern f_status_t private_f_file_flush(const f_file_t file) F_attribute_visibility_internal_d; +#endif // !defined(_di_f_file_clone_at_) || !defined(_di_f_file_close_) || !defined(_di_f_file_copy_) || !defined(_di_f_file_copy_at_) || !defined(_di_f_file_create_) || !defined(_di_f_file_create_at_) || !defined(_di_f_file_flush_) || !defined(_di_f_file_stream_close_) /** * Private implementation of f_file_link(). @@ -505,8 +503,8 @@ extern "C" { * * Intended to be shared to each of the different implementation variations. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which point point path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which point point path is relative to. * @param target * A path that the link points to. * @param point @@ -538,7 +536,7 @@ extern "C" { * @see f_file_link_at() */ #if !defined(_di_f_file_link_at_) - extern f_status_t private_f_file_link_at(const int at_id, const f_string_static_t target, const f_string_static_t point) F_attribute_visibility_internal_d; + extern f_status_t private_f_file_link_at(const f_file_t directory, const f_string_static_t target, const f_string_static_t point) F_attribute_visibility_internal_d; #endif // !defined(_di_f_file_link_at_) /** @@ -585,8 +583,8 @@ extern "C" { * * Intended to be shared to each of the different implementation variations. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param size @@ -619,7 +617,7 @@ extern "C" { * @see f_string_dynamic_terminate_after() */ #if !defined(_di_f_file_link_read_at_) || !defined(_di_f_file_copy_at_) - extern f_status_t private_f_file_link_read_at(const int at_id, const f_string_static_t path, const off_t size, f_string_dynamic_t * const target) F_attribute_visibility_internal_d; + extern f_status_t private_f_file_link_read_at(const f_file_t directory, const f_string_static_t path, const off_t size, f_string_dynamic_t * const target) F_attribute_visibility_internal_d; #endif // !defined(_di_f_file_link_read_at_) || !defined(_di_f_file_copy_at_) /** @@ -661,8 +659,8 @@ extern "C" { * * Intended to be shared to each of the different implementation variations. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param mode @@ -688,7 +686,7 @@ extern "C" { * @see f_file_mode_set_at() */ #if !defined(_di_f_file_mode_set_at_) - extern f_status_t private_f_file_mode_set_at(const int at_id, const f_string_static_t path, const mode_t mode) F_attribute_visibility_internal_d; + extern f_status_t private_f_file_mode_set_at(const f_file_t directory, const f_string_static_t path, const mode_t mode) F_attribute_visibility_internal_d; #endif // !defined(_di_f_file_mode_set_at_) /** @@ -727,8 +725,8 @@ extern "C" { * * Intended to be shared to each of the different implementation variations. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param mode @@ -754,7 +752,7 @@ extern "C" { * @see f_file_copy_at() */ #if !defined(_di_f_file_copy_at_) || !defined(_di_f_file_clone_at_) || !defined(_di_f_file_open_at_) || !defined(_di_f_file_copy_at_) - extern f_status_t private_f_file_open_at(const int at_id, const f_string_static_t path, const mode_t mode, f_file_t * const file) F_attribute_visibility_internal_d; + extern f_status_t private_f_file_open_at(const f_file_t directory, const f_string_static_t path, const mode_t mode, f_file_t * const file) F_attribute_visibility_internal_d; #endif // !defined(_di_f_file_copy_at_) || !defined(_di_f_file_clone_at_) || !defined(_di_f_file_open_at_) || !defined(_di_f_file_copy_at_) /** @@ -804,8 +802,8 @@ extern "C" { * * Intended to be shared to each of the different implementation variations. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param path * The path file name. * @param uid @@ -838,7 +836,7 @@ extern "C" { * @see f_file_role_change_at() */ #if !defined(_di_f_file_role_change_at_) - extern f_status_t private_f_file_role_change_at(const int at_id, const f_string_static_t path, const uid_t uid, const gid_t gid, const int flag) F_attribute_visibility_internal_d; + extern f_status_t private_f_file_role_change_at(const f_file_t directory, const f_string_static_t path, const uid_t uid, const gid_t gid, const int flag) F_attribute_visibility_internal_d; #endif // !defined(_di_f_file_role_change_at_) /** @@ -884,8 +882,8 @@ extern "C" { * * Intended to be shared to each of the different implementation variations. * - * @param at_id - * The parent directory, as an open directory file descriptor, in which path is relative to. + * @param directory + * The parent directory, via an open directory file descriptor, in which path is relative to. * @param file_name * The name of the file. * @param flag @@ -912,7 +910,7 @@ extern "C" { * @see f_file_touch_at() */ #if !defined(_di_f_file_stat_at_) || !defined(_di_f_file_exists_at_) || !defined(_di_f_file_touch_at_) - extern f_status_t private_f_file_stat_at(const int at_id, const f_string_static_t file_name, const int flag, struct stat * const file_stat) F_attribute_visibility_internal_d; + extern f_status_t private_f_file_stat_at(const f_file_t directory, const f_string_static_t file_name, const int flag, struct stat * const file_stat) F_attribute_visibility_internal_d; #endif // !defined(_di_f_file_stat_at_) || !defined(_di_f_file_exists_at_) || !defined(_di_f_file_touch_at_) /** @@ -920,8 +918,8 @@ extern "C" { * * Intended to be shared to each of the different implementation variations. * - * @param id - * The file descriptor. + * @param file + * The file. * @param file_stat * The statistics read. * @@ -943,7 +941,7 @@ extern "C" { * @see f_file_stat_by_id() */ #if !defined(_di_f_file_stat_by_id_) || !defined(_di_f_file_size_by_id_) - extern f_status_t private_f_file_stat_by_id(const int id, struct stat * const file_stat) F_attribute_visibility_internal_d; + extern f_status_t private_f_file_stat_by_id(const f_file_t file, struct stat * const file_stat) F_attribute_visibility_internal_d; #endif // !defined(_di_f_file_stat_by_id_) || !defined(_di_f_file_size_by_id_) /** @@ -972,7 +970,6 @@ extern "C" { * * @param file * The file to write to. - * The file must already be open. * @param buffer * The string to write to the file. * @param total @@ -1007,7 +1004,6 @@ extern "C" { * * @param file * The file to write to. - * The file must already be open. * @param buffer * The string to write to the file. * @param total diff --git a/level_0/f_pipe/c/pipe.c b/level_0/f_pipe/c/pipe.c index d74c002..492ae2b 100644 --- a/level_0/f_pipe/c/pipe.c +++ b/level_0/f_pipe/c/pipe.c @@ -4,81 +4,50 @@ extern "C" { #endif -#ifndef _di_f_pipe_input_exists_ - f_status_t f_pipe_input_exists(void) { - - struct stat st_info; - - memset(&st_info, 0, sizeof(struct stat)); - - if (fstat(F_type_descriptor_input_d, &st_info) != 0) { - return F_status_set_error(F_file_stat); - } - - if (S_ISFIFO(st_info.st_mode)) { - return F_true; - } - - return F_false; - } -#endif // _di_f_pipe_input_exists_ - -#ifndef _di_f_pipe_warning_exists_ - f_status_t f_pipe_warning_exists(void) { +#ifndef _di_f_pipe_error_exists_ + f_status_t f_pipe_error_exists(void) { struct stat st_info; memset(&st_info, 0, sizeof(struct stat)); - if (fstat(F_type_descriptor_warning_d, &st_info) != 0) { + if (fstat(F_type_descriptor_error_d, &st_info) != 0) { return F_status_set_error(F_file_stat); } - if (S_ISFIFO(st_info.st_mode)) { - return F_true; - } - - return F_false; + return S_ISFIFO(st_info.st_mode) ? F_true : F_false; } -#endif // _di_f_pipe_warning_exists_ +#endif // _di_f_pipe_error_exists_ -#ifndef _di_f_pipe_error_exists_ - f_status_t f_pipe_error_exists(void) { +#ifndef _di_f_pipe_input_exists_ + f_status_t f_pipe_input_exists(void) { struct stat st_info; memset(&st_info, 0, sizeof(struct stat)); - if (fstat(F_type_descriptor_error_d, &st_info) != 0) { + if (fstat(F_type_descriptor_input_d, &st_info) != 0) { return F_status_set_error(F_file_stat); } - if (S_ISFIFO(st_info.st_mode)) { - return F_true; - } - - return F_false; + return S_ISFIFO(st_info.st_mode) ? F_true : F_false; } -#endif // _di_f_pipe_error_exists_ +#endif // _di_f_pipe_input_exists_ -#ifndef _di_f_pipe_debug_exists_ - f_status_t f_pipe_debug_exists(void) { +#ifndef _di_f_pipe_output_exists_ + f_status_t f_pipe_output_exists(void) { struct stat st_info; memset(&st_info, 0, sizeof(struct stat)); - if (fstat(F_type_descriptor_debug_d, &st_info) != 0) { + if (fstat(F_type_descriptor_output_d, &st_info) != 0) { return F_status_set_error(F_file_stat); } - if (S_ISFIFO(st_info.st_mode)) { - return F_true; - } - - return F_false; + return S_ISFIFO(st_info.st_mode) ? F_true : F_false; } -#endif // _di_f_pipe_debug_exists_ +#endif // _di_f_pipe_output_exists_ #ifdef __cplusplus } // extern "C" diff --git a/level_0/f_pipe/c/pipe.h b/level_0/f_pipe/c/pipe.h index 69d0144..4372543 100644 --- a/level_0/f_pipe/c/pipe.h +++ b/level_0/f_pipe/c/pipe.h @@ -24,23 +24,7 @@ extern "C" { #endif /** - * Identify whether or not the standard input pipe source contains piped data. - * - * @return - * F_true if there is piped data. - * F_false if there is no piped data. - * F_file_stat (with error bit) on stat() error. - * - * @see fstat() - */ -#ifndef _di_f_pipe_input_exists_ - extern f_status_t f_pipe_input_exists(void); -#endif // _di_f_pipe_input_exists_ - -/** - * Identify whether or not the standard warning pipe contains piped data. - * - * For most systems, standard warning does not exist and instead maps to standard output. + * Identify whether or not the standard error pipe source contains piped data. * * @return * F_true if there is piped data. @@ -50,12 +34,12 @@ extern "C" { * * @see fstat() */ -#ifndef _di_f_pipe_warning_exists_ - extern f_status_t f_pipe_warning_exists(void); -#endif // _di_f_pipe_warning_exists_ +#ifndef _di_f_pipe_error_exists_ + extern f_status_t f_pipe_error_exists(void); +#endif // _di_f_pipe_error_exists_ /** - * Identify whether or not the standard error pipe source contains piped data. + * Identify whether or not the standard input pipe source contains piped data. * * @return * F_true if there is piped data. @@ -65,14 +49,14 @@ extern "C" { * * @see fstat() */ -#ifndef _di_f_pipe_error_exists_ - extern f_status_t f_pipe_error_exists(void); -#endif // _di_f_pipe_error_exists_ +#ifndef _di_f_pipe_input_exists_ + extern f_status_t f_pipe_input_exists(void); +#endif // _di_f_pipe_input_exists_ /** - * Identify whether or not the standard debug pipe source contains piped data. + * Identify whether or not the standard output pipe source contains piped data. * - * For most systems, standard debug does not exist and instead maps to standard output. + * For most systems, standard output does not exist and instead maps to standard output. * * @return * F_false if there is no piped data. @@ -82,9 +66,9 @@ extern "C" { * * @see fstat() */ -#ifndef _di_f_pipe_debug_exists_ - extern f_status_t f_pipe_debug_exists(void); -#endif // _di_f_pipe_debug_exists_ +#ifndef _di_f_pipe_output_exists_ + extern f_status_t f_pipe_output_exists(void); +#endif // _di_f_pipe_output_exists_ #ifdef __cplusplus } // extern "C" diff --git a/level_0/f_type/c/type.h b/level_0/f_type/c/type.h index b640129..bce375d 100644 --- a/level_0/f_type/c/type.h +++ b/level_0/f_type/c/type.h @@ -280,21 +280,24 @@ extern "C" { /** * Standard Input/Output types. * - * For most systems, there is no standard warning nor is there a standard debug. - * Therefore, these will map to standard output. + * F_type_*_d: + * - error: Standard error stream. + * - input: Standard input stream. + * - output: Standard output stream. + * + * F_type_descriptor_*_d: + * - error: Standard error file descriptor. + * - input: Standard input file descriptor. + * - output: Standard output file descriptor. */ #ifndef _di_f_type_input_output_ - #define F_type_debug_d stdout - #define F_type_error_d stderr - #define F_type_input_d stdin - #define F_type_output_d stdout - #define F_type_warning_d stdout - - #define F_type_descriptor_debug_d STDOUT_FILENO - #define F_type_descriptor_error_d STDERR_FILENO - #define F_type_descriptor_input_d STDIN_FILENO - #define F_type_descriptor_output_d STDOUT_FILENO - #define F_type_descriptor_warning_d STDOUT_FILENO + #define F_type_error_d stderr + #define F_type_input_d stdin + #define F_type_output_d stdout + + #define F_type_descriptor_error_d STDERR_FILENO + #define F_type_descriptor_input_d STDIN_FILENO + #define F_type_descriptor_output_d STDOUT_FILENO #endif // _di_f_type_input_output_ /** diff --git a/level_1/fl_control_group/c/control_group.c b/level_1/fl_control_group/c/control_group.c index e57c7ad..9df8727 100644 --- a/level_1/fl_control_group/c/control_group.c +++ b/level_1/fl_control_group/c/control_group.c @@ -47,9 +47,9 @@ extern "C" { if (F_status_is_error(status)) break; fprintf(file.stream, "%d", id); - fflush(file.stream); - status = f_file_stream_close(F_true, &file); + f_file_stream_flush(&file); + status = f_file_stream_close(&file); if (F_status_is_error(status)) break; } // for diff --git a/level_1/fl_directory/c/private-directory.c b/level_1/fl_directory/c/private-directory.c index 1ff7540..949f719 100644 --- a/level_1/fl_directory/c/private-directory.c +++ b/level_1/fl_directory/c/private-directory.c @@ -465,9 +465,9 @@ extern "C" { return F_status_set_error(F_directory_open); } - int parent_fd = dirfd(parent); + const f_file_t directory = macro_f_file_t_initialize_id(dirfd(parent)); - if (parent_fd < 0) { + if (directory.id < 0) { closedir(parent); if (errno == EINVAL) return F_status_set_error(F_directory_stream); @@ -506,7 +506,7 @@ extern "C" { memset(&file_stat, 0, sizeof(struct stat)); - status = f_file_stat_at(parent_fd, name_directory, dereference ? 0 : AT_SYMLINK_NOFOLLOW, &file_stat); + status = f_file_stat_at(directory, name_directory, dereference ? 0 : AT_SYMLINK_NOFOLLOW, &file_stat); if (F_status_is_error(status)) break; mode = macro_f_file_type_get(file_stat.st_mode); diff --git a/level_1/fl_print/c/print/common.c b/level_1/fl_print/c/print/common.c index 4807ed8..290e9ee 100644 --- a/level_1/fl_print/c/print/common.c +++ b/level_1/fl_print/c/print/common.c @@ -4,11 +4,11 @@ extern "C" { #endif -#ifndef _di_fl_print_t_ +#ifndef _di_fl_print_s_ const f_string_static_t fl_print_debug_s = macro_f_string_static_t_initialize(FL_print_debug_s, 0, FL_print_debug_s_length); const f_string_static_t fl_print_error_s = macro_f_string_static_t_initialize(FL_print_error_s, 0, FL_print_error_s_length); const f_string_static_t fl_print_warning_s = macro_f_string_static_t_initialize(FL_print_warning_s, 0, FL_print_warning_s_length); -#endif // _di_fl_print_t_ +#endif // _di_fl_print_s_ #ifdef __cplusplus } // extern "C" diff --git a/level_1/fl_print/c/print/common.h b/level_1/fl_print/c/print/common.h index f10eab2..5f2d84b 100644 --- a/level_1/fl_print/c/print/common.h +++ b/level_1/fl_print/c/print/common.h @@ -17,17 +17,14 @@ extern "C" { #endif /** - * Structure for facilitating colored or other managed printing. + * Special print strings. * - * to: The file to print to. - * verbosity: The verbosity mode. - * prefix: An optional prefix string for displaying before any special messages. - * suffix: An optional suffix string for displaying after any special messages. - * context: The color codes for the entire error message. - * notable: The color codes for a part of the message to make more visible. - * set: An optional pointer to a set of all available color contexts for explicit use. + * fl_print_*_s: + * - debug: A string used as part of the message when printing a debug. + * - error: A string used as part of the message when printing an error. + * - warning: A string used as part of the message when printing a warning. */ -#ifndef _di_fl_print_t_ +#ifndef _di_fl_print_s_ #define FL_print_debug_s "DEBUG: " #define FL_print_error_s "ERROR: " #define FL_print_warning_s "WARNING: " @@ -39,7 +36,22 @@ extern "C" { extern const f_string_static_t fl_print_debug_s; extern const f_string_static_t fl_print_error_s; extern const f_string_static_t fl_print_warning_s; +#endif // _di_fl_print_s_ +/** + * Structure for facilitating colored or other managed printing. + * + * to: The file to print to. + * verbosity: The verbosity mode. + * + * prefix: An optional prefix string for displaying before any special messages. + * suffix: An optional suffix string for displaying after any special messages. + * + * context: The color codes for the entire error message. + * notable: The color codes for a part of the message to make more visible. + * set: An optional pointer to a set of all available color contexts for explicit use. + */ +#ifndef _di_fl_print_t_ typedef struct { f_file_t to; uint8_t verbosity; @@ -62,13 +74,16 @@ extern "C" { 0, \ } - #define macro_fl_print_t_initialize(to, verbosity, prefix, suffix, context, notable, set) { to, verbosity, prefix, suffix, context, notable, set } - #define macro_fl_print_t_initialize_debug() macro_fl_print_t_initialize(macro_f_file_t_initialize2(F_type_debug_d, F_type_descriptor_debug_d, F_file_flag_write_only_d), f_console_verbosity_normal_e, fl_print_debug_s, f_string_static_t_initialize, f_color_set_t_initialize, f_color_set_t_initialize, 0) - #define macro_fl_print_t_initialize_debug2(suffix, set) macro_fl_print_t_initialize(macro_f_file_t_initialize2(F_type_debug_d, F_type_descriptor_debug_d, F_file_flag_write_only_d), f_console_verbosity_normal_e, fl_print_debug_s, suffix, f_color_set_t_initialize, f_color_set_t_initialize, set) - #define macro_fl_print_t_initialize_error() macro_fl_print_t_initialize(macro_f_file_t_initialize2(F_type_error_d, F_type_descriptor_error_d, F_file_flag_write_only_d), f_console_verbosity_normal_e, fl_print_error_s, f_string_static_t_initialize, f_color_set_t_initialize, f_color_set_t_initialize, 0) - #define macro_fl_print_t_initialize_error2(suffix, set) macro_fl_print_t_initialize(macro_f_file_t_initialize2(F_type_error_d, F_type_descriptor_error_d, F_file_flag_write_only_d), f_console_verbosity_normal_e, fl_print_error_s, suffix, f_color_set_t_initialize, f_color_set_t_initialize, set) - #define macro_fl_print_t_initialize_warning() macro_fl_print_t_initialize(macro_f_file_t_initialize2(F_type_warning_d, F_type_descriptor_warning_d, F_file_flag_write_only_d), f_console_verbosity_normal_e, fl_print_warning_s, f_string_static_t_initialize, f_color_set_t_initialize, f_color_set_t_initialize, 0) - #define macro_fl_print_t_initialize_warning2(suffix, set) macro_fl_print_t_initialize(macro_f_file_t_initialize2(F_type_warning_d, F_type_descriptor_warning_d, F_file_flag_write_only_d), f_console_verbosity_normal_e, fl_print_warning_s, suffix, f_color_set_t_initialize, f_color_set_t_initialize, set) + #define macro_fl_print_t_initialize(to, verbosity, prefix, suffix, context, notable, set) { \ + to, verbosity, prefix, suffix, context, notable, set \ + } + + #define macro_fl_print_t_initialize_debug() macro_fl_print_t_initialize(macro_f_file_t_initialize2(F_type_output_d, F_type_descriptor_output_d, F_file_flag_write_only_d), f_console_verbosity_normal_e, fl_print_debug_s, f_string_static_t_initialize, f_color_set_t_initialize, f_color_set_t_initialize, 0) + #define macro_fl_print_t_initialize_debug2(suffix, set) macro_fl_print_t_initialize(macro_f_file_t_initialize2(F_type_output_d, F_type_descriptor_output_d, F_file_flag_write_only_d), f_console_verbosity_normal_e, fl_print_debug_s, suffix, f_color_set_t_initialize, f_color_set_t_initialize, set) + #define macro_fl_print_t_initialize_error() macro_fl_print_t_initialize(macro_f_file_t_initialize2(F_type_error_d, F_type_descriptor_error_d, F_file_flag_write_only_d), f_console_verbosity_normal_e, fl_print_error_s, f_string_static_t_initialize, f_color_set_t_initialize, f_color_set_t_initialize, 0) + #define macro_fl_print_t_initialize_error2(suffix, set) macro_fl_print_t_initialize(macro_f_file_t_initialize2(F_type_error_d, F_type_descriptor_error_d, F_file_flag_write_only_d), f_console_verbosity_normal_e, fl_print_error_s, suffix, f_color_set_t_initialize, f_color_set_t_initialize, set) + #define macro_fl_print_t_initialize_warning() macro_fl_print_t_initialize(macro_f_file_t_initialize2(F_type_output_d, F_type_descriptor_output_d, F_file_flag_write_only_d), f_console_verbosity_normal_e, fl_print_warning_s, f_string_static_t_initialize, f_color_set_t_initialize, f_color_set_t_initialize, 0) + #define macro_fl_print_t_initialize_warning2(suffix, set) macro_fl_print_t_initialize(macro_f_file_t_initialize2(F_type_output_d, F_type_descriptor_output_d, F_file_flag_write_only_d), f_console_verbosity_normal_e, fl_print_warning_s, suffix, f_color_set_t_initialize, f_color_set_t_initialize, set) #endif // _di_fl_print_t_ #ifdef __cplusplus