Skip to content

Commit

Permalink
Merge branch 'rs/strbuf-expand'
Browse files Browse the repository at this point in the history
* rs/strbuf-expand:
  remove the unused files interpolate.c and interpolate.h
  daemon: deglobalize variable 'directory'
  daemon: inline fill_in_extra_table_entries()
  daemon: use strbuf_expand() instead of interpolate()
  merge-recursive: use strbuf_expand() instead of interpolate()
  add strbuf_expand_dict_cb(), a helper for simple cases
  • Loading branch information
Junio C Hamano committed Nov 28, 2008
2 parents 14de739 + 7de1950 commit 2d2b3fd
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 204 deletions.
7 changes: 7 additions & 0 deletions Documentation/technical/api-strbuf.txt
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ In order to facilitate caching and to make it possible to give
parameters to the callback, `strbuf_expand()` passes a context pointer,
which can be used by the programmer of the callback as she sees fit.

`strbuf_expand_dict_cb`::

Used as callback for `strbuf_expand()`, expects an array of
struct strbuf_expand_dict_entry as context, i.e. pairs of
placeholder and replacement string. The array needs to be
terminated by an entry with placeholder set to NULL.

`strbuf_addf`::

Add a formatted string to the buffer.
Expand Down
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,6 @@ LIB_OBJS += grep.o
LIB_OBJS += hash.o
LIB_OBJS += help.o
LIB_OBJS += ident.o
LIB_OBJS += interpolate.o
LIB_OBJS += levenshtein.o
LIB_OBJS += list-objects.o
LIB_OBJS += ll-merge.o
Expand Down
111 changes: 50 additions & 61 deletions daemon.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "cache.h"
#include "pkt-line.h"
#include "exec_cmd.h"
#include "interpolate.h"

#include <syslog.h>

Expand Down Expand Up @@ -54,26 +53,10 @@ static const char *user_path;
static unsigned int timeout;
static unsigned int init_timeout;

/*
* Static table for now. Ugh.
* Feel free to make dynamic as needed.
*/
#define INTERP_SLOT_HOST (0)
#define INTERP_SLOT_CANON_HOST (1)
#define INTERP_SLOT_IP (2)
#define INTERP_SLOT_PORT (3)
#define INTERP_SLOT_DIR (4)
#define INTERP_SLOT_PERCENT (5)

static struct interp interp_table[] = {
{ "%H", 0},
{ "%CH", 0},
{ "%IP", 0},
{ "%P", 0},
{ "%D", 0},
{ "%%", 0},
};

static char *hostname;
static char *canon_hostname;
static char *ip_address;
static char *tcp_port;

static void logreport(int priority, const char *err, va_list params)
{
Expand Down Expand Up @@ -163,15 +146,15 @@ static int avoid_alias(char *p)
}
}

static char *path_ok(struct interp *itable)
static char *path_ok(char *directory)
{
static char rpath[PATH_MAX];
static char interp_path[PATH_MAX];
int retried_path = 0;
char *path;
char *dir;

dir = itable[INTERP_SLOT_DIR].value;
dir = directory;

if (avoid_alias(dir)) {
logerror("'%s': aliased", dir);
Expand Down Expand Up @@ -201,14 +184,27 @@ static char *path_ok(struct interp *itable)
}
}
else if (interpolated_path && saw_extended_args) {
struct strbuf expanded_path = STRBUF_INIT;
struct strbuf_expand_dict_entry dict[] = {
{ "H", hostname },
{ "CH", canon_hostname },
{ "IP", ip_address },
{ "P", tcp_port },
{ "D", directory },
{ "%", "%" },
{ NULL }
};

if (*dir != '/') {
/* Allow only absolute */
logerror("'%s': Non-absolute path denied (interpolated-path active)", dir);
return NULL;
}

interpolate(interp_path, PATH_MAX, interpolated_path,
interp_table, ARRAY_SIZE(interp_table));
strbuf_expand(&expanded_path, interpolated_path,
strbuf_expand_dict_cb, &dict);
strlcpy(interp_path, expanded_path.buf, PATH_MAX);
strbuf_release(&expanded_path);
loginfo("Interpolated dir '%s'", interp_path);

dir = interp_path;
Expand All @@ -233,7 +229,7 @@ static char *path_ok(struct interp *itable)
* prefixing the base path
*/
if (base_path && base_path_relaxed && !retried_path) {
dir = itable[INTERP_SLOT_DIR].value;
dir = directory;
retried_path = 1;
continue;
}
Expand Down Expand Up @@ -299,22 +295,20 @@ static int git_daemon_config(const char *var, const char *value, void *cb)
return 0;
}

static int run_service(struct interp *itable, struct daemon_service *service)
static int run_service(char *dir, struct daemon_service *service)
{
const char *path;
int enabled = service->enabled;

loginfo("Request %s for '%s'",
service->name,
itable[INTERP_SLOT_DIR].value);
loginfo("Request %s for '%s'", service->name, dir);

if (!enabled && !service->overridable) {
logerror("'%s': service not enabled.", service->name);
errno = EACCES;
return -1;
}

if (!(path = path_ok(itable)))
if (!(path = path_ok(dir)))
return -1;

/*
Expand Down Expand Up @@ -413,13 +407,13 @@ static void make_service_overridable(const char *name, int ena)

/*
* Separate the "extra args" information as supplied by the client connection.
* Any resulting data is squirreled away in the given interpolation table.
*/
static void parse_extra_args(struct interp *table, char *extra_args, int buflen)
static void parse_extra_args(char *extra_args, int buflen)
{
char *val;
int vallen;
char *end = extra_args + buflen;
char *hp;

while (extra_args < end && *extra_args) {
saw_extended_args = 1;
Expand All @@ -433,25 +427,22 @@ static void parse_extra_args(struct interp *table, char *extra_args, int buflen)
if (port) {
*port = 0;
port++;
interp_set_entry(table, INTERP_SLOT_PORT, port);
free(tcp_port);
tcp_port = xstrdup(port);
}
interp_set_entry(table, INTERP_SLOT_HOST, host);
free(hostname);
hostname = xstrdup(host);
}

/* On to the next one */
extra_args = val + vallen;
}
}
}

static void fill_in_extra_table_entries(struct interp *itable)
{
char *hp;

/*
* Replace literal host with lowercase-ized hostname.
*/
hp = interp_table[INTERP_SLOT_HOST].value;
hp = hostname;
if (!hp)
return;
for ( ; *hp; hp++)
Expand All @@ -470,17 +461,17 @@ static void fill_in_extra_table_entries(struct interp *itable)
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_CANONNAME;

gai = getaddrinfo(interp_table[INTERP_SLOT_HOST].value, 0, &hints, &ai0);
gai = getaddrinfo(hostname, 0, &hints, &ai0);
if (!gai) {
for (ai = ai0; ai; ai = ai->ai_next) {
struct sockaddr_in *sin_addr = (void *)ai->ai_addr;

inet_ntop(AF_INET, &sin_addr->sin_addr,
addrbuf, sizeof(addrbuf));
interp_set_entry(interp_table,
INTERP_SLOT_CANON_HOST, ai->ai_canonname);
interp_set_entry(interp_table,
INTERP_SLOT_IP, addrbuf);
free(canon_hostname);
canon_hostname = xstrdup(ai->ai_canonname);
free(ip_address);
ip_address = xstrdup(addrbuf);
break;
}
freeaddrinfo(ai0);
Expand All @@ -493,7 +484,7 @@ static void fill_in_extra_table_entries(struct interp *itable)
char **ap;
static char addrbuf[HOST_NAME_MAX + 1];

hent = gethostbyname(interp_table[INTERP_SLOT_HOST].value);
hent = gethostbyname(hostname);

ap = hent->h_addr_list;
memset(&sa, 0, sizeof sa);
Expand All @@ -504,8 +495,10 @@ static void fill_in_extra_table_entries(struct interp *itable)
inet_ntop(hent->h_addrtype, &sa.sin_addr,
addrbuf, sizeof(addrbuf));

interp_set_entry(interp_table, INTERP_SLOT_CANON_HOST, hent->h_name);
interp_set_entry(interp_table, INTERP_SLOT_IP, addrbuf);
free(canon_hostname);
canon_hostname = xstrdup(hent->h_name);
free(ip_address);
ip_address = xstrdup(addrbuf);
}
#endif
}
Expand Down Expand Up @@ -557,16 +550,14 @@ static int execute(struct sockaddr *addr)
pktlen--;
}

/*
* Initialize the path interpolation table for this connection.
*/
interp_clear_table(interp_table, ARRAY_SIZE(interp_table));
interp_set_entry(interp_table, INTERP_SLOT_PERCENT, "%");
free(hostname);
free(canon_hostname);
free(ip_address);
free(tcp_port);
hostname = canon_hostname = ip_address = tcp_port = NULL;

if (len != pktlen) {
parse_extra_args(interp_table, line + len + 1, pktlen - len - 1);
fill_in_extra_table_entries(interp_table);
}
if (len != pktlen)
parse_extra_args(line + len + 1, pktlen - len - 1);

for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
struct daemon_service *s = &(daemon_service[i]);
Expand All @@ -578,9 +569,7 @@ static int execute(struct sockaddr *addr)
* Note: The directory here is probably context sensitive,
* and might depend on the actual service being performed.
*/
interp_set_entry(interp_table,
INTERP_SLOT_DIR, line + namelen + 5);
return run_service(interp_table, s);
return run_service(line + namelen + 5, s);
}
}

Expand Down
103 changes: 0 additions & 103 deletions interpolate.c

This file was deleted.

Loading

0 comments on commit 2d2b3fd

Please sign in to comment.