if (unlink(path.string) < 0) {
if (errno == EACCES) return F_status_set_error(F_access_denied);
if (errno == EBUSY) return F_status_set_error(F_busy);
+ if (errno == EFAULT) return F_status_set_error(F_buffer);
if (errno == EIO) return F_status_set_error(F_input_output);
if (errno == EISDIR) return F_status_set_error(F_file_type_directory);
if (errno == ELOOP) return F_status_set_error(F_loop);
if (errno == ENAMETOOLONG) return F_status_set_error(F_name);
- if (errno == EFAULT) return F_status_set_error(F_buffer);
if (errno == ENOENT) return F_status_set_error(F_file_found_not);
if (errno == ENOMEM) return F_status_set_error(F_memory_not);
if (errno == ENOTDIR) return F_status_set_error(F_directory_not);
#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) {
+ 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) {
if (!source.used || !destination.used) {
return F_data_not;
}
- if (renameat(at_id, source.string, to_id, destination.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 == EBUSY) return F_status_set_error(F_busy);
- if (errno == EDQUOT) return F_status_set_error(F_filesystem_quota_block);
- if (errno == EFAULT) return F_status_set_error(F_buffer);
- if (errno == EINVAL) return F_status_set_error(F_parameter);
- if (errno == EISDIR) return F_status_set_error(F_file_type_directory);
- if (errno == ELOOP) return F_status_set_error(F_loop);
- if (errno == EMLINK) return F_status_set_error(F_link);
- if (errno == ENAMETOOLONG) return F_status_set_error(F_name);
- if (errno == ENOENT) return F_status_set_error(F_file_found_not);
- if (errno == ENOMEM) return F_status_set_error(F_memory_not);
- if (errno == ENOSPC) return F_status_set_error(F_space_not);
- if (errno == ENOTDIR) return F_status_set_error(F_directory_not);
- if (errno == ENOTEMPTY) return F_status_set_error(F_directory_empty_not);
- if (errno == EEXIST) return F_status_set_error(F_directory_empty_not);
- if (errno == EPERM) return F_status_set_error(F_prohibited);
- if (errno == EROFS) return F_status_set_error(F_read_only);
- if (errno == EXDEV) return F_status_set_error(F_mount);
+ #ifdef _f_file_rename_use_renameat2_
+ if (renameat2(at_id, source.string, to_id, destination.string, flag) < 0) {
+ #else
+ if (renameat(at_id, source.string, to_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);
+ if (errno == EBUSY) return F_status_set_error(F_busy);
+ if (errno == EDQUOT) return F_status_set_error(F_filesystem_quota_block);
+ if (errno == EFAULT) return F_status_set_error(F_buffer);
+ if (errno == EINVAL) return F_status_set_error(F_parameter);
+ if (errno == EISDIR) return F_status_set_error(F_file_type_directory);
+ if (errno == ELOOP) return F_status_set_error(F_loop);
+ if (errno == EMLINK) return F_status_set_error(F_link);
+ if (errno == ENAMETOOLONG) return F_status_set_error(F_name);
+ if (errno == ENOENT) return F_status_set_error(F_file_found_not);
+ if (errno == ENOMEM) return F_status_set_error(F_memory_not);
+ if (errno == ENOSPC) return F_status_set_error(F_space_not);
+ if (errno == ENOTDIR) return F_status_set_error(F_directory_not);
+ if (errno == ENOTEMPTY) return F_status_set_error(F_directory_empty_not);
+ if (errno == EEXIST) return F_status_set_error(F_directory_empty_not);
+ if (errno == EPERM) return F_status_set_error(F_prohibited);
+ if (errno == EROFS) return F_status_set_error(F_read_only);
+ if (errno == EXDEV) return F_status_set_error(F_mount);
- return F_status_set_error(F_failure);
- }
+ return F_status_set_error(F_failure);
+ }
return F_none;
}
#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) {
- if (uid == -1 && gid == -1) {
+ if (uid == -1 && gid == -1 || !path.used) {
return F_data_not;
}
#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) {
#ifndef _di_level_0_parameter_checking_
- if (id <= 0) return F_status_set_error(F_parameter);
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);
#ifndef _di_f_file_size_by_id_
f_status_t f_file_size_by_id(const int id, off_t * const size) {
#ifndef _di_level_0_parameter_checking_
- if (id <= 0) return F_status_set_error(F_parameter);
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);
+ }
+
struct stat stat_file;
memset(&stat_file, 0, sizeof(struct stat));
#ifndef _di_f_file_stat_by_id_
f_status_t f_file_stat_by_id(const int id, struct stat * const stat_file) {
#ifndef _di_level_0_parameter_checking_
- if (id <= 0) return F_status_set_error(F_parameter);
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);
+ }
+
return private_f_file_stat_by_id(id, stat_file);
}
#endif // _di_f_file_stat_by_id_
f_status_t f_file_stream_descriptor(const f_string_static_t mode, f_file_t * const file) {
#ifndef _di_level_0_parameter_checking_
if (!file) return F_status_set_error(F_parameter);
- if (file->id == -1) 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 (mode.string) {
file->stream = fdopen(file->id, mode.string);
}
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_status_set_error(F_file_closed);
+ }
flockfile(file.stream);
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_status_set_error(F_file_closed);
+ }
flockfile(file.stream);
* The path to the file to copy from.
* @param destination
* The path to copy to.
+ * @param flag
+ * Any valid flag, such as F_file_at_path_empty_d, F_file_at_automount_no_d, or F_file_at_symlink_follow_no_d.
+ * The POSIX renameat() doesn't support flag but Linux has a renameat2() that does.
+ * If this is compiled with renameat2 support (), then flag is used otherwise flag is always 0 regardless of this property.
*
* @return
* F_none on success.
* @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);
+ 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);
#endif // _di_f_file_rename_at_
/**
*
* F_access_denied (with error bit) if access to the file was denied.
* F_directory_not (with error bit) on invalid directory.
+ * 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_found_not (with error bit) if the file was not found.
* F_loop (with error bit) on loop error.
* F_memory_not (with error bit) if out of memory.
* F_access_denied (with error bit) on access denied.
* F_buffer (with error bit) if the buffer is invalid.
* F_deadlock (with error bit) if operation would cause a deadlock.
+ * F_file_closed (with error bit) if file is not open.
* 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_type_not_directory (with error bit) if F_NOTIFY was specified and file.id is not a directory.
}
}
- if (gid != -1) {
+ if (result == 0 && gid != -1) {
result = fchownat(at_id, path.string, -1, gid, flag);
if (result < 0 && errno == EPERM) {
if ((dereference ? stat(path.string, file_stat) : lstat(path.string, file_stat)) < 0) {
if (errno == EACCES) return F_status_set_error(F_access_denied);
if (errno == EFAULT) return F_status_set_error(F_buffer);
+ if (errno == EINVAL) return F_status_set_error(F_parameter);
if (errno == ELOOP) return F_status_set_error(F_loop);
if (errno == ENAMETOOLONG) return F_status_set_error(F_name);
if (errno == ENOENT) return F_status_set_error(F_file_found_not);
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);
+ if (errno == EINVAL) return F_status_set_error(F_parameter);
if (errno == ELOOP) return F_status_set_error(F_loop);
if (errno == ENAMETOOLONG) return F_status_set_error(F_name);
if (errno == ENOENT) return F_status_set_error(F_file_found_not);
if (fstat(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);
+ if (errno == EINVAL) return F_status_set_error(F_parameter);
if (errno == ELOOP) return F_status_set_error(F_loop);
if (errno == ENAMETOOLONG) return F_status_set_error(F_name);
- if (errno == ENOENT) return F_file_found_not;
+ if (errno == ENOENT) return F_status_set_error(F_file_found_not);
if (errno == ENOMEM) return F_status_set_error(F_memory_not);
if (errno == ENOTDIR) return F_status_set_error(F_directory_not);
if (errno == EOVERFLOW) return F_status_set_error(F_number_overflow);