xref: /haiku/src/add-ons/kernel/file_systems/ufs2/DirectoryIterator.cpp (revision 60a6f1d5d7a8715cd3897dd0b626f2e4a64984a8)
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 = fInode->GetBlockPointer() * MINBSIZE;
28 	TRACE("DirectoryIterator::DirectoryIterator() \n");
29 }
30 
31 
32 DirectoryIterator::~DirectoryIterator()
33 {
34 }
35 
36 
37 status_t
38 DirectoryIterator::InitCheck()
39 {
40 	return B_OK;
41 }
42 
43 
44 status_t
45 DirectoryIterator::Lookup(const char* name, size_t length, ino_t* _id)
46 {
47 	if (strcmp(name, ".") == 0) {
48 		*_id = fInode->ID();
49 		return B_OK;
50 	}
51 
52 	char getname[B_FILE_NAME_LENGTH];
53 
54 	status_t status;
55 	while(true) {
56 		status = GetNext(getname, &length, _id);
57 		if (status != B_OK)
58 			return status;
59 		if (strcmp(getname, name) == 0)
60 			return B_OK;
61 	}
62 
63 }
64 
65 
66 status_t
67 DirectoryIterator::GetNext(char* name, size_t* _nameLength, ino_t* _id)
68 {
69 	dir direct;
70 	int fd = fInode->GetVolume()->Device();
71 
72 	if (read_pos(fd, fOffset, &direct, sizeof(dir)) != sizeof(dir)) {
73 		return B_BAD_DATA;
74 	}
75 
76 	int remainder = direct.namlen % 4;
77 	if(remainder != 0) {
78 		remainder = 4 - remainder;
79 		remainder = direct.namlen + remainder;
80 	} else {
81 		remainder = direct.namlen + 4;
82 	}
83 
84 	fOffset = fOffset + 8 + remainder;
85 
86 	if (direct.next_ino > 0) {
87 		TRACE("direct.next_ino %d\n",direct.next_ino);
88 
89 		strlcpy(name, direct.name, remainder);
90 		*_id = direct.next_ino;
91 		*_nameLength = direct.namlen;
92 		return B_OK;
93 	}
94 
95 	return B_ENTRY_NOT_FOUND;
96 
97 }
98 
99