source: rtems/c/src/exec/libfs/src/imfs/imfs_directory.c @ b568ccb

4.104.114.84.95
Last change on this file since b568ccb was b568ccb, checked in by Joel Sherrill <joel.sherrill@…>, on 11/02/99 at 20:20:13

The object memfile.o was being included in the miniIMFS even though it
should not have been. This required that IMFS_rmnod be split into
three separate (per file type) routines to avoid dependencies.
In the end, a miniIMFS application is 6K smaller than one using the
full IMFS.

  • Property mode set to 100644
File size: 9.5 KB
Line 
1/*
2 *  XXX
3 *
4 *  COPYRIGHT (c) 1989-1998.
5 *  On-Line Applications Research Corporation (OAR).
6 *  Copyright assigned to U.S. Government, 1994.
7 *
8 *  The license and distribution terms for this file may be
9 *  found in the file LICENSE in this distribution or at
10 *  http://www.OARcorp.com/rtems/license.html.
11 *
12 *  $Id$
13 */
14
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <chain.h>
18#include <fcntl.h>
19#include <errno.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <assert.h>
24#include <dirent.h>
25
26#include "imfs.h"
27#include "libio_.h"
28
29/* -----------------------------------------------------------------------
30 * This rountine will verify that the node being opened as a directory is
31 * in fact a directory node. If it is then the offset into the directory
32 * will be set to 0 to position to the first directory entry.
33 */
34
35int imfs_dir_open(
36  rtems_libio_t  *iop,
37  const char *pathname,
38  unsigned32 flag,
39  unsigned32 mode
40)
41{
42  IMFS_jnode_t      *the_jnode;
43
44  /* Is the node a directory ? */
45  the_jnode = (IMFS_jnode_t *) iop->file_info;
46
47  if ( the_jnode->type != IMFS_DIRECTORY )
48     return -1;         /* It wasn't a directory --> return error */
49
50  iop->offset = 0;
51  return 0;
52}
53
54
55
56/* -----------------------------------------------------------------------
57 * This routine will read the next directory entry based on the directory
58 * offset. The offset should be equal to -n- time the size of an individual
59 * dirent structure. If n is not an integer multiple of the sizeof a
60 * dirent structure, an integer division will be performed to determine
61 * directory entry that will be returned in the buffer. Count should reflect
62 * -m- times the sizeof dirent bytes to be placed in the buffer.
63 * If there are not -m- dirent elements from the current directory position
64 * to the end of the exisiting file, the remaining entries will be placed in
65 * the buffer and the returned value will be equal to -m actual- times the
66 * size of a directory entry.
67 */
68
69int imfs_dir_read(
70  rtems_libio_t  *iop,
71  void *buffer,
72  unsigned32 count
73)
74{
75  /*
76   *  Read up to element  iop->offset in the directory chain of the
77   *  imfs_jnode_t struct for this file descriptor.
78   */
79   Chain_Node        *the_node;
80   Chain_Control     *the_chain;
81   IMFS_jnode_t      *the_jnode;
82   int                bytes_transferred;
83   int                current_entry;
84   int                first_entry;
85   int                last_entry;
86   struct dirent      tmp_dirent;
87
88   the_jnode = (IMFS_jnode_t *)iop->file_info;
89   the_chain = &the_jnode->info.directory.Entries;
90   
91   if ( Chain_Is_empty( the_chain ) )
92      return 0;
93
94   /* Move to the first of the desired directory entries */
95   the_node = the_chain->first;
96
97   bytes_transferred = 0;
98   first_entry = iop->offset;
99   /* protect against using sizes that are not exact multiples of the */
100   /* -dirent- size. These could result in unexpected results          */
101   last_entry = first_entry + (count/sizeof(struct dirent)) * sizeof(struct dirent);
102
103   /* The directory was not empty so try to move to the desired entry in chain*/
104   for(
105      current_entry = 0;
106      current_entry < last_entry;
107      current_entry = current_entry + sizeof(struct dirent) ){
108
109      if ( Chain_Is_tail( the_chain, the_node ) ){
110         /* We hit the tail of the chain while trying to move to the first */
111         /* entry in the read */
112         return bytes_transferred;  /* Indicate that there are no more */
113                                    /* entries to return */
114      }
115   
116      if( current_entry >= first_entry ) {
117         /* Move the entry to the return buffer */
118         tmp_dirent.d_ino = 1;
119         tmp_dirent.d_off = current_entry;
120         tmp_dirent.d_reclen = sizeof( struct dirent );
121         the_jnode = (IMFS_jnode_t *) the_node;
122         tmp_dirent.d_namlen = strlen( the_jnode->name );
123         strcpy( tmp_dirent.d_name, the_jnode->name );
124         memcpy(
125            buffer + bytes_transferred,
126            (void *)&tmp_dirent,
127            sizeof( struct dirent )
128         );
129         iop->offset = iop->offset + sizeof(struct dirent);
130         bytes_transferred = bytes_transferred + sizeof( struct dirent );
131      }
132
133      the_node = the_node->next;
134   }
135
136   /* Success */
137   return bytes_transferred;
138}
139
140
141
142/* -----------------------------------------------------------------------
143 * This routine will be called by the generic close routine to cleanup any
144 * resources that have been allocated for the management of the file
145 */
146
147int imfs_dir_close(
148  rtems_libio_t  *iop
149)
150{
151  /* The generic close routine handles the deallocation of the file control */
152  /* and associated memory. At present the imfs_dir_close simply      */
153  /* returns a successful completion status                                 */
154 
155  return 0;
156}
157
158
159
160/* -----------------------------------------------------------------------
161 * This routine will behave in one of three ways based on the state of
162 * argument whence. Based on the state of its value the offset argument will
163 * be interpreted using one of the following methods:
164 *
165 *      SEEK_SET - offset is the absolute byte offset from the start of the
166 *                 logical start of the dirent sequence that represents the
167 *                 directory
168 *      SEEK_CUR - offset is used as the relative byte offset from the current
169 *                 directory position index held in the iop structure
170 *      SEEK_END - N/A --> This will cause an assert.
171 */
172
173int imfs_dir_lseek(
174  rtems_libio_t  *iop,
175  off_t           offset,
176  int             whence
177)
178{
179  off_t normal_offset;
180
181  normal_offset = (offset/sizeof(struct dirent)) * sizeof(struct dirent);
182
183
184  switch( whence )
185  {
186     case SEEK_SET:     /* absolute move from the start of the file */
187        iop->offset = normal_offset;
188        break;
189
190     case SEEK_CUR:     /* relative move */
191        iop->offset = iop->offset + normal_offset;
192        break;
193
194     case SEEK_END:     /* Movement past the end of the directory via lseek */
195                        /* is not a permitted operation                     */
196      default:
197        set_errno_and_return_minus_one( EINVAL );
198        break;
199
200  }
201
202  return 0;
203}
204
205
206
207/* -----------------------------------------------------------------------
208 * This routine will obtain the following information concerning the current
209 * directory:
210 *      st_dev          0ll
211 *      st_ino          1
212 *      st_mode         mode extracted from the jnode
213 *      st_nlink        number of links to this node
214 *      st_uid          uid extracted from the jnode
215 *      st_gid          gid extracted from the jnode
216 *      st_rdev         0ll
217 *      st_size         the number of bytes in the directory
218 *                      This is calculated by taking the number of entries
219 *                      in the directory and multiplying by the size of a
220 *                      dirent structure
221 *      st_blksize      0
222 *      st_blocks       0
223 *      st_atime        time of last access
224 *      st_mtime        time of last modification
225 *      st_ctime        time of the last change
226 *
227 * This information will be returned to the calling function in a -stat- struct
228 *     
229 */
230
231int imfs_dir_fstat(
232  rtems_filesystem_location_info_t *loc,
233  struct stat                      *buf
234)
235{
236   Chain_Node        *the_node;
237   Chain_Control     *the_chain;
238   IMFS_jnode_t      *the_jnode;
239
240
241   the_jnode = (IMFS_jnode_t *) loc->node_access;
242
243   buf->st_dev = 0ll;
244   buf->st_ino   = the_jnode->st_ino;
245   buf->st_mode  = the_jnode->st_mode;
246   buf->st_nlink = the_jnode->st_nlink;
247   buf->st_uid   = the_jnode->st_uid;
248   buf->st_gid   = the_jnode->st_gid;
249   buf->st_rdev = 0ll;
250   buf->st_blksize = 0;
251   buf->st_blocks = 0;
252   buf->st_atime = the_jnode->st_atime;
253   buf->st_mtime = the_jnode->st_mtime;
254   buf->st_ctime = the_jnode->st_ctime;
255
256   buf->st_size = 0;
257
258   the_chain = &the_jnode->info.directory.Entries;
259
260   /* Run through the chain and count the number of directory entries */
261   /* that are subordinate to this directory node                     */
262   for ( the_node = the_chain->first ;
263         !_Chain_Is_tail( the_chain, the_node ) ;
264         the_node = the_node->next ) {
265 
266      buf->st_size = buf->st_size + sizeof( struct dirent );
267   }
268
269   return 0;
270}
271
272/*
273 *  IMFS_dir_rmnod
274 *
275 *  This routine is available from the optable to remove a node
276 *  from the IMFS file system.
277 */
278
279int imfs_dir_rmnod(
280  rtems_filesystem_location_info_t      *pathloc       /* IN */
281)
282{
283  IMFS_jnode_t *the_jnode; 
284
285  the_jnode = (IMFS_jnode_t *) pathloc->node_access;
286
287  /*
288   * You cannot remove a node that still has children
289   */
290
291  if ( ! Chain_Is_empty( &the_jnode->info.directory.Entries ) )
292     set_errno_and_return_minus_one( ENOTEMPTY );
293
294  /*
295   * You cannot remove the file system root node.
296   */
297
298  if ( pathloc->mt_entry->mt_fs_root.node_access == pathloc->node_access )
299     set_errno_and_return_minus_one( EBUSY );
300
301  /*
302   * You cannot remove a mountpoint.
303   */
304
305   if ( the_jnode->info.directory.mt_fs != NULL )
306     set_errno_and_return_minus_one( EBUSY );         
307 
308  /*
309   * Take the node out of the parent's chain that contains this node
310   */
311
312  if ( the_jnode->Parent != NULL ) {
313    Chain_Extract( (Chain_Node *) the_jnode );
314    the_jnode->Parent = NULL;
315  }
316
317  /*
318   * Decrement the link counter and see if we can free the space.
319   */
320
321  the_jnode->st_nlink--;
322  IMFS_update_ctime( the_jnode );
323
324  /*
325   * The file cannot be open and the link must be less than 1 to free.
326   */
327
328  if ( !rtems_libio_is_file_open( the_jnode ) && (the_jnode->st_nlink < 1) ) {
329
330    /*
331     * Is the rtems_filesystem_current is this node?
332     */
333
334    if ( rtems_filesystem_current.node_access == pathloc->node_access )
335       rtems_filesystem_current.node_access = NULL;
336
337    /*
338     * Free memory associated with a memory file.
339     */
340
341    free( the_jnode );
342  }
343
344  return 0;
345
346}
347
348
Note: See TracBrowser for help on using the repository browser.