File: test-method.c

package info (click to toggle)
gnome-vfs 1:2.24.4-6.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, stretch
  • size: 17,144 kB
  • ctags: 10,422
  • sloc: ansic: 78,500; sh: 10,341; makefile: 902; perl: 99
file content (612 lines) | stat: -rw-r--r-- 16,885 bytes parent folder | download | duplicates (4)
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* Test-method.c: Gnome-VFS testing method

   Copyright (C) 2000 Eazel

   The Gnome Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Library 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 GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Authors: Seth Nickell      (seth@eazel.com) */

/* Create a settings file and put it at PREFIX/etc/vfs/Test-conf.xml,
 * then point gnome-vfs clients to test:<uri_minus_scheme> which will
 * translate into the "real" method (e.g. test:///home/seth).
 *
 * The delay is in milliseconds.
 *
 * Here's a sample config file (pointing to the file method):
 *
 *    <?xml version="1.0"?>
 *        <TestModule method="file">
 *	      <function name="open_directory" delay="2000"/>
 *        </TestModule>
 * */

#include <config.h>

#include <glib/gi18n-lib.h>
#include <glib.h>
#include <libgnomevfs/gnome-vfs-cancellable-ops.h>
#include <libgnomevfs/gnome-vfs-ops.h>
#include <libgnomevfs/gnome-vfs-module.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlmemory.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#define TEST_CONF_ENV_VARIABLE "GNOME_VFS_TEST_CONFIG_FILE"

typedef struct {
	char *operation_name;
        int delay;
	gboolean skip;
	gboolean override_result;
	GnomeVFSResult overridden_result_value;
} OperationSettings;

#define NUM_RESULT_STRINGS 41

static gboolean properly_initialized;

static guchar *test_method_name;
static GList *settings_list;

static const char * const
result_strings[NUM_RESULT_STRINGS] = {
	"GNOME_VFS_OK",
	"GNOME_VFS_ERROR_NOT_FOUND",
	"GNOME_VFS_ERROR_GENERIC",
	"GNOME_VFS_ERROR_INTERNAL",
	"GNOME_VFS_ERROR_BAD_PARAMETERS",
	"GNOME_VFS_ERROR_NOT_SUPPORTED",
	"GNOME_VFS_ERROR_IO",
	"GNOME_VFS_ERROR_CORRUPTED_DATA",
	"GNOME_VFS_ERROR_WRONG_FORMAT",
	"GNOME_VFS_ERROR_BAD_FILE",
	"GNOME_VFS_ERROR_TOO_BIG",
	"GNOME_VFS_ERROR_NO_SPACE",
	"GNOME_VFS_ERROR_READ_ONLY",
	"GNOME_VFS_ERROR_INVALID_URI",
	"GNOME_VFS_ERROR_NOT_OPEN",
	"GNOME_VFS_ERROR_INVALID_OPEN_MODE",
	"GNOME_VFS_ERROR_ACCESS_DENIED",
	"GNOME_VFS_ERROR_TOO_MANY_OPEN_FILES",
	"GNOME_VFS_ERROR_EOF",
	"GNOME_VFS_ERROR_NOT_A_DIRECTORY",
	"GNOME_VFS_ERROR_IN_PROGRESS",
	"GNOME_VFS_ERROR_INTERRUPTED",
	"GNOME_VFS_ERROR_FILE_EXISTS",
	"GNOME_VFS_ERROR_LOOP",
	"GNOME_VFS_ERROR_NOT_PERMITTED",
	"GNOME_VFS_ERROR_IS_DIRECTORY",
	"GNOME_VFS_ERROR_NO_MEMORY",
	"GNOME_VFS_ERROR_HOST_NOT_FOUND",
	"GNOME_VFS_ERROR_INVALID_HOST_NAME",
	"GNOME_VFS_ERROR_HOST_HAS_NO_ADDRESS",
	"GNOME_VFS_ERROR_LOGIN_FAILED",
	"GNOME_VFS_ERROR_CANCELLED",
	"GNOME_VFS_ERROR_DIRECTORY_BUSY",
	"GNOME_VFS_ERROR_DIRECTORY_NOT_EMPTY",
	"GNOME_VFS_ERROR_TOO_MANY_LINKS",
	"GNOME_VFS_ERROR_READ_ONLY_FILE_SYSTEM",
	"GNOME_VFS_ERROR_NOT_SAME_FILE_SYSTEM",
	"GNOME_VFS_ERROR_NAME_TOO_LONG",
	"GNOME_VFS_ERROR_SERVICE_NOT_AVAILABLE",
	"GNOME_VFS_NUM_ERRORS"
};

/* Module entry points. */
GnomeVFSMethod *vfs_module_init     (const char     *method_name,
				        const char     *args);
void            vfs_module_shutdown  (GnomeVFSMethod *method);

static GnomeVFSURI *
translate_uri (GnomeVFSURI *uri)
{
	GnomeVFSURI *translated_uri;
	char *uri_text;
	char *translated_uri_text;
	char *no_method;

	uri_text = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE);
	no_method = strchr (uri_text, ':');
	
	if (test_method_name != NULL) {
	  translated_uri_text = g_strconcat ((char *)test_method_name, 
					     no_method, NULL);
	} else {
	  translated_uri_text = NULL;
	}

	if (translated_uri_text != NULL) {
	  translated_uri = gnome_vfs_uri_new (translated_uri_text);
	} else {
	  translated_uri = NULL;
	}

	g_free (translated_uri_text);
	g_free (uri_text);

	return translated_uri;
}

/* reads the configuration file and returns TRUE if there are special options for
   this execution of the operation.

   if TRUE is returned then result will contain the result the operation should return
   and perform_operation will be TRUE if the operation should execute the underlying
   operation anyway
*/
static const OperationSettings *
get_operation_settings (const char *function_identifier)
{
	static OperationSettings empty_settings;
        GList *node;
	OperationSettings *settings;

	for (node = settings_list; node != NULL; node = node->next) {
	        settings = node->data;
		if (g_ascii_strcasecmp (settings->operation_name, function_identifier) == 0) {
			return settings;
		}
	}
	
	return &empty_settings;
}

static const OperationSettings *
start_operation (const char *name,
		 GnomeVFSURI **uri,
		 GnomeVFSURI **saved_uri)
{
	const OperationSettings *settings;

	settings = get_operation_settings (name);

	g_usleep (settings->delay * 1000);
	
	if (uri != NULL) {
		*saved_uri = *uri;
		*uri = translate_uri (*uri);
	}
	return settings;
}

static GnomeVFSResult
finish_operation (const OperationSettings *settings,
		  GnomeVFSResult result,
		  GnomeVFSURI **uri,
		  GnomeVFSURI **saved_uri)
{
	if (uri != NULL) {
		gnome_vfs_uri_unref (*uri);
		*uri = *saved_uri;
	}

	if (settings->override_result) {
		return settings->overridden_result_value;
	}
	return result;
}

#define PERFORM_OPERATION(name, operation)			\
do {								\
	const OperationSettings *settings;			\
	GnomeVFSURI *saved_uri;					\
	GnomeVFSResult result;					\
								\
        if (!properly_initialized) {                            \
                return GNOME_VFS_ERROR_SERVICE_NOT_AVAILABLE;   \
        }                                                       \
                                                                \
	settings = start_operation (#name, &uri, &saved_uri);	\
	if (settings->skip) {					\
		result = GNOME_VFS_OK;				\
	} else {						\
		result = operation;				\
	}							\
	return finish_operation (settings, result,		\
				 &uri, &saved_uri);		\
} while (0)


#define PERFORM_OPERATION_NO_URI(name, operation)		\
do {								\
	const OperationSettings *settings;			\
	GnomeVFSResult result;					\
								\
        if (!properly_initialized) {                            \
                return GNOME_VFS_ERROR_SERVICE_NOT_AVAILABLE;   \
        }                                                       \
                                                                \
	settings = start_operation (#name, NULL, NULL);		\
	if (settings->skip) {					\
		result = GNOME_VFS_OK;				\
	} else {						\
		result = operation;				\
	}							\
	return finish_operation (settings, result,		\
				 NULL, NULL);			\
} while (0)

static gboolean
parse_result_text (const char *result_text,
		   GnomeVFSResult *result_code)
{
	int i;

	for (i = 0; i < NUM_RESULT_STRINGS; i++) {
		if (g_ascii_strcasecmp (result_text, result_strings[i]) == 0) {
			*result_code = i;
			return TRUE;
		}
	}
	
	return FALSE;
}

static gboolean
load_settings (const char *filename) 
{
	xmlDocPtr doc;
	xmlNodePtr node;
	char *name;
	OperationSettings *operation;
	char *str;

	doc = xmlParseFile (filename); 

	if (doc == NULL
	    || doc->xmlRootNode == NULL
	    || doc->xmlRootNode->name == NULL
	    || g_ascii_strcasecmp ((char *)doc->xmlRootNode->name, "testmodule") != 0) {
		xmlFreeDoc(doc);
		return FALSE;
	}

	test_method_name = xmlGetProp (doc->xmlRootNode, (guchar *)"method");
	
	for (node = doc->xmlRootNode->xmlChildrenNode; node != NULL; node = node->next) {
		name = (char *)xmlGetProp (node, (guchar *)"name");
		if (name == NULL) {
			continue;
		}

		operation = g_new0 (OperationSettings, 1);
		operation->operation_name = name;

		str = (char *)xmlGetProp (node, (guchar *)"delay");
		if (str != NULL) {
			sscanf (str, "%d", &operation->delay);
		}
		xmlFree (str);

		str = (char *)xmlGetProp(node, (guchar *)"execute_operation");
		if (str != NULL && g_ascii_strcasecmp (str, "FALSE") == 0) {
			operation->skip = TRUE;
		}
		xmlFree (str);

		str = (char *)xmlGetProp (node, (guchar *)"result");
		if (str != NULL) {
			operation->override_result = parse_result_text
				(str, &operation->overridden_result_value);
		}
		xmlFree (str);
		
		settings_list = g_list_prepend (settings_list, operation);
	}
	return TRUE;
}

static GnomeVFSResult
do_open (GnomeVFSMethod *method,
	 GnomeVFSMethodHandle **method_handle,
	 GnomeVFSURI *uri,
	 GnomeVFSOpenMode mode,
	 GnomeVFSContext *context)
{	
	PERFORM_OPERATION (open, gnome_vfs_open_uri_cancellable ((GnomeVFSHandle **) method_handle, uri, mode, context));
}

static GnomeVFSResult
do_create (GnomeVFSMethod *method,
	   GnomeVFSMethodHandle **method_handle,
	   GnomeVFSURI *uri,
	   GnomeVFSOpenMode mode,
	   gboolean exclusive,
	   guint perm,
	   GnomeVFSContext *context)
{
	/* FIXME bugzilla.eazel.com 3837: Not implemented. */
	return GNOME_VFS_ERROR_INTERNAL;
}

static GnomeVFSResult
do_close (GnomeVFSMethod *method,
	  GnomeVFSMethodHandle *method_handle,
	  GnomeVFSContext *context)
{
	PERFORM_OPERATION_NO_URI (close, gnome_vfs_close_cancellable ((GnomeVFSHandle *) method_handle, context));
}

static GnomeVFSResult
do_read (GnomeVFSMethod *method,
	 GnomeVFSMethodHandle *method_handle,
	 gpointer buffer,
	 GnomeVFSFileSize num_bytes,
	 GnomeVFSFileSize *bytes_read,
	 GnomeVFSContext *context)
{
	PERFORM_OPERATION_NO_URI (read, gnome_vfs_read_cancellable ((GnomeVFSHandle *) method_handle, buffer, num_bytes, bytes_read, context));
}

static GnomeVFSResult
do_write (GnomeVFSMethod *method,
	  GnomeVFSMethodHandle *method_handle,
	  gconstpointer buffer,
	  GnomeVFSFileSize num_bytes,
	  GnomeVFSFileSize *bytes_written,
	  GnomeVFSContext *context)
{
	PERFORM_OPERATION_NO_URI (write, gnome_vfs_write_cancellable((GnomeVFSHandle *) method_handle, buffer, num_bytes, bytes_written, context));
}

static GnomeVFSResult
do_seek (GnomeVFSMethod *method,
	 GnomeVFSMethodHandle *method_handle,
	 GnomeVFSSeekPosition whence,
	 GnomeVFSFileOffset offset,
	 GnomeVFSContext *context)
{
	PERFORM_OPERATION_NO_URI (seek, gnome_vfs_seek_cancellable ((GnomeVFSHandle *) method_handle, whence, offset, context));
}

static GnomeVFSResult
do_tell (GnomeVFSMethod *method,
	 GnomeVFSMethodHandle *method_handle,
	 GnomeVFSFileSize *offset_return)
{
	PERFORM_OPERATION_NO_URI (tell, gnome_vfs_tell ((GnomeVFSHandle *) method_handle, offset_return));
}


static GnomeVFSResult
do_open_directory (GnomeVFSMethod *method,
		   GnomeVFSMethodHandle **method_handle,
		   GnomeVFSURI *uri,
		   GnomeVFSFileInfoOptions options,
		   GnomeVFSContext *context)
{
	PERFORM_OPERATION (open_directory, gnome_vfs_directory_open_from_uri ((GnomeVFSDirectoryHandle **) method_handle, uri, options));
}

static GnomeVFSResult
do_close_directory (GnomeVFSMethod *method,
		    GnomeVFSMethodHandle *method_handle,
		    GnomeVFSContext *context)
{
	PERFORM_OPERATION_NO_URI (close_directory, gnome_vfs_directory_close ((GnomeVFSDirectoryHandle *) method_handle));
}

static GnomeVFSResult
do_read_directory (GnomeVFSMethod *method,
		   GnomeVFSMethodHandle *method_handle,
		   GnomeVFSFileInfo *file_info,
		   GnomeVFSContext *context)
{
	PERFORM_OPERATION_NO_URI (read_directory, gnome_vfs_directory_read_next ((GnomeVFSDirectoryHandle *) method_handle, file_info));
}

static GnomeVFSResult
do_get_file_info (GnomeVFSMethod *method,
		  GnomeVFSURI *uri,
		  GnomeVFSFileInfo *file_info,
		  GnomeVFSFileInfoOptions options,
		  GnomeVFSContext *context)
{
	PERFORM_OPERATION (get_file_info, gnome_vfs_get_file_info_uri_cancellable (uri, file_info, options, context));
}

static GnomeVFSResult
do_get_file_info_from_handle (GnomeVFSMethod *method,
			      GnomeVFSMethodHandle *method_handle,
			      GnomeVFSFileInfo *file_info,
			      GnomeVFSFileInfoOptions options,
			      GnomeVFSContext *context)
{
	PERFORM_OPERATION_NO_URI (get_file_info_from_handle, gnome_vfs_get_file_info_from_handle_cancellable ((GnomeVFSHandle *) method_handle, file_info, options, context));
}

static gboolean
do_is_local (GnomeVFSMethod *method,
	     const GnomeVFSURI *uri)
{
	/* FIXME bugzilla.eazel.com 3837: Not implemented. */
	return TRUE;
}

static GnomeVFSResult
do_make_directory (GnomeVFSMethod *method,
		   GnomeVFSURI *uri,
		   guint perm,
		   GnomeVFSContext *context)
{
	PERFORM_OPERATION (make_directory, gnome_vfs_make_directory_for_uri_cancellable (uri, perm, context));
}

static GnomeVFSResult
do_remove_directory (GnomeVFSMethod *method,
		     GnomeVFSURI *uri,
		     GnomeVFSContext *context)
{
	PERFORM_OPERATION (remove_directory, gnome_vfs_remove_directory_from_uri_cancellable (uri, context));
}

static GnomeVFSResult
do_move (GnomeVFSMethod *method,
	 GnomeVFSURI *old_uri,
	 GnomeVFSURI *new_uri,
	 gboolean force_replace,
	 GnomeVFSContext *context)
{
	/* FIXME bugzilla.eazel.com 3837: Not implemented. */
	return gnome_vfs_move_uri_cancellable (old_uri, new_uri, force_replace, context);
}

static GnomeVFSResult
do_unlink (GnomeVFSMethod *method,
	   GnomeVFSURI *uri,
	   GnomeVFSContext *context)
{
	PERFORM_OPERATION (unlink, gnome_vfs_unlink_from_uri_cancellable (uri, context));
}

static GnomeVFSResult
do_check_same_fs (GnomeVFSMethod *method,
		  GnomeVFSURI *a,
		  GnomeVFSURI *b,
		  gboolean *same_fs_return,
		  GnomeVFSContext *context)
{
	/* FIXME bugzilla.eazel.com 3837: Not implemented. */
	return gnome_vfs_check_same_fs_uris_cancellable (a, b, same_fs_return, context);
}

static GnomeVFSResult
do_set_file_info (GnomeVFSMethod *method,
		  GnomeVFSURI *uri,
		  const GnomeVFSFileInfo *info,
		  GnomeVFSSetFileInfoMask mask,
		  GnomeVFSContext *context)
{	
	PERFORM_OPERATION (set_file_info, gnome_vfs_set_file_info_cancellable (uri, info, mask, context));
}

static GnomeVFSResult
do_truncate (GnomeVFSMethod *method,
	     GnomeVFSURI *uri,
	     GnomeVFSFileSize where,
	     GnomeVFSContext *context)
{
	PERFORM_OPERATION (truncate, gnome_vfs_truncate_uri_cancellable (uri, where, context));
}

static GnomeVFSResult
do_truncate_handle (GnomeVFSMethod *method,
		    GnomeVFSMethodHandle *method_handle,
		    GnomeVFSFileSize where,
		    GnomeVFSContext *context)
{
	PERFORM_OPERATION_NO_URI (truncate_handle, gnome_vfs_truncate_handle_cancellable ((GnomeVFSHandle *) method_handle, where, context));
}

static GnomeVFSResult
do_find_directory (GnomeVFSMethod *method,
		   GnomeVFSURI *uri,
		   GnomeVFSFindDirectoryKind kind,
		   GnomeVFSURI **result_uri,
		   gboolean create_if_needed,
		   gboolean find_if_needed,
		   guint permissions,
		   GnomeVFSContext *context)
{
	PERFORM_OPERATION (find_directory, gnome_vfs_find_directory_cancellable (uri, kind, result_uri, create_if_needed, find_if_needed, permissions, context));
}

static GnomeVFSResult
do_create_symbolic_link (GnomeVFSMethod *method,
			 GnomeVFSURI *uri,
			 const char *target_reference,
			 GnomeVFSContext *context)
{
	PERFORM_OPERATION (create_symbolic_link, gnome_vfs_create_symbolic_link_cancellable (uri, target_reference, context));
}

static GnomeVFSMethod method = {
	sizeof (GnomeVFSMethod),
	do_open,
	do_create,
	do_close,
	do_read,
	do_write,
	do_seek,
	do_tell,
	do_truncate_handle,
	do_open_directory,
	do_close_directory,
	do_read_directory,
	do_get_file_info,
	do_get_file_info_from_handle,
	do_is_local,
	do_make_directory,
	do_remove_directory,
	do_move,
	do_unlink,
	do_check_same_fs,
        do_set_file_info,
	do_truncate,
	do_find_directory,
	do_create_symbolic_link
};


GnomeVFSMethod *
vfs_module_init (const char *method_name, const char *args)
{
	char *conf_file;

	LIBXML_TEST_VERSION
	
	conf_file = getenv (TEST_CONF_ENV_VARIABLE);

	if (conf_file == NULL) {
		conf_file = GNOME_VFS_PREFIX "/etc/vfs/Test-conf.xml";
	}

	if (load_settings (conf_file) == FALSE) {

	  /* FIXME: we probably shouldn't use printf to output the message */
	  printf (_("Didn't find a valid settings file at %s\n"), 
		  conf_file);
	  printf (_("Use the %s environment variable to specify a different location.\n"),
		  TEST_CONF_ENV_VARIABLE);
	  properly_initialized = FALSE;
	} else {
	  properly_initialized = TRUE;
	}

	return &method;
}

void
vfs_module_shutdown (GnomeVFSMethod *method)
{
	GList *node;
	OperationSettings *settings;

	for (node = settings_list; node != NULL; node = node->next) {
	        settings = node->data;
		xmlFree (settings->operation_name);
		g_free (settings);
	}
	g_list_free (settings_list);
	xmlFree (test_method_name);
}