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

4.104.114.84.95
Last change on this file since d6b1d73 was d6b1d73, checked in by Joel Sherrill <joel.sherrill@…>, on Jan 22, 2001 at 2:05:14 PM

2001-01-22 Ralf Corsepius <corsepiu@…>

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