File: bonobo-storage.c

package info (click to toggle)
bonobo 1.0.22-2.2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,412 kB
  • ctags: 5,445
  • sloc: ansic: 51,714; sh: 7,733; makefile: 1,425; yacc: 318; xml: 266; sed: 16
file content (398 lines) | stat: -rw-r--r-- 10,147 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * bonobo-storage.c: Storage manipulation.
 *
 * Authors:
 *   Miguel de Icaza (miguel@gnu.org)
 *   Dietmar Maurer (dietmar@maurer-it.com)
 *
 * Copyright 1999 Helix Code, Inc.
 */
#include <config.h>
#include <gmodule.h>

#include <bonobo/bonobo-storage.h>
#include <bonobo/bonobo-exception.h>
#include <bonobo/bonobo-storage-plugin.h>

#define PARENT_TYPE BONOBO_X_OBJECT_TYPE

#define CLASS(o) BONOBO_STORAGE_CLASS (GTK_OBJECT(o)->klass)

static inline BonoboStorage *
bonobo_storage_from_servant (PortableServer_Servant servant)
{
	return BONOBO_STORAGE (bonobo_object_from_servant (servant));
}

static Bonobo_StorageInfo*
impl_Bonobo_Storage_getInfo (PortableServer_Servant servant,
			     const CORBA_char * path,
			     const Bonobo_StorageInfoFields mask,
			     CORBA_Environment *ev)
{
	BonoboStorage *storage = bonobo_storage_from_servant (servant);

	return CLASS (storage)->get_info (storage, path, mask, ev);
}

static void          
impl_Bonobo_Storage_setInfo (PortableServer_Servant servant,
			     const CORBA_char * path,
			     const Bonobo_StorageInfo *info,
			     const Bonobo_StorageInfoFields mask,
			     CORBA_Environment *ev)
{
	BonoboStorage *storage = bonobo_storage_from_servant (servant);

	CLASS (storage)->set_info (storage, path, info, mask, ev);
}

static Bonobo_Stream
impl_Bonobo_Storage_openStream (PortableServer_Servant servant,
				const CORBA_char       *path,
				Bonobo_Storage_OpenMode mode,
				CORBA_Environment      *ev)
{
	BonoboStorage *storage = bonobo_storage_from_servant (servant);
	BonoboStream *stream;
	
	if ((stream = CLASS (storage)->open_stream (storage, path, mode, ev)))
		return (Bonobo_Stream) CORBA_Object_duplicate (
			BONOBO_OBJREF (stream), ev);
	else
		return CORBA_OBJECT_NIL;
}

static Bonobo_Storage
impl_Bonobo_Storage_openStorage (PortableServer_Servant  servant,
				 const CORBA_char       *path,
				 Bonobo_Storage_OpenMode mode,
				 CORBA_Environment      *ev)
{
	BonoboStorage *storage = bonobo_storage_from_servant (servant);
	BonoboStorage *open_storage;
	
	if ((open_storage = CLASS (storage)->open_storage (
		storage, path, mode, ev)))

		return (Bonobo_Storage) CORBA_Object_duplicate (
			BONOBO_OBJREF (open_storage), ev);
	else
		return CORBA_OBJECT_NIL;
}

static void
impl_Bonobo_Storage_copyTo (PortableServer_Servant servant,
			    Bonobo_Storage         target,
			    CORBA_Environment     *ev)
{
	BonoboStorage *storage = bonobo_storage_from_servant (servant);
	Bonobo_Storage src = BONOBO_OBJREF (storage);	

	if (CLASS (storage)->copy_to)
		CLASS (storage)->copy_to (storage, target, ev);
        else
		bonobo_storage_copy_to (src, target, ev);
}

static void
impl_Bonobo_Storage_rename (PortableServer_Servant servant,
			    const CORBA_char      *path_name,
			    const CORBA_char      *new_path_name,
			    CORBA_Environment     *ev)
{
	BonoboStorage *storage = bonobo_storage_from_servant (servant);
	
	CLASS (storage)->rename (storage, path_name, new_path_name, ev);
}

static void
impl_Bonobo_Storage_commit (PortableServer_Servant servant, 
			    CORBA_Environment *ev)
{
	BonoboStorage *storage = bonobo_storage_from_servant (servant);
	
	CLASS (storage)->commit (storage, ev);
}

static void
impl_Bonobo_Storage_revert (PortableServer_Servant servant, 
			    CORBA_Environment *ev)
{
	BonoboStorage *storage = bonobo_storage_from_servant (servant);
	
	CLASS (storage)->commit (storage, ev);
}

static Bonobo_Storage_DirectoryList *
impl_Bonobo_Storage_listContents (PortableServer_Servant servant,
				  const CORBA_char      *path,
				  Bonobo_StorageInfoFields mask,
				  CORBA_Environment     *ev)
{
	BonoboStorage *storage = bonobo_storage_from_servant (servant);

	return CLASS (storage)->list_contents (storage, path, mask, ev);
}

static void
impl_Bonobo_Storage_erase (PortableServer_Servant servant, 
			   const CORBA_char      *path,
			   CORBA_Environment     *ev)
{
	BonoboStorage *storage = bonobo_storage_from_servant (servant);
	
	CLASS (storage)->erase (storage, path, ev);
}

static void
bonobo_storage_class_init (BonoboStorageClass *klass)
{
	POA_Bonobo_Storage__epv *epv = &klass->epv;

	epv->getInfo      = impl_Bonobo_Storage_getInfo;
	epv->setInfo      = impl_Bonobo_Storage_setInfo;
	epv->openStream	  = impl_Bonobo_Storage_openStream;
	epv->openStorage  = impl_Bonobo_Storage_openStorage;
	epv->copyTo       = impl_Bonobo_Storage_copyTo;
	epv->rename       = impl_Bonobo_Storage_rename;
	epv->commit       = impl_Bonobo_Storage_commit;
	epv->revert       = impl_Bonobo_Storage_revert;
	epv->listContents = impl_Bonobo_Storage_listContents;
	epv->erase        = impl_Bonobo_Storage_erase;
}

static void 
bonobo_storage_init (GtkObject *object)
{
	/* nothing to do */
}

BONOBO_X_TYPE_FUNC_FULL (BonoboStorage, 
			 Bonobo_Storage,
			 PARENT_TYPE,
			 bonobo_storage);

/**
 * bonobo_storage_open:
 * @driver: driver to use for opening.
 * @path: path where the base file resides
 * @flags: Bonobo Storage OpenMode
 * @mode: Unix open(2) mode
 *
 * Opens or creates the file named at @path with the stream driver @driver.
 *
 * @driver is one of: "efs", "vfs" or "fs" for now, please use
 * the macros for this though, see bonobo-stream.h eg.
 * BONOBO_IO_DRIVER_FS
 *
 * Returns: a created BonoboStorage object.
 */
BonoboStorage *
bonobo_storage_open_full (const char *driver, const char *path,
			  gint flags, gint mode,
			  CORBA_Environment *opt_ev)
{
	BonoboStorage *storage = NULL;
	StoragePlugin *p;
	CORBA_Environment ev, *my_ev;
	
	if (!opt_ev) {
		CORBA_exception_init (&ev);
		my_ev = &ev;
	} else
		my_ev = opt_ev;

	if (!driver || !path)
		CORBA_exception_set (my_ev, CORBA_USER_EXCEPTION, 
				     ex_Bonobo_Storage_IOError, NULL);

	else if (!(p = bonobo_storage_plugin_find (driver)) ||
		 !p->storage_open)
		CORBA_exception_set (my_ev, CORBA_USER_EXCEPTION, 
				     ex_Bonobo_Storage_NotSupported, NULL);
	else
		storage = p->storage_open (path, flags, mode, my_ev);

	if (!opt_ev) {
		if (BONOBO_EX (my_ev)) {
			char *text;
			
			text = bonobo_exception_get_text (my_ev);
			g_warning ("bonobo_storage_open failed '%s'", text);
			g_free (text);
		}
		
		CORBA_exception_free (&ev);
	}

	return storage;
}

/**
 * bonobo_storage_open:
 * @driver: driver to use for opening.
 * @path: path where the base file resides
 * @flags: Bonobo Storage OpenMode
 * @mode: Unix open(2) mode
 *
 * Opens or creates the file named at @path with the stream driver
 * @driver.
 *
 * @driver is one of: "efs", "vfs" or "fs" for now, please use
 * the macros for this though, see bonobo-stream.h eg.
 * BONOBO_IO_DRIVER_FS
 *
 * Returns: a created BonoboStorage object.
 **/
BonoboStorage *
bonobo_storage_open (const char *driver, const char *path,
		     gint flags, gint mode)
{
	return bonobo_storage_open_full (driver, path, flags, mode, NULL);
}

static void
copy_stream (Bonobo_Stream src, Bonobo_Stream dest, CORBA_Environment *ev) 
{
	Bonobo_Stream_iobuf *buf;

	do {
		Bonobo_Stream_read (src, 4096, &buf, ev);
		if (BONOBO_EX (ev)) 
			break;

		if (buf->_length == 0) {
			CORBA_free (buf);
			break;
		}

		Bonobo_Stream_write (dest, buf, ev);
		CORBA_free (buf);
		if (BONOBO_EX (ev)) 
			break;

	} while (1);

	if (BONOBO_EX (ev)) /* we must return a Bonobo_Storage exception */
		CORBA_exception_set (ev, CORBA_USER_EXCEPTION, 
				     ex_Bonobo_Storage_IOError, NULL);
}

/**
 * bonobo_storage_copy_to:
 * @src: the source storage
 * @dest: the destination storage
 * @ev: CORBA exception environment
 * 
 * Implements a pure CORBA method for copying one storage into
 * another, this is used by several BonoboStorage implemetations
 * where a fast case localy copy cannot work.
 **/
void
bonobo_storage_copy_to (Bonobo_Storage src, Bonobo_Storage dest,
			CORBA_Environment *ev) 
{
	Bonobo_Storage new_src, new_dest;
	Bonobo_Stream src_stream, dest_stream;
	Bonobo_Storage_DirectoryList *list;
	gint i;

	if ((src == CORBA_OBJECT_NIL) || (dest == CORBA_OBJECT_NIL) || !ev) {
		CORBA_exception_set (ev, CORBA_USER_EXCEPTION, 
				     ex_Bonobo_Storage_IOError, NULL);
		return;
	}

	list = Bonobo_Storage_listContents (src, "", 
					    Bonobo_FIELD_CONTENT_TYPE |
					    Bonobo_FIELD_TYPE,
					    ev);
	if (BONOBO_EX (ev))
		return;

	for (i = 0; i <list->_length; i++) {

		if (list->_buffer[i].type == Bonobo_STORAGE_TYPE_DIRECTORY) {

			new_dest = Bonobo_Storage_openStorage
				(dest, list->_buffer[i].name, 
				 Bonobo_Storage_CREATE | 
				 Bonobo_Storage_FAILIFEXIST, ev);

			if (BONOBO_EX (ev)) 
				break;

			Bonobo_Storage_setInfo (new_dest, "",
						&list->_buffer[i],
						Bonobo_FIELD_CONTENT_TYPE,
						ev);

			if (BONOBO_EX (ev)) {
				bonobo_object_release_unref (new_dest, NULL);
				break;
			}

			new_src = Bonobo_Storage_openStorage
				(src, list->_buffer[i].name, 
				 Bonobo_Storage_READ, ev);
			
			if (BONOBO_EX (ev)) {
				bonobo_object_release_unref (new_dest, NULL);
				break;
			}

			bonobo_storage_copy_to (new_src, new_dest, ev);
			
			bonobo_object_release_unref (new_src, NULL);
			bonobo_object_release_unref (new_dest, NULL);

			if (BONOBO_EX (ev))
				break;

		} else {
			dest_stream = Bonobo_Storage_openStream 
				(dest, list->_buffer[i].name,
				 Bonobo_Storage_CREATE | 
				 Bonobo_Storage_FAILIFEXIST, ev);

			if (BONOBO_EX (ev))
				break;

			Bonobo_Stream_setInfo (dest_stream,
					       &list->_buffer[i],
					       Bonobo_FIELD_CONTENT_TYPE,
					       ev);

			if (BONOBO_EX (ev)) {
				CORBA_exception_set (ev, CORBA_USER_EXCEPTION, 
						     ex_Bonobo_Storage_IOError,
						     NULL);
				bonobo_object_release_unref (dest_stream,
							     NULL);
				break;
			}

			src_stream = Bonobo_Storage_openStream 
				(src, list->_buffer[i].name,
				 Bonobo_Storage_READ, ev);

			if (BONOBO_EX (ev)) {
				bonobo_object_release_unref (dest_stream, 
							     NULL);
				break;
			}

			copy_stream (src_stream, dest_stream, ev);

			bonobo_object_release_unref (src_stream, NULL);
			bonobo_object_release_unref (dest_stream, NULL);

			if (BONOBO_EX (ev))
				break;
		}
	}

	CORBA_free (list);
}