Imported Upstream version 3.2.0
[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 package Amanda::Header::Header;
152
153 # point $hdr->matches_dumpspecs() to Amanda::Cmdline::header_matches_dumpspecs.  When
154 # Amanda is built with --without-server, Amanda::Cmdline is missing, so this will fail.
155 # Note that this assumes the user has already use'd Amanda::Cmdline.
156 sub matches_dumpspecs {
157     Amanda::Cmdline::header_matches_dumpspecs(@_);
158 }
159
160 package Amanda::Header;
161 %}