1/*
2	Copyright 1999-2001, Be Incorporated.   All Rights Reserved.
3	This file may be used under the terms of the Be Sample Code License.
4*/
5/* fake_attributes.c
6 * handles mime type information for ntfs
7 * gets/sets mime information in vnode
8 */
9
10#define MIME_STRING_TYPE 'MIMS'
11
12#include <SupportDefs.h>
13#include <KernelExport.h>
14
15#include <dirent.h>
16#include <file_systems/mime_ext_table.h>
17#include <fs_attr.h>
18#include <string.h>
19#include <malloc.h>
20
21#include "ntfs.h"
22#include "fake_attributes.h"
23
24int32 kOpenTypeCookie = 0;
25int32 kCloseTypeCookie = 1;
26int32 kSetTypeCookie = 0x1234;
27int32 kFreeTypeCookie = 0x87654321;
28
29
30status_t set_mime_type(vnode *node, const char *filename)
31{
32	return set_mime(&node->mime, filename);
33}
34
35
36status_t
37fake_open_attrib_dir(fs_volume *_vol, fs_vnode *_node, void **_cookie)
38{
39	nspace *ns = (nspace *)_vol->private_volume;
40
41	int	result = B_NO_ERROR;
42
43	TRACE("fake_open_attrdir - ENTER\n");
44
45	LOCK_VOL(ns);
46
47	if ((*_cookie = malloc(sizeof(uint32))) == NULL) {
48		result = ENOMEM;
49		goto	exit;
50	}
51
52	*(int32 *)(*_cookie) = kOpenTypeCookie;
53
54exit:
55
56	TRACE("fake_open_attrdir - EXIT, result is %s\n", strerror(result));
57
58	UNLOCK_VOL(ns);
59
60	return result;
61}
62
63
64status_t
65fake_close_attrib_dir(fs_volume *_vol, fs_vnode *_node, void *_cookie)
66{
67	nspace *ns = (nspace *)_vol->private_volume;
68
69	TRACE("fake_close_attrdir - ENTER\n");
70
71	LOCK_VOL(ns);
72
73	*(int32 *)_cookie = kCloseTypeCookie;
74
75	TRACE("fake_close_attrdir - EXIT\n");
76
77	UNLOCK_VOL(ns);
78
79	return B_NO_ERROR;
80}
81
82
83status_t
84fake_free_attrib_dir_cookie(fs_volume *_vol, fs_vnode *_node, void *_cookie)
85{
86	nspace *ns = (nspace *)_vol->private_volume;
87
88	int	result = B_NO_ERROR;
89
90	LOCK_VOL(ns);
91
92	TRACE("fake_free_attrib_dir_cookie - ENTER\n");
93
94	if (_cookie == NULL) {
95		TRACE("fake_free_attrib_dir_cookie - error:called with null cookie\n");
96		result =  EINVAL;
97		goto	exit;
98	}
99
100	*(int32 *)_cookie = kFreeTypeCookie;
101	free(_cookie);
102
103exit:
104
105	TRACE("fake_free_attrib_dir_cookie - EXIT, result is %s\n",
106		strerror(result));
107
108	UNLOCK_VOL(ns);
109
110	return result;
111}
112
113
114status_t
115fake_rewind_attrib_dir(fs_volume *_vol, fs_vnode *_node, void *_cookie)
116{
117	nspace *ns = (nspace *)_vol->private_volume;
118
119	int	result = B_NO_ERROR;
120
121	LOCK_VOL(ns);
122
123	TRACE("fake_rewind_attrcookie  - ENTER\n");
124
125	if (_cookie == NULL) {
126		TRACE("fake_rewind_attrcookie - error: fake_rewind_attrcookie"
127			  "called with null cookie\n");
128		result =  EINVAL;
129		goto	exit;
130	}
131
132	*(uint32 *)_cookie = kOpenTypeCookie;
133
134exit:
135
136	TRACE("fake_rewind_attrcookie - EXIT, result is %s\n", strerror(result));
137
138	UNLOCK_VOL(ns);
139
140	return result;
141}
142
143
144status_t
145fake_read_attrib_dir(fs_volume *_vol, fs_vnode *_node, void *_cookie,
146	struct dirent *entry, size_t bufsize, uint32 *num)
147{
148	nspace *ns = (nspace *)_vol->private_volume;
149	vnode *node = (vnode *)_node->private_node;
150
151	int32 *cookie = (int32 *)_cookie;
152
153	LOCK_VOL(ns);
154
155	TRACE("fake_read_attrdir - ENTER\n");
156
157	*num = 0;
158
159	if ((*cookie == kOpenTypeCookie) && (node->mime)) {
160		*num = 1;
161
162		entry->d_ino = node->vnid;
163		entry->d_dev = ns->id;
164		entry->d_reclen = sizeof(struct dirent) + strlen(kAttrMimeTypeName);
165		strcpy(entry->d_name, kAttrMimeTypeName);
166	}
167
168	*cookie = kCloseTypeCookie;
169
170	TRACE("fake_read_attrdir - EXIT\n");
171
172	UNLOCK_VOL(ns);
173
174	return B_NO_ERROR;
175}
176
177status_t
178fake_create_attrib(fs_volume *_vol, fs_vnode *_node, const char* name,
179	uint32 type, int openMode, void** _cookie)
180{
181	nspace *ns = (nspace *)_vol->private_volume;
182
183	LOCK_VOL(ns);
184
185	TRACE("fake_create_attrib - ENTER (name = [%s])\n",name);
186
187	if (strcmp(name, kAttrMimeTypeName) == 0)
188		*_cookie = &kSetTypeCookie;
189
190	TRACE("fake_create_attrib - EXIT\n");
191
192	UNLOCK_VOL(ns);
193
194	return B_NO_ERROR;
195}
196
197status_t
198fake_open_attrib(fs_volume *_vol, fs_vnode *_node, const char *name,
199	int openMode, void **_cookie)
200{
201	nspace *ns = (nspace *)_vol->private_volume;
202	status_t result = B_NO_ERROR;
203
204	LOCK_VOL(ns);
205
206	TRACE("fake_open_attrib - ENTER (name = [%s])\n",name);
207
208	if (strcmp(name, kAttrMimeTypeName) == 0)
209		*_cookie = &kSetTypeCookie;
210	else
211		result = ENOENT;
212
213	TRACE("fake_open_attrib - EXIT, result is %s\n", strerror(result));
214
215	UNLOCK_VOL(ns);
216
217	return result;
218}
219
220
221status_t
222fake_close_attrib(fs_volume *_vol, fs_vnode *_node, void *cookie)
223{
224	return B_NO_ERROR;
225}
226
227
228status_t
229fake_free_attrib_cookie(fs_volume *_vol, fs_vnode *_node, void *cookie)
230{
231	return B_NO_ERROR;
232}
233
234
235status_t
236fake_read_attrib_stat(fs_volume *_vol, fs_vnode *_node, void *_cookie,
237	struct stat *stat)
238{
239	nspace *ns = (nspace *)_vol->private_volume;
240	vnode *node = (vnode *)_node->private_node;
241	int	result = B_NO_ERROR;
242
243	LOCK_VOL(ns);
244
245	TRACE("fake_read_attr_stat - ENTER\n");
246
247	if (_cookie != &kSetTypeCookie) {
248		result = ENOENT;
249		goto	exit;
250	}
251
252	stat->st_type = MIME_STRING_TYPE;
253	if (node->mime == NULL)
254		stat->st_size = 0;
255	else
256		stat->st_size = strlen(node->mime) + 1;
257
258exit:
259
260	TRACE("fake_read_attrib_stat - EXIT, result is %s\n",
261		strerror(result));
262
263	UNLOCK_VOL(ns);
264
265	return B_NO_ERROR;
266}
267
268
269status_t
270fake_read_attrib(fs_volume *_vol, fs_vnode *_node, void *_cookie,
271	off_t pos,void *buffer, size_t *_length)
272{
273	nspace *ns = (nspace *)_vol->private_volume;
274	vnode *node = (vnode *)_node->private_node;
275
276	int	result = B_NO_ERROR;
277	ssize_t length;
278
279	LOCK_VOL(ns);
280
281	TRACE("fake_read_attr - ENTER\n");
282
283	if (_cookie != &kSetTypeCookie) {
284		result = ENOENT;
285		goto	exit;
286	}
287
288	if (node->mime == NULL) {
289		result = ENOENT;
290		goto	exit;
291	}
292
293	if ((pos < 0) || (pos > strlen(node->mime))) {
294		result = EINVAL;
295		goto	exit;
296	}
297
298	length = user_strlcpy(buffer, node->mime + pos, *_length);
299	if (length < B_OK) {
300		result = B_BAD_ADDRESS;
301		goto exit;
302	}
303	if (length < *_length)
304		*_length = length + 1;
305
306exit:
307
308	TRACE("fake_read_attr - EXIT, result is %s\n", strerror(result));
309
310	UNLOCK_VOL(ns);
311
312	return result;
313}
314
315
316status_t
317fake_write_attrib(fs_volume *_vol, fs_vnode *_node, void *_cookie, off_t pos,
318	const void *buffer, size_t *_length)
319{
320	return B_NO_ERROR;
321}
322