(root)/
acl-2.3.1/
libacl/
__acl_reorder_obj_p.c
       1  /*
       2    File: __acl_reorder_obj_p.c
       3    (Linux Access Control List Management, Posix Library Functions)
       4  
       5    Copyright (C) 1999, 2000
       6    Andreas Gruenbacher, <andreas.gruenbacher@gmail.com>
       7  
       8    This program is free software; you can redistribute it and/or
       9    modify it under the terms of the GNU Lesser General Public
      10    License as published by the Free Software Foundation; either
      11    version 2.1 of the License, or (at your option) any later version.
      12  
      13    This program is distributed in the hope that it will be useful,
      14    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16    Lesser General Public License for more details.
      17  
      18    You should have received a copy of the GNU Lesser General Public
      19    License along with this library; if not, write to the Free Software
      20    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
      21  */
      22  
      23  #include "config.h"
      24  #include <alloca.h>
      25  #include "libacl.h"
      26  
      27  
      28  static inline int
      29  __acl_entry_p_compare(const acl_entry_obj *a_p, const acl_entry_obj *b_p)
      30  {
      31  	if (a_p->etag < b_p->etag)
      32  		return -1;
      33  	else if (a_p->etag > b_p->etag)
      34  		return 1;
      35  
      36  	if (a_p->eid.qid < b_p->eid.qid)
      37  		return -1;
      38  	else if (a_p->eid.qid > b_p->eid.qid)
      39  		return 1;
      40  	else
      41  		return 0;
      42  }
      43  
      44  
      45  static int
      46  __acl_entry_pp_compare(const void *a, const void *b)
      47  {
      48  	return __acl_entry_p_compare(*(const acl_entry_obj **)a,
      49  				   *(const acl_entry_obj **)b);
      50  }
      51  
      52  
      53  /*
      54    Take an ACL entry form its current place in the entry ring,
      55    and insert it at its proper place. Entries that are not valid
      56    (yet) are not reordered.
      57  */
      58  int
      59  __acl_reorder_entry_obj_p(acl_entry_obj *entry_obj_p)
      60  {
      61  	acl_obj *acl_obj_p = entry_obj_p->econtainer;
      62  	acl_entry_obj *here_obj_p;
      63  
      64  	if (acl_obj_p->aused <= 1)
      65  		return 0;
      66  	switch(entry_obj_p->etag) {
      67  		case ACL_UNDEFINED_TAG:
      68  			return 1;
      69  		case ACL_USER:
      70  		case ACL_GROUP:
      71  			if (qualifier_obj_id(entry_obj_p->eid) ==
      72  			    ACL_UNDEFINED_ID)
      73  				return 1;
      74  	}
      75  
      76  	/* Remove entry from ring */
      77  	entry_obj_p->eprev->enext = entry_obj_p->enext;
      78  	entry_obj_p->enext->eprev = entry_obj_p->eprev;
      79  
      80  	/* Search for next greater entry */
      81  	FOREACH_ACL_ENTRY(here_obj_p, acl_obj_p) {
      82  		if (__acl_entry_p_compare(here_obj_p, entry_obj_p) > 0)
      83  			break;
      84  	}
      85  	
      86  	/* Re-insert entry into ring */
      87  	entry_obj_p->eprev = here_obj_p->eprev;
      88  	entry_obj_p->enext = here_obj_p;
      89  	entry_obj_p->eprev->enext = entry_obj_p;
      90  	entry_obj_p->enext->eprev = entry_obj_p;
      91  
      92  	return 0;
      93  }
      94  
      95  
      96  /*
      97    Sort all ACL entries at once, after initializing them. This function is
      98    only used when converting complete ACLs from external formats to ACLs;
      99    the ACL entries are always kept in canonical order while an ACL is
     100    manipulated.
     101  */
     102  int
     103  __acl_reorder_obj_p(acl_obj *acl_obj_p)
     104  {
     105  	acl_entry_obj **vector = alloca(sizeof(acl_entry_obj *) *
     106  					acl_obj_p->aused), **v, *x;
     107  	acl_entry_obj *entry_obj_p;
     108  
     109  	if (acl_obj_p->aused <= 1)
     110  		return 0;
     111  
     112  	v = vector;
     113  	FOREACH_ACL_ENTRY(entry_obj_p, acl_obj_p) {
     114  		*v++ = entry_obj_p;
     115  	}
     116  
     117  	qsort(vector, acl_obj_p->aused, sizeof(acl_entry_obj *),
     118  	       __acl_entry_pp_compare);
     119  
     120  	x = (acl_entry_obj *)acl_obj_p;
     121  	for (v = vector; v != vector + acl_obj_p->aused; v++) {
     122  		(*v)->eprev = x;
     123  		x = *v;
     124  	}
     125  	acl_obj_p->aprev = *(vector + acl_obj_p->aused - 1);
     126  
     127  	x = (acl_entry_obj *)acl_obj_p;
     128  	for (v = vector + acl_obj_p->aused - 1; v != vector - 1; v--) {
     129  		(*v)->enext = x;
     130  		x = *v;
     131  	}
     132  	acl_obj_p->anext = *vector;
     133  	return 0;
     134  }
     135