summaryrefslogtreecommitdiffstats
path: root/cpukit/libfs/src/imfs/imfs_creat.c
blob: 3b602d0c148c47b7d644b2c8eee91aa5d04038ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 *  IMFS_create_node()
 *
 *  Routine to create a new in memory file system node.
 *
 *  COPYRIGHT (c) 1989-2010.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  $Id$
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>
#include "imfs.h"
#include <rtems/libio_.h>

/*
 *  Create an IMFS filesystem node of an arbitrary type that is NOT
 *  the root directory node.
 */
IMFS_jnode_t *IMFS_create_node(
  rtems_filesystem_location_info_t *parent_loc,
  IMFS_jnode_types_t                type,
  const char                       *name,
  mode_t                            mode,
  const IMFS_types_union           *info
)
{
  IMFS_jnode_t        *node;
  IMFS_jnode_t        *parent;
  IMFS_fs_info_t      *fs_info;

  /*
   *  MUST have a parent node to call this routine.
   */
  if ( parent_loc == NULL )
    return NULL;

  parent = parent_loc->node_access;
  fs_info = parent_loc->mt_entry->fs_info;

  /*
   *  Reject creation of FIFOs if support is disabled.
   */
  if ( type == IMFS_FIFO &&
       fs_info->fifo_handlers == &rtems_filesystem_handlers_default )
    return NULL;

  /*
   *  Allocate filesystem node and fill in basic information
   */
  node  = IMFS_allocate_node( type, name, mode & ~rtems_filesystem_umask );
  if ( !node )
    return NULL;

  /*
   *  Set the type specific information
   */
  if ( type == IMFS_DIRECTORY ) {
    rtems_chain_initialize_empty(&node->info.directory.Entries);
  } else if ( type == IMFS_HARD_LINK ) {
    node->info.hard_link.link_node = info->hard_link.link_node;
  } else if ( type == IMFS_SYM_LINK ) {
    node->info.sym_link.name = info->sym_link.name;
  } else if ( type == IMFS_DEVICE ) {
    node->info.device.major = info->device.major;
    node->info.device.minor = info->device.minor;
  } else if ( type == IMFS_LINEAR_FILE ) {
    node->info.linearfile.size      = 0;
    node->info.linearfile.direct    = 0;
  } else if ( type == IMFS_MEMORY_FILE ) {
      node->info.file.size            = 0;
      node->info.file.indirect        = 0;
      node->info.file.doubly_indirect = 0;
      node->info.file.triply_indirect = 0;
  } else if ( type == IMFS_FIFO ) {
    node->info.fifo.pipe = NULL;
  } else {
    IMFS_assert(0);
  }

  /*
   *  This node MUST have a parent, so put it in that directory list.
   */
  node->Parent = parent;
  node->st_ino = ++fs_info->ino_count;

  rtems_chain_append( &parent->info.directory.Entries, &node->Node );

  return node;
}

/*
 *  Allocate filesystem node and fill in basic information
 */
IMFS_jnode_t *IMFS_allocate_node(
  IMFS_jnode_types_t                type,
  const char                       *name,
  mode_t                            mode
)
{
  IMFS_jnode_t        *node;
  struct timeval       tv;

  /*
   *  Allocate an IMFS jnode
   */
  node = calloc( 1, sizeof( IMFS_jnode_t ) );
  if ( !node )
    return NULL;

  /*
   *  Fill in the basic information
   */
  node->st_nlink = 1;
  node->type     = type;
  strncpy( node->name, name, IMFS_NAME_MAX );

  /*
   *  Fill in the mode and permission information for the jnode structure.
   */
  node->st_mode = mode;
  #if defined(RTEMS_POSIX_API)
    node->st_uid = geteuid();
    node->st_gid = getegid();
  #else
    node->st_uid = 0;
    node->st_gid = 0;
  #endif

  /*
   *  Now set all the times.
   */
  gettimeofday( &tv, 0 );

  node->stat_atime  = (time_t) tv.tv_sec;
  node->stat_mtime  = (time_t) tv.tv_sec;
  node->stat_ctime  = (time_t) tv.tv_sec;

  return node;
}

IMFS_jnode_t *IMFS_create_root_node(void)
{
  IMFS_jnode_t        *node;

  /*
   *  Allocate filesystem node and fill in basic information
   */
  node = IMFS_allocate_node( IMFS_DIRECTORY, "", (S_IFDIR | 0755) );
  if ( !node )
    return NULL;

  /*
   *  Set the type specific information
   *
   *  NOTE: Root node is always a directory.
   */
  rtems_chain_initialize_empty(&node->info.directory.Entries);

  return node;
}