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