summaryrefslogtreecommitdiffstats
path: root/xlators/cluster/dht/src/dht-hashfn.c
blob: 478d6cea2f55f6412ea70b2b2e48c06395a95176 (plain)
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
/*
  Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/


#include "glusterfs.h"
#include "xlator.h"
#include "dht-common.h"
#include "hashfn.h"


int
dht_hash_compute_internal (int type, const char *name, uint32_t *hash_p)
{
        int      ret = 0;
        uint32_t hash = 0;

        switch (type) {
        case DHT_HASH_TYPE_DM:
        case DHT_HASH_TYPE_DM_USER:
                hash = gf_dm_hashfn (name, strlen (name));
                break;
        default:
                ret = -1;
                break;
        }

        if (ret == 0) {
                *hash_p = hash;
        }

        return ret;
}


static inline
gf_boolean_t
dht_munge_name (const char *original, char *modified, size_t len, regex_t *re)
{
        regmatch_t      matches[2];
        size_t          new_len;

        if (regexec(re,original,2,matches,0) != REG_NOMATCH) {
                if (matches[1].rm_so != -1) {
                        new_len = matches[1].rm_eo - matches[1].rm_so;
                        /* Equal would fail due to the NUL at the end. */
                        if (new_len < len) {
                                memcpy (modified,original+matches[1].rm_so,
                                        new_len);
                                modified[new_len] = '\0';
                                return _gf_true;
                        }
                }
        }

        /* This is guaranteed safe because of how the dest was allocated. */
        strcpy(modified,original);
        return _gf_false;
}

int
dht_hash_compute (xlator_t *this, int type, const char *name, uint32_t *hash_p)
{
        char            *rsync_friendly_name    = NULL;
        dht_conf_t      *priv                   = this->private;
        size_t           len                    = 0;
        gf_boolean_t     munged                 = _gf_false;

        /*
         * It wouldn't be safe to use alloca in an inline function that doesn't
         * actually get inlined, and it wouldn't be efficient to do a real
         * allocation, so we use alloca here (if needed) and pass that to the
         * inline.
         */

        if (priv->extra_regex_valid) {
                len = strlen(name) + 1;
                rsync_friendly_name = alloca(len);
                munged = dht_munge_name (name, rsync_friendly_name, len,
                                         &priv->extra_regex);
        }

        if (!munged && priv->rsync_regex_valid) {
                len = strlen(name) + 1;
                rsync_friendly_name = alloca(len);
                gf_msg_trace (this->name, 0, "trying regex for %s", name);
                munged = dht_munge_name (name, rsync_friendly_name, len,
                                         &priv->rsync_regex);
                if (munged) {
                        gf_msg_debug (this->name, 0,
                                      "munged down to %s", rsync_friendly_name);
                }
        }

        if (!munged) {
                rsync_friendly_name = (char *)name;
        }

        return dht_hash_compute_internal (type, rsync_friendly_name, hash_p);
}