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