Imported Upstream version 3.2.0
[debian/amanda] / device-src / property.c
1 /*
2  * Copyright (c) 2007, 2008, 2009, 2010 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published
6  * by the Free Software Foundation.
7  *
8  * This program 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 General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16  *
17  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19  */
20
21 #include "amanda.h"
22
23 #include "property.h"
24 #include "glib-util.h"
25
26 /*****
27  * Property-specific Types, etc.
28  */
29
30 static const GEnumValue _concurrency_paradigm_values[] = {
31         { CONCURRENCY_PARADIGM_EXCLUSIVE,
32           "CONCURRENCY_PARADIGM_EXCLUSIVE",
33           "exclusive" },
34         { CONCURRENCY_PARADIGM_SHARED_READ,
35           "CONCURRENCY_PARADIGM_SHARED_READ",
36           "shared-read" },
37         { CONCURRENCY_PARADIGM_RANDOM_ACCESS,
38           "CONCURRENCY_PARADIGM_RANDOM_ACCESS",
39           "random-access" },
40         { 0, NULL, NULL }
41 };
42
43 GType concurrency_paradigm_get_type (void) {
44     static GType type = 0;
45     if (G_UNLIKELY(type == 0)) {
46         type = g_enum_register_static ("ConcurrencyParadigm",
47                                        _concurrency_paradigm_values);
48     }
49     return type;
50 }
51
52 static const GEnumValue _streaming_requirement_values[] = {
53         { STREAMING_REQUIREMENT_NONE,
54           "STREAMING_REQUIREMENT_NONE",
55           "none" },
56         { STREAMING_REQUIREMENT_DESIRED,
57           "STREAMING_REQUIREMENT_DESIRED",
58           "desired" },
59         { STREAMING_REQUIREMENT_REQUIRED,
60           "STREAMING_REQUIREMENT_REQUIRED",
61           "required" },
62         { 0, NULL, NULL }
63 };
64
65 GType streaming_requirement_get_type (void) {
66     static GType type = 0;
67     if (G_UNLIKELY(type == 0)) {
68         type = g_enum_register_static ("StreamingRequirement",
69                                        _streaming_requirement_values);
70     }
71     return type;
72 }
73
74 static const GEnumValue _media_access_mode_values[] = {
75         { MEDIA_ACCESS_MODE_READ_ONLY,
76           "MEDIA_ACCESS_MODE_READ_ONLY",
77           (char *)"read-only" },
78         { MEDIA_ACCESS_MODE_WORM,
79           "MEDIA_ACCESS_MODE_WORM",
80           (char *)"write-once-read-many" },
81         { MEDIA_ACCESS_MODE_READ_WRITE,
82           "MEDIA_ACCESS_MODE_READ_WRITE",
83           (char *)"read-write" },
84         { MEDIA_ACCESS_MODE_WRITE_ONLY,
85           "MEDIA_ACCESS_MODE_WRITE_ONLY",
86           (char *)"write-many-read-never" },
87         { 0, NULL, NULL }
88 };
89
90 GType media_access_mode_get_type (void) {
91     static GType type = 0;
92     if (G_UNLIKELY(type == 0)) {
93         type = g_enum_register_static ("MediaAccessMode",
94                                        _media_access_mode_values);
95     }
96     return type;
97 }
98
99 /******
100  * Property registration and lookup
101  */
102
103 static GPtrArray *device_property_bases = NULL;
104 static GHashTable *device_property_bases_by_name = NULL;
105
106 DevicePropertyBase* device_property_get_by_id(DevicePropertyId id) {
107     if (!device_property_bases || id >= device_property_bases->len)
108         return NULL;
109
110     return g_ptr_array_index(device_property_bases, id);
111 }
112
113 DevicePropertyBase* device_property_get_by_name(const char *name) {
114     gpointer rv;
115
116     if (!device_property_bases_by_name)
117         return NULL;
118
119     rv = g_hash_table_lookup(device_property_bases_by_name, name);
120     if (rv)
121         return (DevicePropertyBase *)rv;
122
123     return NULL;
124 }
125
126 #define toupper_and_underscore(c) (((c)=='-')? '_' : g_ascii_toupper((c)))
127 static guint
128 device_property_hash(
129         gconstpointer key)
130 {
131     /* modified version of glib's hash function, copyright
132      * GLib Team and others 1997-2000. */
133     const char *p = key;
134     guint h = toupper_and_underscore(*p);
135
136     if (h)
137         for (p += 1; *p != '\0'; p++)
138             h = (h << 5) - h + toupper_and_underscore(*p);
139
140     return h;
141 }
142
143 static gboolean
144 device_property_equal(
145         gconstpointer v1,
146         gconstpointer v2)
147 {
148     const char *s1 = v1, *s2 = v2;
149
150     while (*s1 && *s2) {
151         if (toupper_and_underscore(*s1) != toupper_and_underscore(*s2))
152             return FALSE;
153         s1++, s2++;
154     }
155     if (*s1 || *s2)
156         return FALSE;
157
158     return TRUE;
159 }
160
161 void
162 device_property_fill_and_register(DevicePropertyBase *base,
163                     GType type, const char * name, const char * desc) {
164
165     /* create the hash table and array if necessary */
166     if (!device_property_bases) {
167         device_property_bases = g_ptr_array_new();
168         device_property_bases_by_name = g_hash_table_new(device_property_hash, device_property_equal);
169     }
170
171     /* check for a duplicate */
172     if (device_property_get_by_name(name)) {
173         g_critical("A property named '%s' already exists!", name);
174     }
175
176     /* allocate space for this DPB and fill it in */
177     base->ID = device_property_bases->len;
178     base->type = type;
179     base->name = name; /* no strdup -- it's statically allocated */
180     base->description = desc; /* ditto */
181
182     /* add it to the array and hash table; note that its array index and its
183      * ID are the same. */
184     g_ptr_array_add(device_property_bases, base);
185     g_hash_table_insert(device_property_bases_by_name, (gpointer)name, (gpointer)base);
186 }
187
188 /******
189  * Initialization
190  */
191
192 void device_property_init(void) {
193     device_property_fill_and_register(&device_property_concurrency,
194                                       CONCURRENCY_PARADIGM_TYPE, "concurrency",
195       "Supported concurrency mode (none, multiple readers, multiple writers)");
196     device_property_fill_and_register(&device_property_streaming,
197                                       STREAMING_REQUIREMENT_TYPE, "streaming",
198       "Streaming desirability (unnecessary, desired, required)");
199     device_property_fill_and_register(&device_property_compression,
200                                       G_TYPE_BOOLEAN, "compression",
201       "Is device performing data compression?");
202     device_property_fill_and_register(&device_property_compression_rate,
203                                       G_TYPE_DOUBLE, "compression_rate",
204       "Compression rate, "
205           "averaged for some (currently undefined) period of time)");
206     device_property_fill_and_register(&device_property_block_size,
207                                       G_TYPE_INT, "block_size",
208                                       "Block size to use while writing.");
209     device_property_fill_and_register(&device_property_min_block_size,
210                                       G_TYPE_UINT, "min_block_size",
211       "Minimum supported blocking factor.");
212     device_property_fill_and_register(&device_property_max_block_size,
213                                       G_TYPE_UINT, "max_block_size",
214       "Maximum supported blocking factor.");
215     device_property_fill_and_register(&device_property_read_block_size,
216                                       G_TYPE_UINT, "read_block_size",
217       "Minimum size of a read for this device (maximum expected block size)");
218     device_property_fill_and_register(&device_property_appendable,
219                                       G_TYPE_BOOLEAN, "appendable",
220       "Does device support appending to previously-written media?");
221     device_property_fill_and_register(&device_property_canonical_name,
222                                       G_TYPE_STRING, "canonical_name",
223       "The most reliable device name to use to refer to this device.");
224     device_property_fill_and_register(&device_property_medium_access_type,
225                                       MEDIA_ACCESS_MODE_TYPE,
226                                       "medium_access_type",
227       "What kind of media (RO/WORM/RW/WORN) do we have here?");
228     device_property_fill_and_register(&device_property_partial_deletion,
229                                      G_TYPE_BOOLEAN, "partial_deletion",
230       "Does this device support recycling just part of a volume?" );
231     device_property_fill_and_register(&device_property_full_deletion,
232                                      G_TYPE_BOOLEAN, "full_deletion",
233       "Does this device support recycling the entire volume?" );
234     device_property_fill_and_register(&device_property_max_volume_usage,
235                                       G_TYPE_UINT64, "max_volume_usage",
236       "Artificial limit to data written to volume.");
237     device_property_fill_and_register(&device_property_verbose,
238                                      G_TYPE_BOOLEAN, "verbose",
239        "Should the device produce verbose output?");
240     device_property_fill_and_register(&device_property_comment,
241                                      G_TYPE_STRING, "comment",
242        "User-specified comment for the device");
243     device_property_fill_and_register(&device_property_leom,
244                                      G_TYPE_BOOLEAN, "leom",
245        "Does this device support LEOM?");
246 }
247
248 DevicePropertyBase device_property_concurrency;
249 DevicePropertyBase device_property_streaming;
250 DevicePropertyBase device_property_compression;
251 DevicePropertyBase device_property_compression_rate;
252 DevicePropertyBase device_property_block_size;
253 DevicePropertyBase device_property_min_block_size;
254 DevicePropertyBase device_property_max_block_size;
255 DevicePropertyBase device_property_read_block_size;
256 DevicePropertyBase device_property_appendable;
257 DevicePropertyBase device_property_canonical_name;
258 DevicePropertyBase device_property_medium_access_type;
259 DevicePropertyBase device_property_partial_deletion;
260 DevicePropertyBase device_property_full_deletion;
261 DevicePropertyBase device_property_max_volume_usage;
262 DevicePropertyBase device_property_comment;
263 DevicePropertyBase device_property_leom;
264 DevicePropertyBase device_property_verbose;