Debian Codegraph @

device-src/diskflat-device.c

raw
1 /*
2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 2009 University of Maryland at College Park
4 * Copyright (c) 2007-2012 Zmanda, Inc. All Rights Reserved.
5 * Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved.
6 * All Rights Reserved.
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that
11 * copyright notice and this permission notice appear in supporting
12 * documentation, and that the name of U.M. not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. U.M. makes no representations about the
15 * suitability of this software for any purpose. It is provided "as is"
16 * without express or implied warranty.
17 *
18 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
20 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
22 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
23 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 *
25 * Author: Sam Couter <[email protected]>
26 */
27
28 #include "amanda.h"
29 #include "vfs-device.h"
30
31 /*
32 * Type checking and casting macros
33 */
34 #define TYPE_DISKFLAT_DEVICE (diskflat_device_get_type())
35 #define DISKFLAT_DEVICE(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), TYPE_DISKFLAT_DEVICE, DiskflatDevice)
36 #define DISKFLAT_DEVICE_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), TYPE_DISKFLAT_DEVICE, DiskflatDevice const)
37 #define DISKFLAT_DEVICE_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), TYPE_DISKFLAT_DEVICE, DiskflatDeviceClass)
38 #define IS_DISKFLAT_DEVICE(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), TYPE_DISKFLAT_DEVICE)
39 #define DISKFLAT_DEVICE_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), TYPE_DISKFLAT_DEVICE, DiskflatDeviceClass)
40
41 /* Forward declaration */
42 static GType diskflat_device_get_type(void);
43
44 /*
45 * Main object structure
46 */
47 typedef struct _DiskflatDevice DiskflatDevice;
48 struct _DiskflatDevice {
49 VfsDevice __parent__;
50
51 char *filename;
52 };
53
54 /*
55 * Class definition
56 */
57 typedef struct _DiskflatDeviceClass DiskflatDeviceClass;
58 struct _DiskflatDeviceClass {
59 VfsDeviceClass __parent__;
60 };
61
62
63 /* GObject housekeeping */
64 void
65 diskflat_device_register(void);
66
67 static Device*
68 diskflat_device_factory(char *device_name, char *device_type, char *device_node);
69
70 static void
71 diskflat_device_class_init (DiskflatDeviceClass *c);
72
73 static void
74 diskflat_device_init (DiskflatDevice *self);
75
76 /* Methods */
77 static void
78 diskflat_device_open_device(Device *dself, char *device_name, char *device_type, char *device_node);
79
80 static dumpfile_t *
81 diskflat_device_seek_file (Device * dself, guint requested_file);
82
83 static gboolean
84 diskflat_device_seek_block (Device * dself, guint64 block);
85
86 static gboolean
87 diskflat_device_erase (Device * dself);
88
89 static gboolean
90 diskflat_device_finish(Device *dself);
91
92 static void
93 diskflat_device_finalize(GObject *gself);
94
95 static gboolean
96 diskflat_device_start_file_open(Device *dself, dumpfile_t *ji);
97
98 static void
99 diskflat_update_volume_size(Device *dself);
100
101 static void
102 diskflat_release_file(Device *dself);
103
104 static gboolean
105 diskflat_clear_and_prepare_label(Device *dself, char *label, char *timestamp);
106
107 static gboolean
108 diskflat_validate(Device *dself);
109
110 static GType
111 diskflat_device_get_type (void)
112 {
113 static GType type = 0;
114
115 if (G_UNLIKELY(type == 0)) {
116 static const GTypeInfo info = {
117 sizeof (DiskflatDeviceClass),
118 (GBaseInitFunc) NULL,
119 (GBaseFinalizeFunc) NULL,
120 (GClassInitFunc) diskflat_device_class_init,
121 (GClassFinalizeFunc) NULL,
122 NULL /* class_data */,
123 sizeof (DiskflatDevice),
124 0 /* n_preallocs */,
125 (GInstanceInitFunc) diskflat_device_init,
126 NULL
127 };
128
129 type = g_type_register_static (TYPE_VFS_DEVICE, "DiskflatDevice",
130 &info, (GTypeFlags)0);
131 }
132
133 return type;
134 }
135
136 void
137 diskflat_device_register(void)
138 {
139 const char *device_prefix_list[] = { "diskflat", NULL };
140
141 register_device(diskflat_device_factory, device_prefix_list);
142 }
143
144 static Device *
145 diskflat_device_factory(
146 char *device_name,
147 char *device_type,
148 char *device_node)
149 {
150 Device *device;
151
152 g_assert(g_str_has_prefix(device_type, "diskflat"));
153
154 device = DEVICE(g_object_new(TYPE_DISKFLAT_DEVICE, NULL));
155 device_open_device(device, device_name, device_type, device_node);
156
157 return device;
158 }
159
160 static void
161 diskflat_device_class_init (
162 DiskflatDeviceClass *c)
163 {
164 DeviceClass *device_class = DEVICE_CLASS(c);
165 GObjectClass *g_object_class = G_OBJECT_CLASS(c);
166
167 device_class->open_device = diskflat_device_open_device;
168 device_class->seek_file = diskflat_device_seek_file;
169 device_class->seek_block = diskflat_device_seek_block;
170 device_class->erase = diskflat_device_erase;
171 device_class->finish = diskflat_device_finish;
172
173 g_object_class->finalize = diskflat_device_finalize;
174 }
175
176 static void
177 diskflat_device_init (
178 DiskflatDevice *self)
179 {
180 Device *dself = DEVICE(self);
181 VfsDevice *vself = VFS_DEVICE(self);
182 GValue val;
183
184 vself->device_start_file_open = &diskflat_device_start_file_open;
185 vself->update_volume_size = &diskflat_update_volume_size;
186 vself->release_file = &diskflat_release_file;
187 vself->clear_and_prepare_label = &diskflat_clear_and_prepare_label;
188 vself->validate = &diskflat_validate;
189
190 bzero(&val, sizeof(val));
191
192 g_value_init(&val, G_TYPE_BOOLEAN);
193 g_value_set_boolean(&val, FALSE);
194 device_set_simple_property(dself, PROPERTY_APPENDABLE,
195 &val, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
196 g_value_unset(&val);
197
198 g_value_init(&val, G_TYPE_BOOLEAN);
199 g_value_set_boolean(&val, FALSE);
200 device_set_simple_property(dself, PROPERTY_PARTIAL_DELETION,
201 &val, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
202 g_value_unset(&val);
203
204 g_value_init(&val, G_TYPE_BOOLEAN);
205 g_value_set_boolean(&val, TRUE);
206 device_set_simple_property(dself, PROPERTY_FULL_DELETION,
207 &val, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
208 g_value_unset(&val);
209
210 g_value_init(&val, G_TYPE_BOOLEAN);
211 g_value_set_boolean(&val, TRUE);
212 device_set_simple_property(dself, PROPERTY_LEOM,
213 &val, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
214 g_value_unset(&val);
215
216 }
217
218
219 static void
220 diskflat_device_open_device(
221 Device *dself,
222 char *device_name,
223 char *device_type,
224 char *device_node)
225 {
226 DiskflatDevice *self = DISKFLAT_DEVICE(dself);
227 VfsDevice *vself = VFS_DEVICE(dself);
228 char *d;
229 DeviceClass *parent_class = DEVICE_CLASS(g_type_class_peek_parent(DISKFLAT_DEVICE_GET_CLASS(dself)));
230
231 self->filename = g_strdup(device_node);
232 g_debug("device_node: %s", self->filename);
233
234 parent_class->open_device(dself, device_name, device_type, device_node);
235
236 /* retrieve the directory */
237 if ((d = rindex(vself->dir_name, '/')) != NULL) {
238 *d = '\0';
239 if ((d = rindex(vself->dir_name, '/')) != NULL) {
240 *d = '\0';
241 }
242 }
243 }
244
245 static gboolean
246 diskflat_device_start_file_open(
247 Device *dself,
248 dumpfile_t *ji G_GNUC_UNUSED)
249 {
250 if (dself->file >= 1) {
251 device_set_error(dself,
252 g_strdup_printf(_("Can't write more than one file to the diskflat device")),
253 DEVICE_STATUS_VOLUME_ERROR);
254 return FALSE;
255 }
256
257 dself->file++;
258 return TRUE;
259 }
260
261 static dumpfile_t *
262 diskflat_device_seek_file(
263 Device *dself,
264 guint requested_file)
265 {
266 VfsDevice *vself = VFS_DEVICE(dself);
267 DiskflatDevice *self = DISKFLAT_DEVICE(dself);
268 dumpfile_t *rval;
269 char header_buffer[VFS_DEVICE_LABEL_SIZE];
270 int header_buffer_size = sizeof(header_buffer);
271 IoResult result;
272 off_t result_seek;
273
274 if (device_in_error(dself)) return NULL;
275 if (requested_file > 1) {
276 device_set_error(dself,
277 g_strdup_printf(_("Can't seek to file number above 1")),
278 DEVICE_STATUS_VOLUME_ERROR);
279 return NULL;
280 }
281
282 /* read_label before start */
283 if (requested_file == 0 && vself->open_file_fd == -1) {
284 vself->open_file_fd = robust_open(self->filename,
285 O_RDONLY, 0);
286 if (vself->open_file_fd < 0) {
287 if (errno == ENOENT) {
288 device_set_error(dself,
289 g_strdup_printf(_("Couldn't open file %s: %s (unlabeled)"), self->filename, strerror(errno)),
290 DEVICE_STATUS_VOLUME_UNLABELED);
291 rval = g_new(dumpfile_t, 1);
292 fh_init(rval);
293 return rval;
294 } else {
295 device_set_error(dself,
296 g_strdup_printf(_("Couldn't open file %s: %s"), self->filename, strerror(errno)),
297 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
298 return NULL;
299 }
300 }
301 }
302
303 dself->is_eof = FALSE;
304 dself->block = 0;
305 g_mutex_lock(dself->device_mutex);
306 dself->in_file = FALSE;
307 dself->bytes_read = 0;
308 g_mutex_unlock(dself->device_mutex);
309
310 result_seek = lseek(vself->open_file_fd, requested_file * DISK_BLOCK_BYTES,
311 SEEK_SET);
312 if (result_seek == -1) {
313 device_set_error(dself,
314 g_strdup_printf(_("Error seeking within file: %s"), strerror(errno)),
315 DEVICE_STATUS_DEVICE_ERROR);
316 return NULL;
317 }
318
319 result = vfs_device_robust_read(vself, header_buffer,
320 &header_buffer_size);
321 if (result == RESULT_NO_DATA) {
322 device_set_error(dself,
323 g_strdup_printf(_("Problem reading Amanda header: empty file")),
324 DEVICE_STATUS_VOLUME_UNLABELED);
325 return NULL;
326 } else if (result != RESULT_SUCCESS) {
327 device_set_error(dself,
328 g_strdup_printf(_("Problem reading Amanda header: %s"), device_error(dself)),
329 DEVICE_STATUS_VOLUME_ERROR);
330 return NULL;
331 }
332
333 rval = g_new(dumpfile_t, 1);
334 parse_file_header(header_buffer, rval, header_buffer_size);
335 switch (rval->type) {
336 case F_DUMPFILE:
337 case F_CONT_DUMPFILE:
338 case F_SPLIT_DUMPFILE:
339 break;
340
341 case F_TAPESTART:
342 /* file 0 should have a TAPESTART header; diskflat_device_read_label
343 * uses this */
344 if (requested_file == 0)
345 break;
346 /* FALLTHROUGH */
347
348 default:
349 device_set_error(dself,
350 g_strdup(_("Invalid amanda header while reading file header")),
351 DEVICE_STATUS_VOLUME_ERROR);
352 amfree(rval);
353 return NULL;
354 }
355
356 /* update our state */
357 if (requested_file == 0) {
358 dself->header_block_size = header_buffer_size;
359 } else {
360 g_mutex_lock(dself->device_mutex);
361 dself->in_file = TRUE;
362 g_mutex_unlock(dself->device_mutex);
363 }
364 dself->file = requested_file;
365
366 return rval;
367 }
368
369 static gboolean
370 diskflat_device_seek_block(
371 Device *dself,
372 guint64 block)
373 {
374 DiskflatDevice *self;
375 VfsDevice *vself;
376 off_t result;
377
378 self = DISKFLAT_DEVICE(dself);
379 vself = VFS_DEVICE(dself);
380
381 g_assert(vself->open_file_fd >= 0);
382 g_assert(sizeof(off_t) >= sizeof(guint64));
383 if (device_in_error(self)) return FALSE;
384
385 /* Pretty simple. We figure out the blocksize and use that. */
386 result = lseek(vself->open_file_fd,
387 (block) * dself->block_size + 2 * VFS_DEVICE_LABEL_SIZE,
388 SEEK_SET);
389
390 dself->block = block;
391
392 if (result == (off_t)(-1)) {
393 device_set_error(dself,
394 g_strdup_printf(_("Error seeking within file: %s"), strerror(errno)),
395 DEVICE_STATUS_DEVICE_ERROR);
396 return FALSE;
397 }
398
399 return TRUE;
400 }
401
402 static gboolean
403 diskflat_device_erase(
404 Device *dself)
405 {
406 DiskflatDevice *self = DISKFLAT_DEVICE(dself);
407 VfsDevice *vself = VFS_DEVICE(dself);
408
409 if (vself->open_file_fd >= 0) {
410 robust_close(vself->open_file_fd);
411 vself->open_file_fd = -1;
412 }
413
414 if (unlink(self->filename) == -1 && errno != ENOENT) {
415 device_set_error(dself,
416 g_strdup_printf(_("Can't unlink file %s: %s"), self->filename, strerror(errno)),
417 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
418 return FALSE;
419 }
420 vself->release_file(dself);
421
422 dumpfile_free(dself->volume_header);
423 dself->volume_header = NULL;
424 device_set_error(dself, g_strdup("Unlabeled volume"),
425 DEVICE_STATUS_VOLUME_UNLABELED);
426
427 return TRUE;
428 }
429
430 static gboolean
431 diskflat_device_finish(
432 Device *dself)
433 {
434 VfsDevice *vself = VFS_DEVICE(dself);
435 gboolean result;
436 DeviceClass *parent_class = DEVICE_CLASS(g_type_class_peek_parent(DISKFLAT_DEVICE_GET_CLASS(dself)));
437
438 g_debug("Finish DISKFLAT device");
439
440 /* Save access mode before parent class messes with it */
441 if (vself->open_file_fd != -1) {
442 robust_close(vself->open_file_fd);
443 vself->open_file_fd = -1;
444 }
445
446 result = parent_class->finish(dself);
447
448 if (!result || device_in_error(dself)) {
449 return FALSE;
450 }
451
452 return TRUE;
453 }
454
455 static void
456 diskflat_device_finalize(
457 GObject *gself)
458 {
459 DiskflatDevice *self = DISKFLAT_DEVICE(gself);
460
461 GObjectClass *parent_class = G_OBJECT_CLASS(g_type_class_peek_parent(DISKFLAT_DEVICE_GET_CLASS(gself)));
462
463 if (parent_class->finalize) {
464 parent_class->finalize(gself);
465 }
466 amfree(self->filename);
467 }
468
469 static void
470 diskflat_release_file(
471 Device *dselfi G_GNUC_UNUSED)
472 {
473 return;
474 }
475
476 void
477 diskflat_update_volume_size(
478 Device *dself)
479 {
480 VfsDevice *vself = VFS_DEVICE(dself);
481 DiskflatDevice *self = DISKFLAT_DEVICE(dself);
482 struct stat stat_buf;
483
484 /* stat the file */
485 if (stat(self->filename, &stat_buf) < 0) {
486 g_warning("Couldn't stat file %s: %s", self->filename, strerror(errno));
487 return;
488 }
489
490 vself->volume_bytes += stat_buf.st_size;
491
492 return;
493 }
494
495 static gboolean
496 diskflat_clear_and_prepare_label(
497 Device *dself,
498 char *label,
499 char *timestamp)
500 {
501 dumpfile_t *label_header;
502 VfsDevice *vself = VFS_DEVICE(dself);
503 DiskflatDevice *self = DISKFLAT_DEVICE(dself);
504
505 vself->open_file_fd = robust_open(self->filename,
506 O_CREAT | O_WRONLY,
507 VFS_DEVICE_CREAT_MODE);
508 if (vself->open_file_fd < 0) {
509 device_set_error(dself,
510 g_strdup_printf(_("Can't open file %s: %s"), self->filename, strerror(errno)),
511 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
512 return FALSE;
513 }
514
515 label_header = make_tapestart_header(dself, label, timestamp);
516 if (!vfs_write_amanda_header(vself, label_header)) {
517 /* vfs_write_amanda_header sets error status if necessary */
518 dumpfile_free(label_header);
519 return FALSE;
520 }
521 dumpfile_free(dself->volume_header);
522 if (ftruncate(vself->open_file_fd, VFS_DEVICE_LABEL_SIZE) == -1) {
523 device_set_error(dself,
524 g_strdup_printf("ftruncate of '%s' failed: %s", self->filename, strerror(errno)),
525 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
526 return FALSE;
527 }
528 dself->header_block_size = VFS_DEVICE_LABEL_SIZE;
529 dself->volume_header = label_header;
530 dself->file = 0;
531 vself->volume_bytes = VFS_DEVICE_LABEL_SIZE;
532 return TRUE;
533 }
534
535 static gboolean
536 diskflat_validate(
537 Device *dself G_GNUC_UNUSED)
538 {
539 return TRUE;
540 }