checked in first draft of "Dynamic Uml Mesh Modeler"
[strongswan.git] / src / dumm / dumm.c
1 #include <sys/stat.h>
2
3 #include <debug.h>
4
5 #include "dumm.h"
6
7 typedef struct private_dumm_t private_dumm_t;
8
9 struct private_dumm_t {
10 dumm_t public;
11 linked_list_t *guests;
12 };
13
14 static guest_t* start_guest(private_dumm_t *this, char *name, char *kernel,
15 char *master, int mem)
16 {
17 guest_t *guest;
18
19 guest = guest_create(name, kernel, master, mem);
20 if (guest)
21 {
22 if (guest->start(guest))
23 {
24 this->guests->insert_last(this->guests, guest);
25 return guest;
26 }
27 guest->destroy(guest);
28 }
29 return NULL;
30 }
31
32 static iterator_t* create_guest_iterator(private_dumm_t *this)
33 {
34 return this->guests->create_iterator(this->guests, TRUE);
35 }
36
37 static void destroy(private_dumm_t *this)
38 {
39 this->guests->destroy_offset(this->guests, offsetof(guest_t, destroy));
40 free(this);
41 }
42
43 /**
44 * check for a directory, create if it does not exist
45 */
46 static bool makedir(char *dir)
47 {
48 struct stat st;
49
50 if (stat(dir, &st) != 0)
51 {
52 return mkdir(dir, S_IRWXU) == 0;
53 }
54 return S_ISDIR(st.st_mode);
55 }
56
57 dumm_t *dumm_create()
58 {
59 private_dumm_t *this = malloc_thing(private_dumm_t);
60
61 this->public.start_guest = (void*)start_guest;
62 this->public.create_guest_iterator = (void*)create_guest_iterator;
63 this->public.destroy = (void*)destroy;
64
65 if (!makedir(HOST_DIR) || !makedir(MOUNT_DIR) || !makedir(RUN_DIR))
66 {
67 free(this);
68 return NULL;
69 }
70
71 this->guests = linked_list_create();
72 return &this->public;
73 }
74