OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [io/] [fileio/] [v2_0/] [src/] [dir.cxx] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      dir.cxx
4
//
5
//      Fileio directory support
6
//
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.
12
//
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.
16
//
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
20
// for more details.
21
//
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.
25
//
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.
32
//
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
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37
// at http://sources.redhat.com/ecos/ecos-license/
38
// -------------------------------------------
39
//####ECOSGPLCOPYRIGHTEND####
40
//==========================================================================
41
//#####DESCRIPTIONBEGIN####
42
//
43
// Author(s):           nickg
44
// Contributors:        nickg
45
// Date:                2000-05-25
46
// Purpose:             Fileio directory support
47
// Description:         Support for directory operations.
48
//                      
49
//              
50
//              
51
//
52
//####DESCRIPTIONEND####
53
//
54
//==========================================================================
55
 
56
#include <pkgconf/hal.h>
57
#include <pkgconf/kernel.h>
58
#include <pkgconf/io_fileio.h>
59
 
60
#include <cyg/kernel/ktypes.h>         // base kernel types
61
#include <cyg/infra/cyg_trac.h>        // tracing macros
62
#include <cyg/infra/cyg_ass.h>         // assertion macros
63
 
64
#include <stdarg.h>                     // for fcntl()
65
 
66
#include "fio.h"                       // Private header
67
 
68
#include <dirent.h>                    // struct dirent
69
 
70
//==========================================================================
71
 
72
#define DIROPEN_RETURN_ERR( err )               \
73
CYG_MACRO_START                                 \
74
    errno = err;                                \
75
    CYG_REPORT_RETVAL( NULL );                  \
76
    return NULL;                                \
77
CYG_MACRO_END
78
 
79
//==========================================================================
80
// Implement filesystem locking protocol. 
81
 
82
#define LOCK_FS( _mte_ )  {                             \
83
   CYG_ASSERT(_mte_ != NULL, "Bad mount table entry");  \
84
   cyg_fs_lock( _mte_, (_mte_)->fs->syncmode);          \
85
}
86
 
87
#define UNLOCK_FS( _mte_ ) cyg_fs_unlock( _mte_, (_mte_)->fs->syncmode)
88
 
89
//==========================================================================
90
// Open a directory for reading
91
 
92
extern DIR *opendir( const char *dirname )
93
{
94
    FILEIO_ENTRY();
95
 
96
    CYG_CANCELLATION_POINT;
97
 
98
    int ret = 0;
99
    int fd;
100
    cyg_file *file;
101
    cyg_mtab_entry *mte = cdir_mtab_entry;
102
    cyg_dir dir = cdir_dir;
103
    const char *name = dirname;
104
 
105
    fd = cyg_fd_alloc(1); // Never return fd 0
106
 
107
    if( fd < 0 )
108
        DIROPEN_RETURN_ERR(EMFILE);
109
 
110
    file = cyg_file_alloc();
111
 
112
    if( file == NULL )
113
    {
114
        cyg_fd_free(fd);
115
        DIROPEN_RETURN_ERR(ENFILE);
116
    }
117
 
118
    ret = cyg_mtab_lookup( &dir, &name, &mte );
119
 
120
    if( 0 != ret )
121
    {
122
        cyg_fd_free(fd);
123
        cyg_file_free(file);
124
        DIROPEN_RETURN_ERR(ENOENT);
125
    }
126
 
127
    LOCK_FS( mte );
128
 
129
    ret = mte->fs->opendir( mte, dir, name, file );
130
 
131
    UNLOCK_FS( mte );
132
 
133
    if( 0 != ret )
134
    {
135
        cyg_fd_free(fd);
136
        cyg_file_free(file);
137
        DIROPEN_RETURN_ERR(ret);
138
    }
139
 
140
    file->f_flag |= CYG_FDIR|CYG_FREAD;
141
    file->f_mte = mte;
142
    file->f_syncmode = mte->fs->syncmode;
143
 
144
    cyg_fd_assign( fd, file );
145
 
146
    DIR *dirp = (DIR *)fd;
147
 
148
    FILEIO_RETURN_VALUE(dirp);
149
}
150
 
151
//==========================================================================
152
// Read a directory entry.
153
// This is the thread-unsafe version that uses a static result buffer.
154
// It just calls the thread-safe version to do the work.
155
 
156
extern struct dirent *readdir( DIR *dirp )
157
{
158
    FILEIO_ENTRY();
159
 
160
    static struct dirent ent;
161
    struct dirent *result;
162
    int err;
163
 
164
    err = readdir_r( dirp, &ent, &result );
165
 
166
    if( err != 0 )
167
    {
168
        errno = err;
169
        FILEIO_RETURN_VALUE( NULL );
170
    }
171
 
172
    FILEIO_RETURN_VALUE( result );
173
}
174
 
175
//==========================================================================
176
 
177
extern int readdir_r( DIR *dirp, struct dirent *entry, struct dirent **result )
178
{
179
    FILEIO_ENTRY();
180
 
181
    int fd = (int)dirp;
182
    ssize_t res;
183
 
184
    *result = NULL;
185
 
186
    res = read( fd, (void *)entry, sizeof(struct dirent));
187
 
188
    if( res < 0 )
189
    {
190
        FILEIO_RETURN_VALUE( errno );
191
    }
192
 
193
    if( res > 0 )
194
        *result = entry;
195
 
196
    FILEIO_RETURN( ENOERR );
197
}
198
 
199
//==========================================================================
200
 
201
extern void rewinddir( DIR *dirp )
202
{
203
    FILEIO_ENTRY();
204
 
205
    int fd = (int)dirp;
206
 
207
    lseek( fd, 0, SEEK_SET );
208
 
209
    FILEIO_RETURN_VOID();
210
}
211
 
212
//==========================================================================
213
 
214
extern int closedir( DIR *dirp )
215
{
216
    FILEIO_ENTRY();
217
 
218
    int fd = (int)dirp;
219
    int err = close( fd );
220
 
221
    FILEIO_RETURN_VALUE( err );
222
}
223
 
224
// -------------------------------------------------------------------------
225
// EOF dir.cxx

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.