xref: /haiku/src/add-ons/kernel/file_systems/btrfs/AttributeIterator.cpp (revision 16de9db54bf69fe8d02b228ff706026828f9cb7a)
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 
10 //#define TRACE_BTRFS
11 #ifdef TRACE_BTRFS
12 #	define TRACE(x...) dprintf("\33[34mbtrfs:\33[0m " x)
13 #else
14 #	define TRACE(x...) ;
15 #endif
16 #	define ERROR(x...) dprintf("\33[34mbtrfs:\33[0m " x)
17 
18 
19 AttributeIterator::AttributeIterator(Inode* inode)
20 	:
21 	fOffset(-1ULL),
22 	fInode(inode),
23 	fIterator(NULL)
24 {
25 	struct btrfs_key key;
26 	key.SetType(BTRFS_KEY_TYPE_XATTR_ITEM);
27 	key.SetObjectID(inode->ID());
28 	fIterator = new(std::nothrow) TreeIterator(inode->GetVolume()->FSTree(),
29 		key);
30 }
31 
32 
33 AttributeIterator::~AttributeIterator()
34 {
35 	delete fIterator;
36 }
37 
38 
39 status_t
40 AttributeIterator::InitCheck()
41 {
42 	return fIterator != NULL ? B_OK : B_NO_MEMORY;
43 }
44 
45 
46 status_t
47 AttributeIterator::GetNext(char* name, size_t* _nameLength)
48 {
49 	btrfs_key key;
50 	btrfs_dir_entry* entries;
51 	uint32 entries_length;
52 	status_t status = fIterator->GetPreviousEntry(key, (void**)&entries,
53 		&entries_length);
54 	if (status != B_OK)
55 		return status;
56 
57 	btrfs_dir_entry* entry = entries;
58 	uint16 current = 0;
59 	while (current < entries_length) {
60 		current += entry->Length();
61 		break;
62 		// TODO there could be several entries with the same name hash
63 		entry = (btrfs_dir_entry*)((uint8*)entry + entry->Length());
64 	}
65 
66 	TRACE("DirectoryIterator::GetNext() entries_length %ld name_length %d\n",
67 		entries_length, entry->NameLength());
68 
69 	memcpy(name, entry + 1, entry->NameLength());
70 	name[entry->NameLength()] = '\0';
71 	*_nameLength = entry->NameLength();
72 	free(entries);
73 
74 	return B_OK;
75 }
76 
77 
78 status_t
79 AttributeIterator::Rewind()
80 {
81 	fIterator->Rewind();
82 	fOffset = -1ULL;
83 	return B_OK;
84 }
85