source: rtems/c/src/lib/libc/imfs_directory.c @ 1fd36613

Last change on this file since 1fd36613 was 1fd36613, checked in by Joel Sherrill <joel.sherrill@…>, on Nov 8, 2001 at 12:31:44 AM

2001-11-07 Jennifer Averett <jennifer@…>

Reported by Ibragimov Ilya <ibr@…> and tracked as PR49.

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