source: rtems/cpukit/libfs/src/imfs/imfs_directory.c @ 7192476f

4.104.114.84.95
Last change on this file since 7192476f was 7192476f, checked in by Ralf Corsepius <ralf.corsepius@…>, on Dec 8, 2006 at 7:18:27 AM

Use size_t instead of uint32_t for read/write count-args.

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