File indexing completed on 2025-05-11 08:24:15
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 #ifdef HAVE_CONFIG_H
0036 #include "config.h"
0037 #endif
0038
0039 #include <sys/types.h>
0040 #include <sys/stat.h>
0041 #include <rtems/chain.h>
0042 #include <rtems/seterr.h>
0043 #include <fcntl.h>
0044 #include <unistd.h>
0045 #include <errno.h>
0046 #include <stdlib.h>
0047 #include <string.h>
0048
0049 #include <rtems/libio_.h>
0050
0051 typedef struct {
0052 rtems_chain_node node;
0053 rtems_filesystem_table_t entry;
0054 } filesystem_node;
0055
0056 static RTEMS_CHAIN_DEFINE_EMPTY(filesystem_chain);
0057
0058 bool rtems_filesystem_iterate(
0059 rtems_per_filesystem_routine routine,
0060 void *routine_arg
0061 )
0062 {
0063 rtems_chain_control *chain = &filesystem_chain;
0064 const rtems_filesystem_table_t *table_entry = &rtems_filesystem_table [0];
0065 rtems_chain_node *node = NULL;
0066 bool stop = false;
0067
0068 while ( table_entry->type && !stop ) {
0069 stop = (*routine)( table_entry, routine_arg );
0070 ++table_entry;
0071 }
0072
0073 if ( !stop ) {
0074 rtems_libio_lock();
0075 for (
0076 node = rtems_chain_first( chain );
0077 !rtems_chain_is_tail( chain, node ) && !stop;
0078 node = rtems_chain_next( node )
0079 ) {
0080 const filesystem_node *fsn = (filesystem_node *) node;
0081
0082 stop = (*routine)( &fsn->entry, routine_arg );
0083 }
0084 rtems_libio_unlock();
0085 }
0086
0087 return stop;
0088 }
0089
0090 typedef struct {
0091 const char *type;
0092 rtems_filesystem_fsmount_me_t mount_h;
0093 } find_arg;
0094
0095 static bool find_handler(const rtems_filesystem_table_t *entry, void *arg)
0096 {
0097 find_arg *fa = arg;
0098
0099 if ( strcmp( entry->type, fa->type ) != 0 ) {
0100 return false;
0101 } else {
0102 fa->mount_h = entry->mount_h;
0103
0104 return true;
0105 }
0106 }
0107
0108 rtems_filesystem_fsmount_me_t
0109 rtems_filesystem_get_mount_handler(
0110 const char *type
0111 )
0112 {
0113 find_arg fa = {
0114 .type = type,
0115 .mount_h = NULL
0116 };
0117
0118 if ( type != NULL ) {
0119 rtems_filesystem_iterate( find_handler, &fa );
0120 }
0121
0122 return fa.mount_h;
0123 }
0124
0125 int
0126 rtems_filesystem_register(
0127 const char *type,
0128 rtems_filesystem_fsmount_me_t mount_h
0129 )
0130 {
0131 rtems_chain_control *chain = &filesystem_chain;
0132 size_t type_size = strlen(type) + 1;
0133 size_t fsn_size = sizeof( filesystem_node ) + type_size;
0134 filesystem_node *fsn = malloc( fsn_size );
0135 char *type_storage = (char *) fsn + sizeof( *fsn );
0136
0137 if ( fsn == NULL )
0138 rtems_set_errno_and_return_minus_one( ENOMEM );
0139
0140 memcpy(type_storage, type, type_size);
0141 fsn->entry.type = type_storage;
0142 fsn->entry.mount_h = mount_h;
0143
0144 rtems_libio_lock();
0145 if ( rtems_filesystem_get_mount_handler( type ) == NULL ) {
0146 rtems_chain_initialize_node( &fsn->node );
0147 rtems_chain_append_unprotected( chain, &fsn->node );
0148 } else {
0149 rtems_libio_unlock();
0150 free( fsn );
0151
0152 rtems_set_errno_and_return_minus_one( EINVAL );
0153 }
0154 rtems_libio_unlock();
0155
0156 return 0;
0157 }
0158
0159 int
0160 rtems_filesystem_unregister(
0161 const char *type
0162 )
0163 {
0164 rtems_chain_control *chain = &filesystem_chain;
0165 rtems_chain_node *node = NULL;
0166
0167 if ( type == NULL ) {
0168 rtems_set_errno_and_return_minus_one( EINVAL );
0169 }
0170
0171 rtems_libio_lock();
0172 for (
0173 node = rtems_chain_first( chain );
0174 !rtems_chain_is_tail( chain, node );
0175 node = rtems_chain_next( node )
0176 ) {
0177 filesystem_node *fsn = (filesystem_node *) node;
0178
0179 if ( strcmp( fsn->entry.type, type ) == 0 ) {
0180 rtems_chain_extract_unprotected( node );
0181 free( fsn );
0182 rtems_libio_unlock();
0183
0184 return 0;
0185 }
0186 }
0187 rtems_libio_unlock();
0188
0189 rtems_set_errno_and_return_minus_one( ENOENT );
0190 }