xref: /haiku/src/system/libroot/os/fs_index.c (revision 1e36cfc2721ef13a187c6f7354dc9cbc485e89d3)
1 /*
2 ** Copyright 2002, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 ** Distributed under the terms of the OpenBeOS License.
4 */
5 
6 
7 #include <fs_index.h>
8 
9 #include <stdlib.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 
13 #include "syscalls.h"
14 
15 
16 #define RETURN_AND_SET_ERRNO(status) \
17 	{ \
18 		if (status < 0) { \
19 			errno = status; \
20 			return -1; \
21 		} \
22 		return status; \
23 	}
24 
25 // for the DIR structure
26 #define BUFFER_SIZE 2048
27 
28 
29 int
30 fs_create_index(dev_t device, const char *name, uint32 type, uint32 flags)
31 {
32 	status_t status = _kern_create_index(device, name, type, flags);
33 
34 	RETURN_AND_SET_ERRNO(status);
35 }
36 
37 
38 int
39 fs_remove_index(dev_t device, const char *name)
40 {
41 	status_t status = _kern_remove_index(device, name);
42 
43 	RETURN_AND_SET_ERRNO(status);
44 }
45 
46 
47 int
48 fs_stat_index(dev_t device, const char *name, struct index_info *indexInfo)
49 {
50 	struct stat stat;
51 
52 	status_t status = _kern_read_index_stat(device, name, &stat);
53 	if (status == B_OK) {
54 		indexInfo->type = stat.st_type;
55 		indexInfo->size = stat.st_size;
56 		indexInfo->modification_time = stat.st_mtime;
57 		indexInfo->creation_time = stat.st_crtime;
58 		indexInfo->uid = stat.st_uid;
59 		indexInfo->gid = stat.st_gid;
60 	}
61 
62 	RETURN_AND_SET_ERRNO(status);
63 }
64 
65 
66 DIR *
67 fs_open_index_dir(dev_t device)
68 {
69 	DIR *dir;
70 
71 	int fd = _kern_open_index_dir(device);
72 	if (fd < 0) {
73 		errno = fd;
74 		return NULL;
75 	}
76 
77 	/* allocate the memory for the DIR structure */
78 	if ((dir = (DIR *)malloc(sizeof(DIR) + BUFFER_SIZE)) == NULL) {
79 		errno = B_NO_MEMORY;
80 		_kern_close(fd);
81 		return NULL;
82 	}
83 
84 	dir->fd = fd;
85 
86 	return dir;
87 }
88 
89 
90 int
91 fs_close_index_dir(DIR *dir)
92 {
93 	int status = _kern_close(dir->fd);
94 
95 	free(dir);
96 
97 	RETURN_AND_SET_ERRNO(status);
98 }
99 
100 
101 struct dirent *
102 fs_read_index_dir(DIR *dir)
103 {
104 	ssize_t count = _kern_read_dir(dir->fd, &dir->ent, BUFFER_SIZE, 1);
105 	if (count <= 0) {
106 		if (count < 0)
107 			errno = count;
108 		return NULL;
109 	}
110 
111 	return &dir->ent;
112 }
113 
114 
115 void
116 fs_rewind_index_dir(DIR *dir)
117 {
118 	int status = _kern_rewind_dir(dir->fd);
119 	if (status < 0)
120 		errno = status;
121 }
122 
123