xref: /haiku/src/add-ons/kernel/file_systems/ufs2/DirectoryIterator.cpp (revision a127b88ecbfab58f64944c98aa47722a18e363b2)
1 /*
2  * Copyright 2020 Suhel Mehta, mehtasuhel@gmail.com
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "DirectoryIterator.h"
8 
9 #include <stdlib.h>
10 
11 #include "Inode.h"
12 
13 #define TRACE_UFS2
14 #ifdef TRACE_UFS2
15 #	define TRACE(x...) dprintf("\33[34mufs2:\33[0m " x)
16 #else
17 #	define TRACE(x...) ;
18 #endif
19 
20 #define ERROR(x...) dprintf("\33[34mufs2:\33[0m " x)
21 
22 
23 DirectoryIterator::DirectoryIterator(Inode* inode)
24 	:
25 	fInode(inode)
26 {
27 	fOffset = 0;
28 }
29 
30 
31 DirectoryIterator::~DirectoryIterator()
32 {
33 }
34 
35 
36 status_t
37 DirectoryIterator::InitCheck()
38 {
39 	return B_OK;
40 }
41 
42 
43 status_t
44 DirectoryIterator::Lookup(const char* name, size_t length, ino_t* _id)
45 {
46 	if (strcmp(name, ".") == 0) {
47 		*_id = fInode->ID();
48 		return B_OK;
49 	}
50 
51 	char getname[B_FILE_NAME_LENGTH];
52 
53 	status_t status;
54 	while(true) {
55 		status = GetNext(getname, &length, _id);
56 		if (status != B_OK)
57 			return status;
58 		if (strcmp(getname, name) == 0)
59 			return B_OK;
60 	}
61 
62 }
63 
64 
65 status_t
66 DirectoryIterator::GetNext(char* name, size_t* _nameLength, ino_t* _id)
67 {
68 	dir direct;
69 	size_t size = sizeof(dir);
70 	status_t status = fInode->ReadAt(fOffset, (uint8_t*)&direct, &size);
71 	if (status == B_OK) {
72 		int remainder = direct.namlen % 4;
73 		if(remainder != 0) {
74 			remainder = 4 - remainder;
75 			remainder = direct.namlen + remainder;
76 		} else {
77 			remainder = direct.namlen + 4;
78 		}
79 
80 		fOffset = fOffset + 8 + remainder;
81 
82 		if (direct.next_ino > 0) {
83 			if ((direct.namlen + 1) > *_nameLength)
84 				return B_BUFFER_OVERFLOW;
85 			strlcpy(name, direct.name, direct.namlen + 1);
86 			*_id = direct.next_ino;
87 			*_nameLength = direct.namlen;
88 			return B_OK;
89 		}
90 
91 		return B_ENTRY_NOT_FOUND;
92 	}
93 
94 	return B_ERROR;
95 }
96 
97