[07a3253d] | 1 | /* |
---|
| 2 | * IMFS_create_node() |
---|
| 3 | * |
---|
| 4 | * Routine to create a new in memory file system node. |
---|
| 5 | * |
---|
[cb4e992] | 6 | * COPYRIGHT (c) 1989-2010. |
---|
[07a3253d] | 7 | * On-Line Applications Research Corporation (OAR). |
---|
| 8 | * |
---|
| 9 | * The license and distribution terms for this file may be |
---|
| 10 | * found in the file LICENSE in this distribution or at |
---|
[cf0539b1] | 11 | * http://www.rtems.com/license/LICENSE. |
---|
[07a3253d] | 12 | * |
---|
| 13 | * $Id$ |
---|
| 14 | */ |
---|
| 15 | |
---|
[d6b1d73] | 16 | #if HAVE_CONFIG_H |
---|
| 17 | #include "config.h" |
---|
| 18 | #endif |
---|
| 19 | |
---|
[07a3253d] | 20 | #include <assert.h> |
---|
| 21 | #include <stdlib.h> |
---|
| 22 | #include <string.h> |
---|
| 23 | #include "imfs.h" |
---|
[c058578] | 24 | #include <rtems/libio_.h> |
---|
[07a3253d] | 25 | |
---|
[cb4e992] | 26 | /* |
---|
| 27 | * Create an IMFS filesystem node of an arbitrary type that is NOT |
---|
| 28 | * the root directory node. |
---|
| 29 | */ |
---|
[07a3253d] | 30 | IMFS_jnode_t *IMFS_create_node( |
---|
[bf95ccb5] | 31 | rtems_filesystem_location_info_t *parent_loc, |
---|
| 32 | IMFS_jnode_types_t type, |
---|
| 33 | const char *name, |
---|
| 34 | mode_t mode, |
---|
| 35 | const IMFS_types_union *info |
---|
[07a3253d] | 36 | ) |
---|
| 37 | { |
---|
| 38 | IMFS_jnode_t *node; |
---|
[cb4e992] | 39 | IMFS_jnode_t *parent; |
---|
[07a3253d] | 40 | IMFS_fs_info_t *fs_info; |
---|
| 41 | |
---|
| 42 | /* |
---|
[cb4e992] | 43 | * MUST have a parent node to call this routine. |
---|
[07a3253d] | 44 | */ |
---|
[cb4e992] | 45 | if ( parent_loc == NULL ) |
---|
[07a3253d] | 46 | return NULL; |
---|
| 47 | |
---|
[68cd765] | 48 | parent = parent_loc->node_access; |
---|
| 49 | fs_info = parent_loc->mt_entry->fs_info; |
---|
| 50 | |
---|
| 51 | /* |
---|
| 52 | * Reject creation of FIFOs if support is disabled. |
---|
| 53 | */ |
---|
| 54 | if ( type == IMFS_FIFO && |
---|
| 55 | fs_info->fifo_handlers == &rtems_filesystem_null_handlers ) |
---|
| 56 | return NULL; |
---|
| 57 | |
---|
[07a3253d] | 58 | /* |
---|
[cb4e992] | 59 | * Allocate filesystem node and fill in basic information |
---|
[07a3253d] | 60 | */ |
---|
[cb4e992] | 61 | node = IMFS_allocate_node( type, name, mode & ~rtems_filesystem_umask ); |
---|
| 62 | if ( !node ) |
---|
| 63 | return NULL; |
---|
[07a3253d] | 64 | |
---|
| 65 | /* |
---|
| 66 | * Set the type specific information |
---|
| 67 | */ |
---|
| 68 | switch (type) { |
---|
| 69 | case IMFS_DIRECTORY: |
---|
[72d2ec4d] | 70 | rtems_chain_initialize_empty(&node->info.directory.Entries); |
---|
[07a3253d] | 71 | break; |
---|
| 72 | |
---|
| 73 | case IMFS_HARD_LINK: |
---|
| 74 | node->info.hard_link.link_node = info->hard_link.link_node; |
---|
| 75 | break; |
---|
| 76 | |
---|
| 77 | case IMFS_SYM_LINK: |
---|
[bf95ccb5] | 78 | node->info.sym_link.name = info->sym_link.name; |
---|
[07a3253d] | 79 | break; |
---|
| 80 | |
---|
| 81 | case IMFS_DEVICE: |
---|
| 82 | node->info.device.major = info->device.major; |
---|
| 83 | node->info.device.minor = info->device.minor; |
---|
| 84 | break; |
---|
| 85 | |
---|
[0ef748fb] | 86 | case IMFS_LINEAR_FILE: |
---|
| 87 | node->info.linearfile.size = 0; |
---|
| 88 | node->info.linearfile.direct = 0; |
---|
| 89 | |
---|
[07a3253d] | 90 | case IMFS_MEMORY_FILE: |
---|
| 91 | node->info.file.size = 0; |
---|
| 92 | node->info.file.indirect = 0; |
---|
| 93 | node->info.file.doubly_indirect = 0; |
---|
| 94 | node->info.file.triply_indirect = 0; |
---|
| 95 | break; |
---|
| 96 | |
---|
[e2324c0] | 97 | case IMFS_FIFO: |
---|
| 98 | node->info.fifo.pipe = NULL; |
---|
| 99 | break; |
---|
| 100 | |
---|
[07a3253d] | 101 | default: |
---|
| 102 | assert(0); |
---|
| 103 | break; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /* |
---|
[cb4e992] | 107 | * This node MUST have a parent, so put it in that directory list. |
---|
[07a3253d] | 108 | */ |
---|
| 109 | |
---|
[cb4e992] | 110 | node->Parent = parent; |
---|
| 111 | node->st_ino = ++fs_info->ino_count; |
---|
[07a3253d] | 112 | |
---|
[cb4e992] | 113 | rtems_chain_append( &parent->info.directory.Entries, &node->Node ); |
---|
| 114 | |
---|
| 115 | return node; |
---|
| 116 | } |
---|
[07a3253d] | 117 | |
---|
[cb4e992] | 118 | /* |
---|
| 119 | * Allocate filesystem node and fill in basic information |
---|
| 120 | */ |
---|
| 121 | IMFS_jnode_t *IMFS_allocate_node( |
---|
| 122 | IMFS_jnode_types_t type, |
---|
| 123 | const char *name, |
---|
| 124 | mode_t mode |
---|
| 125 | ) |
---|
| 126 | { |
---|
| 127 | IMFS_jnode_t *node; |
---|
| 128 | struct timeval tv; |
---|
| 129 | |
---|
| 130 | /* |
---|
| 131 | * Allocate an IMFS jnode |
---|
| 132 | */ |
---|
| 133 | node = calloc( 1, sizeof( IMFS_jnode_t ) ); |
---|
| 134 | if ( !node ) |
---|
| 135 | return NULL; |
---|
| 136 | |
---|
| 137 | /* |
---|
| 138 | * Fill in the basic information |
---|
| 139 | */ |
---|
| 140 | node->st_nlink = 1; |
---|
| 141 | node->type = type; |
---|
| 142 | strncpy( node->name, name, IMFS_NAME_MAX ); |
---|
| 143 | |
---|
| 144 | /* |
---|
| 145 | * Fill in the mode and permission information for the jnode structure. |
---|
| 146 | */ |
---|
| 147 | node->st_mode = mode; |
---|
| 148 | #if defined(RTEMS_POSIX_API) |
---|
| 149 | node->st_uid = geteuid(); |
---|
| 150 | node->st_gid = getegid(); |
---|
| 151 | #else |
---|
| 152 | node->st_uid = 0; |
---|
| 153 | node->st_gid = 0; |
---|
| 154 | #endif |
---|
| 155 | |
---|
| 156 | /* |
---|
| 157 | * Now set all the times. |
---|
| 158 | */ |
---|
| 159 | gettimeofday( &tv, 0 ); |
---|
| 160 | |
---|
| 161 | node->stat_atime = (time_t) tv.tv_sec; |
---|
| 162 | node->stat_mtime = (time_t) tv.tv_sec; |
---|
| 163 | node->stat_ctime = (time_t) tv.tv_sec; |
---|
| 164 | |
---|
| 165 | return node; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | IMFS_jnode_t *IMFS_create_root_node(void) |
---|
| 169 | { |
---|
| 170 | IMFS_jnode_t *node; |
---|
| 171 | |
---|
| 172 | /* |
---|
| 173 | * Allocate filesystem node and fill in basic information |
---|
| 174 | */ |
---|
| 175 | node = IMFS_allocate_node( IMFS_DIRECTORY, "", (S_IFDIR | 0755) ); |
---|
| 176 | if ( !node ) |
---|
| 177 | return NULL; |
---|
| 178 | |
---|
| 179 | /* |
---|
| 180 | * Set the type specific information |
---|
| 181 | * |
---|
| 182 | * NOTE: Root node is always a directory. |
---|
| 183 | */ |
---|
| 184 | rtems_chain_initialize_empty(&node->info.directory.Entries); |
---|
[07a3253d] | 185 | |
---|
| 186 | return node; |
---|
| 187 | } |
---|