Imported Upstream version 2.6.0
[debian/amanda] / device-src / property.c
1 /*
2  * Copyright (c) 2005 Zmanda, Inc.  All Rights Reserved.
3  * 
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 2.1 as 
6  * published by the Free Software Foundation.
7  * 
8  * This library is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
11  * License for more details.
12  * 
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
16  * 
17  * Contact information: Zmanda Inc., 505 N Mathlida Ave, Suite 120
18  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19  */
20
21 #include "amanda.h"
22
23 #include "property.h"
24
25 static const GEnumValue _concurrency_paradigm_values[] = {
26         { CONCURRENCY_PARADIGM_EXCLUSIVE,
27           "CONCURRENCY_PARADIGM_EXCLUSIVE",
28           "exclusive" },
29         { CONCURRENCY_PARADIGM_SHARED_READ, 
30           "CONCURRENCY_PARADIGM_SHARED_READ",
31           "shared-read" },
32         { CONCURRENCY_PARADIGM_RANDOM_ACCESS,
33           "CONCURRENCY_PARADIGM_RANDOM_ACCESS",
34           "random-access" },
35         { 0, NULL, NULL }
36 };
37
38 GType concurrency_paradigm_get_type (void) {
39     static GType type = 0;
40     if (G_UNLIKELY(type == 0)) {
41         type = g_enum_register_static ("ConcurrencyParadigm",
42                                        _concurrency_paradigm_values);
43     }
44     return type;
45 }
46
47 static const GEnumValue _streaming_requirement_values[] = {
48         { STREAMING_REQUIREMENT_NONE,
49           "STREAMING_REQUIREMENT_NONE",
50           "none" },
51         { STREAMING_REQUIREMENT_DESIRED,
52           "STREAMING_REQUIREMENT_DESIRED",
53           "desired" },
54         { STREAMING_REQUIREMENT_REQUIRED,
55           "STREAMING_REQUIREMENT_REQUIRED",
56           "required" },
57         { 0, NULL, NULL }
58 };
59
60 GType streaming_requirement_get_type (void) {
61     static GType type = 0;
62     if (G_UNLIKELY(type == 0)) {
63         type = g_enum_register_static ("StreamingRequirement",
64                                        _streaming_requirement_values);
65     }
66     return type;
67 }
68
69 static const GEnumValue _media_access_mode_values[] = {
70         { MEDIA_ACCESS_MODE_READ_ONLY,
71           "MEDIA_ACCESS_MODE_READ_ONLY",
72           (char *)"read-only" },
73         { MEDIA_ACCESS_MODE_WORM,
74           "MEDIA_ACCESS_MODE_WORM",
75           (char *)"write-once-read-many" },
76         { MEDIA_ACCESS_MODE_READ_WRITE,
77           "MEDIA_ACCESS_MODE_READ_WRITE",
78           (char *)"read-write" },
79         { MEDIA_ACCESS_MODE_WRITE_ONLY,
80           "MEDIA_ACCESS_MODE_WRITE_ONLY",
81           (char *)"write-many-read-never" },
82         { 0, NULL, NULL }
83 };
84
85 GType media_access_mode_get_type (void) {
86     static GType type = 0;
87     if (G_UNLIKELY(type == 0)) {
88         type = g_enum_register_static ("MediaAccessMode",
89                                        _media_access_mode_values);
90     }
91     return type;
92 }
93
94 /* Copy function for GBoxed QualifiedSize. */
95 static gpointer qualified_size_copy(gpointer source) {
96     gpointer rval = malloc(sizeof(QualifiedSize));
97     memcpy(rval, source, sizeof(QualifiedSize));
98     return rval;
99 }
100
101 GType qualified_size_get_type (void) {
102     static GType type = 0;
103     if (G_UNLIKELY(type == 0)) {
104         type = g_boxed_type_register_static ("QualifiedSize",
105                                              qualified_size_copy,
106                                              free);
107     }
108     return type;
109 }
110
111 static const GFlagsValue _feature_support_flags_values[] = {
112     { FEATURE_STATUS_ENABLED,
113       "FEATURE_STATUS_ENABLED",
114       "enabled" },
115     { FEATURE_STATUS_DISABLED,
116       "FEATURE_STATUS_DISABLED",
117       "disabled" },
118     { FEATURE_SURETY_BAD,
119       "FEATURE_SURETY_BAD",
120       "bad" },
121     { FEATURE_SURETY_GOOD,
122       "FEATURE_SURETY_GOOD",
123       "good" },
124     { FEATURE_SOURCE_DEFAULT,
125       "FEATURE_SOURCE_DEFAULT",
126       "default" },
127     { FEATURE_SOURCE_DETECTED,
128       "FEATURE_SOURCE_DETECTED",
129       "detected" },
130     { FEATURE_SOURCE_USER,
131       "FEATURE_SOURCE_USER",
132       "user"},
133     { 0, NULL, NULL }
134 };
135
136 GType feature_support_get_type (void) {
137     static GType type = 0;
138     if (G_UNLIKELY(type == 0)) {
139         type = g_flags_register_static ("FeatureSupportFlags",
140                                         _feature_support_flags_values);
141     }
142     return type;
143 }
144
145 gboolean feature_support_flags_is_valid(FeatureSupportFlags f) {
146     int status = 0, surety = 0, source = 0;
147
148     if (f & FEATURE_STATUS_ENABLED)
149         status ++;
150     if (f & FEATURE_STATUS_DISABLED)
151         status ++;
152     if (f & FEATURE_SURETY_BAD)
153         surety ++;
154     if (f & FEATURE_SURETY_GOOD)
155         surety ++;
156     if (f & FEATURE_SOURCE_DEFAULT)
157         source ++;
158     if (f & FEATURE_SOURCE_DETECTED)
159         source ++;
160     if (f & FEATURE_SOURCE_USER)
161         source ++;
162
163     return (!(f & ~FEATURE_SUPPORT_FLAGS_MASK) &&
164             status == 1  &&  surety == 1  &&  source == 1);
165 }
166
167 static GSList* device_property_base_list = NULL;
168
169 const DevicePropertyBase* device_property_get_by_id(DevicePropertyId id) {
170     GSList *iter;
171
172     iter = device_property_base_list;
173     while (iter != NULL) {
174         DevicePropertyBase* rval = (DevicePropertyBase*)(iter->data);
175         if (rval->ID == id) {
176             return rval;
177         }
178         iter = g_slist_next(iter);
179     }
180
181     return NULL;
182 }
183
184 const DevicePropertyBase* device_property_get_by_name(const char *name) {
185     GSList *iter = device_property_base_list;
186
187     g_return_val_if_fail(name != NULL, NULL);
188
189     while (iter != NULL) {
190         DevicePropertyBase* rval = (DevicePropertyBase*)(iter->data);
191         if (strcasecmp(rval->name, name) == 0) {
192             return rval;
193         }
194         iter = g_slist_next(iter);
195     }
196
197     return NULL;
198 }
199
200 DevicePropertyId device_property_register(DevicePropertyBase* base) {
201     static guint id = 0;
202     g_assert(base != NULL);
203     g_assert(base->ID == -1);
204     g_assert(base->name != NULL);
205     g_assert(base->description != NULL);
206     
207     base->ID = id++;
208
209     device_property_base_list = g_slist_prepend(device_property_base_list,
210                                                 base);
211     return id;
212 }
213
214 /* Does the same thing, but fills in a new DevicePropertyBase. */
215 static void
216 device_property_fill_and_register(DevicePropertyBase * base,
217                                   GType type,
218                                   const char * name,
219                                   const char * desc) {
220     base->type = type;
221     base->name = name;
222     base->description = desc;
223     base->ID = -1;
224     device_property_register(base);
225 }
226
227
228 void device_property_init(void) {
229     device_property_fill_and_register(&device_property_concurrency,
230                                       CONCURRENCY_PARADIGM_TYPE, "concurrency",
231       "Supported concurrency mode (none, multiple readers, multiple writers)");
232     device_property_fill_and_register(&device_property_streaming,
233                                       STREAMING_REQUIREMENT_TYPE, "streaming",
234       "Streaming desirability (unnecessary, desired, required)");
235     device_property_fill_and_register(&device_property_compression,
236                                       G_TYPE_BOOLEAN, "compression",
237       "Is device performing data compression?");
238     device_property_fill_and_register(&device_property_compression_rate,
239                                       G_TYPE_DOUBLE, "compression_rate",
240       "Compression rate, "
241           "averaged for some (currently undefined) period of time)");
242     device_property_fill_and_register(&device_property_block_size,
243                                       G_TYPE_INT, "block_size",
244                                       "Device blocking factor in bytes.");
245     device_property_fill_and_register(&device_property_min_block_size,
246                                       G_TYPE_UINT, "min_block_size",
247       "Minimum supported blocking factor.");
248     device_property_fill_and_register(&device_property_max_block_size,
249                                       G_TYPE_UINT, "max_block_size",
250       "Maximum supported blocking factor.");
251     device_property_fill_and_register(&device_property_appendable,
252                                       G_TYPE_BOOLEAN, "appendable",
253       "Does device support appending to previously-written media?");
254     device_property_fill_and_register(&device_property_canonical_name,
255                                       G_TYPE_STRING, "canonical_name",
256       "The most reliable device name to use to refer to this device.");
257     device_property_fill_and_register(&device_property_medium_access_type,
258                                       MEDIA_ACCESS_MODE_TYPE,
259                                       "medium_access_type",
260       "What kind of media (RO/WORM/RW/WORN) do we have here?");
261     device_property_fill_and_register(&device_property_partial_deletion,
262                                      G_TYPE_BOOLEAN, "partial_deletion",
263       "Does this device support recycling just part of a volume?" );
264     device_property_fill_and_register(&device_property_free_space,
265                                       QUALIFIED_SIZE_TYPE, "free_space",
266       "Remaining capacity of the device.");
267     device_property_fill_and_register(&device_property_max_volume_usage,
268                                       G_TYPE_UINT64, "max_volume_usage",
269       "Artificial limit to data written to volume.");
270     device_property_fill_and_register(&device_property_fsf,
271                                       FEATURE_SUPPORT_FLAGS_TYPE, "fsf",
272       "Does this drive support the MTFSF command?");
273     device_property_fill_and_register(&device_property_bsf,
274                                       FEATURE_SUPPORT_FLAGS_TYPE, "bsf",
275       "Does this drive support the MTBSF command?" );
276     device_property_fill_and_register(&device_property_fsr,
277                                       FEATURE_SUPPORT_FLAGS_TYPE, "fsr",
278       "Does this drive support the MTFSR command?");
279     device_property_fill_and_register(&device_property_bsr,
280                                       FEATURE_SUPPORT_FLAGS_TYPE, "bsr",
281       "Does this drive support the MTBSR command?");
282     /* FIXME: Is this feature even useful? */
283     device_property_fill_and_register(&device_property_eom,
284                                       FEATURE_SUPPORT_FLAGS_TYPE, "eom",
285       "Does this drive support the MTEOM command?");
286     device_property_fill_and_register(&device_property_bsf_after_eom,
287                                       FEATURE_SUPPORT_FLAGS_TYPE,
288                                       "bsf_after_eom",
289       "Does this drive require an MTBSF after MTEOM in order to append?" );
290     device_property_fill_and_register(&device_property_final_filemarks,
291                                       G_TYPE_UINT, "final_filemarks",
292       "How many filemarks to write after the last tape file?" );
293     device_property_fill_and_register(&device_property_read_buffer_size,
294                                       G_TYPE_UINT, "read_buffer_size",
295       "What buffer size should be used for reading?");
296     device_property_fill_and_register(&device_property_s3_secret_key,
297                                       G_TYPE_STRING, "s3_secret_key",
298        "Secret access key to authenticate with Amazon S3");
299     device_property_fill_and_register(&device_property_s3_access_key,
300                                       G_TYPE_STRING, "s3_access_key",
301        "Access key ID to authenticate with Amazon S3");
302 #ifdef WANT_DEVPAY
303     device_property_fill_and_register(&device_property_s3_user_token,
304                                       G_TYPE_STRING, "s3_user_token",
305        "User token for authentication Amazon devpay requests");
306 #endif
307     device_property_fill_and_register(&device_property_verbose,
308                                      G_TYPE_BOOLEAN, "verbose",
309        "Should the device produce verbose output?");
310 }
311
312 DevicePropertyBase device_property_concurrency;
313 DevicePropertyBase device_property_streaming;
314 DevicePropertyBase device_property_compression;
315 DevicePropertyBase device_property_compression_rate;
316 DevicePropertyBase device_property_block_size;
317 DevicePropertyBase device_property_min_block_size;
318 DevicePropertyBase device_property_max_block_size;
319 DevicePropertyBase device_property_appendable;
320 DevicePropertyBase device_property_canonical_name;
321 DevicePropertyBase device_property_medium_access_type;
322 DevicePropertyBase device_property_partial_deletion;
323 DevicePropertyBase device_property_free_space;
324 DevicePropertyBase device_property_max_volume_usage;
325 DevicePropertyBase device_property_fsf;
326 DevicePropertyBase device_property_bsf;
327 DevicePropertyBase device_property_fsr;
328 DevicePropertyBase device_property_bsr;
329 DevicePropertyBase device_property_eom;
330 DevicePropertyBase device_property_bsf_after_eom;
331 DevicePropertyBase device_property_final_filemarks;
332 DevicePropertyBase device_property_read_buffer_size;
333 DevicePropertyBase device_property_s3_access_key;
334 DevicePropertyBase device_property_s3_secret_key;
335 DevicePropertyBase device_property_s3_user_token;
336 DevicePropertyBase device_property_verbose;