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