source: rtems/c/src/libfs/src/imfs/imfs_directory.c @ 662678d1

4.104.114.84.95
Last change on this file since 662678d1 was 662678d1, checked in by Joel Sherrill <joel.sherrill@…>, on 08/25/00 at 13:13:57

2000-08-25 Joel Sherrill <joel.sherrill@…>

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