file_systems/fs_ops_support: Add open_mode_to_access.

This is duplicated across multiple filesystems, and could probably be
used in more still.

Adjusted only BFS, EXT2, and NTFS in this commit, as they are the ones
which make use of fs_ops_support.h already and thus need to be modified
to avoid duplicate-definition errors.

Also tweak next_dirent to support being built under fs_shell.
(Possibly we should define ASSERT there, though?)
This commit is contained in:
Augustin Cavalier
2023-01-27 23:53:53 -05:00
parent dc95ec5777
commit 35b40030a7
6 changed files with 23 additions and 48 deletions
@@ -14,6 +14,22 @@
#endif #endif
/*! Converts a given open mode (e.g. O_RDONLY) into access modes (e.g. R_OK).
*/
static inline int
open_mode_to_access(int openMode)
{
openMode &= O_RWMASK;
if (openMode == O_RDONLY)
return R_OK;
if (openMode == O_WRONLY)
return W_OK;
if (openMode == O_RDWR)
return R_OK | W_OK;
return 0;
}
/*! Computes and assigns `dirent->d_reclen`, adjusts `bufferRemaining` accordingly, /*! Computes and assigns `dirent->d_reclen`, adjusts `bufferRemaining` accordingly,
* and either advances to the next buffer, or returns NULL if no space remains. * and either advances to the next buffer, or returns NULL if no space remains.
*/ */
@@ -21,7 +37,9 @@ static inline struct dirent*
next_dirent(struct dirent* dirent, size_t nameLength, size_t& bufferRemaining) next_dirent(struct dirent* dirent, size_t nameLength, size_t& bufferRemaining)
{ {
const size_t reclen = offsetof(struct dirent, d_name) + nameLength + 1; const size_t reclen = offsetof(struct dirent, d_name) + nameLength + 1;
#ifdef ASSERT
ASSERT(reclen <= bufferRemaining); ASSERT(reclen <= bufferRemaining);
#endif
dirent->d_reclen = reclen; dirent->d_reclen = reclen;
const size_t roundedReclen = ROUNDUP(reclen, alignof(struct dirent)); const size_t roundedReclen = ROUNDUP(reclen, alignof(struct dirent));
+2 -16
View File
@@ -8,6 +8,8 @@
#include "system_dependencies.h" #include "system_dependencies.h"
#include <file_systems/fs_ops_support.h>
#include "bfs.h" #include "bfs.h"
@@ -131,20 +133,4 @@ is_directory(int mode)
} }
/*! Converts the open mode, the open flags given to bfs_open(), into
access modes, e.g. since O_RDONLY requires read access to the
file, it will be converted to R_OK.
*/
inline int
open_mode_to_access(int openMode)
{
openMode &= O_RWMASK;
if (openMode == O_RDONLY)
return R_OK;
if (openMode == O_WRONLY)
return W_OK;
return R_OK | W_OK;
}
#endif // UTILITY_H #endif // UTILITY_H
@@ -19,8 +19,6 @@
#include "bfs_control.h" #include "bfs_control.h"
#include "bfs_disk_system.h" #include "bfs_disk_system.h"
#include <file_systems/fs_ops_support.h>
// TODO: temporary solution as long as there is no public I/O requests API // TODO: temporary solution as long as there is no public I/O requests API
#ifndef FS_SHELL #ifndef FS_SHELL
# include <io_requests.h> # include <io_requests.h>
+3 -16
View File
@@ -7,6 +7,9 @@
#include "ext2.h" #include "ext2.h"
#include <file_systems/fs_ops_support.h>
enum inode_type { enum inode_type {
S_DIRECTORY = S_IFDIR, S_DIRECTORY = S_IFDIR,
S_FILE = S_IFREG, S_FILE = S_IFREG,
@@ -20,20 +23,4 @@ enum inode_type {
}; };
/*! Converts the open mode, the open flags given to bfs_open(), into
access modes, e.g. since O_RDONLY requires read access to the
file, it will be converted to R_OK.
*/
inline int
open_mode_to_access(int openMode)
{
openMode &= O_RWMASK;
if (openMode == O_RDONLY)
return R_OK;
if (openMode == O_WRONLY)
return W_OK;
return R_OK | W_OK;
}
#endif // UTILITY_H #endif // UTILITY_H
@@ -12,7 +12,6 @@
#include <string.h> #include <string.h>
#include <AutoDeleter.h> #include <AutoDeleter.h>
#include <file_systems/fs_ops_support.h>
#include <fs_cache.h> #include <fs_cache.h>
#include <fs_info.h> #include <fs_info.h>
#include <io_requests.h> #include <io_requests.h>
@@ -713,19 +713,6 @@ fs_write_stat(fs_volume* _volume, fs_vnode* _node, const struct stat* stat, uint
} }
static inline int
open_mode_to_access(int openMode)
{
if ((openMode & O_RWMASK) == O_RDONLY)
return R_OK;
if ((openMode & O_RWMASK) == O_WRONLY)
return W_OK;
if ((openMode & O_RWMASK) == O_RDWR)
return R_OK | W_OK;
return 0;
}
static status_t static status_t
fs_generic_create(fs_volume* _volume, vnode* directory, const char* name, int mode, fs_generic_create(fs_volume* _volume, vnode* directory, const char* name, int mode,
ino_t* _inode) ino_t* _inode)