Imported Upstream version 3.2.0
[debian/amanda] / xfer-src / dest-fd.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 2008, 2009, 2010 Zmanda, Inc.  All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published
7  * by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17  *
18  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20  */
21
22 #include "amanda.h"
23 #include "amxfer.h"
24
25 /*
26  * Class declaration
27  *
28  * This declaration is entirely private; nothing but xfer_dest_fd() references
29  * it directly.
30  */
31
32 GType xfer_dest_fd_get_type(void);
33 #define XFER_DEST_FD_TYPE (xfer_dest_fd_get_type())
34 #define XFER_DEST_FD(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_dest_fd_get_type(), XferDestFd)
35 #define XFER_DEST_FD_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_dest_fd_get_type(), XferDestFd const)
36 #define XFER_DEST_FD_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_dest_fd_get_type(), XferDestFdClass)
37 #define IS_XFER_DEST_FD(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_dest_fd_get_type ())
38 #define XFER_DEST_FD_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_dest_fd_get_type(), XferDestFdClass)
39
40 static GObjectClass *parent_class = NULL;
41
42 /*
43  * Main object structure
44  */
45
46 typedef struct XferDestFd {
47     XferElement __parent__;
48 } XferDestFd;
49
50 /*
51  * Class definition
52  */
53
54 typedef struct {
55     XferElementClass __parent__;
56 } XferDestFdClass;
57
58 /*
59  * Implementation
60  */
61
62 static void
63 class_init(
64     XferDestFdClass * selfc)
65 {
66     XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
67     static xfer_element_mech_pair_t mech_pairs[] = {
68         { XFER_MECH_WRITEFD, XFER_MECH_NONE, 0, 0},
69         { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
70     };
71
72     klass->perl_class = "Amanda::Xfer::Dest::Fd";
73     klass->mech_pairs = mech_pairs;
74
75     parent_class = g_type_class_peek_parent(selfc);
76 }
77
78 GType
79 xfer_dest_fd_get_type (void)
80 {
81     static GType type = 0;
82
83     if G_UNLIKELY(type == 0) {
84         static const GTypeInfo info = {
85             sizeof (XferDestFdClass),
86             (GBaseInitFunc) NULL,
87             (GBaseFinalizeFunc) NULL,
88             (GClassInitFunc) class_init,
89             (GClassFinalizeFunc) NULL,
90             NULL /* class_data */,
91             sizeof (XferDestFd),
92             0 /* n_preallocs */,
93             (GInstanceInitFunc) NULL,
94             NULL
95         };
96
97         type = g_type_register_static (XFER_ELEMENT_TYPE, "XferDestFd", &info, 0);
98     }
99
100     return type;
101 }
102
103 /* create an element of this class; prototype is in xfer-element.h */
104 XferElement *
105 xfer_dest_fd(
106     int fd)
107 {
108     XferDestFd *self = (XferDestFd *)g_object_new(XFER_DEST_FD_TYPE, NULL);
109     XferElement *elt = XFER_ELEMENT(self);
110
111     g_assert(fd >= 0);
112
113     /* We keep a *copy* of this fd, because our caller will close it to
114      * indicate EOF */
115     g_assert(xfer_element_swap_input_fd(elt, dup(fd)) == -1);
116
117     return elt;
118 }