Tizen 2.1 base
[external/device-mapper.git] / liblvm / lvm_misc.c
1 /*
2  * Copyright (C) 2008,2010 Red Hat, Inc. All rights reserved.
3  *
4  * This file is part of LVM2.
5  *
6  * This copyrighted material is made available to anyone wishing to use,
7  * modify, copy, or redistribute it subject to the terms and conditions
8  * of the GNU Lesser General Public License v.2.1.
9  *
10  * You should have received a copy of the GNU Lesser General Public License
11  * along with this program; if not, write to the Free Software Foundation,
12  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
13  */
14
15 #include "lib.h"
16 #include "properties.h"
17 #include "lvm_misc.h"
18 #include "lvm2app.h"
19
20 struct dm_list *tag_list_copy(struct dm_pool *p, struct dm_list *tag_list)
21 {
22         struct dm_list *list;
23         lvm_str_list_t *lsl;
24         struct str_list *sl;
25
26         if (!(list = dm_pool_zalloc(p, sizeof(*list)))) {
27                 log_errno(ENOMEM, "Memory allocation fail for dm_list.");
28                 return NULL;
29         }
30         dm_list_init(list);
31
32         dm_list_iterate_items(sl, tag_list) {
33                 if (!(lsl = dm_pool_zalloc(p, sizeof(*lsl)))) {
34                         log_errno(ENOMEM,
35                                 "Memory allocation fail for lvm_lv_list.");
36                         return NULL;
37                 }
38                 if (!(lsl->str = dm_pool_strdup(p, sl->str))) {
39                         log_errno(ENOMEM,
40                                 "Memory allocation fail for lvm_lv_list->str.");
41                         return NULL;
42                 }
43                 dm_list_add(list, &lsl->list);
44         }
45         return list;
46 }
47
48 struct lvm_property_value get_property(const pv_t pv, const vg_t vg,
49                                        const lv_t lv, const lvseg_t lvseg,
50                                        const pvseg_t pvseg, const char *name)
51 {
52         struct lvm_property_type prop;
53         struct lvm_property_value v;
54
55         prop.id = name;
56         if (pv) {
57                 if (!pv_get_property(pv, &prop)) {
58                         v.is_valid = 0;
59                         return v;
60                 }
61         } else if (vg) {
62                 if (!vg_get_property(vg, &prop)) {
63                         v.is_valid = 0;
64                         return v;
65                 }
66         } else if (lv) {
67                 if (!lv_get_property(lv, &prop)) {
68                         v.is_valid = 0;
69                         return v;
70                 }
71         } else if (lvseg) {
72                 if (!lvseg_get_property(lvseg, &prop)) {
73                         v.is_valid = 0;
74                         return v;
75                 }
76         } else if (pvseg) {
77                 if (!pvseg_get_property(pvseg, &prop)) {
78                         v.is_valid = 0;
79                         return v;
80                 }
81         }
82         v.is_settable = prop.is_settable;
83         v.is_string = prop.is_string;
84         v.is_integer = prop.is_integer;
85         if (v.is_string)
86                 v.value.string = prop.value.string;
87         if (v.is_integer)
88                 v.value.integer = prop.value.integer;
89         v.is_valid = 1;
90         return v;
91 }
92
93
94 int set_property(const pv_t pv, const vg_t vg, const lv_t lv,
95                  const char *name, struct lvm_property_value *v)
96 {
97         struct lvm_property_type prop;
98
99         prop.id = name;
100         if (v->is_string)
101                 prop.value.string = v->value.string;
102         else
103                 prop.value.integer = v->value.integer;
104         if (pv) {
105                 if (!pv_set_property(pv, &prop)) {
106                         v->is_valid = 0;
107                         return -1;
108                 }
109         } else if (vg) {
110                 if (!vg_set_property(vg, &prop)) {
111                         v->is_valid = 0;
112                         return -1;
113                 }
114         } else if (lv) {
115                 if (!lv_set_property(lv, &prop)) {
116                         v->is_valid = 0;
117                         return -1;
118                 }
119         }
120         return 0;
121 }