a7d391b630246cf6c0de37835f12729e2e3c1c57
[debian/amanda] / perl / Amanda / Header.swg
1 /*
2  * Copyright (c) 2009, 2010 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 %newobject C_from_string;
127 %inline %{
128 static dumpfile_t *C_from_string(const char *string) {
129     dumpfile_t *result = g_new(dumpfile_t, 1);
130     parse_file_header(string, result, strlen(string));
131     return result;
132 }
133 %}
134
135 %perlcode %{
136
137 # SWIG produces a sub-package for the Header "class", in this case named
138 # Amanda::Header::Header.  For user convenience, we allow Amanda::Header->new(..) to
139 # do the same thing.  This is a wrapper function, and not just a typeglob assignment,
140 # because we want to get the right blessing.
141 sub new {
142     shift; # ignore class
143     Amanda::Header::Header->new(@_);
144 }
145
146 sub from_string {
147     shift; # ignore class
148     return C_from_string(@_);
149 }
150
151 sub get_dle {
152     my $self = shift;
153
154     return Amanda::Header::HeaderXML->new($self->{'dle_str'});
155 }
156
157 package Amanda::Header::Header;
158
159 # point $hdr->matches_dumpspecs() to Amanda::Cmdline::header_matches_dumpspecs.  When
160 # Amanda is built with --without-server, Amanda::Cmdline is missing, so this will fail.
161 # Note that this assumes the user has already use'd Amanda::Cmdline.
162 sub matches_dumpspecs {
163     Amanda::Cmdline::header_matches_dumpspecs(@_);
164 }
165
166 package Amanda::Header;
167 %}
168
169 %{
170 #include "amxml.h"
171 %}
172
173 %rename(HeaderXML) dle_t;
174 typedef struct {
175     char   *disk;
176     char   *device;
177     int     program_is_application_api;
178     char   *program;
179     estimatelist_t estimatelist;
180     int     spindle;
181     int     compress;
182     int     encrypt;
183     int     kencrypt;
184     levellist_t levellist;
185     int     nb_level;
186     char   *dumpdate;
187     char   *compprog;
188     char   *srv_encrypt;
189     char   *clnt_encrypt;
190     char   *srv_decrypt_opt;
191     char   *clnt_decrypt_opt;
192     int     record;
193     int     create_index;
194     char   *auth;
195     sl_t   *exclude_file;
196     sl_t   *exclude_list;
197     sl_t   *include_file;
198     sl_t   *include_list;
199     int     exclude_optional;
200     int     include_optional;
201     proplist_t application_property;
202     scriptlist_t scriptlist;
203     data_path_t  data_path;
204     GSList      *directtcp_list;
205     struct a_dle_s *next;
206
207     %extend {
208         /* constructor */
209         dle_t(char *dle_str) {
210             char  *errmsg = NULL;
211             dle_t *dle;
212             dle = amxml_parse_node_CHAR(dle_str, &errmsg);
213             amfree(errmsg);
214             
215             return dle;
216         }
217
218         /* destructor */
219         ~dle_t(void) {
220             amfree(self);
221         }
222     }
223 } dle_t;
224