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

4.104.114.84.95
Last change on this file since 47901d8 was 47901d8, checked in by Joel Sherrill <joel.sherrill@…>, on Nov 8, 2001 at 12:24:55 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.4 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  switch( whence ) {
191     case SEEK_SET:   /* absolute move from the start of the file */
192     case SEEK_CUR:   /* relative move */
193        iop->offset = (iop->offset/sizeof(struct dirent)) *
194              sizeof(struct dirent);
195        break;
196
197     case SEEK_END:   /* Movement past the end of the directory via lseek */
198                      /* is not a permitted operation                     */
199      default:
200        set_errno_and_return_minus_one( EINVAL );
201        break;
202  }
203
204  return 0;
205}
206
207
208
209/*
210 *  imfs_dir_fstat
211 *
212 *  This routine will obtain the following information concerning the current
213 *  directory:
214 *        st_dev      0ll
215 *        st_ino      1
216 *        st_mode     mode extracted from the jnode
217 *        st_nlink    number of links to this node
218 *        st_uid      uid extracted from the jnode
219 *        st_gid      gid extracted from the jnode
220 *        st_rdev     0ll
221 *        st_size     the number of bytes in the directory
222 *                    This is calculated by taking the number of entries
223 *                    in the directory and multiplying by the size of a
224 *                    dirent structure
225 *        st_blksize  0
226 *        st_blocks   0
227 *        stat_atime  time of last access
228 *        stat_mtime  time of last modification
229 *        stat_ctime  time of the last change
230 *
231 *  This information will be returned to the calling function in a -stat- struct
232 *
233 */
234
235int imfs_dir_fstat(
236  rtems_filesystem_location_info_t *loc,
237  struct stat                      *buf
238)
239{
240   Chain_Node        *the_node;
241   Chain_Control     *the_chain;
242   IMFS_jnode_t      *the_jnode;
243
244
245   the_jnode = (IMFS_jnode_t *) loc->node_access;
246
247   buf->st_dev = 0ll;
248   buf->st_ino   = the_jnode->st_ino;
249   buf->st_mode  = the_jnode->st_mode;
250   buf->st_nlink = the_jnode->st_nlink;
251   buf->st_uid   = the_jnode->st_uid;
252   buf->st_gid   = the_jnode->st_gid;
253   buf->st_rdev = 0ll;
254   buf->st_blksize = 0;
255   buf->st_blocks = 0;
256   buf->st_atime = the_jnode->stat_atime;
257   buf->st_mtime = the_jnode->stat_mtime;
258   buf->st_ctime = the_jnode->stat_ctime;
259
260   buf->st_size = 0;
261
262   the_chain = &the_jnode->info.directory.Entries;
263
264   /* Run through the chain and count the number of directory entries */
265   /* that are subordinate to this directory node                     */
266   for ( the_node = the_chain->first ;
267         !_Chain_Is_tail( the_chain, the_node ) ;
268         the_node = the_node->next ) {
269 
270      buf->st_size = buf->st_size + sizeof( struct dirent );
271   }
272
273   return 0;
274}
275
276/*
277 *  IMFS_dir_rmnod
278 *
279 *  This routine is available from the optable to remove a node
280 *  from the IMFS file system.
281 */
282
283int imfs_dir_rmnod(
284  rtems_filesystem_location_info_t      *pathloc       /* IN */
285)
286{
287  IMFS_jnode_t *the_jnode; 
288
289  the_jnode = (IMFS_jnode_t *) pathloc->node_access;
290
291  /*
292   * You cannot remove a node that still has children
293   */
294
295  if ( ! Chain_Is_empty( &the_jnode->info.directory.Entries ) )
296     set_errno_and_return_minus_one( ENOTEMPTY );
297
298  /*
299   * You cannot remove the file system root node.
300   */
301
302  if ( pathloc->mt_entry->mt_fs_root.node_access == pathloc->node_access )
303     set_errno_and_return_minus_one( EBUSY );
304
305  /*
306   * You cannot remove a mountpoint.
307   */
308
309   if ( the_jnode->info.directory.mt_fs != NULL )
310     set_errno_and_return_minus_one( EBUSY );         
311 
312  /*
313   * Take the node out of the parent's chain that contains this node
314   */
315
316  if ( the_jnode->Parent != NULL ) {
317    Chain_Extract( (Chain_Node *) the_jnode );
318    the_jnode->Parent = NULL;
319  }
320
321  /*
322   * Decrement the link counter and see if we can free the space.
323   */
324
325  the_jnode->st_nlink--;
326  IMFS_update_ctime( the_jnode );
327
328  /*
329   * The file cannot be open and the link must be less than 1 to free.
330   */
331
332  if ( !rtems_libio_is_file_open( the_jnode ) && (the_jnode->st_nlink < 1) ) {
333
334    /*
335     * Is the rtems_filesystem_current is this node?
336     */
337
338    if ( rtems_filesystem_current.node_access == pathloc->node_access )
339       rtems_filesystem_current.node_access = NULL;
340
341    /*
342     * Free memory associated with a memory file.
343     */
344
345    free( the_jnode );
346  }
347
348  return 0;
349
350}
351
352
Note: See TracBrowser for help on using the repository browser.