summaryrefslogtreecommitdiff
path: root/cesar/ecos/packages/io/fileio/current/src/file.cxx
blob: 11e0049bad732d60a16a8f141e54cb6e6ea56fe8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
//==========================================================================
//
//      file.cxx
//
//      Fileio file operations
//
//==========================================================================
//####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 or (at your option) any later version.
//
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with eCos; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
//
// As a special exception, if other files instantiate templates or use macros
// or inline functions from this file, or you compile this file and link it
// with other works to produce a work based on this file, this file does not
// by itself cause the resulting work to be covered by the GNU General Public
// License. However the source code for this file must still be made available
// in accordance with section (3) of the GNU General Public License.
//
// This exception does not invalidate any other reasons why a work based on
// this file might be covered by the GNU General Public License.
//
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
// at http://sources.redhat.com/ecos/ecos-license/
// -------------------------------------------
//####ECOSGPLCOPYRIGHTEND####
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):           nickg
// Contributors:        nickg
// Date:                2000-05-25
// Purpose:             Fileio file operations
// Description:         These are the functions that operate on files as a whole,
//                      such as open(), creat(), mkdir() etc.
//              
//              
//
//####DESCRIPTIONEND####
//
//==========================================================================

#include <pkgconf/hal.h>
#include <pkgconf/io_fileio.h>
#include <pkgconf/isoinfra.h>

#include <cyg/infra/cyg_trac.h>        // tracing macros
#include <cyg/infra/cyg_ass.h>         // assertion macros

#include <string.h>                    // string functions
#include <dirent.h>
#include <stdio.h>                     // stdin, stdout, stderr

#include "fio.h"                       // Private header

//==========================================================================
// Implement filesystem locking protocol. 

#define LOCK_FS( _mte_ )  {                             \
   CYG_ASSERT(_mte_ != NULL, "Bad mount table entry");  \
   CYG_ASSERT(_mte_->fs != NULL, "Bad mount filesystem entry");  \
   cyg_fs_lock( _mte_, (_mte_)->fs->syncmode);          \
}

#define UNLOCK_FS( _mte_ ) cyg_fs_unlock( _mte_, (_mte_)->fs->syncmode)

//==========================================================================
// A local strcpy clone that returns a pointer to the end of the copied
// string, not the beginning.

static char *my_strcpy( char *s1, const char *s2 )
{
    while( (*s1++ = *s2++) != 0);
    return s1-1;
}

//==========================================================================
// Compare a pathname fragment with an element in a pathname. This
// deals with zero or separator termination and avoids substring
// matches.

static int pathcmp( const char *path, const char *name )
{
    while( *path == *name && *path != '\0' )
        path++, name++;
    if( *name != '\0' ) return false;
    if( *path == '/' || *path == '\0' ) return true;
    return false;
}

//==========================================================================
// CWD support

#ifdef CYGPKG_IO_FILEIO_TRACK_CWD

// buffer for storing CWD path
static char cwd[PATH_MAX];
static size_t cwd_size = 0;


static void update_cwd( cyg_mtab_entry *mte, cyg_dir dir, const char *path )
{
    char *p = cwd;

    if( mte != cyg_cdir_mtab_entry || dir != cyg_cdir_dir )
    {
        // Here, the path is relative to the root of the filesystem,
        // or in a totally new filesystem, initialize the cwd with the
        // mount point name of the filesystem.

        p = my_strcpy( p, mte->name );
    }
    else p = cwd+cwd_size;

    // We must now copy the path into the cwd buffer while dealing
    // with any "." and ".."  components textually.

    while( *path != '\0' )
    {
        // skip any stray directory separators.
        if( *path == '/' ) path++;

        // Look for a "." entry and just ignore it.
        if( pathcmp( path, "." ) )
        {
            path++;
            continue;
        }

        // Look for a ".." entry. If found, chew off the last cwd
        // entry.
        if( pathcmp( path, ".." ) )
        {
            while( *(--p) != '/' );     // back up to last '/'
            path += 2;                  // skip "..".
            continue;
        }

        // Otherwise just copy the name fragment over to the cwd.

        if( *(p-1) != '/' )
            *p++ = '/';        // add a directory separator
        while( *path != '/' && *path != '\0' )
            *p++ = *path++;

    }

    // Zero terminate the cwd.
    *p = '\0';
    
    // update size
    cwd_size = p-cwd;
}

#else

#ifdef CYGPKG_KERNEL
static Cyg_Mutex getcwd_lock CYGBLD_ATTRIB_INIT_PRI(CYG_INIT_IO_FS);
#endif

#endif


//==========================================================================
// Open a file

__externC int open( const char *path, int oflag, ... )
{
    FILEIO_ENTRY();

    // we want to be sure we pull in stdin/out/err, so they can be
    // assigned to fds 0, 1 and 2
#ifdef CYGINT_ISO_STDIO_STREAMS
    CYG_REFERENCE_OBJECT(stdin);
    CYG_REFERENCE_OBJECT(stdout);
    CYG_REFERENCE_OBJECT(stderr);
#endif

    CYG_CANCELLATION_POINT;

    int ret = 0;
    int fd;
    cyg_file *file;
    cyg_mtab_entry *mte = cyg_cdir_mtab_entry;
    cyg_dir dir = cyg_cdir_dir;
    const char *name = path;

    // At least one of O_RDONLY, O_WRONLY, O_RDWR must be provided
    if( (oflag & O_RDWR) == 0 )
        FILEIO_RETURN(EINVAL);

    fd = cyg_fd_alloc(0);

    if( fd < 0 )
        FILEIO_RETURN(EMFILE);
    
    file = cyg_file_alloc();

    if( file == NULL )
    {
        cyg_fd_free(fd);
        FILEIO_RETURN(ENFILE);
    }
    
    ret = cyg_mtab_lookup( &dir, &name, &mte );
    
    if( 0 != ret )
    {
        cyg_fd_free(fd);
        cyg_file_free(file);
        FILEIO_RETURN(ENOENT);
    }

    LOCK_FS( mte );
    
    ret = mte->fs->open( mte, dir, name, oflag, file );
    
    UNLOCK_FS( mte );
    
    if( 0 != ret )
    {
        cyg_fd_free(fd);
        cyg_file_free(file);
        FILEIO_RETURN(ret);
    }

    file->f_mte = mte;
    file->f_syncmode = mte->fs->syncmode;
    
    cyg_fd_assign( fd, file );

    FILEIO_RETURN_VALUE(fd);
}

//==========================================================================
// create a file

__externC int creat( const char *path, mode_t mode )
{
    return open( path, O_WRONLY | O_CREAT | O_TRUNC, mode );
}


//==========================================================================
// Unlink/remove a file

__externC int unlink( const char *path )
{
    FILEIO_ENTRY();
    
    int ret = 0;
    cyg_mtab_entry *mte = cyg_cdir_mtab_entry;
    cyg_dir dir = cyg_cdir_dir;
    const char *name = path;

    ret = cyg_mtab_lookup( &dir, &name, &mte );
    
    if( 0 != ret )
        FILEIO_RETURN(ENOENT);

    LOCK_FS( mte );
        
    ret = mte->fs->unlink( mte, dir, name );
    
    UNLOCK_FS( mte );
    
    FILEIO_RETURN(ret);
}

//==========================================================================
// Make a directory

__externC int mkdir( const char *path, mode_t mode )
{
    FILEIO_ENTRY();
    
    int ret = 0;
    cyg_mtab_entry *mte = cyg_cdir_mtab_entry;
    cyg_dir dir = cyg_cdir_dir;
    const char *name = path;

    mode=mode;
    
    ret = cyg_mtab_lookup( &dir, &name, &mte );
    
    if( 0 != ret )
        FILEIO_RETURN(ENOENT);

    LOCK_FS( mte );
    
    ret = mte->fs->mkdir( mte, dir, name );
    
    UNLOCK_FS( mte );
    
    FILEIO_RETURN(ret);
}

//==========================================================================
// Remove a directory

__externC int rmdir( const char *path )
{
    FILEIO_ENTRY();
    
    int ret = 0;
    cyg_mtab_entry *mte = cyg_cdir_mtab_entry;
    cyg_dir dir = cyg_cdir_dir;
    const char *name = path;

    ret = cyg_mtab_lookup( &dir, &name, &mte );
    
    if( 0 != ret )
        FILEIO_RETURN(ENOENT);

    LOCK_FS( mte );
    
    ret = mte->fs->rmdir( mte, dir, name );
    
    UNLOCK_FS( mte );
    
    FILEIO_RETURN(ret);
}

//==========================================================================
// Rename a file

__externC int rename( const char *path1, const char *path2 ) __THROW
{
    FILEIO_ENTRY();
    
    int ret = 0;
    cyg_mtab_entry *mte1 = cyg_cdir_mtab_entry;
    cyg_mtab_entry *mte2 = cyg_cdir_mtab_entry;
    cyg_dir dir1 = cyg_cdir_dir;
    cyg_dir dir2 = cyg_cdir_dir;
    const char *name1 = path1;
    const char *name2 = path2;

    ret = cyg_mtab_lookup( &dir1, &name1, &mte1 );
    
    if( 0 != ret )
        FILEIO_RETURN(ENOENT);

    ret = cyg_mtab_lookup( &dir2, &name2, &mte2 );
    
    if( 0 != ret )
        FILEIO_RETURN(ENOENT);

    // Cannot rename between different filesystems
    if( mte1 != mte2 )
        FILEIO_RETURN(EXDEV);

    LOCK_FS( mte1 );
    
    ret = mte1->fs->rename( mte1, dir1, name1, dir2, name2 );

    UNLOCK_FS( mte1 );
    
    FILEIO_RETURN(ret);
}

//==========================================================================
// Create a link from an existing file (path1) to a new one (path2)

__externC int link( const char *path1, const char *path2 )
{
    FILEIO_ENTRY();
    
    int ret = 0;
    cyg_mtab_entry *mte1 = cyg_cdir_mtab_entry;
    cyg_mtab_entry *mte2 = cyg_cdir_mtab_entry;
    cyg_dir dir1 = cyg_cdir_dir;
    cyg_dir dir2 = cyg_cdir_dir;
    const char *name1 = path1;
    const char *name2 = path2;

    ret = cyg_mtab_lookup( &dir1, &name1, &mte1 );
    
    if( 0 != ret )
        FILEIO_RETURN(ENOENT);

    ret = cyg_mtab_lookup( &dir2, &name2, &mte2 );
    
    if( 0 != ret )
        FILEIO_RETURN(ENOENT);

    // Cannot hard-link between different filesystems
    if( mte1 != mte2 )
        FILEIO_RETURN(EXDEV);

    LOCK_FS( mte1 );
    
    ret = mte1->fs->link( mte1, dir1, name1, dir2, name2, CYG_FSLINK_HARD );

    UNLOCK_FS( mte1 );
    
    FILEIO_RETURN(ret);
}

//==========================================================================
// Change current directory

__externC int chdir( const char *path )
{
    FILEIO_ENTRY();
    
    int ret = 0;
    cyg_mtab_entry *mte = cyg_cdir_mtab_entry;
    cyg_dir dir = cyg_cdir_dir;
    cyg_dir newdir;
    const char *name = path;

    ret = cyg_mtab_lookup( &dir, &name, &mte );
    
    if( 0 != ret )
        FILEIO_RETURN(ENOENT);

    LOCK_FS(mte);
    
    ret = mte->fs->chdir( mte, dir, name, &newdir );

    UNLOCK_FS(mte);
    
    if( 0 != ret )
        FILEIO_RETURN(ret);

#ifdef CYGPKG_IO_FILEIO_TRACK_CWD
    update_cwd( mte, dir, name );
#endif
    
    if( cyg_cdir_mtab_entry != NULL && cyg_cdir_dir != CYG_DIR_NULL )
    {
        // Now detach from current cyg_cdir. We call the current directory's
        // chdir routine with a NULL dir_out pointer.

        LOCK_FS(cyg_cdir_mtab_entry);

        ret = cyg_cdir_mtab_entry->fs->chdir( cyg_cdir_mtab_entry, cyg_cdir_dir, NULL, NULL );
    
        UNLOCK_FS(cyg_cdir_mtab_entry);

        // We really shouldn't get an error here.
        if( 0 != ret )
            FILEIO_RETURN(ret);
    }
    
    cyg_cdir_mtab_entry = mte;
    cyg_cdir_dir = newdir;
    
    FILEIO_RETURN(ENOERR);
}

//==========================================================================
// Get file statistics

__externC int stat( const char *path, struct stat *buf )
{
    FILEIO_ENTRY();
    
    int ret = 0;
    cyg_mtab_entry *mte = cyg_cdir_mtab_entry;
    cyg_dir dir = cyg_cdir_dir;
    const char *name = path;

    ret = cyg_mtab_lookup( &dir, &name, &mte );
    
    if( 0 != ret )
        FILEIO_RETURN(ENOENT);

    LOCK_FS( mte );
    
    ret = mte->fs->stat( mte, dir, name, buf );
    
    UNLOCK_FS( mte );
    
    FILEIO_RETURN(ret);
}

//==========================================================================
// Get file configurable pathname variables

__externC long pathconf( const char *path, int vname )
{
    FILEIO_ENTRY();
    
    int ret = 0;
    cyg_mtab_entry *mte = cyg_cdir_mtab_entry;
    cyg_dir dir = cyg_cdir_dir;
    const char *name = path;

    ret = cyg_mtab_lookup( &dir, &name, &mte );
    
    if( 0 != ret )
        FILEIO_RETURN(ENOENT);

    struct cyg_pathconf_info info;

    info.name = vname;
    info.value = 0;
        
    LOCK_FS( mte );
    
    ret = mte->fs->getinfo( mte, dir, name,
                            FS_INFO_CONF, (char *)&info, sizeof(info) );
    
    UNLOCK_FS( mte );
    
    if( 0 != ret )
        FILEIO_RETURN(ret);

    FILEIO_RETURN_VALUE(info.value);
}

//==========================================================================
// Sync filesystem without unmounting

__externC int cyg_fs_fssync( const char *path )
{
    FILEIO_ENTRY();
    
    int ret = 0;
    cyg_mtab_entry *mte = cyg_cdir_mtab_entry;
    cyg_dir dir = cyg_cdir_dir;
    const char *name = path;

    ret = cyg_mtab_lookup( &dir, &name, &mte );
    
    if( 0 != ret )
        FILEIO_RETURN(ENOENT);

    LOCK_FS( mte );
    
    ret = mte->fs->setinfo( mte, dir, name, FS_INFO_SYNC, NULL, 0 );
    
    UNLOCK_FS( mte );
    
    if( 0 != ret )
        FILEIO_RETURN(ret);

    FILEIO_RETURN_VALUE(ENOERR);
}

//==========================================================================
// Set file attributes

__externC int cyg_fs_set_attrib( const char *fname, 
                                 const cyg_fs_attrib_t new_attrib )
{
    FILEIO_ENTRY();
    
    int ret = 0;
    cyg_mtab_entry *mte = cyg_cdir_mtab_entry;
    cyg_dir dir = cyg_cdir_dir;
    const char *name = fname;

    ret = cyg_mtab_lookup( &dir, &name, &mte );
    
    if( 0 != ret )
        FILEIO_RETURN(ENOENT);

    LOCK_FS( mte );
    
    ret = mte->fs->setinfo( mte, dir, name,
                            FS_INFO_ATTRIB, 
                            (char *)&new_attrib, sizeof(new_attrib) );
    
    UNLOCK_FS( mte );
    
    if( 0 != ret )
        FILEIO_RETURN(ret);

    FILEIO_RETURN(ENOERR);
}

//==========================================================================
// Get file attributes

__externC int cyg_fs_get_attrib( const char *fname, 
                                 cyg_fs_attrib_t * const file_attrib )
{
    FILEIO_ENTRY();
    
    int ret = 0;
    cyg_mtab_entry *mte = cyg_cdir_mtab_entry;
    cyg_dir dir = cyg_cdir_dir;
    const char *name = fname;

    ret = cyg_mtab_lookup( &dir, &name, &mte );
    
    if( 0 != ret )
        FILEIO_RETURN(ENOENT);

    LOCK_FS( mte );
    
    ret = mte->fs->getinfo( mte, dir, name,
                            FS_INFO_ATTRIB, 
                            (char *)file_attrib, sizeof(*file_attrib) );
    
    UNLOCK_FS( mte );
    
    if( 0 != ret )
        FILEIO_RETURN(ret);

    FILEIO_RETURN(ENOERR);
}

//==========================================================================
// Access() function.
// This simply piggybacks onto stat().

extern int 	access(const char *path, int amode)
{
    FILEIO_ENTRY();

    int ret;
    struct stat buf;
    
    ret = stat( path, &buf );

    // Translate not found into EACCES if the F_OK bit is
    // set.
    if( (amode & F_OK) && (ret < 0) && (errno == ENOENT) )
        FILEIO_RETURN(EACCES);

    // All other errors go straight back to the user.
    if( ret < 0 )
        FILEIO_RETURN_VALUE(ret);

    // At present we do not have any access modes, so there is nothing
    // to test.  Just return success for all access modes.
    
    FILEIO_RETURN(ENOERR);
}

//==========================================================================
// getcwd()

__externC char *getcwd( char *buf, size_t size )
{
    FILEIO_ENTRY();

    int err = ENOERR;
    cyg_mtab_entry *mte = cyg_cdir_mtab_entry;
    cyg_dir dir = cyg_cdir_dir;
    cyg_getcwd_info info;

    if( size == 0 )
    {
        errno = EINVAL;
        FILEIO_RETURN_VALUE(NULL);        
    }
        
    info.buf = buf;
    info.size = size;

    LOCK_FS( mte );
    
    err = mte->fs->getinfo( mte, dir, "",
                            FS_INFO_GETCWD, (char *)&info, sizeof(info) );
    
    UNLOCK_FS( mte );

    if( err == ENOERR )
        FILEIO_RETURN_VALUE(buf);

    // Attempting to use filesystem support for getcwd() has
    // failed.

#ifdef CYGPKG_IO_FILEIO_TRACK_CWD

    // If this option is set, the current directory path has been
    // tracked in chdir(). Just report that value here.
    
    if( size < cwd_size+1 )
    {
        errno = ERANGE;
        FILEIO_RETURN_VALUE(NULL);        
    }

    char *p = my_strcpy( buf, cwd );
    *p = '\0';

#else

    // As a fallback we try to use ".." entries in the directory tree
    // to climb back up to the root.  Because we cannot assume that
    // any filesystem can handle more than one directory pointer we
    // have to do the climbing textually, by manufacturing a path name
    // consisting of ".."s. At each level we then scan the parent
    // directory looking for the entry for the lower level directory
    // by matching st_ino values. This is not guaranteed to work at
    // all since there is no requirement on filesystems to support "."
    // and "..", or for them to report distinct inode values in
    // stat().

    static char ddbuf[PATH_MAX];
    char *p = buf+size-1;
    int ddbufpos;

    // Claim lock to serialize use of ddbuf.
    FILEIO_MUTEX_LOCK(getcwd_lock);

    // Initialize ddbuf with ".".
    ddbuf[0] = '.';
    ddbuf[1] = '\0';
    ddbufpos = 1;

    // Start result buffer with a zero terminator. We accumulate the
    // path name in the top end of the result buffer.
    *p = '\0';
    
    for(;;)
    {
        struct stat sdbuf;
        struct stat sddbuf;

        // Get status for "." and "..". If the filesystem does not
        // support these, then this whole function will fail here.
        
        err = stat( ddbuf, &sdbuf );
        if( err < 0 ) break;

        ddbuf[ddbufpos++] = '/';
        ddbuf[ddbufpos++] = '.';
        ddbuf[ddbufpos++] = '.';
        ddbuf[ddbufpos] = '\0';
        
        err = stat( ddbuf, &sddbuf );
        if( err < 0 ) break;

        // See whether we are at the root. This will be true when
        // the inode numbers of "." and ".." are the same.
        if( sdbuf.st_ino == sddbuf.st_ino )
            break;

        // We now need to find an entry in the ".." directory that
        // matches the inode number of ".".

        struct dirent de;
        DIR *d = opendir( ddbuf );
        if( d == NULL )
        {
            err = -1;
            break;
        }
        
        for(;;)
        {
            struct dirent *res;
            struct stat objstat;
            int i;
            
            err = readdir_r( d, &de, &res );
            if( err < 0 || res == NULL ) break;

            // Skip "." and ".." entries.
            if( pathcmp( de.d_name, "." ) || pathcmp( de.d_name, ".." ) )
                continue;
            
            // Tack the name of the directory entry on to the ddbuf
            // and stat the object.
            
            ddbuf[ddbufpos] = '/';
            for( i = 0; de.d_name[i] != '\0'; i++ )
                ddbuf[ddbufpos+i+1] = de.d_name[i];
            ddbuf[ddbufpos+i+1] = '\0';

            // Take a look at it
            err = stat( ddbuf, &objstat );
            if( err < 0 ) break;

            // Cast out directories
            if( !S_ISDIR(objstat.st_mode) )
                continue;

            // We have a directory. Compare its inode with that of "."
            // and if they are the same, we have found our entry.

            if( sdbuf.st_ino == objstat.st_ino )
                break;
        }

        ddbuf[ddbufpos] = '\0'; // reterminate ddbuf
        
        closedir( d );

        // Halt on any errors.
        if( err < 0 )
            break;

        // Here de contains the name of the directory entry in ".."
        // that has the same inode as ".". Add the name to the path we
        // are accumulating in the buffer.

        char *q = de.d_name;
        while( *q != '\0' ) q++;        // skip to end of name

        do
        {
            *--p = *--q;
        } while( q != de.d_name );

        *--p = '/';                     // add a separator
    }

    // We have finished using ddbuf now.
    FILEIO_MUTEX_UNLOCK(getcwd_lock);
    
    if( err < 0 )
        FILEIO_RETURN_VALUE(NULL);

    // We have the directory path in the top end of the buffer.  Add
    // the mount point name at the beginning and copy the rest of the
    // name down.

    char *bp = buf;
    
    bp = my_strcpy( bp, mte->name );

    // Sort out the separators between the mount name and the
    // pathname.  This is a bit messy since we have to deal with mount
    // names of both "/" and "/foo" and pathnames that start with '/'
    // or are empty.
    if( *(bp-1) != '/' && *p != '\0' ) *bp++ = '/';
    if( *p == '/' ) p++;

    // Now copy the path over.
    while( *p )
        *bp++ = *p++;

    *bp = '\0';                         // Terminate the string

    // All done!
    
#endif
    
    FILEIO_RETURN_VALUE(buf);
}

//==========================================================================
// FS get info.

__externC int cyg_fs_getinfo( const char *path, int key, void *buf, int len )
{
    FILEIO_ENTRY();
    
    int ret = 0;
    cyg_mtab_entry *mte = cyg_cdir_mtab_entry;
    cyg_dir dir = cyg_cdir_dir;
    const char *name = path;
    
    ret = cyg_mtab_lookup( &dir, &name, &mte );
    
    if( 0 != ret )
        FILEIO_RETURN(ENOENT);

    LOCK_FS( mte );
    
    ret = mte->fs->getinfo( mte, dir, name, key, buf, len );
    
    UNLOCK_FS( mte );
    
    FILEIO_RETURN(ret);
}

//==========================================================================
// FS set info.

__externC int cyg_fs_setinfo( const char *path, int key, void *buf, int len )
{
    FILEIO_ENTRY();
    
    int ret = 0;
    cyg_mtab_entry *mte = cyg_cdir_mtab_entry;
    cyg_dir dir = cyg_cdir_dir;
    const char *name = path;
    
    ret = cyg_mtab_lookup( &dir, &name, &mte );
    
    if( 0 != ret )
        FILEIO_RETURN(ENOENT);

    LOCK_FS( mte );
    
    ret = mte->fs->setinfo( mte, dir, name, key, buf, len );
    
    UNLOCK_FS( mte );
    
    FILEIO_RETURN(ret);
}

// -------------------------------------------------------------------------
// EOF file.cxx