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