File: gnome-vfs-ace.c

package info (click to toggle)
gnome-vfs 1%3A2.24.4-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,120 kB
  • ctags: 10,397
  • sloc: ansic: 78,516; sh: 10,341; makefile: 902; perl: 99
file content (584 lines) | stat: -rw-r--r-- 12,284 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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gnome-vfs-acl.h - ACL Handling for the GNOME Virtual File System.
   Access Control Entry Class

   Copyright (C) 2005 Christian Kellner

   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.

   Author: Christian Kellner <gicmo@gnome.org>
           Alvaro Lopez Ortega <alvaro@sun.com>
*/

#include "gnome-vfs-ace.h"
#include "gnome-vfs-acl.h"

#include <string.h>

/* ************************************************************************** */
/* GnomeVFSACLEntry */
/* ************************************************************************** */
typedef struct {

	GnomeVFSACLPerm *perms;
	guint            count;
	guint            array_len;

} PermSet;

struct _GnomeVFSACEPrivate {
	
	GnomeVFSACLKind   kind;
	char             *id;

	PermSet           perm_set;
	
	gboolean          negative;
	gboolean          inherit;
	
};

#define GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GNOME_VFS_TYPE_ACE, GnomeVFSACEPrivate))

static void
gnome_vfs_ace_set_property (GObject      *object,
                            guint         prop_id,
                            const GValue *value,
                            GParamSpec   *pspec);

static void
gnome_vfs_ace_get_property (GObject    *object,
                            guint       prop_id,
                            GValue     *value,
                            GParamSpec *pspec);
static void 
permset_copy (PermSet *source, PermSet *dest);
static void
permset_set (PermSet *set, GnomeVFSACLPerm *perms);
	
#if 0
static gboolean
permset_equal (PermSet *a, PermSet *b);
#endif 

/* ************************************************************************** */
/* GObject Stuff */

enum {
	PROP_0 = 0,
	PROP_KIND,
	PROP_ID,
	PROP_PERMS,
	PROP_NEGATIVE,
	PROP_INHERIT
};


static GObjectClass *entry_pclass = NULL;
G_DEFINE_TYPE (GnomeVFSACE, gnome_vfs_ace, G_TYPE_OBJECT);


static void
gnome_vfs_ace_finalize (GObject *object)
{
	GnomeVFSACEPrivate *priv;
	
	priv = GNOME_VFS_ACE (object)->priv;
	
	g_free (priv->perm_set.perms);
	g_free (priv->id);
	
	if (G_OBJECT_CLASS (entry_pclass)->finalize)
		(*G_OBJECT_CLASS (entry_pclass)->finalize) (object);
}

static void
gnome_vfs_ace_class_init (GnomeVFSACEClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
	GParamSpec   *pspec;

	entry_pclass = g_type_class_peek_parent (klass);

	g_type_class_add_private (klass, sizeof (GnomeVFSACEPrivate));
	
	/* override virtuals */
	gobject_class->finalize     = gnome_vfs_ace_finalize;
	gobject_class->set_property = gnome_vfs_ace_set_property;
	gobject_class->get_property = gnome_vfs_ace_get_property;
	
	/* properties */
	
	pspec =  g_param_spec_uint ("kind",
				    "fixme",
				    "fixme",
				    GNOME_VFS_ACL_KIND_NULL,
				    G_MAXUINT32,
				    GNOME_VFS_ACL_KIND_NULL,
				    G_PARAM_READWRITE |
				    G_PARAM_CONSTRUCT);

	g_object_class_install_property (gobject_class,
					 PROP_KIND,
					 pspec);



	pspec =  g_param_spec_string ("id",
				      "fixme",
				      "fixme",
				      NULL,
				      G_PARAM_READWRITE |
				      G_PARAM_CONSTRUCT);

	g_object_class_install_property (gobject_class,
					 PROP_ID,
					 pspec);



	pspec = g_param_spec_pointer ("permissions",
				      "Permissions",
				      "fixme",
				      G_PARAM_READWRITE |
				      G_PARAM_CONSTRUCT);

	g_object_class_install_property (gobject_class,
					 PROP_PERMS,
					 pspec);



	pspec = g_param_spec_boolean ("negative",
				      "fixme",
				      "fixme",
				      FALSE,
				      G_PARAM_READWRITE);

	g_object_class_install_property (gobject_class,
					 PROP_NEGATIVE,
					 pspec);


	pspec = g_param_spec_boolean ("inherit",
				      "fixme",
				      "fixme",
				      FALSE,
				      G_PARAM_READWRITE);

	g_object_class_install_property (gobject_class,
					 PROP_INHERIT,
					 pspec);
	
}

static void
gnome_vfs_ace_init (GnomeVFSACE *entry)
{
	entry->priv = GET_PRIVATE (entry);
}

static void
gnome_vfs_ace_set_property (GObject      *object,
                            guint         prop_id,
                            const GValue *value,
                            GParamSpec   *pspec)
{
	GnomeVFSACEPrivate *priv;
	GnomeVFSACLPerm    *pset;
	
	priv = GNOME_VFS_ACE (object)->priv;
	
	switch (prop_id) {
		
	case PROP_KIND:
		priv->kind = g_value_get_uint (value);
		break;
		
	case PROP_ID:
		g_free (priv->id);
		priv->id = g_value_dup_string (value);
		break;
		
	case PROP_PERMS:
		pset = g_value_get_pointer (value);
		
		if (pset == NULL) {
			g_free (priv->perm_set.perms);
			priv->perm_set.perms = NULL;
			priv->perm_set.count = 0;
			break;
		}
		
		permset_set (&priv->perm_set, pset);
		break;
		
	case PROP_INHERIT:
		priv->inherit = g_value_get_boolean (value);
		break;
		
	case PROP_NEGATIVE:
		priv->negative = g_value_get_boolean (value);
		break;
		
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
	}
}

static void
gnome_vfs_ace_get_property (GObject    *object,
                            guint       prop_id,
                            GValue     *value,
                            GParamSpec *pspec)
{
	GnomeVFSACEPrivate *priv;
	
	priv = GNOME_VFS_ACE (object)->priv;
	
	switch (prop_id) {
		
	case PROP_KIND:
		g_value_set_uint (value, priv->kind);
		break;
		
	case PROP_ID:
		g_value_set_string (value, priv->id);
		break;
		
	case PROP_PERMS:
		g_value_set_pointer (value, priv->perm_set.perms);
		break;
		
	case PROP_INHERIT:
		g_value_set_boolean (value, priv->inherit);
		break;
		
	case PROP_NEGATIVE:
		g_value_set_boolean (value, priv->negative);
		break;
		
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
	}
	
}

/* ************************************************************************** */

static gboolean
g_str_equal_safe (const char *a, const char *b)
{
	return g_str_equal (a ? a : "", b ? b : "");	
}

/* copy  */
static void 
permset_copy (PermSet *source, PermSet *dest)
{
	int i;
	
	if (dest->array_len < source->count) {
		guint bytes_to_copy;
		
		/* too bad, we need to do some mem ops here */
		bytes_to_copy = sizeof (GnomeVFSACLPerm) * 
			        (source->count + 1);
		
		g_free (dest->perms);
		dest->perms = g_memdup (source->perms, bytes_to_copy);
		dest->count = dest->array_len = source->count;
		/* sorting? */
		return;
	}

	for (i = 0; i < source->count; i++) {
		dest->perms[i] = source->perms[i];
	}

	dest->perms[source->count] = 0;
	dest->count = source->count;
}

static gint
cmp_perm (int *a, int *b, gpointer *data)
{
	
	if (*a == *b) {
		return 0;
	}
	
	return *a < *b ? -1 : 1; 
}

static void
permset_set (PermSet *set, GnomeVFSACLPerm *perms)
{
	GnomeVFSACLPerm *iter;
	guint            bytes_to_copy;
	int              n;
	
	g_assert (perms);
		
	for (iter = perms, n = 0; *iter; iter++) {
		n++;
	}

	bytes_to_copy = sizeof (GnomeVFSACLPerm) * (n + 1);

	g_free (set->perms);

	set->perms = g_memdup (perms, bytes_to_copy);

	/* sort the array */
	g_qsort_with_data (set->perms,
			   n,
			   sizeof (GnomeVFSACLPerm),
			   (GCompareDataFunc) cmp_perm,
			   NULL);
	set->count = n;
	set->array_len = n;
}

#if 0
static gboolean
permset_equal (PermSet *a, PermSet *b)
{
	int      i;
	gboolean result;
	
	if (a->count != b->count) {
		return FALSE;
	}
	
	result = TRUE;
	
	/* Permsets MUST be sorted for this to work */
	for (i = 0; i < a->count; i++) {
		if (a->perms[i] != b->perms[i]) {
			result = FALSE;
			break;
		}
	}

	return result;
}
#endif

/* ************************************************************************** */
/* Public Interface */

GnomeVFSACE *
gnome_vfs_ace_new (GnomeVFSACLKind  kind,
                   const char      *id,
                   GnomeVFSACLPerm *perms)
{
	GnomeVFSACE *entry;

	entry = g_object_new (GNOME_VFS_TYPE_ACE, 
			      "kind", kind,
			      "id", id,
			      "permissions", perms,
			      NULL);
	return entry;
}

GnomeVFSACLKind
gnome_vfs_ace_get_kind (GnomeVFSACE *entry)
{
	return entry->priv->kind;
}

void
gnome_vfs_ace_set_kind (GnomeVFSACE      *entry,
                        GnomeVFSACLKind   kind)
{
	entry->priv->kind = kind;      	
}

const char*
gnome_vfs_ace_get_id (GnomeVFSACE *entry)
{
	return entry->priv->id; 
}

void
gnome_vfs_ace_set_id (GnomeVFSACE *entry,
                      const char  *id)
{
	GnomeVFSACEPrivate *priv;
	priv = entry->priv;
	
	g_free (priv->id);
	priv->id = g_strdup (id);
}

void 
gnome_vfs_ace_set_inherit (GnomeVFSACE *entry,
			   gboolean     inherit)
{
	g_object_set (G_OBJECT(entry), "inherit", inherit, NULL);
}

gboolean
gnome_vfs_ace_get_inherit (GnomeVFSACE *entry)
{
	gboolean inherit;

	g_object_get (G_OBJECT(entry), "inherit", &inherit, NULL);
	return inherit;
}


void 
gnome_vfs_ace_set_negative (GnomeVFSACE *entry,
			    gboolean     negative)
{
	g_object_set (G_OBJECT(entry), "negative", negative, NULL);
}

gboolean
gnome_vfs_ace_get_negative (GnomeVFSACE *entry)
{
	gboolean negative;

	g_object_get (G_OBJECT(entry), "negative", &negative, NULL);
	return negative;
}


const GnomeVFSACLPerm  *
gnome_vfs_ace_get_perms (GnomeVFSACE *entry)
{
	return entry->priv->perm_set.perms; 
}

void
gnome_vfs_ace_set_perms (GnomeVFSACE     *entry,
                         GnomeVFSACLPerm *perms)
{
	permset_set (&entry->priv->perm_set, perms);	
}

void
gnome_vfs_ace_add_perm (GnomeVFSACE     *entry,
                        GnomeVFSACLPerm  perm)
{
	GnomeVFSACEPrivate *priv;
	PermSet            *permset;
	gint                i;

	g_assert (GNOME_VFS_IS_ACE(entry));

	priv    = entry->priv;
	permset = &priv->perm_set;

	for (i=0; i<permset->count; i++) {
		if (permset->perms[i] == perm)
			return;
	}
	
	if (permset->array_len < permset->count) {
		permset->perms = g_realloc (permset->perms, 
					    sizeof(GnomeVFSACLPerm) * (permset->count + 2));
		permset->array_len++;
	}

	permset->perms[permset->count] = perm;
	permset->count++;

	g_qsort_with_data (permset->perms,
			   permset->count++,
			   sizeof (GnomeVFSACLPerm),
			   (GCompareDataFunc) cmp_perm,
			   NULL);
}

void
gnome_vfs_ace_del_perm (GnomeVFSACE      *entry,
			GnomeVFSACLPerm   perm)
{
	guint               i;
	GnomeVFSACEPrivate *priv;
	PermSet            *permset;

	priv    = entry->priv;
	permset = &priv->perm_set;

	if (permset->perms == NULL) 
		return;

	for (i=0; i<permset->count; i++) {
		if (permset->perms[i] == perm) {
			g_memmove (&permset->perms[i], &permset->perms[i+1], permset->count - i);
			permset->count--;
			break;
		}
	}
}

gboolean
gnome_vfs_ace_check_perm (GnomeVFSACE     *entry,
                          GnomeVFSACLPerm  perm)
{
	GnomeVFSACEPrivate *priv;
	GnomeVFSACLPerm    *piter;
	
	priv = entry->priv;
	
	if (priv->perm_set.perms == NULL || perm == 0) {
		return FALSE;	
	}
	
	for (piter = priv->perm_set.perms; *piter; piter++) {
		if (*piter == perm) {
			return TRUE;	
		}
	}
	
	return FALSE;
}

void
gnome_vfs_ace_copy_perms (GnomeVFSACE     *source,
			  GnomeVFSACE     *dest)
{
	GnomeVFSACEPrivate *priv_source;
	GnomeVFSACEPrivate *priv_dest;
	
	priv_source = source->priv;
	priv_dest = dest->priv;
	
	permset_copy (&priv_source->perm_set, &priv_dest->perm_set);
}


gboolean
gnome_vfs_ace_equal (GnomeVFSACE     *entry_a,
                     GnomeVFSACE     *entry_b)
{
	GnomeVFSACEPrivate *priv_a;
	GnomeVFSACEPrivate *priv_b;
	
	priv_a = entry_a->priv;
	priv_b = entry_b->priv;


	return (priv_a->kind == priv_b->kind &&
	        g_str_equal_safe (priv_a->id, priv_b->id) &&
	        priv_a->inherit == priv_b->inherit &&
	        priv_a->negative == priv_b->negative);
}