File: dirents_wrapper.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (200 lines) | stat: -rw-r--r-- 5,078 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
 /*
 * Copyright (c) 1997-1999 University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */

#include <oskit/com.h>
#include <oskit/com/wrapper.h>
#include <oskit/c/string.h>
#include <oskit/boolean.h>
#include <oskit/fs/dir.h>
#include <oskit/c/assert.h>
#include <oskit/c/malloc.h>

/*
 * Wrappers for oskit_dirents components.
 */
struct gdirents {
	oskit_dirents_t	direntsi;	/* COM dirents interface */
	int		count;
	oskit_dirents_t	*w_direntsi;	/* Wrapped COM dirents interface */
	void		(*before)(void *);
	void		(*after)(void *);
	void		*cookie;
};

#define WRAPPERCALL(g, ops, iface, call, args...) ({			      \
	oskit_error_t __err;						      \
	g->before(g->cookie);						      \
	__err = oskit_##iface##_##call(g->w_##ops ,##args);        	      \
	DEBUGWRAPPER(iface, call, __err);				      \
	g->after(g->cookie);						      \
	__err; })

#define WRAPPERCALL_NOARGS(g, ops, iface, call) ({			      \
	oskit_error_t __err;						      \
	g->before(g->cookie);						      \
	__err = oskit_##iface##_##call(g->w_##ops);	      	      	      \
	DEBUGWRAPPER(iface, call, __err);				      \
	g->after(g->cookie);						      \
	__err; })

/*
 * oskit_dirents methods
 */
static OSKIT_COMDECL
dirents_query(oskit_dirents_t *d,
	      const struct oskit_guid *iid, void **out_ihandle)
{
	struct gdirents	*dirents = (struct gdirents *) d;

	if (!dirents || !dirents->count)
		return OSKIT_E_INVALIDARG;

	if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
	    memcmp(iid, &oskit_dirents_iid, sizeof(*iid)) == 0) {
		*out_ihandle = &dirents->direntsi;
		dirents->before(dirents->cookie);
		++dirents->count;
		dirents->after(dirents->cookie);
		return 0;
	}
	
	*out_ihandle = NULL;
	return OSKIT_E_NOINTERFACE;
}

static OSKIT_COMDECL_U
dirents_addref(oskit_dirents_t *d)
{
	struct gdirents	*dirents = (struct gdirents *) d;
	unsigned	newcount;
	
	if (!dirents || !dirents->count)
		return OSKIT_E_INVALIDARG;

	dirents->before(dirents->cookie);
	newcount = ++dirents->count;
	dirents->after(dirents->cookie);

	return newcount;
}

static OSKIT_COMDECL_U
dirents_release(oskit_dirents_t *d)
{
	struct gdirents	*dirents = (struct gdirents *) d;
	unsigned	newcount;
	
	if (!dirents || !dirents->count)
		return OSKIT_E_INVALIDARG;

	dirents->before(dirents->cookie);
	if ((newcount = --dirents->count) == 0) {
		assert(dirents->w_direntsi);
		oskit_dirents_release(dirents->w_direntsi);
		dirents->after(dirents->cookie);
		sfree(dirents, sizeof(*dirents));
	}
	else
		dirents->after(dirents->cookie);
		
	return newcount;
}

static OSKIT_COMDECL
dirents_getcount(oskit_dirents_t *d, int *out_count)
{
	struct gdirents	*dirents = (struct gdirents *) d;
	oskit_error_t	err;

	if (!dirents || !dirents->count)
		return OSKIT_E_INVALIDARG;

	err = WRAPPERCALL(dirents, direntsi, dirents, getcount, out_count);

	return err;
}

static OSKIT_COMDECL
dirents_getnext(oskit_dirents_t *d, oskit_dirent_t *out_dirent)
{
	struct gdirents	*dirents = (struct gdirents *) d;
	oskit_error_t	err;

	if (!dirents || !dirents->count)
		return OSKIT_E_INVALIDARG;

	err = WRAPPERCALL(dirents, direntsi, dirents, getnext, out_dirent);

	return err;
}

static OSKIT_COMDECL
dirents_rewind(oskit_dirents_t *d)
{
	struct gdirents	*dirents = (struct gdirents *) d;
	oskit_error_t	err;

	if (!dirents || !dirents->count)
		return OSKIT_E_INVALIDARG;

	err = WRAPPERCALL_NOARGS(dirents, direntsi, dirents, rewind);

	return err;
}

static struct oskit_dirents_ops dirents_ops = {
	dirents_query,
	dirents_addref,
	dirents_release,
	dirents_getcount,
	dirents_getnext,
	dirents_rewind,
};

/*
 * oskit_dirents wrapper constructor.
 */
oskit_error_t
oskit_wrap_dirents(oskit_dirents_t *in,
		   void (*before)(),
		   void (*after)(),
		   void *cookie,
		   oskit_dirents_t **out)
{
        struct gdirents	*newdirents;

	before(cookie);

	newdirents = smalloc(sizeof(*newdirents));
        if (newdirents == NULL)
                return OSKIT_ENOMEM;
        memset(newdirents, 0, sizeof(*newdirents));

        newdirents->count         = 1;
        newdirents->direntsi.ops  = &dirents_ops;
 	newdirents->w_direntsi    = in;
	oskit_dirents_addref(in);

	newdirents->before = before;
	newdirents->after  = after;
	newdirents->cookie = cookie;

	after(cookie);
	
	*out = &newdirents->direntsi;
	return 0;
}