diff options
author | Tobias Brunner <tobias@strongswan.org> | 2008-08-28 08:05:07 +0000 |
---|---|---|
committer | Tobias Brunner <tobias@strongswan.org> | 2008-08-28 08:05:07 +0000 |
commit | 8bbc7ca71034db65bf40f656643459d81a8a1cd0 (patch) | |
tree | 6495cfc503f4fda355905f27537abcce8de7ca73 | |
parent | 6c20579a43a73916574f887bf6dc67bc122c0c65 (diff) | |
download | strongswan-8bbc7ca71034db65bf40f656643459d81a8a1cd0.tar.bz2 strongswan-8bbc7ca71034db65bf40f656643459d81a8a1cd0.tar.xz |
* allow to load templates from arbitrary places
* changed implementation of guest?/iface?
-rw-r--r-- | src/dumm/dumm.c | 38 | ||||
-rw-r--r-- | src/dumm/dumm.h | 5 | ||||
-rw-r--r-- | src/dumm/ext/dumm.c | 22 | ||||
-rw-r--r-- | src/dumm/guest.c | 2 |
4 files changed, 19 insertions, 48 deletions
diff --git a/src/dumm/dumm.c b/src/dumm/dumm.c index 0bb74e886..190b7cc9b 100644 --- a/src/dumm/dumm.c +++ b/src/dumm/dumm.c @@ -1,4 +1,5 @@ /* + * Copyright (C) 2008 Tobias Brunner * Copyright (C) 2007 Martin Willi * Hochschule fuer Technik Rapperswil * @@ -29,8 +30,6 @@ #define PERME (S_IRWXU | S_IRWXG) #define GUEST_DIR "guests" -#define TEMPLATE_DIR "templates" -#define TEMPLATE_DIR_DIR "diff" typedef struct private_dumm_t private_dumm_t; @@ -41,8 +40,6 @@ struct private_dumm_t { char *dir; /** directory of guests */ char *guest_dir; - /** directory of templates */ - char *template_dir; /** directory of loaded template */ char *template; /** list of managed guests */ @@ -151,45 +148,36 @@ static void clear_template(private_dumm_t *this) /** * Implementation of dumm_t.load_template. */ -static bool load_template(private_dumm_t *this, char *name) +static bool load_template(private_dumm_t *this, char *dir) { enumerator_t *enumerator; guest_t *guest; - char dir[PATH_MAX]; - size_t len; clear_template(this); - if (name == NULL) + if (dir == NULL) { return TRUE; } - - free(this->template); - asprintf(&this->template, "%s/%s", this->template_dir, name); - len = snprintf(dir, sizeof(dir), "%s/%s", this->template, TEMPLATE_DIR_DIR); - if (len < 0 || len >= sizeof(dir)) + if (strlen(dir) > PATH_MAX) { + DBG1("template directory string '%s' is too long", dir); return FALSE; } + this->template = strdup(dir); if (access(this->template, F_OK) != 0) { /* does not exist, create template */ - if (mkdir(this->template, PERME) != 0) + if (!mkdir_p(this->template, PERME)) { DBG1("creating template directory '%s' failed: %m", this->template); return FALSE; } - if (mkdir(dir, PERME) != 0) - { - DBG1("creating template overlay directory '%s' failed: %m", dir); - return FALSE; - } } enumerator = this->guests->create_enumerator(this->guests); while (enumerator->enumerate(enumerator, (void**)&guest)) { - if (!guest->load_template(guest, dir)) + if (!guest->load_template(guest, this->template)) { enumerator->destroy(enumerator); clear_template(this); @@ -223,7 +211,6 @@ static void destroy(private_dumm_t *this) } this->guests->destroy(this->guests); free(this->guest_dir); - free(this->template_dir); free(this->template); free(this->dir); free(this); @@ -302,7 +289,6 @@ dumm_t *dumm_create(char *dir) } this->template = NULL; asprintf(&this->guest_dir, "%s/%s", this->dir, GUEST_DIR); - asprintf(&this->template_dir, "%s/%s", this->dir, TEMPLATE_DIR); this->guests = linked_list_create(); this->bridges = linked_list_create(); @@ -312,13 +298,7 @@ dumm_t *dumm_create(char *dir) destroy(this); return NULL; } - if (mkdir(this->template_dir, PERME) < 0 && errno != EEXIST) - { - DBG1("creating template directory '%s' failed: %m", this->template_dir); - destroy(this); - return NULL; - } - + load_guests(this); return &this->public; } diff --git a/src/dumm/dumm.h b/src/dumm/dumm.h index 6abf4fc92..686b80a88 100644 --- a/src/dumm/dumm.h +++ b/src/dumm/dumm.h @@ -1,4 +1,5 @@ /* + * Copyright (C) 2008 Tobias Brunner * Copyright (C) 2007 Martin Willi * Hochschule fuer Technik Rapperswil * @@ -84,10 +85,10 @@ struct dumm_t { /** * @brief Loads a template, create a new one if it does not exist. * - * @param name name of the template, NULL to close + * @param name dir to the template, NULL to close * @return FALSE if load/create failed */ - bool (*load_template)(dumm_t *this, char *name); + bool (*load_template)(dumm_t *this, char *dir); /** * @brief stop all guests and destroy the modeler diff --git a/src/dumm/ext/dumm.c b/src/dumm/ext/dumm.c index 3322c77da..521311f61 100644 --- a/src/dumm/ext/dumm.c +++ b/src/dumm/ext/dumm.c @@ -126,11 +126,6 @@ static VALUE guest_get(VALUE class, VALUE key) return guest; } -static VALUE guest_exist(VALUE class, VALUE key) -{ - return NIL_P(guest_find(class, key)) ? Qfalse : Qtrue; -} - static VALUE guest_each(int argc, VALUE *argv, VALUE class) { enumerator_t *enumerator; @@ -272,11 +267,6 @@ static VALUE guest_get_iface(VALUE self, VALUE key) return iface; } -static VALUE guest_exist_iface(VALUE self, VALUE key) -{ - return NIL_P(guest_find_iface(self, key)) ? Qfalse : Qtrue; -} - static VALUE guest_each_iface(int argc, VALUE *argv, VALUE self) { enumerator_t *enumerator; @@ -315,8 +305,8 @@ static void guest_init() rb_define_singleton_method(rbc_guest, "[]", guest_get, 1); rb_define_singleton_method(rbc_guest, "each", guest_each, -1); rb_define_singleton_method(rbc_guest, "new", guest_new, 4); - rb_define_singleton_method(rbc_guest, "include?", guest_exist, 1); - rb_define_singleton_method(rbc_guest, "guest?", guest_exist, 1); + rb_define_singleton_method(rbc_guest, "include?", guest_find, 1); + rb_define_singleton_method(rbc_guest, "guest?", guest_find, 1); rb_define_method(rbc_guest, "to_s", guest_to_s, 0); rb_define_method(rbc_guest, "start", guest_start, 0); @@ -326,8 +316,8 @@ static void guest_init() rb_define_method(rbc_guest, "add", guest_add_iface, 1); rb_define_method(rbc_guest, "[]", guest_get_iface, 1); rb_define_method(rbc_guest, "each", guest_each_iface, -1); - rb_define_method(rbc_guest, "include?", guest_exist_iface, 1); - rb_define_method(rbc_guest, "iface?", guest_exist_iface, 1); + rb_define_method(rbc_guest, "include?", guest_find_iface, 1); + rb_define_method(rbc_guest, "iface?", guest_find_iface, 1); rb_define_method(rbc_guest, "delete", guest_delete, 0); } @@ -573,9 +563,9 @@ static void iface_init() rb_define_method(rbc_iface, "delete", iface_delete, 0); } -static VALUE template_load(VALUE class, VALUE name) +static VALUE template_load(VALUE class, VALUE dir) { - if (!dumm->load_template(dumm, StringValuePtr(name))) + if (!dumm->load_template(dumm, StringValuePtr(dir))) { rb_raise(rb_eRuntimeError, "loading template failed"); } diff --git a/src/dumm/guest.c b/src/dumm/guest.c index bf0d88dde..53e61692c 100644 --- a/src/dumm/guest.c +++ b/src/dumm/guest.c @@ -311,7 +311,7 @@ static bool load_template(private_guest_t *this, char *path) } if (access(dir, F_OK) != 0) { - if (mkdir(dir, PERME) != 0) + if (!mkdir_p(dir, PERME)) { DBG1("creating overlay for guest '%s' failed: %m", this->name); return FALSE; |