1//----------------------------------------------------------------------
2//  This software is part of the OpenBeOS distribution and is covered
3//  by the OpenBeOS license.
4//
5//  Copyright (c) 2003 Tyler Dauwalder, tyler@dauwalder.net
6//---------------------------------------------------------------------
7
8/*! \file DirectoryIterator.cpp
9*/
10
11#include "DirectoryIterator.h"
12
13#include <dirent.h>
14#include <stdio.h>
15
16#include "Icb.h"
17
18#include "UdfString.h"
19#include "Utils.h"
20
21using namespace Udf;
22
23status_t
24DirectoryIterator::GetNextEntry(char *name, uint32 *length, vnode_id *id)
25{
26	DEBUG_INIT_ETC("DirectoryIterator",
27	               ("name: %p, length: %p, id: %p", name, length, id));
28
29	if (!id || !name || !length)
30		return B_BAD_VALUE;
31
32	PRINT(("fPosition:          %Ld\n", fPosition));
33	PRINT(("Parent()->Length(): %Ld\n", Parent()->Length()));
34
35
36	status_t error = B_OK;
37	if (fAtBeginning) {
38		sprintf(name, ".");
39		*length = 2;
40		*id = Parent()->Id();
41		fAtBeginning = false;
42	} else {
43
44	if (uint64(fPosition) >= Parent()->Length())
45		RETURN(B_ENTRY_NOT_FOUND);
46
47	uint8 data[kMaxFileIdSize];
48	file_id_descriptor *entry = reinterpret_cast<file_id_descriptor*>(data);
49
50	uint32 block = 0;
51	off_t offset = fPosition;
52
53	size_t entryLength = kMaxFileIdSize;
54	// First read in the static portion of the file id descriptor,
55	// then, based on the information therein, read in the variable
56	// length tail portion as well.
57	error = Parent()->Read(offset, entry, &entryLength, &block);
58	if (!error && entryLength >= sizeof(file_id_descriptor) && entry->tag().init_check(block) == B_OK) {
59		PDUMP(entry);
60		offset += entry->total_length();
61
62		if (entry->is_parent()) {
63			sprintf(name, "..");
64			*length = 3;
65		} else {
66			String string(entry->id(), entry->id_length());
67			PRINT(("id == `%s'\n", string.Utf8()));
68			DUMP(entry->icb());
69			sprintf(name, "%s", string.Utf8());
70			*length = string.Utf8Length();
71		}
72		*id = to_vnode_id(entry->icb());
73	}
74
75	if (!error)
76		fPosition = offset;
77	}
78
79 	RETURN(error);
80}
81
82/*	\brief Rewinds the iterator to point to the first
83	entry in the directory.
84*/
85void
86DirectoryIterator::Rewind()
87{
88	fPosition = 0;
89	fAtBeginning = true;
90}
91
92DirectoryIterator::DirectoryIterator(Icb *parent)
93	: fParent(parent)
94	, fPosition(0)
95	, fAtBeginning(true)
96{
97}
98
99void
100DirectoryIterator::Invalidate()
101{
102	fParent = NULL;
103}
104
105
106