b1f95626c4518df95704d411b3db167949a6277a
[debian/amanda] / xfer-src / source-fd.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 2008 Zmanda Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #include "amxfer.h"
21 #include "amanda.h"
22
23 /*
24  * Class declaration
25  *
26  * This declaration is entirely private; nothing but xfer_source_fd() references
27  * it directly.
28  */
29
30 GType xfer_source_fd_get_type(void);
31 #define XFER_SOURCE_FD_TYPE (xfer_source_fd_get_type())
32 #define XFER_SOURCE_FD(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_fd_get_type(), XferSourceFd)
33 #define XFER_SOURCE_FD_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_fd_get_type(), XferSourceFd const)
34 #define XFER_SOURCE_FD_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_source_fd_get_type(), XferSourceFdClass)
35 #define IS_XFER_SOURCE_FD(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_source_fd_get_type ())
36 #define XFER_SOURCE_FD_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_source_fd_get_type(), XferSourceFdClass)
37
38 static GObjectClass *parent_class = NULL;
39
40 /*
41  * Main object structure
42  */
43
44 typedef struct XferSourceFd {
45     XferElement __parent__;
46 } XferSourceFd;
47
48 /*
49  * Class definition
50  */
51
52 typedef struct {
53     XferElementClass __parent__;
54 } XferSourceFdClass;
55
56 /*
57  * Implementation
58  */
59
60 static void
61 class_init(
62     XferSourceFdClass * selfc)
63 {
64     XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
65     static xfer_element_mech_pair_t mech_pairs[] = {
66         { XFER_MECH_NONE, XFER_MECH_READFD, 0, 0},
67         { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
68     };
69
70     klass->perl_class = "Amanda::Xfer::Source::Fd";
71     klass->mech_pairs = mech_pairs;
72
73     parent_class = g_type_class_peek_parent(selfc);
74 }
75
76 GType
77 xfer_source_fd_get_type (void)
78 {
79     static GType type = 0;
80
81     if G_UNLIKELY(type == 0) {
82         static const GTypeInfo info = {
83             sizeof (XferSourceFdClass),
84             (GBaseInitFunc) NULL,
85             (GBaseFinalizeFunc) NULL,
86             (GClassInitFunc) class_init,
87             (GClassFinalizeFunc) NULL,
88             NULL /* class_data */,
89             sizeof (XferSourceFd),
90             0 /* n_preallocs */,
91             (GInstanceInitFunc) NULL,
92             NULL
93         };
94
95         type = g_type_register_static (XFER_ELEMENT_TYPE, "XferSourceFd", &info, 0);
96     }
97
98     return type;
99 }
100
101 /* create an element of this class; prototype is in xfer-element.h */
102 XferElement *
103 xfer_source_fd(
104     int fd)
105 {
106     XferSourceFd *self = (XferSourceFd *)g_object_new(XFER_SOURCE_FD_TYPE, NULL);
107     XferElement *elt = XFER_ELEMENT(self);
108
109     g_assert(fd >= 0);
110
111     /* we read from a *copy* of this file descriptor, as the downstream element
112      * will close output_fd on EOF */
113     elt->output_fd = dup(fd);
114
115     return elt;
116 }