fs_query.cpp revision adbf8b25
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 <syscalls.h>
16#include <syscall_utils.h>
17
18
19static DIR *
20open_query_etc(dev_t device, const char *query,
21	uint32 flags, port_id port, int32 token)
22{
23	if (device < 0 || query == NULL || query[0] == '\0') {
24		errno = B_BAD_VALUE;
25		return NULL;
26	}
27
28	// open
29	int fd = _kern_open_query(device, query, strlen(query), flags, port, token);
30	if (fd < 0) {
31		errno = fd;
32		return NULL;
33	}
34
35	// allocate the DIR structure
36	DIR *dir = fdopendir(fd);
37	if (dir == NULL) {
38		_kern_close(fd);
39		return NULL;
40	}
41
42	return dir;
43}
44
45
46DIR *
47fs_open_query(dev_t device, const char *query, uint32 flags)
48{
49	return open_query_etc(device, query, flags, -1, -1);
50}
51
52
53DIR *
54fs_open_live_query(dev_t device, const char *query,
55	uint32 flags, port_id port, int32 token)
56{
57	// check parameters
58	if (port < 0) {
59		errno = B_BAD_VALUE;
60		return NULL;
61	}
62
63	return open_query_etc(device, query, flags | B_LIVE_QUERY, port, token);
64}
65
66
67int
68fs_close_query(DIR *dir)
69{
70	return closedir(dir);
71}
72
73
74struct dirent *
75fs_read_query(DIR *dir)
76{
77	return readdir(dir);
78}
79
80
81status_t
82get_path_for_dirent(struct dirent *dent, char *buffer, size_t length)
83{
84	if (dent == NULL || buffer == NULL)
85		return B_BAD_VALUE;
86
87	return _kern_entry_ref_to_path(dent->d_pdev, dent->d_pino, dent->d_name,
88		buffer, length);
89}
90
91