797e0d42ca226f641b52f5b6f955c42961dcad03
[debian/amanda] / xfer-src / dest-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_dest_fd() references
27  * it directly.
28  */
29
30 GType xfer_dest_fd_get_type(void);
31 #define XFER_DEST_FD_TYPE (xfer_dest_fd_get_type())
32 #define XFER_DEST_FD(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_dest_fd_get_type(), XferDestFd)
33 #define XFER_DEST_FD_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_dest_fd_get_type(), XferDestFd const)
34 #define XFER_DEST_FD_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_dest_fd_get_type(), XferDestFdClass)
35 #define IS_XFER_DEST_FD(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_dest_fd_get_type ())
36 #define XFER_DEST_FD_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_dest_fd_get_type(), XferDestFdClass)
37
38 static GObjectClass *parent_class = NULL;
39
40 /*
41  * Main object structure
42  */
43
44 typedef struct XferDestFd {
45     XferElement __parent__;
46 } XferDestFd;
47
48 /*
49  * Class definition
50  */
51
52 typedef struct {
53     XferElementClass __parent__;
54 } XferDestFdClass;
55
56 /*
57  * Implementation
58  */
59
60 static void
61 class_init(
62     XferDestFdClass * selfc)
63 {
64     XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
65     static xfer_element_mech_pair_t mech_pairs[] = {
66         { XFER_MECH_WRITEFD, XFER_MECH_NONE, 0, 0},
67         { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
68     };
69
70     klass->perl_class = "Amanda::Xfer::Dest::Fd";
71     klass->mech_pairs = mech_pairs;
72
73     parent_class = g_type_class_peek_parent(selfc);
74 }
75
76 GType
77 xfer_dest_fd_get_type (void)
78 {
79     static GType type = 0;
80
81     if G_UNLIKELY(type == 0) {
82         static const GTypeInfo info = {
83             sizeof (XferDestFdClass),
84             (GBaseInitFunc) NULL,
85             (GBaseFinalizeFunc) NULL,
86             (GClassInitFunc) class_init,
87             (GClassFinalizeFunc) NULL,
88             NULL /* class_data */,
89             sizeof (XferDestFd),
90             0 /* n_preallocs */,
91             (GInstanceInitFunc) NULL,
92             NULL
93         };
94
95         type = g_type_register_static (XFER_ELEMENT_TYPE, "XferDestFd", &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_dest_fd(
104     int fd)
105 {
106     XferDestFd *self = (XferDestFd *)g_object_new(XFER_DEST_FD_TYPE, NULL);
107     XferElement *elt = XFER_ELEMENT(self);
108
109     g_assert(fd >= 0);
110
111     /* We keep a *copy* of this fd, because our caller will close it to indicate
112      * EOF */
113     elt->input_fd = dup(fd);
114
115     return elt;
116 }