1 //==========================================================================
5 // Fileio miscellaneous functions
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 // Copyright (C) 2003 Gary Thomas
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 // You should have received a copy of the GNU General Public License along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
37 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38 // at http://sources.redhat.com/ecos/ecos-license/
39 // -------------------------------------------
40 //####ECOSGPLCOPYRIGHTEND####
41 //==========================================================================
42 //#####DESCRIPTIONBEGIN####
45 // Contributors: nickg
47 // Purpose: Fileio miscellaneous functions
48 // Description: This file contains various miscellaneous functions
49 // for use with the fileio system. These include startup,
50 // table management, and other service routines.
52 //####DESCRIPTIONEND####
54 //==========================================================================
56 #include <pkgconf/system.h>
57 #include <pkgconf/hal.h>
58 #include <pkgconf/io_fileio.h>
59 #ifdef CYGPKG_LIBC_TIME
60 #include <pkgconf/libc_time.h>
64 #include <cyg/infra/cyg_trac.h> // tracing macros
65 #include <cyg/infra/cyg_ass.h> // assertion macros
66 #include <string.h> // strcmp()
67 #include <time.h> // time()
69 #ifdef CYGPKG_IO_WALLCLOCK
70 # include <cyg/io/wallclock.hxx> // Wallclock class
74 #include <pkgconf/kernel.h>
75 #include <cyg/kernel/ktypes.h> // base kernel types
76 #include <cyg/kernel/clock.inl> // Clock inlines
79 #include "fio.h" // Private header
81 //==========================================================================
82 // forward definitions
84 static void cyg_mtab_init();
86 __externC int chdir( const char *path );
88 //==========================================================================
91 // -------------------------------------------------------------------------
94 // This array contains entries for all filesystem that are installed in
96 __externC cyg_fstab_entry cyg_fstab[];
97 CYG_HAL_TABLE_BEGIN( cyg_fstab, fstab );
99 // end of filesystem table, set in linker script.
100 __externC cyg_fstab_entry cyg_fstab_end;
101 CYG_HAL_TABLE_END( cyg_fstab_end, fstab );
104 // Array of mutexes for locking the fstab entries
105 static Cyg_Mutex fstab_lock[CYGNUM_FILEIO_FSTAB_MAX] CYGBLD_ATTRIB_INIT_PRI(CYG_INIT_IO_FS);
108 // -------------------------------------------------------------------------
111 // This array contains entries for all valid running filesystems.
112 __externC cyg_mtab_entry cyg_mtab[];
113 CYG_HAL_TABLE_BEGIN( cyg_mtab, mtab );
115 // Extra entries at end of mtab for dynamic mount points.
116 cyg_mtab_entry cyg_mtab_extra[CYGNUM_FILEIO_MTAB_EXTRA] CYG_HAL_TABLE_EXTRA(mtab) = { { NULL } };
118 // End of mount table, set in the linker script.
119 __externC cyg_mtab_entry cyg_mtab_end;
120 CYG_HAL_TABLE_END( cyg_mtab_end, mtab );
123 // Array of mutexes for locking the mtab entries
124 static Cyg_Mutex mtab_lock[CYGNUM_FILEIO_MTAB_MAX] CYGBLD_ATTRIB_INIT_PRI(CYG_INIT_IO_FS);
127 //==========================================================================
130 cyg_mtab_entry *cyg_cdir_mtab_entry = NULL;
131 cyg_dir cyg_cdir_dir = CYG_DIR_NULL;
133 //==========================================================================
134 // Initialization object
136 class Cyg_Fileio_Init_Class
139 Cyg_Fileio_Init_Class();
142 static Cyg_Fileio_Init_Class fileio_initializer CYGBLD_ATTRIB_INIT_PRI(CYG_INIT_IO_FS);
144 Cyg_Fileio_Init_Class::Cyg_Fileio_Init_Class()
153 //==========================================================================
154 // Mount table initializer
156 static void cyg_mtab_init()
160 for( m = &cyg_mtab[0]; m != &cyg_mtab_end; m++ )
162 const char *fsname = m->fsname;
165 // Ignore empty entries
166 if( m->name == NULL )
169 // stop if there are more than the configured maximum
170 if( m-&cyg_mtab[0] >= CYGNUM_FILEIO_MTAB_MAX )
173 for( f = &cyg_fstab[0]; f != &cyg_fstab_end; f++ )
175 // stop if there are more than the configured maximum
176 if( f-&cyg_fstab[0] >= CYGNUM_FILEIO_FSTAB_MAX )
179 if( strcmp( fsname, f->name) == 0 )
183 if( f->mount( f, m ) == 0 )
187 // m->root installed by fs.
200 //==========================================================================
201 // Mount table matching
203 // -------------------------------------------------------------------------
204 // matchlen() compares two strings and returns the number of bytes by which
207 static int matchlen( const char *s1, const char *s2 )
210 while( s1[len] == s2[len] && s1[len] && s2[len] ) len++;
212 // Return length only if s2 is an initial substring of s1,
213 // and it terminates in s1 at end-of-string or a '/'.
215 // Special case for s2 == "/"
216 if( len == 1 && s2[0] == '/' && s2[1] == 0 )
219 if( (s2[len] == 0) && (s1[len] == 0 || s1[len] == '/'))
224 // -------------------------------------------------------------------------
225 // Search the mtab for the entry that matches the longest substring of
228 __externC int cyg_mtab_lookup( cyg_dir *dir, const char **name, cyg_mtab_entry **mte)
230 cyg_mtab_entry *m, *best = NULL;
233 // Unrooted file names go straight to current dir
234 if( **name != '/' ) {
235 if (*mte == (cyg_mtab_entry *)NULL) {
236 // No known current directory
239 // Current directory is well known
243 // Otherwise search the mount table.
244 for( m = &cyg_mtab[0]; m != &cyg_mtab_end; m++ )
246 if( m->name != NULL && m->valid )
248 int len = matchlen(*name,m->name);
250 best = m, best_len = len;
254 // No match found, bad path name...
255 if( best_len == 0 ) return -1;
266 //==========================================================================
269 __externC int mount( const char *devname,
280 // Search the mount table for an empty entry
281 for( m = &cyg_mtab[0]; m != &cyg_mtab_end; m++ )
283 // stop if there are more than the configured maximum
284 if( m-&cyg_mtab[0] >= CYGNUM_FILEIO_MTAB_MAX )
290 if( m->name == NULL ) break;
293 if( m == &cyg_mtab_end )
294 FILEIO_RETURN(ENOMEM);
296 // Now search the fstab for the filesystem implementation
297 for( f = &cyg_fstab[0]; f != &cyg_fstab_end; f++ )
299 // stop if there are more than the configured maximum
300 if( f-&cyg_fstab[0] >= CYGNUM_FILEIO_FSTAB_MAX )
303 if( strcmp( fsname, f->name) == 0 )
307 if( f == &cyg_fstab_end )
308 FILEIO_RETURN(ENODEV);
314 m->devname = devname;
316 if( (result = f->mount( f, m )) == 0 )
320 // m->root installed by fs.
328 // Make sure that there is something to search (for open)
330 if (cyg_cdir_mtab_entry == (cyg_mtab_entry *)NULL) {
331 cyg_cdir_mtab_entry = m;
334 FILEIO_RETURN(result);
337 //==========================================================================
338 // unmount filesystem
340 __externC int umount( const char *name)
348 // Search the mount table for a matching entry
349 for( m = &cyg_mtab[0]; m != &cyg_mtab_end; m++ )
351 // stop if there are more than the configured maximum
352 if( m-&cyg_mtab[0] >= CYGNUM_FILEIO_MTAB_MAX )
358 // Ignore empty or invalid entries
359 if( m->name == NULL || !m->valid ) continue;
362 if( strcmp(name,m->name) == 0 ) break;
364 // Match device name too?
367 if( m == &cyg_mtab_end )
368 FILEIO_RETURN(EINVAL);
370 // We have a match, call the umount function
372 err = m->fs->umount( m );
383 //==========================================================================
384 // Implement filesystem locking protocol.
386 void cyg_fs_lock( cyg_mtab_entry *mte, cyg_uint32 syncmode )
388 CYG_ASSERT(mte != NULL, "Bad mount table entry");
390 if( syncmode & CYG_SYNCMODE_FILE_FILESYSTEM ) {
391 CYG_ASSERT(mte->fs-&cyg_fstab[0] < CYGNUM_FILEIO_FSTAB_MAX, "Bad file system");
392 FILEIO_MUTEX_LOCK( fstab_lock[mte->fs-&cyg_fstab[0]] );
395 if( syncmode & CYG_SYNCMODE_FILE_MOUNTPOINT ) {
396 CYG_ASSERT(mte-&cyg_mtab[0] < CYGNUM_FILEIO_MTAB_MAX, "Bad mount point");
397 FILEIO_MUTEX_LOCK( mtab_lock[mte-&cyg_mtab[0]] );
401 void cyg_fs_unlock( cyg_mtab_entry *mte, cyg_uint32 syncmode )
403 CYG_ASSERT(mte != NULL, "Bad mount table entry");
405 if( syncmode & CYG_SYNCMODE_FILE_FILESYSTEM ) {
406 CYG_ASSERT(mte->fs-&cyg_fstab[0] < CYGNUM_FILEIO_FSTAB_MAX, "Bad file system");
407 FILEIO_MUTEX_UNLOCK( fstab_lock[mte->fs-&cyg_fstab[0]] );
410 if( syncmode & CYG_SYNCMODE_FILE_MOUNTPOINT ) {
411 CYG_ASSERT(mte-&cyg_mtab[0] < CYGNUM_FILEIO_MTAB_MAX, "Bad mount point");
412 FILEIO_MUTEX_UNLOCK( mtab_lock[mte-&cyg_mtab[0]] );
416 //==========================================================================
417 // Search mount table for a filesystems root.
419 __externC cyg_mtab_entry * cyg_fs_root_lookup( cyg_dir *root )
423 for( m = &cyg_mtab[0]; m != &cyg_mtab_end; m++ )
425 if( (cyg_dir *)m->root == root )
433 //==========================================================================
435 // This provides access to the current time/date, expressed as a
436 // time_t. It uses a number of mechanisms to do this, selecting
437 // whichever is available in the current configuration.
439 __externC time_t cyg_timestamp()
441 #if defined(CYGPKG_IO_WALLCLOCK)
443 // First, try to get the time from the wallclock device.
445 return (time_t) Cyg_WallClock::wallclock->get_current_time();
447 #elif defined(CYGINT_ISO_POSIX_TIMERS)
449 // If POSIX is present, use the current value of the realtime
454 clock_gettime( CLOCK_REALTIME, &tp );
456 return (time_t) tp.tv_sec;
458 #elif defined(CYGPKG_KERNEL)
460 // If all else fails, get the current realtime clock value and
461 // convert it to seconds ourself.
463 static struct Cyg_Clock::converter sec_converter;
464 static cyg_bool initialized = false;
465 cyg_tick_count ticks;
469 Cyg_Clock::real_time_clock->get_clock_to_other_converter( 1000000000, &sec_converter );
473 ticks = Cyg_Clock::real_time_clock->current_value();
475 return (time_t) Cyg_Clock::convert( ticks, &sec_converter );
477 /* No clock support at all. */
483 //==========================================================================
486 __externC int cyg_fileio_enosys() { return ENOSYS; }
487 __externC int cyg_fileio_erofs() { return EROFS; }
488 __externC int cyg_fileio_enoerr() { return ENOERR; }
489 __externC int cyg_fileio_enotdir() { return ENOTDIR; }
491 __externC cyg_bool cyg_fileio_seltrue (struct CYG_FILE_TAG *fp, int which, CYG_ADDRWORD info)
494 // -------------------------------------------------------------------------