Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:17

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup IMFS
0007  *
0008  * @brief IMFS Add a Node
0009  */
0010 
0011 /*
0012  * Copyright (C) 2020 embedded brains GmbH & Co. KG
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #ifdef HAVE_CONFIG_H
0037 #include "config.h"
0038 #endif
0039 
0040 #include <rtems/imfsimpl.h>
0041 
0042 int IMFS_add_node( const char *path, IMFS_jnode_t *node, void *arg )
0043 {
0044   mode_t                                  mode;
0045   rtems_filesystem_eval_path_context_t    ctx;
0046   const rtems_filesystem_location_info_t *currentloc;
0047   int                                     eval_flags;
0048   int                                     rv;
0049 
0050   mode = node->st_mode;
0051   mode &= ~rtems_filesystem_umask;
0052 
0053   switch (mode & S_IFMT) {
0054     case S_IFBLK:
0055     case S_IFCHR:
0056     case S_IFIFO:
0057     case S_IFREG:
0058     case S_IFSOCK:
0059       break;
0060     default:
0061       errno = EINVAL;
0062       return -1;
0063   }
0064 
0065   eval_flags = RTEMS_FS_FOLLOW_LINK;
0066   currentloc = rtems_filesystem_eval_path_start( &ctx, path, eval_flags );
0067 
0068   if ( IMFS_is_imfs_instance( currentloc ) ) {
0069     eval_flags = RTEMS_FS_MAKE | RTEMS_FS_EXCLUSIVE;
0070     rtems_filesystem_eval_path_set_flags( &ctx, eval_flags );
0071     rtems_filesystem_eval_path_set_path( &ctx, node->name, node->namelen );
0072     rtems_filesystem_eval_path_continue( &ctx );
0073 
0074     if ( rtems_filesystem_eval_path_get_token( &ctx ) == node->name ) {
0075       IMFS_assert(
0076         rtems_filesystem_eval_path_get_tokenlen( &ctx ) == node->namelen
0077       );
0078       node = IMFS_initialize_node(
0079         node,
0080         node->control,
0081         node->name,
0082         node->namelen,
0083         mode,
0084         arg
0085       );
0086       if ( node != NULL ) {
0087         IMFS_jnode_t *parent;
0088 
0089         currentloc = rtems_filesystem_eval_path_get_currentloc( &ctx );
0090         parent = currentloc->node_access;
0091         IMFS_assert( parent != NULL );
0092         IMFS_add_to_directory( parent, node );
0093         IMFS_mtime_ctime_update( parent );
0094         rv = 0;
0095       } else {
0096         rv = -1;
0097       }
0098     } else {
0099       if ( rtems_filesystem_eval_path_get_token( &ctx ) != NULL ) {
0100         rtems_filesystem_eval_path_error( &ctx, EINVAL );
0101       }
0102 
0103       rv = -1;
0104     }
0105   } else {
0106     rtems_filesystem_eval_path_error( &ctx, ENOTSUP );
0107     rv = -1;
0108   }
0109 
0110   rtems_filesystem_eval_path_cleanup( &ctx );
0111   return rv;
0112 }