1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup IMFS |
---|
7 | * |
---|
8 | * @brief IMFS Add a Node |
---|
9 | */ |
---|
10 | |
---|
11 | /* |
---|
12 | * Copyright (C) 2020 embedded brains GmbH (http://www.embedded-brains.de) |
---|
13 | * |
---|
14 | * Redistribution and use in source and binary forms, with or without |
---|
15 | * modification, are permitted provided that the following conditions |
---|
16 | * are met: |
---|
17 | * 1. Redistributions of source code must retain the above copyright |
---|
18 | * notice, this list of conditions and the following disclaimer. |
---|
19 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
20 | * notice, this list of conditions and the following disclaimer in the |
---|
21 | * documentation and/or other materials provided with the distribution. |
---|
22 | * |
---|
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
33 | * POSSIBILITY OF SUCH DAMAGE. |
---|
34 | */ |
---|
35 | |
---|
36 | #if HAVE_CONFIG_H |
---|
37 | #include "config.h" |
---|
38 | #endif |
---|
39 | |
---|
40 | #include <rtems/imfs.h> |
---|
41 | |
---|
42 | int IMFS_add_node( const char *path, IMFS_jnode_t *node, void *arg ) |
---|
43 | { |
---|
44 | mode_t mode; |
---|
45 | rtems_filesystem_eval_path_context_t ctx; |
---|
46 | const rtems_filesystem_location_info_t *currentloc; |
---|
47 | int eval_flags; |
---|
48 | int rv; |
---|
49 | |
---|
50 | mode = node->st_mode; |
---|
51 | mode &= ~rtems_filesystem_umask; |
---|
52 | |
---|
53 | switch (mode & S_IFMT) { |
---|
54 | case S_IFBLK: |
---|
55 | case S_IFCHR: |
---|
56 | case S_IFIFO: |
---|
57 | case S_IFREG: |
---|
58 | case S_IFSOCK: |
---|
59 | break; |
---|
60 | default: |
---|
61 | errno = EINVAL; |
---|
62 | return -1; |
---|
63 | } |
---|
64 | |
---|
65 | eval_flags = RTEMS_FS_FOLLOW_LINK; |
---|
66 | currentloc = rtems_filesystem_eval_path_start( &ctx, path, eval_flags ); |
---|
67 | |
---|
68 | if ( IMFS_is_imfs_instance( currentloc ) ) { |
---|
69 | eval_flags = RTEMS_FS_MAKE | RTEMS_FS_EXCLUSIVE; |
---|
70 | rtems_filesystem_eval_path_set_flags( &ctx, eval_flags ); |
---|
71 | rtems_filesystem_eval_path_set_path( &ctx, node->name, node->namelen ); |
---|
72 | rtems_filesystem_eval_path_continue( &ctx ); |
---|
73 | |
---|
74 | if ( rtems_filesystem_eval_path_get_token( &ctx ) == node->name ) { |
---|
75 | IMFS_assert( |
---|
76 | rtems_filesystem_eval_path_get_tokenlen( &ctx ) == node->namelen |
---|
77 | ); |
---|
78 | node = IMFS_initialize_node( |
---|
79 | node, |
---|
80 | node->control, |
---|
81 | node->name, |
---|
82 | node->namelen, |
---|
83 | mode, |
---|
84 | arg |
---|
85 | ); |
---|
86 | if ( node != NULL ) { |
---|
87 | IMFS_jnode_t *parent; |
---|
88 | |
---|
89 | currentloc = rtems_filesystem_eval_path_get_currentloc( &ctx ); |
---|
90 | parent = currentloc->node_access; |
---|
91 | IMFS_assert( parent != NULL ); |
---|
92 | IMFS_add_to_directory( parent, node ); |
---|
93 | IMFS_mtime_ctime_update( parent ); |
---|
94 | rv = 0; |
---|
95 | } else { |
---|
96 | rv = -1; |
---|
97 | } |
---|
98 | } else { |
---|
99 | if ( rtems_filesystem_eval_path_get_token( &ctx ) != NULL ) { |
---|
100 | rtems_filesystem_eval_path_error( &ctx, EINVAL ); |
---|
101 | } |
---|
102 | |
---|
103 | rv = -1; |
---|
104 | } |
---|
105 | } else { |
---|
106 | rtems_filesystem_eval_path_error( &ctx, ENOTSUP ); |
---|
107 | rv = -1; |
---|
108 | } |
---|
109 | |
---|
110 | rtems_filesystem_eval_path_cleanup( &ctx ); |
---|
111 | return rv; |
---|
112 | } |
---|