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#include <common.h>
32#include <malloc.h>
33#include <linux/ctype.h>
34#include <linux/time.h>
35#include <asm/byteorder.h>
36#include <reiserfs.h>
37
38#include "reiserfs_private.h"
39
40#undef REISERDEBUG
41
42
43
44
45
46static char fsys_buf[FSYS_BUFLEN];
47static reiserfs_error_t errnum = ERR_NONE;
48static int print_possibilities;
49static unsigned int filepos, filemax;
50
51static int
52substring (const char *s1, const char *s2)
53{
54 while (*s1 == *s2)
55 {
56
57 if (! *(s1++))
58 return 0;
59 s2 ++;
60 }
61
62
63 if (*s1 == 0)
64 return -1;
65
66
67 return 1;
68}
69
70static void sd_print_item (struct item_head * ih, char * item)
71{
72 char filetime[30];
73 time_t ttime;
74
75 if (stat_data_v1 (ih)) {
76 struct stat_data_v1 * sd = (struct stat_data_v1 *)item;
77 ttime = sd_v1_mtime(sd);
78 ctime_r(&ttime, filetime);
79 printf ("%-10s %4hd %6d %6d %9d %24.24s",
80 bb_mode_string(sd_v1_mode(sd)), sd_v1_nlink(sd),sd_v1_uid(sd), sd_v1_gid(sd),
81 sd_v1_size(sd), filetime);
82 } else {
83 struct stat_data * sd = (struct stat_data *)item;
84 ttime = sd_v2_mtime(sd);
85 ctime_r(&ttime, filetime);
86 printf ("%-10s %4d %6d %6d %9d %24.24s",
87 bb_mode_string(sd_v2_mode(sd)), sd_v2_nlink(sd),sd_v2_uid(sd),sd_v2_gid(sd),
88 (__u32) sd_v2_size(sd), filetime);
89 }
90}
91
92static int
93journal_read (int block, int len, char *buffer)
94{
95 return reiserfs_devread ((INFO->journal_block + block) << INFO->blocksize_shift,
96 0, len, buffer);
97}
98
99
100
101
102
103static int
104block_read (unsigned int blockNr, int start, int len, char *buffer)
105{
106 int transactions = INFO->journal_transactions;
107 int desc_block = INFO->journal_first_desc;
108 int journal_mask = INFO->journal_block_count - 1;
109 int translatedNr = blockNr;
110 __u32 *journal_table = JOURNAL_START;
111 while (transactions-- > 0)
112 {
113 int i = 0;
114 int j_len;
115 if (__le32_to_cpu(*journal_table) != 0xffffffff)
116 {
117
118 j_len = __le32_to_cpu(*journal_table++);
119 while (i++ < j_len)
120 {
121 if (__le32_to_cpu(*journal_table++) == blockNr)
122 {
123 journal_table += j_len - i;
124 goto found;
125 }
126 }
127 }
128 else
129 {
130
131
132
133 struct reiserfs_journal_desc desc;
134 struct reiserfs_journal_commit commit;
135
136 if (! journal_read (desc_block, sizeof (desc), (char *) &desc))
137 return 0;
138
139 j_len = __le32_to_cpu(desc.j_len);
140 while (i < j_len && i < JOURNAL_TRANS_HALF)
141 if (__le32_to_cpu(desc.j_realblock[i++]) == blockNr)
142 goto found;
143
144 if (j_len >= JOURNAL_TRANS_HALF)
145 {
146 int commit_block = (desc_block + 1 + j_len) & journal_mask;
147 if (! journal_read (commit_block,
148 sizeof (commit), (char *) &commit))
149 return 0;
150 while (i < j_len)
151 if (__le32_to_cpu(commit.j_realblock[i++ - JOURNAL_TRANS_HALF]) == blockNr)
152 goto found;
153 }
154 }
155 goto not_found;
156
157 found:
158 translatedNr = INFO->journal_block + ((desc_block + i) & journal_mask);
159#ifdef REISERDEBUG
160 printf ("block_read: block %d is mapped to journal block %d.\n",
161 blockNr, translatedNr - INFO->journal_block);
162#endif
163
164
165
166 not_found:
167 desc_block = (desc_block + 2 + j_len) & journal_mask;
168 }
169 return reiserfs_devread (translatedNr << INFO->blocksize_shift, start, len, buffer);
170}
171
172
173
174
175
176
177
178
179
180static int
181journal_init (void)
182{
183 unsigned int block_count = INFO->journal_block_count;
184 unsigned int desc_block;
185 unsigned int commit_block;
186 unsigned int next_trans_id;
187 struct reiserfs_journal_header header;
188 struct reiserfs_journal_desc desc;
189 struct reiserfs_journal_commit commit;
190 __u32 *journal_table = JOURNAL_START;
191
192 journal_read (block_count, sizeof (header), (char *) &header);
193 desc_block = __le32_to_cpu(header.j_first_unflushed_offset);
194 if (desc_block >= block_count)
195 return 0;
196
197 INFO->journal_first_desc = desc_block;
198 next_trans_id = __le32_to_cpu(header.j_last_flush_trans_id) + 1;
199
200#ifdef REISERDEBUG
201 printf ("journal_init: last flushed %d\n",
202 __le32_to_cpu(header.j_last_flush_trans_id));
203#endif
204
205 while (1)
206 {
207 journal_read (desc_block, sizeof (desc), (char *) &desc);
208 if (substring (JOURNAL_DESC_MAGIC, desc.j_magic) > 0
209 || __le32_to_cpu(desc.j_trans_id) != next_trans_id
210 || __le32_to_cpu(desc.j_mount_id) != __le32_to_cpu(header.j_mount_id))
211
212 break;
213
214 commit_block = (desc_block + __le32_to_cpu(desc.j_len) + 1) & (block_count - 1);
215 journal_read (commit_block, sizeof (commit), (char *) &commit);
216 if (__le32_to_cpu(desc.j_trans_id) != commit.j_trans_id
217 || __le32_to_cpu(desc.j_len) != __le32_to_cpu(commit.j_len))
218
219 break;
220
221#ifdef REISERDEBUG
222 printf ("Found valid transaction %d/%d at %d.\n",
223 __le32_to_cpu(desc.j_trans_id), __le32_to_cpu(desc.j_mount_id), desc_block);
224#endif
225
226 next_trans_id++;
227 if (journal_table < JOURNAL_END)
228 {
229 if ((journal_table + 1 + __le32_to_cpu(desc.j_len)) >= JOURNAL_END)
230 {
231
232
233 *journal_table = __cpu_to_le32(0xffffffff);
234 journal_table = JOURNAL_END;
235 }
236 else
237 {
238 unsigned int i;
239
240
241
242
243
244
245 *journal_table++ = desc.j_len;
246 for (i = 0; i < __le32_to_cpu(desc.j_len) && i < JOURNAL_TRANS_HALF; i++)
247 {
248
249 *journal_table++ = desc.j_realblock[i];
250#ifdef REISERDEBUG
251 printf ("block %d is in journal %d.\n",
252 __le32_to_cpu(desc.j_realblock[i]), desc_block);
253#endif
254 }
255 for ( ; i < __le32_to_cpu(desc.j_len); i++)
256 {
257
258 *journal_table++ = commit.j_realblock[i-JOURNAL_TRANS_HALF];
259#ifdef REISERDEBUG
260 printf ("block %d is in journal %d.\n",
261 __le32_to_cpu(commit.j_realblock[i-JOURNAL_TRANS_HALF]),
262 desc_block);
263#endif
264 }
265 }
266 }
267 desc_block = (commit_block + 1) & (block_count - 1);
268 }
269#ifdef REISERDEBUG
270 printf ("Transaction %d/%d at %d isn't valid.\n",
271 __le32_to_cpu(desc.j_trans_id), __le32_to_cpu(desc.j_mount_id), desc_block);
272#endif
273
274 INFO->journal_transactions
275 = next_trans_id - __le32_to_cpu(header.j_last_flush_trans_id) - 1;
276 return errnum == 0;
277}
278
279
280int
281reiserfs_mount (unsigned part_length)
282{
283 struct reiserfs_super_block super;
284 int superblock = REISERFS_DISK_OFFSET_IN_BYTES >> SECTOR_BITS;
285
286 if (part_length < superblock + (sizeof (super) >> SECTOR_BITS)
287 || ! reiserfs_devread (superblock, 0, sizeof (struct reiserfs_super_block),
288 (char *) &super)
289 || (substring (REISER3FS_SUPER_MAGIC_STRING, super.s_magic) > 0
290 && substring (REISER2FS_SUPER_MAGIC_STRING, super.s_magic) > 0
291 && substring (REISERFS_SUPER_MAGIC_STRING, super.s_magic) > 0)
292 || (
293 sb_journal_block(&super) * sb_blocksize(&super)
294 <= REISERFS_DISK_OFFSET_IN_BYTES))
295 {
296
297 superblock = REISERFS_OLD_DISK_OFFSET_IN_BYTES >> SECTOR_BITS;
298 if (part_length < superblock + (sizeof (super) >> SECTOR_BITS)
299 || ! reiserfs_devread (superblock, 0, sizeof (struct reiserfs_super_block),
300 (char *) &super))
301 return 0;
302
303 if (substring (REISER2FS_SUPER_MAGIC_STRING, super.s_magic) > 0
304 && substring (REISERFS_SUPER_MAGIC_STRING, super.s_magic) > 0)
305 {
306
307 if (substring (REISERFS_SUPER_MAGIC_STRING,
308 (char*) ((int) &super + 20)) > 0)
309 return 0;
310
311 set_sb_blocksize(&super, REISERFS_OLD_BLOCKSIZE);
312 set_sb_journal_block(&super, 0);
313 set_sb_version(&super, 0);
314 }
315 }
316
317
318 if (sb_version(&super) > REISERFS_MAX_SUPPORTED_VERSION)
319 return 0;
320
321 INFO->version = sb_version(&super);
322 INFO->blocksize = sb_blocksize(&super);
323 INFO->fullblocksize_shift = log2 (sb_blocksize(&super));
324 INFO->blocksize_shift = INFO->fullblocksize_shift - SECTOR_BITS;
325 INFO->cached_slots =
326 (FSYSREISER_CACHE_SIZE >> INFO->fullblocksize_shift) - 1;
327
328#ifdef REISERDEBUG
329 printf ("reiserfs_mount: version=%d, blocksize=%d\n",
330 INFO->version, INFO->blocksize);
331#endif
332
333
334 memset (INFO->blocks, 0, sizeof (INFO->blocks));
335
336 if (sb_blocksize(&super) < FSYSREISER_MIN_BLOCKSIZE
337 || sb_blocksize(&super) > FSYSREISER_MAX_BLOCKSIZE
338 || (SECTOR_SIZE << INFO->blocksize_shift) != sb_blocksize(&super))
339 return 0;
340
341
342
343
344 INFO->journal_transactions = 0;
345 if (sb_journal_block(&super) != 0 && super.s_journal_dev == 0)
346 {
347 INFO->journal_block = sb_journal_block(&super);
348 INFO->journal_block_count = sb_journal_size(&super);
349 if (is_power_of_two (INFO->journal_block_count))
350 journal_init ();
351
352
353 block_read (superblock >> INFO->blocksize_shift,
354 0, sizeof (struct reiserfs_super_block), (char *) &super);
355 }
356
357 if (! block_read (sb_root_block(&super), 0, INFO->blocksize, (char*) ROOT))
358 return 0;
359
360 INFO->tree_depth = __le16_to_cpu(BLOCKHEAD (ROOT)->blk_level);
361
362#ifdef REISERDEBUG
363 printf ("root read_in: block=%d, depth=%d\n",
364 sb_root_block(&super), INFO->tree_depth);
365#endif
366
367 if (INFO->tree_depth >= MAX_HEIGHT)
368 return 0;
369 if (INFO->tree_depth == DISK_LEAF_NODE_LEVEL)
370 {
371
372
373 memcpy (LEAF, ROOT, INFO->blocksize);
374 }
375 return 1;
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
402static char *
403read_tree_node (unsigned int blockNr, int depth)
404{
405 char* cache = CACHE(depth);
406 int num_cached = INFO->cached_slots;
407 if (depth < num_cached)
408 {
409
410
411
412 if (blockNr == INFO->blocks[depth])
413 return cache;
414 }
415 else
416 cache = CACHE(num_cached);
417
418#ifdef REISERDEBUG
419 printf (" next read_in: block=%d (depth=%d)\n",
420 blockNr, depth);
421#endif
422 if (! block_read (blockNr, 0, INFO->blocksize, cache))
423 return 0;
424
425 if (__le16_to_cpu(BLOCKHEAD (cache)->blk_level) != depth)
426 {
427 errnum = ERR_FSYS_CORRUPT;
428 return 0;
429 }
430
431 INFO->blocks[depth] = blockNr;
432 return cache;
433}
434
435
436
437
438static int
439next_key (void)
440{
441 int depth;
442 struct item_head *ih = INFO->current_ih + 1;
443 char *cache;
444
445#ifdef REISERDEBUG
446 printf ("next_key:\n old ih: key %d:%d:%d:%d version:%d\n",
447 __le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
448 __le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
449 __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_offset),
450 __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_uniqueness),
451 __le16_to_cpu(INFO->current_ih->ih_version));
452#endif
453
454 if (ih == &ITEMHEAD[__le16_to_cpu(BLOCKHEAD (LEAF)->blk_nr_item)])
455 {
456 depth = DISK_LEAF_NODE_LEVEL;
457
458
459
460 do
461 {
462 if (depth == INFO->tree_depth)
463 {
464
465
466 ih = (struct item_head *) &BLOCKHEAD (LEAF)->blk_right_delim_key;
467 goto found;
468 }
469 depth++;
470#ifdef REISERDEBUG
471 printf (" depth=%d, i=%d\n", depth, INFO->next_key_nr[depth]);
472#endif
473 }
474 while (INFO->next_key_nr[depth] == 0);
475
476 if (depth == INFO->tree_depth)
477 cache = ROOT;
478 else if (depth <= INFO->cached_slots)
479 cache = CACHE (depth);
480 else
481 {
482 cache = read_tree_node (INFO->blocks[depth], depth);
483 if (! cache)
484 return 0;
485 }
486
487 do
488 {
489 int nr_item = __le16_to_cpu(BLOCKHEAD (cache)->blk_nr_item);
490 int key_nr = INFO->next_key_nr[depth]++;
491#ifdef REISERDEBUG
492 printf (" depth=%d, i=%d/%d\n", depth, key_nr, nr_item);
493#endif
494 if (key_nr == nr_item)
495
496 INFO->next_key_nr[depth] = 0;
497
498 cache = read_tree_node (dc_block_number(&(DC (cache)[key_nr])), --depth);
499 if (! cache)
500 return 0;
501 }
502 while (depth > DISK_LEAF_NODE_LEVEL);
503
504 ih = ITEMHEAD;
505 }
506 found:
507 INFO->current_ih = ih;
508 INFO->current_item = &LEAF[__le16_to_cpu(ih->ih_item_location)];
509#ifdef REISERDEBUG
510 printf (" new ih: key %d:%d:%d:%d version:%d\n",
511 __le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
512 __le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
513 __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_offset),
514 __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_uniqueness),
515 __le16_to_cpu(INFO->current_ih->ih_version));
516#endif
517 return 1;
518}
519
520
521
522
523
524
525
526
527
528
529
530static int
531search_stat (__u32 dir_id, __u32 objectid)
532{
533 char *cache;
534 int depth;
535 int nr_item;
536 int i;
537 struct item_head *ih;
538#ifdef REISERDEBUG
539 printf ("search_stat:\n key %d:%d:0:0\n", dir_id, objectid);
540#endif
541
542 depth = INFO->tree_depth;
543 cache = ROOT;
544
545 while (depth > DISK_LEAF_NODE_LEVEL)
546 {
547 struct key *key;
548 nr_item = __le16_to_cpu(BLOCKHEAD (cache)->blk_nr_item);
549
550 key = KEY (cache);
551
552 for (i = 0; i < nr_item; i++)
553 {
554 if (__le32_to_cpu(key->k_dir_id) > dir_id
555 || (__le32_to_cpu(key->k_dir_id) == dir_id
556 && (__le32_to_cpu(key->k_objectid) > objectid
557 || (__le32_to_cpu(key->k_objectid) == objectid
558 && (__le32_to_cpu(key->u.v1.k_offset)
559 | __le32_to_cpu(key->u.v1.k_uniqueness)) > 0))))
560 break;
561 key++;
562 }
563
564#ifdef REISERDEBUG
565 printf (" depth=%d, i=%d/%d\n", depth, i, nr_item);
566#endif
567 INFO->next_key_nr[depth] = (i == nr_item) ? 0 : i+1;
568 cache = read_tree_node (dc_block_number(&(DC (cache)[i])), --depth);
569 if (! cache)
570 return 0;
571 }
572
573
574 nr_item = __le16_to_cpu(BLOCKHEAD (LEAF)->blk_nr_item);
575 ih = ITEMHEAD;
576 for (i = 0; i < nr_item; i++)
577 {
578 if (__le32_to_cpu(ih->ih_key.k_dir_id) == dir_id
579 && __le32_to_cpu(ih->ih_key.k_objectid) == objectid
580 && __le32_to_cpu(ih->ih_key.u.v1.k_offset) == 0
581 && __le32_to_cpu(ih->ih_key.u.v1.k_uniqueness) == 0)
582 {
583#ifdef REISERDEBUG
584 printf (" depth=%d, i=%d/%d\n", depth, i, nr_item);
585#endif
586 INFO->current_ih = ih;
587 INFO->current_item = &LEAF[__le16_to_cpu(ih->ih_item_location)];
588 return 1;
589 }
590 ih++;
591 }
592 errnum = ERR_FSYS_CORRUPT;
593 return 0;
594}
595
596int
597reiserfs_read (char *buf, unsigned len)
598{
599 unsigned int blocksize;
600 unsigned int offset;
601 unsigned int to_read;
602 char *prev_buf = buf;
603
604#ifdef REISERDEBUG
605 printf ("reiserfs_read: filepos=%d len=%d, offset=%Lx\n",
606 filepos, len, (__u64) IH_KEY_OFFSET (INFO->current_ih) - 1);
607#endif
608
609 if (__le32_to_cpu(INFO->current_ih->ih_key.k_objectid) != INFO->fileinfo.k_objectid
610 || IH_KEY_OFFSET (INFO->current_ih) > filepos + 1)
611 {
612 search_stat (INFO->fileinfo.k_dir_id, INFO->fileinfo.k_objectid);
613 goto get_next_key;
614 }
615
616 while (! errnum)
617 {
618 if (__le32_to_cpu(INFO->current_ih->ih_key.k_objectid) != INFO->fileinfo.k_objectid) {
619 break;
620 }
621
622 offset = filepos - IH_KEY_OFFSET (INFO->current_ih) + 1;
623 blocksize = __le16_to_cpu(INFO->current_ih->ih_item_len);
624
625#ifdef REISERDEBUG
626 printf (" loop: filepos=%d len=%d, offset=%d blocksize=%d\n",
627 filepos, len, offset, blocksize);
628#endif
629
630 if (IH_KEY_ISTYPE(INFO->current_ih, TYPE_DIRECT)
631 && offset < blocksize)
632 {
633#ifdef REISERDEBUG
634 printf ("direct_read: offset=%d, blocksize=%d\n",
635 offset, blocksize);
636#endif
637 to_read = blocksize - offset;
638 if (to_read > len)
639 to_read = len;
640
641 memcpy (buf, INFO->current_item + offset, to_read);
642 goto update_buf_len;
643 }
644 else if (IH_KEY_ISTYPE(INFO->current_ih, TYPE_INDIRECT))
645 {
646 blocksize = (blocksize >> 2) << INFO->fullblocksize_shift;
647#ifdef REISERDEBUG
648 printf ("indirect_read: offset=%d, blocksize=%d\n",
649 offset, blocksize);
650#endif
651
652 while (offset < blocksize)
653 {
654 __u32 blocknr = __le32_to_cpu(((__u32 *) INFO->current_item)
655 [offset >> INFO->fullblocksize_shift]);
656 int blk_offset = offset & (INFO->blocksize-1);
657 to_read = INFO->blocksize - blk_offset;
658 if (to_read > len)
659 to_read = len;
660
661
662
663
664 reiserfs_devread (blocknr << INFO->blocksize_shift,
665 blk_offset, to_read, buf);
666 update_buf_len:
667 len -= to_read;
668 buf += to_read;
669 offset += to_read;
670 filepos += to_read;
671 if (len == 0)
672 goto done;
673 }
674 }
675 get_next_key:
676 next_key ();
677 }
678 done:
679 return errnum ? 0 : buf - prev_buf;
680}
681
682
683
684
685
686
687
688
689
690static int
691reiserfs_dir (char *dirname)
692{
693 struct reiserfs_de_head *de_head;
694 char *rest, ch;
695 __u32 dir_id, objectid, parent_dir_id = 0, parent_objectid = 0;
696#ifndef STAGE1_5
697 int do_possibilities = 0;
698#endif
699 char linkbuf[PATH_MAX];
700 int link_count = 0;
701 int mode;
702
703 dir_id = REISERFS_ROOT_PARENT_OBJECTID;
704 objectid = REISERFS_ROOT_OBJECTID;
705
706 while (1)
707 {
708#ifdef REISERDEBUG
709 printf ("dirname=%s\n", dirname);
710#endif
711
712
713 if (! search_stat (dir_id, objectid))
714 return 0;
715
716#ifdef REISERDEBUG
717 printf ("sd_mode=%x sd_size=%d\n",
718 stat_data_v1(INFO->current_ih) ? sd_v1_mode((struct stat_data_v1 *) INFO->current_item) :
719 sd_v2_mode((struct stat_data *) (INFO->current_item)),
720 stat_data_v1(INFO->current_ih) ? sd_v1_size((struct stat_data_v1 *) INFO->current_item) :
721 sd_v2_size((struct stat_data *) INFO->current_item)
722 );
723
724#endif
725 mode = stat_data_v1(INFO->current_ih) ?
726 sd_v1_mode((struct stat_data_v1 *) INFO->current_item) :
727 sd_v2_mode((struct stat_data *) INFO->current_item);
728
729
730 if (S_ISLNK (mode))
731 {
732 unsigned int len;
733 if (++link_count > MAX_LINK_COUNT)
734 {
735 errnum = ERR_SYMLINK_LOOP;
736 return 0;
737 }
738
739
740 filemax = stat_data_v1(INFO->current_ih) ?
741 sd_v1_size((struct stat_data_v1 *) INFO->current_item) :
742 sd_v2_size((struct stat_data *) INFO->current_item);
743
744
745 len = 0;
746 while (dirname[len] && !isspace (dirname[len]))
747 len++;
748
749 if (filemax + len > sizeof (linkbuf) - 1)
750 {
751 errnum = ERR_FILELENGTH;
752 return 0;
753 }
754
755
756
757 memmove (linkbuf + filemax, dirname, len+1);
758
759 INFO->fileinfo.k_dir_id = dir_id;
760 INFO->fileinfo.k_objectid = objectid;
761 filepos = 0;
762 if (! next_key ()
763 || reiserfs_read (linkbuf, filemax) != filemax)
764 {
765 if (! errnum)
766 errnum = ERR_FSYS_CORRUPT;
767 return 0;
768 }
769
770#ifdef REISERDEBUG
771 printf ("symlink=%s\n", linkbuf);
772#endif
773
774 dirname = linkbuf;
775 if (*dirname == '/')
776 {
777
778 dir_id = REISERFS_ROOT_PARENT_OBJECTID;
779 objectid = REISERFS_ROOT_OBJECTID;
780 }
781 else
782 {
783
784 dir_id = parent_dir_id;
785 objectid = parent_objectid;
786 }
787
788
789 continue;
790 }
791
792
793
794
795 if (! *dirname || isspace (*dirname))
796 {
797 if (! S_ISREG (mode))
798 {
799 errnum = ERR_BAD_FILETYPE;
800 return 0;
801 }
802
803 filepos = 0;
804 filemax = stat_data_v1(INFO->current_ih) ?
805 sd_v1_size((struct stat_data_v1 *) INFO->current_item) :
806 sd_v2_size((struct stat_data *) INFO->current_item);
807#if 0
808
809
810
811 if (__le16_to_cpu(INFO->current_ih->ih_version) == ITEM_VERSION_2
812 && sd_size_hi((struct stat_data *) INFO->current_item) > 0)
813 filemax = 0xffffffff;
814#endif
815 INFO->fileinfo.k_dir_id = dir_id;
816 INFO->fileinfo.k_objectid = objectid;
817 return next_key ();
818 }
819
820
821 while (*dirname == '/')
822 dirname++;
823 if (! S_ISDIR (mode))
824 {
825 errnum = ERR_BAD_FILETYPE;
826 return 0;
827 }
828 for (rest = dirname; (ch = *rest) && ! isspace (ch) && ch != '/'; rest++);
829 *rest = 0;
830
831# ifndef STAGE1_5
832 if (print_possibilities && ch != '/')
833 do_possibilities = 1;
834# endif
835
836 while (1)
837 {
838 char *name_end;
839 int num_entries;
840
841 if (! next_key ())
842 return 0;
843#ifdef REISERDEBUG
844 printf ("ih: key %d:%d:%d:%d version:%d\n",
845 __le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
846 __le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
847 __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_offset),
848 __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_uniqueness),
849 __le16_to_cpu(INFO->current_ih->ih_version));
850#endif
851
852 if (__le32_to_cpu(INFO->current_ih->ih_key.k_objectid) != objectid)
853 break;
854
855 name_end = INFO->current_item + __le16_to_cpu(INFO->current_ih->ih_item_len);
856 de_head = (struct reiserfs_de_head *) INFO->current_item;
857 num_entries = __le16_to_cpu(INFO->current_ih->u.ih_entry_count);
858 while (num_entries > 0)
859 {
860 char *filename = INFO->current_item + deh_location(de_head);
861 char tmp = *name_end;
862 if ((deh_state(de_head) & DEH_Visible))
863 {
864 int cmp;
865
866
867
868
869
870
871 *name_end = 0;
872 cmp = substring (dirname, filename);
873 *name_end = tmp;
874# ifndef STAGE1_5
875 if (do_possibilities)
876 {
877 if (cmp <= 0)
878 {
879 char fn[PATH_MAX];
880 struct fsys_reiser_info info_save;
881
882 if (print_possibilities > 0)
883 print_possibilities = -print_possibilities;
884 *name_end = 0;
885 strcpy(fn, filename);
886 *name_end = tmp;
887
888
889 if (strcmp (fn, ".") != 0 && strcmp (fn, "..") != 0) {
890 memcpy(&info_save, INFO, sizeof(struct fsys_reiser_info));
891 search_stat (deh_dir_id(de_head), deh_objectid(de_head));
892 sd_print_item(INFO->current_ih, INFO->current_item);
893 printf(" %s\n", fn);
894 search_stat (dir_id, objectid);
895 memcpy(INFO, &info_save, sizeof(struct fsys_reiser_info));
896 }
897 }
898 }
899 else
900# endif
901 if (cmp == 0)
902 goto found;
903 }
904
905
906 name_end = filename;
907 de_head++;
908 num_entries--;
909 }
910 }
911
912# ifndef STAGE1_5
913 if (print_possibilities < 0)
914 return 1;
915# endif
916
917 errnum = ERR_FILE_NOT_FOUND;
918 *rest = ch;
919 return 0;
920
921 found:
922 *rest = ch;
923 dirname = rest;
924
925 parent_dir_id = dir_id;
926 parent_objectid = objectid;
927 dir_id = deh_dir_id(de_head);
928 objectid = deh_objectid(de_head);
929 }
930}
931
932
933
934
935
936
937
938
939
940
941int
942reiserfs_ls (char *dirname)
943{
944 char *dir_slash;
945 int res;
946
947 errnum = 0;
948 dir_slash = malloc(strlen(dirname) + 1);
949 if (dir_slash == NULL) {
950 return ERR_NUMBER_OVERFLOW;
951 }
952 strcpy(dir_slash, dirname);
953
954 strcat(dir_slash, "/");
955
956 print_possibilities = 1;
957 res = reiserfs_dir (dir_slash);
958 free(dir_slash);
959 if (!res || errnum) {
960 return errnum;
961 }
962
963 return 0;
964}
965
966
967
968
969
970
971
972int
973reiserfs_open (char *filename)
974{
975
976 errnum = 0;
977 print_possibilities = 0;
978 if (!reiserfs_dir (filename) || errnum) {
979 return -errnum;
980 }
981 return filemax;
982}
983