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