* allow to load templates from arbitrary places

* changed implementation of guest?/iface?
This commit is contained in:
Tobias Brunner
2008-08-28 08:05:07 +00:00
parent 6c20579a43
commit 8bbc7ca710
4 changed files with 19 additions and 48 deletions
+8 -28
View File
@@ -1,4 +1,5 @@
/* /*
* Copyright (C) 2008 Tobias Brunner
* Copyright (C) 2007 Martin Willi * Copyright (C) 2007 Martin Willi
* Hochschule fuer Technik Rapperswil * Hochschule fuer Technik Rapperswil
* *
@@ -29,8 +30,6 @@
#define PERME (S_IRWXU | S_IRWXG) #define PERME (S_IRWXU | S_IRWXG)
#define GUEST_DIR "guests" #define GUEST_DIR "guests"
#define TEMPLATE_DIR "templates"
#define TEMPLATE_DIR_DIR "diff"
typedef struct private_dumm_t private_dumm_t; typedef struct private_dumm_t private_dumm_t;
@@ -41,8 +40,6 @@ struct private_dumm_t {
char *dir; char *dir;
/** directory of guests */ /** directory of guests */
char *guest_dir; char *guest_dir;
/** directory of templates */
char *template_dir;
/** directory of loaded template */ /** directory of loaded template */
char *template; char *template;
/** list of managed guests */ /** list of managed guests */
@@ -151,45 +148,36 @@ static void clear_template(private_dumm_t *this)
/** /**
* Implementation of dumm_t.load_template. * 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; enumerator_t *enumerator;
guest_t *guest; guest_t *guest;
char dir[PATH_MAX];
size_t len;
clear_template(this); clear_template(this);
if (name == NULL) if (dir == NULL)
{ {
return TRUE; return TRUE;
} }
if (strlen(dir) > PATH_MAX)
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))
{ {
DBG1("template directory string '%s' is too long", dir);
return FALSE; return FALSE;
} }
this->template = strdup(dir);
if (access(this->template, F_OK) != 0) if (access(this->template, F_OK) != 0)
{ /* does not exist, create template */ { /* 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); DBG1("creating template directory '%s' failed: %m", this->template);
return FALSE; 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); enumerator = this->guests->create_enumerator(this->guests);
while (enumerator->enumerate(enumerator, (void**)&guest)) while (enumerator->enumerate(enumerator, (void**)&guest))
{ {
if (!guest->load_template(guest, dir)) if (!guest->load_template(guest, this->template))
{ {
enumerator->destroy(enumerator); enumerator->destroy(enumerator);
clear_template(this); clear_template(this);
@@ -223,7 +211,6 @@ static void destroy(private_dumm_t *this)
} }
this->guests->destroy(this->guests); this->guests->destroy(this->guests);
free(this->guest_dir); free(this->guest_dir);
free(this->template_dir);
free(this->template); free(this->template);
free(this->dir); free(this->dir);
free(this); free(this);
@@ -302,7 +289,6 @@ dumm_t *dumm_create(char *dir)
} }
this->template = NULL; this->template = NULL;
asprintf(&this->guest_dir, "%s/%s", this->dir, GUEST_DIR); 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->guests = linked_list_create();
this->bridges = linked_list_create(); this->bridges = linked_list_create();
@@ -312,12 +298,6 @@ dumm_t *dumm_create(char *dir)
destroy(this); destroy(this);
return NULL; 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); load_guests(this);
return &this->public; return &this->public;
+3 -2
View File
@@ -1,4 +1,5 @@
/* /*
* Copyright (C) 2008 Tobias Brunner
* Copyright (C) 2007 Martin Willi * Copyright (C) 2007 Martin Willi
* Hochschule fuer Technik Rapperswil * Hochschule fuer Technik Rapperswil
* *
@@ -84,10 +85,10 @@ struct dumm_t {
/** /**
* @brief Loads a template, create a new one if it does not exist. * @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 * @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 * @brief stop all guests and destroy the modeler
+6 -16
View File
@@ -126,11 +126,6 @@ static VALUE guest_get(VALUE class, VALUE key)
return guest; 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) static VALUE guest_each(int argc, VALUE *argv, VALUE class)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
@@ -272,11 +267,6 @@ static VALUE guest_get_iface(VALUE self, VALUE key)
return iface; 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) static VALUE guest_each_iface(int argc, VALUE *argv, VALUE self)
{ {
enumerator_t *enumerator; 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, "[]", guest_get, 1);
rb_define_singleton_method(rbc_guest, "each", guest_each, -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, "new", guest_new, 4);
rb_define_singleton_method(rbc_guest, "include?", guest_exist, 1); rb_define_singleton_method(rbc_guest, "include?", guest_find, 1);
rb_define_singleton_method(rbc_guest, "guest?", guest_exist, 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, "to_s", guest_to_s, 0);
rb_define_method(rbc_guest, "start", guest_start, 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, "add", guest_add_iface, 1);
rb_define_method(rbc_guest, "[]", guest_get_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, "each", guest_each_iface, -1);
rb_define_method(rbc_guest, "include?", guest_exist_iface, 1); rb_define_method(rbc_guest, "include?", guest_find_iface, 1);
rb_define_method(rbc_guest, "iface?", guest_exist_iface, 1); rb_define_method(rbc_guest, "iface?", guest_find_iface, 1);
rb_define_method(rbc_guest, "delete", guest_delete, 0); 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); 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"); rb_raise(rb_eRuntimeError, "loading template failed");
} }
+1 -1
View File
@@ -311,7 +311,7 @@ static bool load_template(private_guest_t *this, char *path)
} }
if (access(dir, F_OK) != 0) 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); DBG1("creating overlay for guest '%s' failed: %m", this->name);
return FALSE; return FALSE;