~smartboyhw/wubi/bug-1080090-new

« back to all changes in this revision

Viewing changes to src/grub4dos/stage2/.svn/text-base/fsys_xfs.c.svn-base

  • Committer: Howard Chan
  • Date: 2012-11-20 10:16:05 UTC
  • Revision ID: smartboyhw@gmail.com-20121120101605-qfmjfsdynpzg9an9
Added images

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* fsys_xfs.c - an implementation for the SGI XFS file system */
2
 
/*  
3
 
 *  GRUB  --  GRand Unified Bootloader
4
 
 *  Copyright (C) 2001,2002,2004  Free Software Foundation, Inc.
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; either version 2 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with this program; if not, write to the Free Software
18
 
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
 
 */
20
 
 
21
 
#ifdef FSYS_XFS
22
 
 
23
 
#include "shared.h"
24
 
#include "filesys.h"
25
 
#include "xfs.h"
26
 
 
27
 
#define MAX_LINK_COUNT  8
28
 
 
29
 
typedef struct xad {
30
 
        xfs_fileoff_t offset;
31
 
        xfs_fsblock_t start;
32
 
        xfs_filblks_t len;
33
 
} xad_t;
34
 
 
35
 
struct xfs_info {
36
 
        int bsize;
37
 
        int dirbsize;
38
 
        int isize;
39
 
        unsigned int agblocks;
40
 
        int bdlog;
41
 
        int blklog;
42
 
        int inopblog;
43
 
        int agblklog;
44
 
        int agnolog;
45
 
        unsigned int nextents;
46
 
        xfs_daddr_t next;
47
 
        xfs_daddr_t daddr;
48
 
        xfs_dablk_t forw;
49
 
        xfs_dablk_t dablk;
50
 
        xfs_bmbt_rec_32_t *xt;
51
 
        xfs_bmbt_ptr_t ptr0;
52
 
        int btnode_ptr0_off;
53
 
        int i8param;
54
 
        int dirpos;
55
 
        int dirmax;
56
 
        int blkoff;
57
 
        int fpos;
58
 
        xfs_ino_t rootino;
59
 
};
60
 
 
61
 
static struct xfs_info xfs;
62
 
 
63
 
#define dirbuf          ((char *)FSYS_BUF)
64
 
#define filebuf         ((char *)FSYS_BUF + 4096)
65
 
#define inode           ((xfs_dinode_t *)((char *)FSYS_BUF + 8192))
66
 
#define icore           (inode->di_core)
67
 
 
68
 
#define mask32lo(n)     (((xfs_uint32_t)1 << (n)) - 1)
69
 
 
70
 
#define XFS_INO_MASK(k)         ((xfs_uint32_t)((1ULL << (k)) - 1))
71
 
#define XFS_INO_OFFSET_BITS     xfs.inopblog
72
 
#define XFS_INO_AGBNO_BITS      xfs.agblklog
73
 
#define XFS_INO_AGINO_BITS      (xfs.agblklog + xfs.inopblog)
74
 
#define XFS_INO_AGNO_BITS       xfs.agnolog
75
 
#define NAME_BUF                ((char *)FSYS_BUF + 12288)
76
 
 
77
 
static inline xfs_agblock_t
78
 
agino2agbno (xfs_agino_t agino)
79
 
{
80
 
        return agino >> XFS_INO_OFFSET_BITS;
81
 
}
82
 
 
83
 
static inline xfs_agnumber_t
84
 
ino2agno (xfs_ino_t ino)
85
 
{
86
 
        return ino >> XFS_INO_AGINO_BITS;
87
 
}
88
 
 
89
 
static inline xfs_agino_t
90
 
ino2agino (xfs_ino_t ino)
91
 
{
92
 
        return ino & XFS_INO_MASK(XFS_INO_AGINO_BITS);
93
 
}
94
 
 
95
 
static inline int
96
 
ino2offset (xfs_ino_t ino)
97
 
{
98
 
        return ino & XFS_INO_MASK(XFS_INO_OFFSET_BITS);
99
 
}
100
 
 
101
 
static inline /*__const__*/ xfs_uint16_t
102
 
le16 (xfs_uint16_t x)
103
 
{
104
 
        __asm__("xchgb %b0,%h0" \
105
 
                : "=q" (x) \
106
 
                :  "0" (x));
107
 
        return x;
108
 
}
109
 
 
110
 
static inline /*__const__*/ xfs_uint32_t
111
 
le32 (xfs_uint32_t x)
112
 
{
113
 
#if 0
114
 
        /* 386 doesn't have bswap.  */
115
 
        __asm__("bswap %0" : "=r" (x) : "0" (x));
116
 
#else
117
 
        /* This is slower but this works on all x86 architectures.  */
118
 
        __asm__("xchgb %b0, %h0" \
119
 
                "\n\troll $16, %0" \
120
 
                "\n\txchgb %b0, %h0" \
121
 
                : "=q" (x) : "0" (x));
122
 
#endif
123
 
        return x;
124
 
}
125
 
 
126
 
static inline /*__const__*/ xfs_uint64_t
127
 
le64 (xfs_uint64_t x)
128
 
{
129
 
        xfs_uint32_t h = x >> 32;
130
 
        xfs_uint32_t l = x & ((1ULL<<32)-1);
131
 
        return (((xfs_uint64_t)le32(l)) << 32) | ((xfs_uint64_t)(le32(h)));
132
 
}
133
 
 
134
 
 
135
 
static xfs_fsblock_t
136
 
xt_start (xfs_bmbt_rec_32_t *r)
137
 
{
138
 
        return (((xfs_fsblock_t)(le32 (r->l1) & mask32lo(9))) << 43) | 
139
 
               (((xfs_fsblock_t)le32 (r->l2)) << 11) |
140
 
               (((xfs_fsblock_t)le32 (r->l3)) >> 21);
141
 
}
142
 
 
143
 
static xfs_fileoff_t
144
 
xt_offset (xfs_bmbt_rec_32_t *r)
145
 
{
146
 
        return (((xfs_fileoff_t)le32 (r->l0) &
147
 
                mask32lo(31)) << 23) |
148
 
                (((xfs_fileoff_t)le32 (r->l1)) >> 9);
149
 
}
150
 
 
151
 
static xfs_filblks_t
152
 
xt_len (xfs_bmbt_rec_32_t *r)
153
 
{
154
 
        return le32(r->l3) & mask32lo(21);
155
 
}
156
 
 
157
 
static inline int
158
 
xfs_highbit32(xfs_uint32_t v)
159
 
{
160
 
        int i;
161
 
 
162
 
        if (--v) {
163
 
                for (i = 0; i < 31; i++, v >>= 1) {
164
 
                        if (v == 0)
165
 
                                return i;
166
 
                }
167
 
        }
168
 
        return 0;
169
 
}
170
 
 
171
 
static int
172
 
isinxt (xfs_fileoff_t key, xfs_fileoff_t offset, xfs_filblks_t len)
173
 
{
174
 
        return (key >= offset) ? (key < offset + len ? 1 : 0) : 0;
175
 
}
176
 
 
177
 
static xfs_daddr_t
178
 
agb2daddr (xfs_agnumber_t agno, xfs_agblock_t agbno)
179
 
{
180
 
        return ((xfs_fsblock_t)agno*xfs.agblocks + agbno) << xfs.bdlog;
181
 
}
182
 
 
183
 
static xfs_daddr_t
184
 
fsb2daddr (xfs_fsblock_t fsbno)
185
 
{
186
 
        return agb2daddr ((xfs_agnumber_t)(fsbno >> xfs.agblklog),
187
 
                         (xfs_agblock_t)(fsbno & mask32lo(xfs.agblklog)));
188
 
}
189
 
 
190
 
#undef offsetof
191
 
#define offsetof(t,m)   ((int)&(((t *)0)->m))
192
 
 
193
 
static inline int
194
 
btroot_maxrecs (void)
195
 
{
196
 
        int tmp = icore.di_forkoff ? (icore.di_forkoff << 3) : xfs.isize;
197
 
 
198
 
        return (tmp - sizeof(xfs_bmdr_block_t) - offsetof(xfs_dinode_t, di_u)) /
199
 
                (sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t));
200
 
}
201
 
 
202
 
static int
203
 
di_read (xfs_ino_t ino)
204
 
{
205
 
        xfs_agino_t agino;
206
 
        xfs_agnumber_t agno;
207
 
        xfs_agblock_t agbno;
208
 
        xfs_daddr_t daddr;
209
 
        int offset;
210
 
 
211
 
        agno = ino2agno (ino);
212
 
        agino = ino2agino (ino);
213
 
        agbno = agino2agbno (agino);
214
 
        offset = ino2offset (ino);
215
 
        daddr = agb2daddr (agno, agbno);
216
 
 
217
 
        devread (daddr, offset*xfs.isize, xfs.isize, (char *)inode);
218
 
 
219
 
        xfs.ptr0 = *(xfs_bmbt_ptr_t *)
220
 
                    (inode->di_u.di_c + sizeof(xfs_bmdr_block_t)
221
 
                    + btroot_maxrecs ()*sizeof(xfs_bmbt_key_t));
222
 
 
223
 
        return 1;
224
 
}
225
 
 
226
 
static void
227
 
init_extents (void)
228
 
{
229
 
        xfs_bmbt_ptr_t ptr0;
230
 
        xfs_btree_lblock_t h;
231
 
 
232
 
        switch (icore.di_format) {
233
 
        case XFS_DINODE_FMT_EXTENTS:
234
 
                xfs.xt = inode->di_u.di_bmx;
235
 
                xfs.nextents = le32 (icore.di_nextents);
236
 
                break;
237
 
        case XFS_DINODE_FMT_BTREE:
238
 
                ptr0 = xfs.ptr0;
239
 
                for (;;) {
240
 
                        xfs.daddr = fsb2daddr (le64(ptr0));
241
 
                        devread (xfs.daddr, 0,
242
 
                                 sizeof(xfs_btree_lblock_t), (char *)&h);
243
 
                        if (!h.bb_level) {
244
 
                                xfs.nextents = le16(h.bb_numrecs);
245
 
                                xfs.next = fsb2daddr (le64(h.bb_rightsib));
246
 
                                xfs.fpos = sizeof(xfs_btree_block_t);
247
 
                                return;
248
 
                        }
249
 
                        devread (xfs.daddr, xfs.btnode_ptr0_off,
250
 
                                 sizeof(xfs_bmbt_ptr_t), (char *)&ptr0);
251
 
                }
252
 
        }
253
 
}
254
 
 
255
 
static xad_t *
256
 
next_extent (void)
257
 
{
258
 
        static xad_t xad;
259
 
 
260
 
        switch (icore.di_format) {
261
 
        case XFS_DINODE_FMT_EXTENTS:
262
 
                if (xfs.nextents == 0)
263
 
                        return NULL;
264
 
                break;
265
 
        case XFS_DINODE_FMT_BTREE:
266
 
                if (xfs.nextents == 0) {
267
 
                        xfs_btree_lblock_t h;
268
 
                        if (xfs.next == 0)
269
 
                                return NULL;
270
 
                        xfs.daddr = xfs.next;
271
 
                        devread (xfs.daddr, 0, sizeof(xfs_btree_lblock_t), (char *)&h);
272
 
                        xfs.nextents = le16(h.bb_numrecs);
273
 
                        xfs.next = fsb2daddr (le64(h.bb_rightsib));
274
 
                        xfs.fpos = sizeof(xfs_btree_block_t);
275
 
                }
276
 
                /* Yeah, I know that's slow, but I really don't care */
277
 
                devread (xfs.daddr, xfs.fpos, sizeof(xfs_bmbt_rec_t), filebuf);
278
 
                xfs.xt = (xfs_bmbt_rec_32_t *)filebuf;
279
 
                xfs.fpos += sizeof(xfs_bmbt_rec_32_t);
280
 
        }
281
 
        xad.offset = xt_offset (xfs.xt);
282
 
        xad.start = xt_start (xfs.xt);
283
 
        xad.len = xt_len (xfs.xt);
284
 
        ++xfs.xt;
285
 
        --xfs.nextents;
286
 
 
287
 
        return &xad;
288
 
}
289
 
 
290
 
/*
291
 
 * Name lies - the function reads only first 100 bytes
292
 
 */
293
 
static void
294
 
xfs_dabread (void)
295
 
{
296
 
        xad_t *xad;
297
 
        xfs_fileoff_t offset;;
298
 
 
299
 
        init_extents ();
300
 
        while ((xad = next_extent ())) {
301
 
                offset = xad->offset;
302
 
                if (isinxt (xfs.dablk, offset, xad->len)) {
303
 
                        devread (fsb2daddr (xad->start + xfs.dablk - offset),
304
 
                                 0, 100, dirbuf);
305
 
                        break;
306
 
                }
307
 
        }
308
 
}
309
 
 
310
 
static inline xfs_ino_t
311
 
sf_ino (char *sfe, int namelen)
312
 
{
313
 
        void *p = sfe + namelen + 3;
314
 
 
315
 
        return (xfs.i8param == 0)
316
 
                ? le64(*(xfs_ino_t *)p) : le32(*(xfs_uint32_t *)p);
317
 
}
318
 
 
319
 
static inline xfs_ino_t
320
 
sf_parent_ino (void)
321
 
{
322
 
        return (xfs.i8param == 0)
323
 
                ? le64(*(xfs_ino_t *)(&inode->di_u.di_dir2sf.hdr.parent))
324
 
                : le32(*(xfs_uint32_t *)(&inode->di_u.di_dir2sf.hdr.parent));
325
 
}
326
 
 
327
 
static inline int
328
 
roundup8 (int n)
329
 
{
330
 
        return ((n+7)&~7);
331
 
}
332
 
 
333
 
static char *
334
 
next_dentry (xfs_ino_t *ino)
335
 
{
336
 
        int namelen = 1;
337
 
        int toread;
338
 
        static char usual[2][3] = {".", ".."};
339
 
        static xfs_dir2_sf_entry_t *sfe;
340
 
        char *name = usual[0];
341
 
        int j, k;
342
 
        char ch1;
343
 
//#ifdef GRUB_UTIL
344
 
//      char tmp_name[512];
345
 
//#else
346
 
        char *tmp_name = NAME_BUF;      /* MAXNAMLEN is 255, so 512 byte buffer is needed. */
347
 
//#endif
348
 
 
349
 
        if (xfs.dirpos >= xfs.dirmax) {
350
 
                if (xfs.forw == 0)
351
 
                        return NULL;
352
 
                xfs.dablk = xfs.forw;
353
 
                xfs_dabread ();
354
 
#define h       ((xfs_dir2_leaf_hdr_t *)dirbuf)
355
 
                xfs.dirmax = le16 (h->count) - le16 (h->stale);
356
 
                xfs.forw = le32 (h->info.forw);
357
 
#undef h
358
 
                xfs.dirpos = 0;
359
 
        }
360
 
 
361
 
        switch (icore.di_format) {
362
 
        case XFS_DINODE_FMT_LOCAL:
363
 
                switch (xfs.dirpos) {
364
 
                case -2:
365
 
                        *ino = 0;
366
 
                        break;
367
 
                case -1:
368
 
                        *ino = sf_parent_ino ();
369
 
                        ++name;
370
 
                        ++namelen;
371
 
                        sfe = (xfs_dir2_sf_entry_t *)
372
 
                                (inode->di_u.di_c 
373
 
                                 + sizeof(xfs_dir2_sf_hdr_t)
374
 
                                 - xfs.i8param);
375
 
                        break;
376
 
                default:
377
 
                        namelen = sfe->namelen;
378
 
                        *ino = sf_ino ((char *)sfe, namelen);
379
 
                        name = (char *)(sfe->name);
380
 
                        sfe = (xfs_dir2_sf_entry_t *)
381
 
                                  ((char *)sfe + namelen + 11 - xfs.i8param);
382
 
                }
383
 
                break;
384
 
        case XFS_DINODE_FMT_BTREE:
385
 
        case XFS_DINODE_FMT_EXTENTS:
386
 
#define dau     ((xfs_dir2_data_union_t *)dirbuf)
387
 
                for (;;) {
388
 
                        if (xfs.blkoff >= xfs.dirbsize) {
389
 
                                xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
390
 
                                filepos &= ~(xfs.dirbsize - 1);
391
 
                                filepos |= xfs.blkoff;
392
 
                        }
393
 
                        xfs_read (dirbuf, 4);
394
 
                        xfs.blkoff += 4;
395
 
                        if (dau->unused.freetag == XFS_DIR2_DATA_FREE_TAG) {
396
 
                                toread = roundup8 (le16(dau->unused.length)) - 4;
397
 
                                xfs.blkoff += toread;
398
 
                                filepos += toread;
399
 
                                continue;
400
 
                        }
401
 
                        break;
402
 
                }
403
 
                xfs_read ((char *)dirbuf + 4, 5);
404
 
                *ino = le64 (dau->entry.inumber);
405
 
                namelen = dau->entry.namelen;
406
 
#undef dau
407
 
                toread = roundup8 (namelen + 11) - 9;
408
 
                xfs_read (dirbuf, toread);
409
 
                name = (char *)dirbuf;
410
 
                xfs.blkoff += toread + 5;
411
 
        }
412
 
        ++xfs.dirpos;
413
 
        name[namelen] = 0;
414
 
 
415
 
        /* copy name to tmp_name, and quote the spaces with a '\\' */
416
 
        for (j = 0, k = 0; j < namelen; j++)
417
 
        {
418
 
                if (! (ch1 = name[j]))
419
 
                        break;
420
 
                if (ch1 == ' ')
421
 
                        tmp_name[k++] = '\\';
422
 
                tmp_name[k++] = ch1;
423
 
        }
424
 
        tmp_name[k] = 0;
425
 
        return tmp_name;
426
 
}
427
 
 
428
 
static char *
429
 
first_dentry (xfs_ino_t *ino)
430
 
{
431
 
        xfs.forw = 0;
432
 
        switch (icore.di_format) {
433
 
        case XFS_DINODE_FMT_LOCAL:
434
 
                xfs.dirmax = inode->di_u.di_dir2sf.hdr.count;
435
 
                xfs.i8param = inode->di_u.di_dir2sf.hdr.i8count ? 0 : 4;
436
 
                xfs.dirpos = -2;
437
 
                break;
438
 
        case XFS_DINODE_FMT_EXTENTS:
439
 
        case XFS_DINODE_FMT_BTREE:
440
 
                filepos = 0;
441
 
                xfs_read (dirbuf, sizeof(xfs_dir2_data_hdr_t));
442
 
                if (((xfs_dir2_data_hdr_t *)dirbuf)->magic == le32(XFS_DIR2_BLOCK_MAGIC)) {
443
 
#define tail            ((xfs_dir2_block_tail_t *)dirbuf)
444
 
                        filepos = xfs.dirbsize - sizeof(*tail);
445
 
                        xfs_read (dirbuf, sizeof(*tail));
446
 
                        xfs.dirmax = le32 (tail->count) - le32 (tail->stale);
447
 
#undef tail
448
 
                } else {
449
 
                        xfs.dablk = (1ULL << 35) >> xfs.blklog;
450
 
#define h               ((xfs_dir2_leaf_hdr_t *)dirbuf)
451
 
#define n               ((xfs_da_intnode_t *)dirbuf)
452
 
                        for (;;) {
453
 
                                xfs_dabread ();
454
 
                                if ((n->hdr.info.magic == le16(XFS_DIR2_LEAFN_MAGIC))
455
 
                                    || (n->hdr.info.magic == le16(XFS_DIR2_LEAF1_MAGIC))) {
456
 
                                        xfs.dirmax = le16 (h->count) - le16 (h->stale);
457
 
                                        xfs.forw = le32 (h->info.forw);
458
 
                                        break;
459
 
                                }
460
 
                                xfs.dablk = le32 (n->btree[0].before);
461
 
                        }
462
 
#undef n
463
 
#undef h
464
 
                }
465
 
                xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
466
 
                filepos = xfs.blkoff;
467
 
                xfs.dirpos = 0;
468
 
        }
469
 
        return next_dentry (ino);
470
 
}
471
 
 
472
 
int
473
 
xfs_mount (void)
474
 
{
475
 
#ifdef GRUB_UTIL
476
 
        xfs_sb_t super1;                /* struct size 188 */
477
 
        xfs_sb_t *super = &super1;
478
 
#else
479
 
        xfs_sb_t *super = (xfs_sb_t *)0x600;
480
 
#endif
481
 
 
482
 
        if (!devread (0, 0, sizeof(xfs_sb_t), (char *)super)
483
 
            || (le32(super->sb_magicnum) != XFS_SB_MAGIC)
484
 
            || ((le16(super->sb_versionnum) 
485
 
                & XFS_SB_VERSION_NUMBITS) != XFS_SB_VERSION_4) ) {
486
 
                return 0;
487
 
        }
488
 
 
489
 
        xfs.bsize = le32 (super->sb_blocksize);
490
 
        xfs.blklog = super->sb_blocklog;
491
 
        xfs.bdlog = xfs.blklog - SECTOR_BITS;
492
 
        xfs.rootino = le64 (super->sb_rootino);
493
 
        xfs.isize = le16 (super->sb_inodesize);
494
 
        xfs.agblocks = le32 (super->sb_agblocks);
495
 
        xfs.dirbsize = xfs.bsize << super->sb_dirblklog;
496
 
 
497
 
        xfs.inopblog = super->sb_inopblog;
498
 
        xfs.agblklog = super->sb_agblklog;
499
 
        xfs.agnolog = xfs_highbit32 (le32(super->sb_agcount));
500
 
 
501
 
        xfs.btnode_ptr0_off =
502
 
                ((xfs.bsize - sizeof(xfs_btree_block_t)) /
503
 
                (sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t)))
504
 
                 * sizeof(xfs_bmbt_key_t) + sizeof(xfs_btree_block_t);
505
 
 
506
 
        return 1;
507
 
}
508
 
 
509
 
unsigned long
510
 
xfs_read (char *buf, unsigned long len)
511
 
{
512
 
        xad_t *xad;
513
 
        xfs_fileoff_t endofprev, endofcur, offset;
514
 
        xfs_filblks_t xadlen;
515
 
        unsigned long toread, startpos, endpos;
516
 
 
517
 
        if (icore.di_format == XFS_DINODE_FMT_LOCAL) {
518
 
                grub_memmove (buf, inode->di_u.di_c + filepos, len);
519
 
                filepos += len;
520
 
                return len;
521
 
        }
522
 
 
523
 
        startpos = filepos;
524
 
        endpos = filepos + len;
525
 
        endofprev = (xfs_fileoff_t)-1;
526
 
        init_extents ();
527
 
        while (len > 0 && (xad = next_extent ()))
528
 
        {
529
 
                offset = xad->offset;
530
 
                xadlen = xad->len;
531
 
                if (isinxt (filepos >> xfs.blklog, offset, xadlen)) {
532
 
                        endofcur = (offset + xadlen) << xfs.blklog; 
533
 
                        toread = (endofcur >= endpos)
534
 
                                  ? len : (endofcur - filepos);
535
 
 
536
 
                        disk_read_func = disk_read_hook;
537
 
                        devread (fsb2daddr (xad->start),
538
 
                                 filepos - (offset << xfs.blklog), toread, buf);
539
 
                        disk_read_func = NULL;
540
 
 
541
 
                        buf += toread;
542
 
                        len -= toread;  /* len always >= 0 */
543
 
                        filepos += toread;
544
 
                } else if (offset > endofprev) {
545
 
                        toread = ((offset << xfs.blklog) >= endpos)
546
 
                                  ? len : ((offset - endofprev) << xfs.blklog);
547
 
                        len -= toread;
548
 
                        filepos += toread;
549
 
                        for (; toread; toread--) {
550
 
                                *buf++ = 0;
551
 
                        }
552
 
                        if (len + toread < toread)
553
 
                                break;
554
 
                        continue;
555
 
                }
556
 
                endofprev = offset + xadlen; 
557
 
        }
558
 
 
559
 
        return filepos - startpos;
560
 
}
561
 
 
562
 
int
563
 
xfs_dir (char *dirname)
564
 
{
565
 
        xfs_ino_t ino, parent_ino, new_ino;
566
 
        xfs_fsize_t di_size;
567
 
        unsigned long di_mode;
568
 
        int cmp;
569
 
        unsigned long n, link_count;
570
 
        char *rest, *name, ch;
571
 
 
572
 
#ifdef GRUB_UTIL
573
 
        char linkbuf[xfs.bsize];
574
 
#else
575
 
        char *linkbuf = (char *)(FSYS_BUF - xfs.bsize);
576
 
#endif
577
 
 
578
 
        parent_ino = ino = xfs.rootino;
579
 
        link_count = 0;
580
 
        for (;;) {
581
 
                di_read (ino);
582
 
                di_size = le64 (icore.di_size);
583
 
                di_mode = le16 (icore.di_mode);
584
 
 
585
 
                if ((di_mode & IFMT) == IFLNK) {
586
 
                        if (++link_count > MAX_LINK_COUNT) {
587
 
                                errnum = ERR_SYMLINK_LOOP;
588
 
                                return 0;
589
 
                        }
590
 
                        if (di_size < xfs.bsize - 1) {
591
 
                                filepos = 0;
592
 
                                filemax = di_size;
593
 
                                n = xfs_read (linkbuf, filemax);
594
 
                        } else {
595
 
                                errnum = ERR_FILELENGTH;
596
 
                                return 0;
597
 
                        }
598
 
 
599
 
                        ino = (linkbuf[0] == '/') ? xfs.rootino : parent_ino;
600
 
                        while (n < (xfs.bsize - 1) && (linkbuf[n++] = *dirname++));
601
 
                        linkbuf[n] = 0;
602
 
                        dirname = linkbuf;
603
 
                        continue;
604
 
                }
605
 
 
606
 
                if (!*dirname || isspace (*dirname)) {
607
 
                        if ((di_mode & IFMT) != IFREG) {
608
 
                                errnum = ERR_BAD_FILETYPE;
609
 
                                return 0;
610
 
                        }
611
 
                        filepos = 0;
612
 
                        filemax = di_size;
613
 
                        return 1;
614
 
                }
615
 
 
616
 
                if ((di_mode & IFMT) != IFDIR) {
617
 
                        errnum = ERR_BAD_FILETYPE;
618
 
                        return 0;
619
 
                }
620
 
 
621
 
                for (; *dirname == '/'; dirname++);
622
 
 
623
 
                //for (rest = dirname; (ch = *rest) && !isspace (ch) && ch != '/'; rest++);
624
 
                for (rest = dirname; (ch = *rest) && !isspace (ch) && ch != '/'; rest++)
625
 
                {
626
 
                        if (ch == '\\')
627
 
                        {
628
 
                                rest++;
629
 
                                if (! (ch = *rest))
630
 
                                        break;
631
 
                        }
632
 
                }
633
 
                *rest = 0;
634
 
 
635
 
                name = first_dentry (&new_ino);
636
 
                for (;;) {
637
 
                        cmp = (!*dirname) ? -1 : substring (dirname, name, 0);
638
 
#ifndef STAGE1_5
639
 
                        if (print_possibilities && ch != '/' && cmp <= 0) {
640
 
                                if (print_possibilities > 0)
641
 
                                        print_possibilities = -print_possibilities;
642
 
                                print_a_completion (name);
643
 
                        } else
644
 
#endif
645
 
                        if (cmp == 0) {
646
 
                                parent_ino = ino;
647
 
                                if (new_ino)
648
 
                                        ino = new_ino;
649
 
                                *(dirname = rest) = ch;
650
 
                                break;
651
 
                        }
652
 
                        name = next_dentry (&new_ino);
653
 
                        if (name == NULL) {
654
 
                                if (print_possibilities < 0)
655
 
                                        return 1;
656
 
 
657
 
                                errnum = ERR_FILE_NOT_FOUND;
658
 
                                *rest = ch;
659
 
                                return 0;
660
 
                        }
661
 
                }
662
 
        }
663
 
}
664
 
 
665
 
#endif /* FSYS_XFS */