Debian Codegraph @

device-src/null-device.c

raw
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 #include "device.h"
25
26 #define NULL_DEVICE_MIN_BLOCK_SIZE (1)
27 #define NULL_DEVICE_MAX_BLOCK_SIZE (INT_MAX)
28 #define NULL_DEVICE_DEFAULT_BLOCK_SIZE DISK_BLOCK_BYTES
29
30 /*
31 * Type checking and casting macros
32 */
33 #define TYPE_NULL_DEVICE (null_device_get_type())
34 #define NULL_DEVICE(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), null_device_get_type(), NullDevice)
35 #define NULL_DEVICE_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), null_device_get_type(), NullDevice const)
36 #define NULL_DEVICE_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), null_device_get_type(), NullDeviceClass)
37 #define IS_NULL_DEVICE(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), null_device_get_type ())
38 #define NULL_DEVICE_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), null_device_get_type(), NullDeviceClass)
39 static GType null_device_get_type (void);
40
41 /*
42 * Main object structure
43 */
44 typedef struct _NullDevice NullDevice;
45 struct _NullDevice {
46 Device __parent__;
47 };
48
49 /*
50 * Class definition
51 */
52 typedef struct _NullDeviceClass NullDeviceClass;
53 struct _NullDeviceClass {
54 DeviceClass __parent__;
55 };
56
57 void null_device_register(void);
58
59 /* here are local prototypes */
60 static void null_device_init (NullDevice * o);
61 static void null_device_class_init (NullDeviceClass * c);
62 static void null_device_base_init (NullDeviceClass * c);
63 static DeviceStatusFlags null_device_read_label(Device * dself);
64 static void null_device_open_device(Device * self, char *device_name,
65 char * device_type, char * device_node);
66 static gboolean null_device_start (Device * self, DeviceAccessMode mode,
67 char * label, char * timestamp);
68 static gboolean null_device_finish (Device * pself);
69 static gboolean null_device_start_file(Device * self, dumpfile_t * jobInfo);
70 static DeviceWriteResult null_device_write_block(Device * self, guint size,
71 gpointer data);
72 static gboolean null_device_finish_file(Device * self);
73 static Device* null_device_factory(char * device_name, char * device_type, char * device_node);
74
75 /* pointer to the class of our parent */
76 static DeviceClass *parent_class = NULL;
77
78 void null_device_register(void) {
79 static const char * device_prefix_list[] = { "null", NULL };
80 register_device(null_device_factory, device_prefix_list);
81 }
82
83 static GType
84 null_device_get_type (void)
85 {
86 static GType type = 0;
87
88 if (G_UNLIKELY(type == 0)) {
89 static const GTypeInfo info = {
90 sizeof (NullDeviceClass),
91 (GBaseInitFunc) null_device_base_init,
92 (GBaseFinalizeFunc) NULL,
93 (GClassInitFunc) null_device_class_init,
94 (GClassFinalizeFunc) NULL,
95 NULL /* class_data */,
96 sizeof (NullDevice),
97 0 /* n_preallocs */,
98 (GInstanceInitFunc) null_device_init,
99 NULL
100 };
101
102 type = g_type_register_static (TYPE_DEVICE, "NullDevice", &info,
103 (GTypeFlags)0);
104 }
105
106 return type;
107 }
108
109 static void
110 null_device_init (NullDevice * self)
111 {
112 Device * dself;
113 GValue response;
114
115 dself = (Device*)(self);
116 bzero(&response, sizeof(response));
117
118 /* Register properties */
119 g_value_init(&response, CONCURRENCY_PARADIGM_TYPE);
120 g_value_set_enum(&response, CONCURRENCY_PARADIGM_RANDOM_ACCESS);
121 device_set_simple_property(dself, PROPERTY_CONCURRENCY,
122 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
123 g_value_unset(&response);
124
125 g_value_init(&response, STREAMING_REQUIREMENT_TYPE);
126 g_value_set_enum(&response, STREAMING_REQUIREMENT_NONE);
127 device_set_simple_property(dself, PROPERTY_STREAMING,
128 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
129 g_value_unset(&response);
130
131 g_value_init(&response, G_TYPE_BOOLEAN);
132 g_value_set_boolean(&response, FALSE);
133 device_set_simple_property(dself, PROPERTY_APPENDABLE,
134 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
135 g_value_unset(&response);
136
137 g_value_init(&response, G_TYPE_BOOLEAN);
138 g_value_set_boolean(&response, FALSE);
139 device_set_simple_property(dself, PROPERTY_PARTIAL_DELETION,
140 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
141 g_value_unset(&response);
142
143 g_value_init(&response, G_TYPE_BOOLEAN);
144 g_value_set_boolean(&response, FALSE);
145 device_set_simple_property(dself, PROPERTY_FULL_DELETION,
146 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
147 g_value_unset(&response);
148
149 g_value_init(&response, G_TYPE_BOOLEAN);
150 g_value_set_boolean(&response, FALSE);
151 device_set_simple_property(dself, PROPERTY_LEOM,
152 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
153 g_value_unset(&response);
154
155 /* this device's canonical name is always "null:", regardless of
156 * the name the user supplies; note that we install the simple
157 * getter in null_device_class_init. */
158 g_value_init(&response, G_TYPE_STRING);
159 g_value_set_static_string(&response, "null:");
160 device_set_simple_property(dself, PROPERTY_CANONICAL_NAME,
161 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DEFAULT);
162 g_value_unset(&response);
163
164 g_value_init(&response, MEDIA_ACCESS_MODE_TYPE);
165 g_value_set_enum(&response, MEDIA_ACCESS_MODE_WRITE_ONLY);
166 device_set_simple_property(dself, PROPERTY_MEDIUM_ACCESS_TYPE,
167 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
168 g_value_unset(&response);
169 }
170
171 static void
172 null_device_class_init (NullDeviceClass * c)
173 {
174 DeviceClass *device_class = (DeviceClass *)c;
175
176 parent_class = g_type_class_ref (TYPE_DEVICE);
177
178 device_class->read_label = null_device_read_label;
179 device_class->open_device = null_device_open_device;
180 device_class->start = null_device_start;
181 device_class->finish = null_device_finish;
182 device_class->start_file = null_device_start_file;
183 device_class->write_block = null_device_write_block;
184 device_class->finish_file = null_device_finish_file;
185 }
186
187 static void
188 null_device_base_init (NullDeviceClass * c)
189 {
190 DeviceClass *device_class = (DeviceClass *)c;
191
192 /* Our canonical name is simpler than most devices' */
193 device_class_register_property(device_class, PROPERTY_CANONICAL_NAME,
194 PROPERTY_ACCESS_GET_MASK,
195 device_simple_property_get_fn,
196 device_simple_property_set_fn);
197 }
198
199
200 static Device* null_device_factory(char * device_name, char * device_type, char * device_node) {
201 Device * device;
202 g_assert(g_str_equal(device_type, "null"));
203 device = DEVICE(g_object_new(TYPE_NULL_DEVICE, NULL));
204 device_open_device(device, device_name, device_type, device_node);
205 return device;
206 }
207
208 /* Begin virtual function overrides */
209
210 static DeviceStatusFlags
211 null_device_read_label(Device * dself) {
212 if (device_in_error(dself)) return FALSE;
213
214 device_set_error(dself,
215 g_strdup(_("Can't open NULL device for reading or appending.")),
216 DEVICE_STATUS_VOLUME_UNLABELED | DEVICE_STATUS_VOLUME_ERROR);
217 return FALSE;
218 }
219
220 static void
221 null_device_open_device(Device * pself, char *device_name,
222 char * device_type, char * device_node)
223 {
224 pself->min_block_size = NULL_DEVICE_MIN_BLOCK_SIZE;
225 pself->max_block_size = NULL_DEVICE_MAX_BLOCK_SIZE;
226 pself->block_size = NULL_DEVICE_DEFAULT_BLOCK_SIZE;
227
228 if (parent_class->open_device) {
229 parent_class->open_device(pself, device_name, device_type, device_node);
230 }
231 }
232
233 static gboolean
234 null_device_start (Device * pself, DeviceAccessMode mode,
235 char * label, char * timestamp) {
236 NullDevice * self;
237 self = NULL_DEVICE(pself);
238
239 if (device_in_error(self)) return FALSE;
240
241 pself->access_mode = mode;
242 g_mutex_lock(pself->device_mutex);
243 pself->in_file = FALSE;
244 g_mutex_unlock(pself->device_mutex);
245
246 if (mode == ACCESS_WRITE) {
247 g_free(pself->volume_label);
248 pself->volume_label = g_strdup(label);
249 g_free(pself->volume_time);
250 pself->volume_time = g_strdup(timestamp);
251 pself->header_block_size = 32768;
252 return TRUE;
253 } else {
254 device_set_error(pself,
255 g_strdup(_("Can't open NULL device for reading or appending.")),
256 DEVICE_STATUS_VOLUME_UNLABELED | DEVICE_STATUS_VOLUME_ERROR);
257 return FALSE;
258 }
259 }
260
261 /* This default implementation does very little. */
262 static gboolean
263 null_device_finish (Device * pself) {
264 pself->access_mode = ACCESS_NULL;
265
266 if (device_in_error(pself)) return FALSE;
267
268 return TRUE;
269 }
270
271 static gboolean
272 null_device_start_file(Device * d_self,
273 dumpfile_t * jobInfo G_GNUC_UNUSED)
274 {
275 g_mutex_lock(d_self->device_mutex);
276 d_self->in_file = TRUE;
277 g_mutex_unlock(d_self->device_mutex);
278 d_self->is_eom = FALSE;
279 d_self->block = 0;
280 if (d_self->file <= 0)
281 d_self->file = 1;
282 else
283 d_self->file ++;
284
285 return TRUE;
286 }
287
288 static DeviceWriteResult
289 null_device_write_block (Device * pself, guint size G_GNUC_UNUSED,
290 gpointer data G_GNUC_UNUSED) {
291 NullDevice * self;
292 self = NULL_DEVICE(pself);
293
294 if (device_in_error(self)) return WRITE_FAILED;
295
296 pself->block++;
297
298 return WRITE_SUCCEED;
299 }
300
301 static gboolean
302 null_device_finish_file(Device * pself) {
303
304 if (!pself->in_file)
305 return TRUE;
306
307 g_mutex_lock(pself->device_mutex);
308 pself->in_file = FALSE;
309 g_mutex_unlock(pself->device_mutex);
310
311 if (device_in_error(pself)) return FALSE;
312
313 return TRUE;
314 }