"Fossies" - the Fresh Open Source Software Archive

Member "glusterfs-8.2/xlators/cluster/dht/src/dht-hashfn.c" (16 Sep 2020, 2953 Bytes) of package /linux/misc/glusterfs-8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "dht-hashfn.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2   Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
    3   This file is part of GlusterFS.
    4 
    5   This file is licensed to you under your choice of the GNU Lesser
    6   General Public License, version 3 or any later version (LGPLv3 or
    7   later), or the GNU General Public License, version 2 (GPLv2), in all
    8   cases as published by the Free Software Foundation.
    9 */
   10 
   11 #include "dht-common.h"
   12 #include <glusterfs/hashfn.h>
   13 
   14 static int
   15 dht_hash_compute_internal(int type, const char *name, const int len,
   16                           uint32_t *hash_p)
   17 {
   18     int ret = 0;
   19     uint32_t hash = 0;
   20 
   21     switch (type) {
   22         case DHT_HASH_TYPE_DM:
   23         case DHT_HASH_TYPE_DM_USER:
   24             hash = gf_dm_hashfn(name, len);
   25             break;
   26         default:
   27             ret = -1;
   28             break;
   29     }
   30 
   31     if (ret == 0) {
   32         *hash_p = hash;
   33     }
   34 
   35     return ret;
   36 }
   37 
   38 /* The function returns:
   39  * 0  : in case no munge took place
   40  * >0 : the length (inc. terminating NULL!) of the newly modified string,
   41  *      if it was munged.
   42  */
   43 static int
   44 dht_munge_name(const char *original, char *modified, size_t len, regex_t *re)
   45 {
   46     regmatch_t matches[2] = {
   47         {0},
   48     };
   49     size_t new_len = 0;
   50     int ret = 0;
   51 
   52     ret = regexec(re, original, 2, matches, 0);
   53 
   54     if (ret != REG_NOMATCH) {
   55         if (matches[1].rm_so != -1) {
   56             new_len = matches[1].rm_eo - matches[1].rm_so;
   57             /* Equal would fail due to the NUL at the end. */
   58             if (new_len < len) {
   59                 memcpy(modified, original + matches[1].rm_so, new_len);
   60                 modified[new_len] = '\0';
   61                 return new_len + 1; /* +1 for the terminating NULL */
   62             }
   63         }
   64     }
   65 
   66     /* This is guaranteed safe because of how the dest was allocated. */
   67     strcpy(modified, original);
   68     return 0;
   69 }
   70 
   71 int
   72 dht_hash_compute(xlator_t *this, int type, const char *name, uint32_t *hash_p)
   73 {
   74     char *rsync_friendly_name = NULL;
   75     dht_conf_t *priv = NULL;
   76     size_t len = 0;
   77     int munged = 0;
   78 
   79     priv = this->private;
   80 
   81     if (name == NULL)
   82         return -1;
   83 
   84     len = strlen(name) + 1;
   85     rsync_friendly_name = alloca(len);
   86 
   87     LOCK(&priv->lock);
   88     {
   89         if (priv->extra_regex_valid) {
   90             munged = dht_munge_name(name, rsync_friendly_name, len,
   91                                     &priv->extra_regex);
   92         }
   93 
   94         if (!munged && priv->rsync_regex_valid) {
   95             gf_msg_trace(this->name, 0, "trying regex for %s", name);
   96             munged = dht_munge_name(name, rsync_friendly_name, len,
   97                                     &priv->rsync_regex);
   98         }
   99     }
  100     UNLOCK(&priv->lock);
  101     if (munged) {
  102         gf_msg_debug(this->name, 0, "munged down to %s", rsync_friendly_name);
  103         len = munged;
  104     } else {
  105         rsync_friendly_name = (char *)name;
  106     }
  107 
  108     return dht_hash_compute_internal(type, rsync_friendly_name, len - 1,
  109                                      hash_p);
  110 }