1 //==========================================================================
5 // Fileio directory support
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 // -------------------------------------------
36 //####ECOSGPLCOPYRIGHTEND####
37 //==========================================================================
38 //#####DESCRIPTIONBEGIN####
41 // Contributors: nickg
43 // Purpose: Fileio directory support
44 // Description: Support for directory operations.
49 //####DESCRIPTIONEND####
51 //==========================================================================
53 #include <pkgconf/hal.h>
54 #include <pkgconf/io_fileio.h>
56 #include <cyg/infra/cyg_trac.h> // tracing macros
57 #include <cyg/infra/cyg_ass.h> // assertion macros
59 #include <stdarg.h> // for fcntl()
61 #include "fio.h" // Private header
63 #include <dirent.h> // struct dirent
65 //==========================================================================
67 #define DIROPEN_RETURN_ERR( err ) \
70 CYG_REPORT_RETVAL( NULL ); \
74 //==========================================================================
75 // Implement filesystem locking protocol.
77 #define LOCK_FS( _mte_ ) { \
78 CYG_ASSERT(_mte_ != NULL, "Bad mount table entry"); \
79 cyg_fs_lock( _mte_, (_mte_)->fs->syncmode); \
82 #define UNLOCK_FS( _mte_ ) cyg_fs_unlock( _mte_, (_mte_)->fs->syncmode)
84 //==========================================================================
85 // Open a directory for reading
87 extern DIR *opendir( const char *dirname )
91 CYG_CANCELLATION_POINT;
96 cyg_mtab_entry *mte = cyg_cdir_mtab_entry;
97 cyg_dir dir = cyg_cdir_dir;
98 const char *name = dirname;
100 fd = cyg_fd_alloc(1); // Never return fd 0
103 DIROPEN_RETURN_ERR(EMFILE);
105 file = cyg_file_alloc();
110 DIROPEN_RETURN_ERR(ENFILE);
113 ret = cyg_mtab_lookup( &dir, &name, &mte );
119 DIROPEN_RETURN_ERR(ENOENT);
124 ret = mte->fs->opendir( mte, dir, name, file );
132 DIROPEN_RETURN_ERR(ret);
135 file->f_flag |= CYG_FDIR|CYG_FREAD;
137 file->f_syncmode = mte->fs->syncmode;
139 cyg_fd_assign( fd, file );
141 DIR *dirp = (DIR *)fd;
143 FILEIO_RETURN_VALUE(dirp);
146 //==========================================================================
147 // Read a directory entry.
148 // This is the thread-unsafe version that uses a static result buffer.
149 // It just calls the thread-safe version to do the work.
151 extern struct dirent *readdir( DIR *dirp )
155 static struct dirent ent;
156 struct dirent *result;
159 err = readdir_r( dirp, &ent, &result );
164 FILEIO_RETURN_VALUE( NULL );
167 FILEIO_RETURN_VALUE( result );
170 //==========================================================================
172 extern int readdir_r( DIR *dirp, struct dirent *entry, struct dirent **result )
183 FILEIO_RETURN_VALUE( EBADF );
186 #ifdef CYGPKG_FILEIO_DIRENT_DTYPE
187 // d_type is only supposed by a few filesystems, so make sure other
188 // filesystems return a sane value;
192 res = read( fd, (void *)entry, sizeof(struct dirent));
196 FILEIO_RETURN_VALUE( errno );
202 #ifdef CYGPKG_FILEIO_DIRENT_DTYPE
203 // Only the lower bits contain the type of file, so and those out.
204 entry->d_type &= S_IFMT;
208 FILEIO_RETURN( ENOERR );
211 //==========================================================================
213 extern void rewinddir( DIR *dirp )
219 lseek( fd, 0, SEEK_SET );
221 FILEIO_RETURN_VOID();
224 //==========================================================================
226 extern int closedir( DIR *dirp )
231 int err = close( fd );
233 FILEIO_RETURN_VALUE( err );
236 // -------------------------------------------------------------------------