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 | |
---|
14 | #if HAVE_CONFIG_H |
---|
15 | #include "config.h" |
---|
16 | #endif |
---|
17 | |
---|
18 | #include "imfs.h" |
---|
19 | |
---|
20 | #include <stdlib.h> |
---|
21 | #include <string.h> |
---|
22 | |
---|
23 | IMFS_jnode_t *IMFS_allocate_node( |
---|
24 | IMFS_fs_info_t *fs_info, |
---|
25 | const IMFS_node_control *node_control, |
---|
26 | const char *name, |
---|
27 | size_t namelen, |
---|
28 | mode_t mode, |
---|
29 | const IMFS_types_union *info |
---|
30 | ) |
---|
31 | { |
---|
32 | IMFS_jnode_t *node; |
---|
33 | struct timeval tv; |
---|
34 | |
---|
35 | if ( namelen > IMFS_NAME_MAX ) { |
---|
36 | errno = ENAMETOOLONG; |
---|
37 | |
---|
38 | return NULL; |
---|
39 | } |
---|
40 | |
---|
41 | gettimeofday( &tv, 0 ); |
---|
42 | |
---|
43 | /* |
---|
44 | * Allocate an IMFS jnode |
---|
45 | */ |
---|
46 | node = calloc( 1, sizeof( IMFS_jnode_t ) ); |
---|
47 | if ( !node ) { |
---|
48 | errno = ENOMEM; |
---|
49 | |
---|
50 | return NULL; |
---|
51 | } |
---|
52 | |
---|
53 | /* |
---|
54 | * Fill in the basic information |
---|
55 | */ |
---|
56 | node->reference_count = 1; |
---|
57 | node->st_nlink = 1; |
---|
58 | memcpy( node->name, name, namelen ); |
---|
59 | node->name [namelen] = '\0'; |
---|
60 | node->control = node_control; |
---|
61 | |
---|
62 | /* |
---|
63 | * Fill in the mode and permission information for the jnode structure. |
---|
64 | */ |
---|
65 | node->st_mode = mode; |
---|
66 | #if defined(RTEMS_POSIX_API) |
---|
67 | node->st_uid = geteuid(); |
---|
68 | node->st_gid = getegid(); |
---|
69 | #else |
---|
70 | node->st_uid = 0; |
---|
71 | node->st_gid = 0; |
---|
72 | #endif |
---|
73 | |
---|
74 | /* |
---|
75 | * Now set all the times. |
---|
76 | */ |
---|
77 | |
---|
78 | node->stat_atime = (time_t) tv.tv_sec; |
---|
79 | node->stat_mtime = (time_t) tv.tv_sec; |
---|
80 | node->stat_ctime = (time_t) tv.tv_sec; |
---|
81 | node->st_ino = ++fs_info->ino_count; |
---|
82 | |
---|
83 | return (*node->control->node_initialize)( node, info ); |
---|
84 | } |
---|
85 | |
---|
86 | /* |
---|
87 | * Create an IMFS filesystem node of an arbitrary type that is NOT |
---|
88 | * the root directory node. |
---|
89 | */ |
---|
90 | IMFS_jnode_t *IMFS_create_node_with_control( |
---|
91 | const rtems_filesystem_location_info_t *parentloc, |
---|
92 | const IMFS_node_control *node_control, |
---|
93 | const char *name, |
---|
94 | size_t namelen, |
---|
95 | mode_t mode, |
---|
96 | const IMFS_types_union *info |
---|
97 | ) |
---|
98 | { |
---|
99 | IMFS_fs_info_t *fs_info = parentloc->mt_entry->fs_info; |
---|
100 | IMFS_jnode_t *node = IMFS_allocate_node( |
---|
101 | fs_info, |
---|
102 | node_control, |
---|
103 | name, |
---|
104 | namelen, |
---|
105 | mode, |
---|
106 | info |
---|
107 | ); |
---|
108 | |
---|
109 | if ( node != NULL ) { |
---|
110 | IMFS_jnode_t *parent = parentloc->node_access; |
---|
111 | |
---|
112 | /* |
---|
113 | * This node MUST have a parent, so put it in that directory list. |
---|
114 | */ |
---|
115 | IMFS_assert( parent != NULL ); |
---|
116 | IMFS_add_to_directory( parent, node ); |
---|
117 | } |
---|
118 | |
---|
119 | return node; |
---|
120 | } |
---|
121 | |
---|