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