File: diskflat-device.c

package info (click to toggle)
amanda 1%3A3.5.4-2.1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 23,420 kB
  • sloc: ansic: 197,218; perl: 109,331; xml: 16,126; sh: 4,180; makefile: 2,811; awk: 502; lex: 407; yacc: 347; tcl: 118; sql: 19; sed: 16; php: 2
file content (540 lines) | stat: -rw-r--r-- 15,319 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
 * Copyright (c) 2009 University of Maryland at College Park
 * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
 * Copyright (c) 2013-2016 Carbonite, Inc.  All Rights Reserved.
 * All Rights Reserved.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of U.M. not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  U.M. makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: Sam Couter <sam@couter.id.au>
 */

#include "amanda.h"
#include "vfs-device.h"

/*
 * Type checking and casting macros
 */
#define TYPE_DISKFLAT_DEVICE	(diskflat_device_get_type())
#define DISKFLAT_DEVICE(obj)	G_TYPE_CHECK_INSTANCE_CAST((obj), TYPE_DISKFLAT_DEVICE, DiskflatDevice)
#define DISKFLAT_DEVICE_CONST(obj)	G_TYPE_CHECK_INSTANCE_CAST((obj), TYPE_DISKFLAT_DEVICE, DiskflatDevice const)
#define DISKFLAT_DEVICE_CLASS(klass)	G_TYPE_CHECK_CLASS_CAST((klass), TYPE_DISKFLAT_DEVICE, DiskflatDeviceClass)
#define IS_DISKFLAT_DEVICE(obj)	G_TYPE_CHECK_INSTANCE_TYPE((obj), TYPE_DISKFLAT_DEVICE)
#define DISKFLAT_DEVICE_GET_CLASS(obj)	G_TYPE_INSTANCE_GET_CLASS((obj), TYPE_DISKFLAT_DEVICE, DiskflatDeviceClass)

/* Forward declaration */
static GType diskflat_device_get_type(void);

/*
 * Main object structure
 */
typedef struct _DiskflatDevice DiskflatDevice;
struct _DiskflatDevice {
    VfsDevice __parent__;

    char *filename;
};

/*
 * Class definition
 */
typedef struct _DiskflatDeviceClass DiskflatDeviceClass;
struct _DiskflatDeviceClass {
    VfsDeviceClass __parent__;
};


/* GObject housekeeping */
void
diskflat_device_register(void);

static Device*
diskflat_device_factory(char *device_name, char *device_type, char *device_node);

static void
diskflat_device_class_init (DiskflatDeviceClass *c);

static void
diskflat_device_init (DiskflatDevice *self);

/* Methods */
static void
diskflat_device_open_device(Device *dself, char *device_name, char *device_type, char *device_node);

static dumpfile_t *
diskflat_device_seek_file (Device * dself, guint requested_file);

static gboolean
diskflat_device_seek_block (Device * dself, guint64 block);

static gboolean
diskflat_device_erase (Device * dself);

static gboolean
diskflat_device_finish(Device *dself);

static void
diskflat_device_finalize(GObject *gself);

static gboolean
diskflat_device_start_file_open(Device *dself, dumpfile_t *ji);

static void
diskflat_update_volume_size(Device *dself);

static void
diskflat_release_file(Device *dself);

static gboolean
diskflat_clear_and_prepare_label(Device *dself, char *label, char *timestamp);

static gboolean
diskflat_validate(Device *dself);

static GType
diskflat_device_get_type (void)
{
    static GType type = 0;

    if (G_UNLIKELY(type == 0)) {
        static const GTypeInfo info = {
            sizeof (DiskflatDeviceClass),
            (GBaseInitFunc) NULL,
            (GBaseFinalizeFunc) NULL,
            (GClassInitFunc) diskflat_device_class_init,
            (GClassFinalizeFunc) NULL,
            NULL /* class_data */,
            sizeof (DiskflatDevice),
            0 /* n_preallocs */,
            (GInstanceInitFunc) diskflat_device_init,
            NULL
        };

        type = g_type_register_static (TYPE_VFS_DEVICE, "DiskflatDevice",
                                       &info, (GTypeFlags)0);
    }

    return type;
}

void
diskflat_device_register(void)
{
    const char *device_prefix_list[] = { "diskflat", NULL };

    register_device(diskflat_device_factory, device_prefix_list);
}

static Device *
diskflat_device_factory(
    char *device_name,
    char *device_type,
    char *device_node)
{
    Device *device;

    g_assert(g_str_has_prefix(device_type, "diskflat"));

    device = DEVICE(g_object_new(TYPE_DISKFLAT_DEVICE, NULL));
    device_open_device(device, device_name, device_type, device_node);

    return device;
}

static void
diskflat_device_class_init (
    DiskflatDeviceClass *c)
{
    DeviceClass *device_class = DEVICE_CLASS(c);
    GObjectClass *g_object_class = G_OBJECT_CLASS(c);

    device_class->open_device = diskflat_device_open_device;
    device_class->seek_file = diskflat_device_seek_file;
    device_class->seek_block = diskflat_device_seek_block;
    device_class->erase = diskflat_device_erase;
    device_class->finish = diskflat_device_finish;

    g_object_class->finalize = diskflat_device_finalize;
}

static void
diskflat_device_init (
    DiskflatDevice *self)
{
    Device *dself = DEVICE(self);
    VfsDevice *vself = VFS_DEVICE(self);
    GValue val;

    vself->device_start_file_open = &diskflat_device_start_file_open;
    vself->update_volume_size = &diskflat_update_volume_size;
    vself->release_file = &diskflat_release_file;
    vself->clear_and_prepare_label = &diskflat_clear_and_prepare_label;
    vself->validate = &diskflat_validate;

    bzero(&val, sizeof(val));

    g_value_init(&val, G_TYPE_BOOLEAN);
    g_value_set_boolean(&val, FALSE);
    device_set_simple_property(dself, PROPERTY_APPENDABLE,
	&val, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
    g_value_unset(&val);

    g_value_init(&val, G_TYPE_BOOLEAN);
    g_value_set_boolean(&val, FALSE);
    device_set_simple_property(dself, PROPERTY_PARTIAL_DELETION,
	&val, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
    g_value_unset(&val);

    g_value_init(&val, G_TYPE_BOOLEAN);
    g_value_set_boolean(&val, TRUE);
    device_set_simple_property(dself, PROPERTY_FULL_DELETION,
	&val, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
    g_value_unset(&val);

    g_value_init(&val, G_TYPE_BOOLEAN);
    g_value_set_boolean(&val, TRUE);
    device_set_simple_property(dself, PROPERTY_LEOM,
	&val, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
    g_value_unset(&val);

}


static void
diskflat_device_open_device(
    Device *dself,
    char *device_name,
    char *device_type,
    char *device_node)
{
    DiskflatDevice *self = DISKFLAT_DEVICE(dself);
    VfsDevice *vself = VFS_DEVICE(dself);
    char *d;
    DeviceClass *parent_class = DEVICE_CLASS(g_type_class_peek_parent(DISKFLAT_DEVICE_GET_CLASS(dself)));

    self->filename = g_strdup(device_node);
    g_debug("device_node: %s", self->filename);

    parent_class->open_device(dself, device_name, device_type, device_node);

    /* retrieve the directory */
    if ((d = rindex(vself->dir_name, '/')) != NULL) {
	*d = '\0';
	if ((d = rindex(vself->dir_name, '/')) != NULL) {
	    *d = '\0';
	}
    }
}

static gboolean
diskflat_device_start_file_open(
    Device     *dself,
    dumpfile_t *ji G_GNUC_UNUSED)
{
    if (dself->file >= 1) {
        device_set_error(dself,
            g_strdup_printf(_("Can't write more than one file to the diskflat device")),
            DEVICE_STATUS_VOLUME_ERROR);
	return FALSE;
    }

    dself->file++;
    return TRUE;
}

static dumpfile_t *
diskflat_device_seek_file(
    Device *dself,
    guint requested_file)
{
    VfsDevice     *vself = VFS_DEVICE(dself);
    DiskflatDevice *self  = DISKFLAT_DEVICE(dself);
    dumpfile_t *rval;
    char header_buffer[VFS_DEVICE_LABEL_SIZE];
    int header_buffer_size = sizeof(header_buffer);
    IoResult result;
    off_t result_seek;

    if (device_in_error(dself)) return NULL;
    if (requested_file > 1) {
        device_set_error(dself,
            g_strdup_printf(_("Can't seek to file number above 1")),
            DEVICE_STATUS_VOLUME_ERROR);
	return NULL;
    }

    /* read_label before start */
    if (requested_file == 0 && vself->open_file_fd == -1) {
	vself->open_file_fd = robust_open(self->filename,
                                          O_RDONLY, 0);
	if (vself->open_file_fd < 0) {
	    if (errno == ENOENT) {
		device_set_error(dself,
		    g_strdup_printf(_("Couldn't open file %s: %s (unlabeled)"), self->filename, strerror(errno)),
			DEVICE_STATUS_VOLUME_UNLABELED);
		rval = g_new(dumpfile_t, 1);
		fh_init(rval);
		return rval;
	    } else {
		device_set_error(dself,
		    g_strdup_printf(_("Couldn't open file %s: %s"), self->filename, strerror(errno)),
			DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
		return NULL;
	    }
	}
    }

    dself->is_eof = FALSE;
    dself->block = 0;
    g_mutex_lock(dself->device_mutex);
    dself->in_file = FALSE;
    dself->bytes_read = 0;
    g_mutex_unlock(dself->device_mutex);

    result_seek = lseek(vself->open_file_fd, requested_file * DISK_BLOCK_BYTES,
			SEEK_SET);
    if (result_seek == -1) {
        device_set_error(dself,
            g_strdup_printf(_("Error seeking within file: %s"), strerror(errno)),
            DEVICE_STATUS_DEVICE_ERROR);
        return NULL;
    }

    result = vfs_device_robust_read(vself, header_buffer,
                                    &header_buffer_size);
    if (result == RESULT_NO_DATA) {
        device_set_error(dself,
            g_strdup_printf(_("Problem reading Amanda header: empty file")),
            DEVICE_STATUS_VOLUME_UNLABELED);
        return NULL;
    } else if (result != RESULT_SUCCESS) {
        device_set_error(dself,
            g_strdup_printf(_("Problem reading Amanda header: %s"), device_error(dself)),
            DEVICE_STATUS_VOLUME_ERROR);
        return NULL;
    }

    rval = g_new(dumpfile_t, 1);
    parse_file_header(header_buffer, rval, header_buffer_size);
    switch (rval->type) {
	case F_DUMPFILE:
	case F_CONT_DUMPFILE:
	case F_SPLIT_DUMPFILE:
	    break;

	case F_TAPESTART:
	    /* file 0 should have a TAPESTART header; diskflat_device_read_label
	        * uses this */
	    if (requested_file == 0)
		break;
	    /* FALLTHROUGH */

	default:
	    device_set_error(dself,
		g_strdup(_("Invalid amanda header while reading file header")),
		DEVICE_STATUS_VOLUME_ERROR);
	    amfree(rval);
	    return NULL;
    }

    /* update our state */
    if (requested_file == 0) {
	dself->header_block_size = header_buffer_size;
    } else {
	g_mutex_lock(dself->device_mutex);
	dself->in_file = TRUE;
	g_mutex_unlock(dself->device_mutex);
    }
    dself->file = requested_file;

    return rval;
}

static gboolean
diskflat_device_seek_block(
    Device *dself,
    guint64 block)
{
    DiskflatDevice *self;
    VfsDevice *vself;
    off_t result;

    self = DISKFLAT_DEVICE(dself);
    vself = VFS_DEVICE(dself);

    g_assert(vself->open_file_fd >= 0);
    g_assert(sizeof(off_t) >= sizeof(guint64));
    if (device_in_error(self)) return FALSE;

    /* Pretty simple. We figure out the blocksize and use that. */
    result = lseek(vself->open_file_fd,
                   (block) * dself->block_size + 2 * VFS_DEVICE_LABEL_SIZE,
                   SEEK_SET);

    dself->block = block;

    if (result == (off_t)(-1)) {
        device_set_error(dself,
            g_strdup_printf(_("Error seeking within file: %s"), strerror(errno)),
            DEVICE_STATUS_DEVICE_ERROR);
        return FALSE;
    }

    return TRUE;
}

static gboolean
diskflat_device_erase(
    Device *dself)
{
    DiskflatDevice *self = DISKFLAT_DEVICE(dself);
    VfsDevice *vself = VFS_DEVICE(dself);

    if (vself->open_file_fd >= 0) {
	robust_close(vself->open_file_fd);
	vself->open_file_fd = -1;
    }

    if (unlink(self->filename) == -1 && errno != ENOENT)  {
	device_set_error(dself,
	    g_strdup_printf(_("Can't unlink file %s: %s"), self->filename, strerror(errno)),
			    DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
	return FALSE;
    }
    vself->release_file(dself);

    dumpfile_free(dself->volume_header);
    dself->volume_header = NULL;
    device_set_error(dself, g_strdup("Unlabeled volume"),
                     DEVICE_STATUS_VOLUME_UNLABELED);

    return TRUE;
}

static gboolean
diskflat_device_finish(
    Device *dself)
{
    VfsDevice *vself = VFS_DEVICE(dself);
    gboolean result;
    DeviceClass *parent_class = DEVICE_CLASS(g_type_class_peek_parent(DISKFLAT_DEVICE_GET_CLASS(dself)));

    g_debug("Finish DISKFLAT device");

    /* Save access mode before parent class messes with it */
    if (vself->open_file_fd != -1) {
	robust_close(vself->open_file_fd);
	vself->open_file_fd = -1;
    }

    result = parent_class->finish(dself);

    if (!result || device_in_error(dself)) {
	return FALSE;
    }

    return TRUE;
}

static void
diskflat_device_finalize(
    GObject *gself)
{
    DiskflatDevice *self  = DISKFLAT_DEVICE(gself);

    GObjectClass *parent_class = G_OBJECT_CLASS(g_type_class_peek_parent(DISKFLAT_DEVICE_GET_CLASS(gself)));

    if (parent_class->finalize) {
	parent_class->finalize(gself);
    }
    amfree(self->filename);
}

static void 
diskflat_release_file(
    Device *dselfi G_GNUC_UNUSED)
{
    return;
}

void
diskflat_update_volume_size(
    Device *dself)
{
    VfsDevice     *vself = VFS_DEVICE(dself);
    DiskflatDevice *self  = DISKFLAT_DEVICE(dself);
    struct stat stat_buf;

    /* stat the file */
    if (stat(self->filename, &stat_buf) < 0) {
	g_warning("Couldn't stat file %s: %s", self->filename, strerror(errno));
	return;
    }

    vself->volume_bytes += stat_buf.st_size;

    return;
}

static gboolean
diskflat_clear_and_prepare_label(
    Device *dself,
    char *label,
    char *timestamp)
{
    dumpfile_t *label_header;
    VfsDevice *vself = VFS_DEVICE(dself);
    DiskflatDevice *self = DISKFLAT_DEVICE(dself);

    vself->open_file_fd = robust_open(self->filename,
                                     O_CREAT | O_WRONLY,
                                     VFS_DEVICE_CREAT_MODE);
    if (vself->open_file_fd < 0) {
	device_set_error(dself,
	    g_strdup_printf(_("Can't open file %s: %s"), self->filename, strerror(errno)),
	    DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
	return FALSE;
    }

    label_header = make_tapestart_header(dself, label, timestamp);
    if (!vfs_write_amanda_header(vself, label_header)) {
	/* vfs_write_amanda_header sets error status if necessary */
	dumpfile_free(label_header);
	return FALSE;
    }
    dumpfile_free(dself->volume_header);
    if (ftruncate(vself->open_file_fd, VFS_DEVICE_LABEL_SIZE) == -1) {
	device_set_error(dself,
	    g_strdup_printf("ftruncate of '%s' failed: %s", self->filename, strerror(errno)),
	    DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
	return FALSE;
    }
    dself->header_block_size = VFS_DEVICE_LABEL_SIZE;
    dself->volume_header = label_header;
    dself->file = 0;
    vself->volume_bytes = VFS_DEVICE_LABEL_SIZE;
    return TRUE;
}

static gboolean
diskflat_validate(
    Device *dself G_GNUC_UNUSED)
{
    return TRUE;
}