Imported Upstream version 3.3.3
[debian/amanda] / xfer-src / source-fd.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 2008-2012 Zmanda, 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: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
20  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
21  */
22
23 #include "amanda.h"
24 #include "amxfer.h"
25
26 /*
27  * Class declaration
28  *
29  * This declaration is entirely private; nothing but xfer_source_fd() references
30  * it directly.
31  */
32
33 GType xfer_source_fd_get_type(void);
34 #define XFER_SOURCE_FD_TYPE (xfer_source_fd_get_type())
35 #define XFER_SOURCE_FD(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_fd_get_type(), XferSourceFd)
36 #define XFER_SOURCE_FD_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_fd_get_type(), XferSourceFd const)
37 #define XFER_SOURCE_FD_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_source_fd_get_type(), XferSourceFdClass)
38 #define IS_XFER_SOURCE_FD(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_source_fd_get_type ())
39 #define XFER_SOURCE_FD_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_source_fd_get_type(), XferSourceFdClass)
40
41 static GObjectClass *parent_class = NULL;
42
43 /*
44  * Main object structure
45  */
46
47 typedef struct XferSourceFd {
48     XferElement __parent__;
49 } XferSourceFd;
50
51 /*
52  * Class definition
53  */
54
55 typedef struct {
56     XferElementClass __parent__;
57 } XferSourceFdClass;
58
59 /*
60  * Implementation
61  */
62
63 static void
64 class_init(
65     XferSourceFdClass * selfc)
66 {
67     XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
68     static xfer_element_mech_pair_t mech_pairs[] = {
69         { XFER_MECH_NONE, XFER_MECH_READFD, XFER_NROPS(0), XFER_NTHREADS(0) },
70         { XFER_MECH_NONE, XFER_MECH_NONE, XFER_NROPS(0), XFER_NTHREADS(0) },
71     };
72
73     klass->perl_class = "Amanda::Xfer::Source::Fd";
74     klass->mech_pairs = mech_pairs;
75
76     parent_class = g_type_class_peek_parent(selfc);
77 }
78
79 GType
80 xfer_source_fd_get_type (void)
81 {
82     static GType type = 0;
83
84     if G_UNLIKELY(type == 0) {
85         static const GTypeInfo info = {
86             sizeof (XferSourceFdClass),
87             (GBaseInitFunc) NULL,
88             (GBaseFinalizeFunc) NULL,
89             (GClassInitFunc) class_init,
90             (GClassFinalizeFunc) NULL,
91             NULL /* class_data */,
92             sizeof (XferSourceFd),
93             0 /* n_preallocs */,
94             (GInstanceInitFunc) NULL,
95             NULL
96         };
97
98         type = g_type_register_static (XFER_ELEMENT_TYPE, "XferSourceFd", &info, 0);
99     }
100
101     return type;
102 }
103
104 /* create an element of this class; prototype is in xfer-element.h */
105 XferElement *
106 xfer_source_fd(
107     int fd)
108 {
109     XferSourceFd *self = (XferSourceFd *)g_object_new(XFER_SOURCE_FD_TYPE, NULL);
110     XferElement *elt = XFER_ELEMENT(self);
111
112     g_assert(fd >= 0);
113
114     /* we read from a *copy* of this file descriptor, as the downstream element
115      * will close output_fd on EOF */
116     g_assert(xfer_element_swap_output_fd(elt, dup(fd)) == -1);
117
118     return elt;
119 }