Imported Upstream version 3.3.2
[debian/amanda] / perl / Amanda / Header.swg
1 /*
2  * Copyright (c) 2009-2012 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16  *
17  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19  */
20
21 %module "Amanda::Header"
22 %include "amglue/amglue.swg"
23 %include "exception.i"
24
25 %include "Amanda/Header.pod"
26
27 %{
28 #include "fileheader.h"
29 %}
30
31 amglue_add_enum_tag_fns(filetype_t);
32 amglue_add_constant(F_UNKNOWN, filetype_t);
33 amglue_add_constant(F_WEIRD, filetype_t);
34 amglue_add_constant(F_TAPESTART, filetype_t);
35 amglue_add_constant(F_TAPEEND, filetype_t);
36 amglue_add_constant(F_DUMPFILE, filetype_t);
37 amglue_add_constant(F_CONT_DUMPFILE, filetype_t);
38 amglue_add_constant(F_SPLIT_DUMPFILE, filetype_t);
39 amglue_add_constant(F_EMPTY, filetype_t);
40 amglue_copy_to_tag(filetype_t, constants);
41
42 typedef char string_t[STRMAX];
43 %typemap(memberin) string_t {
44     strncpy($1, $input, STRMAX);
45     if ($1[STRMAX-1] != '\0')
46         SWIG_exception(SWIG_ValueError, "String too large for Amanda::Header");
47 }
48
49 %rename(Header) dumpfile_t;
50 typedef struct {
51     filetype_t type;
52     string_t datestamp;
53     int dumplevel;
54     int compressed;
55     int encrypted;
56     string_t comp_suffix;
57     string_t encrypt_suffix;
58     string_t name;      /* hostname or label */
59     string_t disk;
60     string_t program;
61     string_t application;
62     string_t srvcompprog;
63     string_t clntcompprog;
64     string_t srv_encrypt;
65     string_t clnt_encrypt;
66     string_t recover_cmd;
67     string_t uncompress_cmd;
68     string_t decrypt_cmd;
69     string_t srv_decrypt_opt;
70     string_t clnt_decrypt_opt;
71     string_t cont_filename;
72     char *dle_str;
73     int is_partial;
74     int partnum;
75     int totalparts; /* -1 == UNKNOWN */
76     size_t blocksize;
77     off_t  orig_size;
78
79     %extend {
80         /* constructor */
81         dumpfile_t(void) {
82             dumpfile_t *df = g_new(dumpfile_t, 1);
83             fh_init(df);
84             /* some default values */
85             df->totalparts = -1;
86             df->partnum = 1;
87             return df;
88         }
89
90         /* destructor */
91         ~dumpfile_t(void) {
92             dumpfile_free(self);
93         }
94
95         /* the usual "cheater's typemap" */
96         %typemap(out) SV * "$result = $1; argvi++;";
97         SV *to_string(size_t min_size, size_t max_size) {
98             size_t size = min_size;
99             char *result;
100
101             result = build_header(self, &size, max_size);
102             if (!result) {
103                 /* header didn't fit -> return undef; */
104                 return &PL_sv_undef;
105             } else {
106                 STRLEN strlen_size = (STRLEN)size;
107                 SV *sv;
108                 g_assert((size_t)strlen_size == size); /* check for casting overflow */
109                 sv = sv_2mortal(newSVpvn(result, (STRLEN)size));
110                 g_free(result);
111                 return sv;
112             }
113         }
114
115         void debug_dump(void) {
116             dump_dumpfile_t(self);
117         }
118
119         %newobject summary;
120         char *summary(void) {
121             return summarize_header(self);
122         }
123     }
124 } dumpfile_t;
125
126 %inline %{
127 static dumpfile_t *C_from_string(const char *string) {
128     dumpfile_t *result = g_new(dumpfile_t, 1);
129     parse_file_header(string, result, strlen(string));
130     return result;
131 }
132 %}
133
134 %perlcode %{
135
136 # SWIG produces a sub-package for the Header "class", in this case named
137 # Amanda::Header::Header.  For user convenience, we allow Amanda::Header->new(..) to
138 # do the same thing.  This is a wrapper function, and not just a typeglob assignment,
139 # because we want to get the right blessing.
140 sub new {
141     shift; # ignore class
142     Amanda::Header::Header->new(@_);
143 }
144
145 sub from_string {
146     shift; # ignore class
147     return C_from_string(@_);
148 }
149
150 sub get_dle {
151     my $self = shift;
152
153     if ($self->{'dle_str'}) {
154         return Amanda::Header::HeaderXML->new($self->{'dle_str'});
155     } else {
156         return undef;
157     }
158 }
159
160 package Amanda::Header::Header;
161
162 # point $hdr->matches_dumpspecs() to Amanda::Cmdline::header_matches_dumpspecs.  When
163 # Amanda is built with --without-server, Amanda::Cmdline is missing, so this will fail.
164 # Note that this assumes the user has already use'd Amanda::Cmdline.
165 sub matches_dumpspecs {
166     Amanda::Cmdline::header_matches_dumpspecs(@_);
167 }
168
169 package Amanda::Header;
170 %}
171
172 %{
173 #include "amxml.h"
174 %}
175
176 %rename(HeaderXML) dle_t;
177 typedef struct {
178     char   *disk;
179     char   *device;
180     int     program_is_application_api;
181     char   *program;
182     estimatelist_t estimatelist;
183     int     spindle;
184     int     compress;
185     int     encrypt;
186     int     kencrypt;
187     levellist_t levellist;
188     int     nb_level;
189     char   *dumpdate;
190     char   *compprog;
191     char   *srv_encrypt;
192     char   *clnt_encrypt;
193     char   *srv_decrypt_opt;
194     char   *clnt_decrypt_opt;
195     int     record;
196     int     create_index;
197     char   *auth;
198     am_sl_t   *exclude_file;
199     am_sl_t   *exclude_list;
200     am_sl_t   *include_file;
201     am_sl_t   *include_list;
202     int     exclude_optional;
203     int     include_optional;
204     proplist_t application_property;
205     scriptlist_t scriptlist;
206     data_path_t  data_path;
207     GSList      *directtcp_list;
208     struct a_dle_s *next;
209
210     %extend {
211         /* constructor */
212         dle_t(char *dle_str) {
213             char  *errmsg = NULL;
214             dle_t *dle;
215             dle = amxml_parse_node_CHAR(dle_str, &errmsg);
216             amfree(errmsg);
217             
218             return dle;
219         }
220
221         /* destructor */
222         ~dle_t(void) {
223             amfree(self);
224         }
225     }
226 } dle_t;
227