1/*
2 * Copyright 2004-2008, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include <fs_query.h>
8
9#include <dirent.h>
10#include <errno.h>
11#include <fcntl.h>
12#include <stdlib.h>
13#include <string.h>
14
15#include <dirent_private.h>
16#include <errno_private.h>
17#include <syscalls.h>
18#include <syscall_utils.h>
19
20
21static DIR *
22open_query_etc(dev_t device, const char *query,
23	uint32 flags, port_id port, int32 token)
24{
25	if (device < 0 || query == NULL || query[0] == '\0') {
26		__set_errno(B_BAD_VALUE);
27		return NULL;
28	}
29
30	// open
31	int fd = _kern_open_query(device, query, strlen(query), flags, port, token);
32	if (fd < 0) {
33		__set_errno(fd);
34		return NULL;
35	}
36
37	// allocate the DIR structure
38	DIR *dir = __create_dir_struct(fd);
39	if (dir == NULL) {
40		_kern_close(fd);
41		return NULL;
42	}
43
44	return dir;
45}
46
47
48DIR *
49fs_open_query(dev_t device, const char *query, uint32 flags)
50{
51	return open_query_etc(device, query, flags, -1, -1);
52}
53
54
55DIR *
56fs_open_live_query(dev_t device, const char *query,
57	uint32 flags, port_id port, int32 token)
58{
59	// check parameters
60	if (port < 0) {
61		__set_errno(B_BAD_VALUE);
62		return NULL;
63	}
64
65	return open_query_etc(device, query, flags | B_LIVE_QUERY, port, token);
66}
67
68
69int
70fs_close_query(DIR *dir)
71{
72	return closedir(dir);
73}
74
75
76struct dirent *
77fs_read_query(DIR *dir)
78{
79	return readdir(dir);
80}
81
82
83status_t
84get_path_for_dirent(struct dirent *dent, char *buffer, size_t length)
85{
86	if (dent == NULL || buffer == NULL)
87		return B_BAD_VALUE;
88
89	return _kern_entry_ref_to_path(dent->d_pdev, dent->d_pino, dent->d_name,
90		buffer, length);
91}
92
93