LCOV - code coverage report
Current view: top level - lib/util - util_id.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 20 28 71.4 %
Date: 2024-04-21 15:09:00 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    Helper routines for uid and gid arrays
       5             : 
       6             :    Copyright (C) Guenther Deschner 2009
       7             : 
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (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
      16             :    GNU General Public License for more details.
      17             : 
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include "replace.h"
      23             : #include "lib/util/samba_util.h"
      24             : 
      25             : /****************************************************************************
      26             :  Add a gid to an array of gids if it's not already there.
      27             : ****************************************************************************/
      28             : 
      29      251359 : bool add_gid_to_array_unique(TALLOC_CTX *mem_ctx, gid_t gid,
      30             :                              gid_t **gids, uint32_t *num_gids)
      31             : {
      32           6 :         uint32_t i;
      33             : 
      34      251359 :         if ((*num_gids != 0) && (*gids == NULL)) {
      35             :                 /*
      36             :                  * A former call to this routine has failed to allocate memory
      37             :                  */
      38           0 :                 return false;
      39             :         }
      40             : 
      41      566068 :         for (i=0; i<*num_gids; i++) {
      42      362554 :                 if ((*gids)[i] == gid) {
      43       47842 :                         return true;
      44             :                 }
      45             :         }
      46             : 
      47      203514 :         *gids = talloc_realloc(mem_ctx, *gids, gid_t, *num_gids+1);
      48      203514 :         if (*gids == NULL) {
      49           0 :                 *num_gids = 0;
      50           0 :                 return false;
      51             :         }
      52             : 
      53      203514 :         (*gids)[*num_gids] = gid;
      54      203514 :         *num_gids += 1;
      55      203514 :         return true;
      56             : }
      57             : 
      58             : /****************************************************************************
      59             :  Add a uid to an array of uids if it's not already there.
      60             : ****************************************************************************/
      61             : 
      62          18 : bool add_uid_to_array_unique(TALLOC_CTX *mem_ctx, uid_t uid,
      63             :                              uid_t **uids, uint32_t *num_uids)
      64             : {
      65           0 :         uint32_t i;
      66             : 
      67          18 :         if ((*num_uids != 0) && (*uids == NULL)) {
      68             :                 /*
      69             :                  * A former call to this routine has failed to allocate memory
      70             :                  */
      71           0 :                 return false;
      72             :         }
      73             : 
      74          74 :         for (i=0; i<*num_uids; i++) {
      75          56 :                 if ((*uids)[i] == uid) {
      76           0 :                         return true;
      77             :                 }
      78             :         }
      79             : 
      80          18 :         *uids = talloc_realloc(mem_ctx, *uids, uid_t, *num_uids+1);
      81          18 :         if (*uids == NULL) {
      82           0 :                 *num_uids = 0;
      83           0 :                 return false;
      84             :         }
      85             : 
      86          18 :         (*uids)[*num_uids] = uid;
      87          18 :         *num_uids += 1;
      88          18 :         return true;
      89             : }

Generated by: LCOV version 1.14