bc690c51528404f3dd5293ad04679eae5d837c24
[strongswan.git] / src / dumm / dumm.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <sys/stat.h>
17
18 #include <debug.h>
19
20 #include "dumm.h"
21
22 typedef struct private_dumm_t private_dumm_t;
23
24 struct private_dumm_t {
25 dumm_t public;
26 linked_list_t *guests;
27 };
28
29 static guest_t* create_guest(private_dumm_t *this, char *name, char *master, int mem)
30 {
31 guest_t *guest;
32
33 guest = guest_create(name, master, mem);
34 if (guest)
35 {
36 this->guests->insert_last(this->guests, guest);
37 }
38 return guest;
39 }
40
41 static iterator_t* create_guest_iterator(private_dumm_t *this)
42 {
43 return this->guests->create_iterator(this->guests, TRUE);
44 }
45
46 static void destroy(private_dumm_t *this)
47 {
48 this->guests->destroy_offset(this->guests, offsetof(guest_t, destroy));
49 free(this);
50 }
51
52 /**
53 * check for a directory, create if it does not exist
54 */
55 static bool makedir(char *dir)
56 {
57 struct stat st;
58
59 if (stat(dir, &st) != 0)
60 {
61 return mkdir(dir, S_IRWXU) == 0;
62 }
63 return S_ISDIR(st.st_mode);
64 }
65
66 dumm_t *dumm_create()
67 {
68 private_dumm_t *this = malloc_thing(private_dumm_t);
69
70 this->public.create_guest = (void*)create_guest;
71 this->public.create_guest_iterator = (void*)create_guest_iterator;
72 this->public.destroy = (void*)destroy;
73
74 if (!makedir(HOST_DIR) || !makedir(MOUNT_DIR) || !makedir(RUN_DIR))
75 {
76 free(this);
77 return NULL;
78 }
79
80 this->guests = linked_list_create();
81 return &this->public;
82 }
83