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, <a.gruenbacher@computer.org> 7 8 This program is free software; you can redistribute it and/or 9 modify it under the terms of the GNU Library General Public 10 License as published by the Free Software Foundation; either 11 version 2 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 Library General Public License for more details. 17 18 You should have received a copy of the GNU Library 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 <alloca.h> 24#include "libacl.h" 25 26 27static inline int 28__acl_entry_p_compare(const acl_entry_obj *a_p, const acl_entry_obj *b_p) 29{ 30 if (a_p->etag < b_p->etag) 31 return -1; 32 else if (a_p->etag > b_p->etag) 33 return 1; 34 35 if (a_p->eid.qid < b_p->eid.qid) 36 return -1; 37 else if (a_p->eid.qid > b_p->eid.qid) 38 return 1; 39 else 40 return 0; 41} 42 43 44static int 45__acl_entry_pp_compare(const void *a, const void *b) 46{ 47 return __acl_entry_p_compare(*(const acl_entry_obj **)a, 48 *(const acl_entry_obj **)b); 49} 50 51 52/* 53 Take an ACL entry form its current place in the entry ring, 54 and insert it at its proper place. Entries that are not valid 55 (yet) are not reordered. 56*/ 57int 58__acl_reorder_entry_obj_p(acl_entry_obj *entry_obj_p) 59{ 60 acl_obj *acl_obj_p = entry_obj_p->econtainer; 61 acl_entry_obj *here_obj_p; 62 63 if (acl_obj_p->aused <= 1) 64 return 0; 65 switch(entry_obj_p->etag) { 66 case ACL_UNDEFINED_TAG: 67 return 1; 68 case ACL_USER: 69 case ACL_GROUP: 70 if (qualifier_obj_id(entry_obj_p->eid) == 71 ACL_UNDEFINED_ID) 72 return 1; 73 } 74 75 /* Remove entry from ring */ 76 entry_obj_p->eprev->enext = entry_obj_p->enext; 77 entry_obj_p->enext->eprev = entry_obj_p->eprev; 78 79 /* Search for next greater entry */ 80 FOREACH_ACL_ENTRY(here_obj_p, acl_obj_p) { 81 if (__acl_entry_p_compare(here_obj_p, entry_obj_p) > 0) 82 break; 83 } 84 85 /* Re-insert entry into ring */ 86 entry_obj_p->eprev = here_obj_p->eprev; 87 entry_obj_p->enext = here_obj_p; 88 entry_obj_p->eprev->enext = entry_obj_p; 89 entry_obj_p->enext->eprev = entry_obj_p; 90 91 return 0; 92} 93 94 95/* 96 Sort all ACL entries at once, after initializing them. This function is 97 only used when converting complete ACLs from external formats to ACLs; 98 the ACL entries are always kept in canonical order while an ACL is 99 manipulated. 100*/ 101int 102__acl_reorder_obj_p(acl_obj *acl_obj_p) 103{ 104 acl_entry_obj **vector = alloca(sizeof(acl_entry_obj *) * 105 acl_obj_p->aused), **v, *x; 106 acl_entry_obj *entry_obj_p; 107 108 if (acl_obj_p->aused <= 1) 109 return 0; 110 111 v = vector; 112 FOREACH_ACL_ENTRY(entry_obj_p, acl_obj_p) { 113 *v++ = entry_obj_p; 114 } 115 116 qsort(vector, acl_obj_p->aused, sizeof(acl_entry_obj *), 117 __acl_entry_pp_compare); 118 119 x = (acl_entry_obj *)acl_obj_p; 120 for (v = vector; v != vector + acl_obj_p->aused; v++) { 121 (*v)->eprev = x; 122 x = *v; 123 } 124 acl_obj_p->aprev = *(vector + acl_obj_p->aused - 1); 125 126 x = (acl_entry_obj *)acl_obj_p; 127 for (v = vector + acl_obj_p->aused - 1; v != vector - 1; v--) { 128 (*v)->enext = x; 129 x = *v; 130 } 131 acl_obj_p->anext = *vector; 132 return 0; 133} 134 135