1/*
2 * Copyright 2011, Jérôme Duval, korli@users.berlios.de.
3 * This file may be used under the terms of the MIT License.
4 */
5
6
7#include "AttributeIterator.h"
8
9#include <new>
10#include <stdlib.h>
11
12
13//#define TRACE_BTRFS
14#ifdef TRACE_BTRFS
15#	define TRACE(x...) dprintf("\33[34mbtrfs:\33[0m " x)
16#else
17#	define TRACE(x...) ;
18#endif
19#	define ERROR(x...) dprintf("\33[34mbtrfs:\33[0m " x)
20
21
22AttributeIterator::AttributeIterator(Inode* inode)
23	:
24	fOffset(-1ULL),
25	fInode(inode),
26	fIterator(NULL)
27{
28	struct btrfs_key key;
29	key.SetType(BTRFS_KEY_TYPE_XATTR_ITEM);
30	key.SetObjectID(inode->ID());
31	fIterator = new(std::nothrow) TreeIterator(inode->GetVolume()->FSTree(),
32		key);
33}
34
35
36AttributeIterator::~AttributeIterator()
37{
38	delete fIterator;
39}
40
41
42status_t
43AttributeIterator::InitCheck()
44{
45	return fIterator != NULL ? B_OK : B_NO_MEMORY;
46}
47
48
49status_t
50AttributeIterator::GetNext(char* name, size_t* _nameLength)
51{
52	btrfs_key key;
53	btrfs_dir_entry *entries;
54	size_t entries_length;
55	status_t status = fIterator->GetPreviousEntry(key, (void**)&entries,
56		&entries_length);
57	if (status != B_OK)
58		return status;
59
60	btrfs_dir_entry *entry = entries;
61	uint16 current = 0;
62	while (current < entries_length) {
63		current += entry->Length();
64		break;
65		// TODO there could be several entries with the same name hash
66		entry = (btrfs_dir_entry *)((uint8*)entry + entry->Length());
67	}
68
69	TRACE("DirectoryIterator::GetNext() entries_length %ld name_length %d\n",
70		entries_length, entry->NameLength());
71
72	memcpy(name, entry + 1, entry->NameLength());
73	name[entry->NameLength()] = '\0';
74	*_nameLength = entry->NameLength();
75	free(entries);
76
77	return B_OK;
78}
79
80
81status_t
82AttributeIterator::Rewind()
83{
84	fIterator->Rewind();
85	fOffset = -1ULL;
86	return B_OK;
87}
88
89