diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-05-10 21:46:51 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-05-10 22:18:02 +0300 |
commit | 84db86f0151385a8f05483bf691fde86b4e4e153 (patch) | |
tree | f4b3e184c30f2bbc462a2b67cb52f869881afd47 | |
parent | 18cf87abfc95f85d184c3f6b328c4a8228609f68 (diff) | |
download | wydawca-84db86f0151385a8f05483bf691fde86b4e4e153.tar.gz wydawca-84db86f0151385a8f05483bf691fde86b4e4e153.tar.bz2 |
Drop dependencies on argmatch and xgethostname.
-rw-r--r-- | NEWS | 2 | ||||
-rw-r--r-- | gnulib.modules | 1 | ||||
-rw-r--r-- | src/config.c | 332 | ||||
-rw-r--r-- | src/lock.c | 43 |
4 files changed, 188 insertions, 190 deletions
@@ -2,12 +2,14 @@ Wydawca NEWS -- history of user-visible changes. 2010-01-06 Copyright (C) 2007, 2008, 2009, 2010 Sergey Poznyakoff See the end of file for copying conditions. Please send Wydawca bug reports to <bug-wydawca@gnu.org.ua>. +Version 2.1.90 (Git) + Version 2.1 "KMB", 2010-01-06 * Incompatible changes The following meta-variables are renamed: diff --git a/gnulib.modules b/gnulib.modules index 61618ad..3d3f7f9 100644 --- a/gnulib.modules +++ b/gnulib.modules @@ -1,7 +1,6 @@ xalloc obstack getline mkdtemp save-cwd backupfile -xgethostname diff --git a/src/config.c b/src/config.c index 7353f29..df9816e 100644 --- a/src/config.c +++ b/src/config.c @@ -13,16 +13,46 @@ You should have received a copy of the GNU General Public License along with wydawca. If not, see <http://www.gnu.org/licenses/>. */ #include "wydawca.h" #include "sql.h" -#include "argmatch.h" #include <mail.h> +struct keyword +{ + char *name; + int tok; +}; + +static int +keyword_to_tok (const char *str, struct keyword *kw, int *pres) +{ + for (; kw->name; kw++) + if (strcmp (kw->name, str) == 0) + { + *pres = kw->tok; + return 0; + } + return 1; +} + +static int +tok_to_keyword (int tok, struct keyword *kw, const char **pres) +{ + for (; kw->name; kw++) + if (kw->tok == tok) + { + *pres = kw->name; + return 0; + } + return 1; +} + + static struct archive_descr default_archive_descr = { archive_none, NULL, no_backups }; @@ -32,13 +62,13 @@ static struct notification *default_notification = NULL; /* safe_file_name: convert a file name possibly containig relative specs (../) into a safer form using only direct descendence. Strip trailing delimiter if present, unless it is the only character left. E.g.: - /home/user/../smith --> /home/smith + /home/user/../smith --> /home/smith /home/user/../.. --> / ../file --> NULL */ char * safe_file_name (char *file_name) { @@ -120,108 +150,88 @@ safe_file_name_alloc (const char *file_name) char *ns = safe_file_name (s); if (!ns) free (s); return ns; } -int -string_to (const char *what, const char *str, - const char **args, int *vals, - int *pret, - grecs_locus_t *locus) -{ - ptrdiff_t x = ARGMATCH (str, args, vals); - - if (x == (ptrdiff_t)-1) - { - grecs_error (locus, 0, _("unknown %s: %s"), what, str); - return 1; - } - else if (x == (ptrdiff_t)-2) - { - grecs_error (locus, 0, _("ambiguous %s: %s"), what, str); - return 1; - } - *pret = vals[x]; - return 0; -} -static const char *event_args[] = { - "success", - "bad-ownership", - "bad-directive-signature", - "bad-detached-signature", - "check-failure", - NULL -}; - -static int event_types[] = { - ev_success, - ev_bad_ownership, - ev_bad_directive_signature, - ev_bad_detached_signature, - ev_check_fail +static struct keyword event_tab[] = { + { "success", ev_success }, + { "bad-ownership", ev_bad_ownership }, + { "bad-directive-signature", ev_bad_directive_signature }, + { "bad-detached-signature", ev_bad_detached_signature }, + { "check-failure", ev_check_fail }, + { NULL } }; -ARGMATCH_VERIFY (event_args, event_types); - const char * notification_event_str (enum notification_event evt) { - return ARGMATCH_TO_ARGUMENT ((char*)&evt, event_args, event_types); + const char *ret; + if (tok_to_keyword (evt, event_tab, &ret)) + { + grecs_error (NULL, 0, + _("INTERNAL ERROR: unknown notification event number: %d"), + evt); + return NULL; + } + return ret; } int string_to_notification_event (grecs_locus_t *locus, const char *val, enum notification_event *pret) { - int rc, res; - rc = string_to ("notification event", val, - event_args, event_types, - &res, - locus); + int res; + if (keyword_to_tok (val, event_tab, &res)) + { + grecs_error (locus, 0, _("unknown notification event: %s"), val); + return 1; + } *pret = res; - return rc; + return 0; } -static const char *target_args[] = { - "read", - "message", - "admin", - "owner", - "user", - NULL -}; - -static int target_types[] = { - notify_read, /* Read recipients from the message headers */ - notify_read, - notify_admin, /* System administrator */ - notify_owner, /* Project admin */ - notify_user /* User (uploader) */ +static struct keyword target_tab[] = { + { "read", notify_read }, /* Read recipients from the message headers */ + { "message", notify_read }, + { "admin", notify_admin}, /* System administrator */ + { "owner", notify_owner }, /* Project admin */ + { "user", notify_user }, /* User (uploader) */ + { NULL } }; -ARGMATCH_VERIFY (target_args, target_types); const char * notification_target_str (enum notification_target tgt) { - return ARGMATCH_TO_ARGUMENT ((char*)&tgt, target_args, target_types); + const char *ret; + if (tok_to_keyword (tgt, target_tab, &ret)) + { + grecs_error (NULL, 0, + _("INTERNAL ERROR: unknown notification target number: %d"), + tgt); + return NULL; + } + return ret; } int string_to_notification_target (grecs_locus_t *locus, const char *val, enum notification_target *pret) { - int rc, res; - rc = string_to ("notification target", val, - target_args, target_types, - &res, - locus); + int res; + if (keyword_to_tok (val, target_tab, &res)) + { + grecs_error (locus, 0, + _("unknown notification target: %s"), + val); + return 1; + } *pret = res; - return rc; + return 0; } int assert_string_arg (grecs_locus_t *locus, enum grecs_callback_command cmd, @@ -325,13 +335,13 @@ cb_email_address (enum grecs_callback_command cmd, break; case GRECS_TYPE_ARRAY: grecs_error (locus, 0, _("too many arguments")); return 1; } - + *(mu_address_t*) varptr = addr; return rc; } static int cb_interval (enum grecs_callback_command cmd, @@ -397,53 +407,35 @@ cb_set_umask (enum grecs_callback_command cmd, else umask (m); return 0; } -static const char *stat_args[] = { - "errors", - "warnings", - "bad-signatures", - "access-violations", - "complete-triplets", - "incomplete-triplets", - "bad-triplets", - "expired-triplets", - "triplet-success", - "uploads", - "archives", - "symlinks", - "rmsymlinks", - NULL -}; - -static unsigned long stat_types[] = { - STAT_ERRORS, - STAT_WARNINGS, - STAT_BAD_SIGNATURE, - STAT_ACCESS_VIOLATIONS, - STAT_COMPLETE_TRIPLETS, - STAT_INCOMPLETE_TRIPLETS, - STAT_BAD_TRIPLETS, - STAT_EXPIRED_TRIPLETS, - STAT_TRIPLET_SUCCESS, - STAT_UPLOADS, - STAT_ARCHIVES, - STAT_SYMLINKS, - STAT_RMSYMLINKS +static struct keyword stat_tab[] = { + { "errors", STAT_ERRORS }, + { "warnings", STAT_WARNINGS }, + { "bad-signatures", STAT_BAD_SIGNATURE }, + { "access-violations", STAT_ACCESS_VIOLATIONS }, + { "complete-triplets", STAT_COMPLETE_TRIPLETS }, + { "incomplete-triplets", STAT_INCOMPLETE_TRIPLETS }, + { "bad-triplets", STAT_BAD_TRIPLETS }, + { "expired-triplets", STAT_EXPIRED_TRIPLETS }, + { "triplet-success", STAT_TRIPLET_SUCCESS }, + { "uploads", STAT_UPLOADS }, + { "archives", STAT_ARCHIVES }, + { "symlinks", STAT_SYMLINKS }, + { "rmsymlinks", STAT_RMSYMLINKS }, + { NULL }, }; -ARGMATCH_VERIFY (stat_args, stat_types); - static int parse_single_statmask (grecs_locus_t *locus, const grecs_value_t *val, unsigned long *pmask, int *invert) { const char *arg; - ptrdiff_t x; + int x; if (val->type != GRECS_TYPE_STRING) { grecs_error (locus, 0, _("expected scalar value but found list")); return 1; } @@ -460,25 +452,18 @@ parse_single_statmask (grecs_locus_t *locus, const grecs_value_t *val, { *pmask = STAT_MASK_NONE; *invert = 0; return 0; } - x = ARGMATCH (arg, stat_args, stat_types); - - if (x == (ptrdiff_t)-1) + if (keyword_to_tok (arg, stat_tab, &x)) { grecs_error (locus, 0, _("unknown statistics type: %s"), arg); return 1; } - else if (x == (ptrdiff_t)-2) - { - grecs_error (locus, 0, _("ambiguous statistics type: %s"), arg); - return 1; - } - *pmask = STAT_MASK (stat_types[x]); + *pmask = STAT_MASK (x); return 0; } static int parse_statmask (grecs_locus_t *loc, grecs_value_t *val, unsigned long *pmask) { @@ -715,64 +700,42 @@ static struct grecs_keyword syslog_kw[] = { { "print-priority", N_("arg"), N_("Prefix each message with its priority"), grecs_type_bool, &syslog_include_prio }, { NULL }, }; -static char const * const backup_args[] = -{ - /* In a series of synonyms, present the most meaningful first, so - that argmatch_valid be more readable. */ - "none", "off", - "simple", "never", - "existing", "nil", - "numbered", "t", - NULL -}; - -static const enum backup_type backup_types[] = +static struct keyword backup_tab[] = { - no_backups, no_backups, - simple_backups, simple_backups, - numbered_existing_backups, numbered_existing_backups, - numbered_backups, numbered_backups + { "none", no_backups }, + { "off", no_backups }, + { "simple", simple_backups }, + { "never", simple_backups }, + { "existing", numbered_existing_backups }, + { "nil", numbered_existing_backups }, + { "numbered", numbered_backups }, + { "t", numbered_backups }, + { NULL } }; -/* Ensure that these two vectors have the same number of elements, - not counting the final NULL in the first one. */ -ARGMATCH_VERIFY (backup_args, backup_types); - static enum backup_type -get_backup_version (grecs_locus_t *locus, const char *ctx, - const char *version) +get_backup_version (grecs_locus_t *locus, const char *ctx, const char *version) { + int d; + if (version == 0 || *version == 0) return numbered_existing_backups; - else + else if (keyword_to_tok (version, backup_tab, &d)) { - ptrdiff_t d = ARGMATCH (version, backup_args, backup_types); - if (d == -2) - { - if (ctx) - grecs_error (locus, 0, _("%s: ambiguous backup type `%s'"), - ctx, version); - else - grecs_error (locus, 0, _("ambiguous backup type `%s'"), version); - return no_backups; - } - else if (d == -1) - { - if (ctx) - grecs_error (locus, 0, _("%s: invalid backup type `%s'"), - ctx, version); - else - grecs_error (locus, 0, _("invalid backup type `%s'"), version); - return no_backups; - } - return backup_types[d]; + if (ctx) + grecs_error (locus, 0, _("%s: ambiguous backup type `%s'"), + ctx, version); + else + grecs_error (locus, 0, _("ambiguous backup type `%s'"), version); + return no_backups; } + return d; } static int cb_backup (enum grecs_callback_command cmd, grecs_locus_t *locus, void *varptr, @@ -1048,32 +1011,28 @@ static struct grecs_keyword dictionary_kw[] = { cb_dictionary_params }, { NULL } }; int string_to_dictionary_id (grecs_locus_t *locus, - const char *str, enum dictionary_id *idp) + const char *str, enum dictionary_id *idp) { - static const char *id_str[] = { - "project-uploader", - "project-owner", - NULL + static struct keyword id_tab[] = { + { "project-uploader", project_uploader_dict }, + { "project-owner", project_owner_dict }, + { NULL } }; - static int id_num[] = { - project_uploader_dict, - project_owner_dict - }; - ARGMATCH_VERIFY (id_str, id_num); + int res; - int rc, res; - rc = string_to ("dictionary", str, - id_str, id_num, - &res, - locus); + if (keyword_to_tok (str, id_tab, &res)) + { + grecs_error (locus, 0, _("unknown dictionary ID: %s"), str); + return 1; + } *idp = res; - return rc; + return 0; } static int cb_dictionary (enum grecs_callback_command cmd, grecs_locus_t *locus, void *varptr, @@ -1174,13 +1133,13 @@ static struct grecs_keyword spool_kw[] = { cb_archive, NULL, archive_kw }, { "notify-event", NULL, N_("Configure notification"), grecs_type_section, NULL, offsetof(struct spool, notification), cb_notify_event, NULL, notify_event_kw }, { "check-script", NULL, N_("A /bin/sh script to verify the tarball"), grecs_type_string, NULL, offsetof(struct spool, check_script) }, - + { NULL } }; static int cb_spool (enum grecs_callback_command cmd, grecs_locus_t *locus, @@ -1249,13 +1208,13 @@ cb_spool (enum grecs_callback_command cmd, _("Sorry, the dictionary type `external' is not yet supported")); rc = 1; } if (rc) return rc; - + if (!spool->notification) spool->notification = default_notification; mu_url_sget_path (spool->dest_url, &spool->dest_dir); register_spool (spool); free (spool); *pdata = NULL; @@ -1273,23 +1232,23 @@ cb_user (enum grecs_callback_command cmd, grecs_locus_t *locus, void *varptr, grecs_value_t *value, void *cb_data) { struct passwd *pw; - + if (assert_string_arg (locus, cmd, value)) return 1; pw = getpwnam (value->v.string); if (!pw) { grecs_error (locus, 0, _("no such user: %s"), value->v.string); return 1; } - + wydawca_uid = pw->pw_uid; wydawca_gid = pw->pw_gid; return 0; } static int @@ -1314,21 +1273,21 @@ cb_supp_groups (enum grecs_callback_command cmd, if (wydawca_supp_groupc == 0) wydawca_supp_groups = NULL; else { int i; struct grecs_list_entry *ep; - + wydawca_supp_groups = xcalloc (wydawca_supp_groupc, sizeof (wydawca_supp_groups[0])); for (i = 0, ep = value->v.list->head; ep; ep = ep->next, i++) { const grecs_value_t *vp = ep->data; struct group *grp; - + if (assert_string_arg (locus, cmd, vp)) break; grp = getgrnam (vp->v.string); if (!grp) { grecs_error (locus, 0, _("no such group: %s"), value->v.string); @@ -1349,13 +1308,13 @@ static struct grecs_keyword locking_kw[] = { { "expire-time", N_("interval"), N_("Define lock expiration interval"), grecs_type_string, &lock_expire_time, 0, cb_interval }, { "timeout", N_("interval"), N_("Locking timeout"), grecs_type_string, &lock_timeout, 0, cb_interval }, { NULL } }; - + static int cb_locking (enum grecs_callback_command cmd, grecs_locus_t *locus, void *varptr, grecs_value_t *value, void *cb_data) @@ -1369,13 +1328,13 @@ cb_locking (enum grecs_callback_command cmd, } grecs_string_convert (&enable_locking, grecs_type_bool, value->v.string, locus); } return 0; } - + static struct grecs_keyword wydawca_kw[] = { { "daemon", NULL, N_("Enable daemon mode"), grecs_type_bool, &daemon_mode }, { "foreground", NULL, N_("Start in foreground even in daemon mode"), @@ -1388,26 +1347,26 @@ static struct grecs_keyword wydawca_kw[] = { grecs_type_string, &pidfile }, { "user", N_("name"), N_("Run with UID and GID of this user"), grecs_type_string, NULL, 0, cb_user }, { "group", NULL, N_("Retain these supplementary groups"), grecs_type_string|GRECS_LIST, NULL, 0, cb_supp_groups }, - + { "locking", NULL, N_("Configure locking"), grecs_type_section, NULL, 0, cb_locking, NULL, locking_kw }, - + { "listen", N_("socket"), N_("Listen on this address"), grecs_type_sockaddr, &listen_sockaddr, }, #ifdef WITH_LIBWRAP { "tcp-wrapper", NULL, N_("Configure TCP wrappers"), grecs_type_section, NULL, 0, NULL, NULL, tcpwrapper_kw }, #endif - + { "mailer", N_("url"), N_("Set mailer URL"), grecs_type_string, &mailer, 0, cb_mailer }, { "admin-address", N_("email"), N_("Set admin email address"), grecs_type_string, &admin_address, 0, cb_email_address }, { "from-address", N_("email"), N_("Set sender email address"), grecs_type_string, &from_address, 0, cb_email_address }, @@ -1420,13 +1379,13 @@ static struct grecs_keyword wydawca_kw[] = { { "umask", N_("mask: octal"), N_("Set umask"), grecs_type_string, NULL, 0, cb_set_umask }, { "archive-signatures", NULL, N_("Control implicit signature archivation"), grecs_type_bool, &archive_signatures }, - + { "statistics", N_("items"), N_("Print these stats at the end of run"), grecs_type_string, &print_stats, 0, cb_statistics }, { "sql", N_("id: string"), N_("Define SQL database"), grecs_type_section, NULL, 0, cb_sql, NULL, sql_kw }, @@ -1461,13 +1420,13 @@ static struct grecs_keyword wydawca_kw[] = { { "all-spools", NULL, N_("Service names that request scanning all spools"), grecs_type_string|GRECS_LIST, &all_spool_aliases }, { "gpg-homedir", NULL, N_("GPG home directory"), grecs_type_string, &wydawca_gpg_homedir }, - + { NULL } }; void config_help () { @@ -1500,7 +1459,6 @@ config_init() void config_finish (struct grecs_node *tree) { if (grecs_tree_process (tree, wydawca_kw)) exit (EX_CONFIG); } - @@ -12,13 +12,12 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with wydawca. If not, see <http://www.gnu.org/licenses/>. */ #include "wydawca.h" -#include "xgethostname.h" int enable_locking = 1; char *lockdir; time_t lock_expire_time = 5*60; time_t lock_timeout = 60; @@ -170,12 +169,52 @@ expire_stale_lock (const char *file) close (fd); if (stale) unlink (file); } +static char * +host_name () +{ + static char *hostbuf = NULL; + size_t size; + int rc; + + if (hostbuf) + return hostbuf; + + do + { + if (!hostbuf) + { + size = 256; + hostbuf = xmalloc (size); + } + else + { + size_t ns = size * 2; + if (size < ns) + xalloc_die (); + size = ns; + hostbuf = xrealloc (hostbuf, size); + } + } + while ((rc = gethostname (hostbuf, size )) == -1 && + (errno == EINVAL +#ifdef ENAMETOOLONG + || errno == ENAMETOOLONG +#endif + )); + if (rc) + { + logmsg (LOG_ERR, _("cannot get hostname: %s"), strerror (rc)); + exit (EX_SOFTWARE); + } + return hostbuf; +} + int wydawca_lock (const char *lockname) { char *tempname = NULL; size_t size = 0; int rc; @@ -185,13 +224,13 @@ wydawca_lock (const char *lockname) expire_stale_lock (lockname); /* build the NFS hitching-post to the lock file */ grecs_asprintf (&tempname, &size, "%s.%lu.%lu.%s", lockname, (unsigned long) getpid (), - (unsigned long) time (NULL), xgethostname ()); + (unsigned long) time (NULL), host_name ()); if (!tempname) return LOCK_FAILURE; if (lock_timeout) { time_t start = time (NULL); |