Imported Upstream version 3.1.0
[debian/amanda] / perl / Amanda / Logfile.swg
1 /*
2  * Copyright (c) 2007, 2008, 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::Logfile"
22 %include "amglue/amglue.swg"
23 %include "exception.i"
24 %include "amglue/dumpspecs.swg"
25 %import "Amanda/Cmdline.swg"
26
27 %include "Amanda/Logfile.pod"
28
29 %{
30 #include <glib.h>
31 #include "logfile.h"
32 #include "find.h"
33 #include "diskfile.h" /* for the gross hack, below */
34 %}
35
36 amglue_export_ok(
37     open_logfile get_logline close_logfile
38     log_add
39 );
40
41
42 amglue_add_enum_tag_fns(logtype_t);
43 amglue_add_constant(L_BOGUS, logtype_t);
44 amglue_add_constant(L_FATAL, logtype_t);
45 amglue_add_constant(L_ERROR, logtype_t);
46 amglue_add_constant(L_WARNING, logtype_t);
47 amglue_add_constant(L_INFO, logtype_t);
48 amglue_add_constant(L_SUMMARY, logtype_t);
49 amglue_add_constant(L_START, logtype_t);
50 amglue_add_constant(L_FINISH, logtype_t);
51 amglue_add_constant(L_DISK, logtype_t);
52 amglue_add_constant(L_DONE, logtype_t);
53 amglue_add_constant(L_PART, logtype_t);
54 amglue_add_constant(L_PARTPARTIAL, logtype_t);
55 amglue_add_constant(L_SUCCESS, logtype_t);
56 amglue_add_constant(L_PARTIAL, logtype_t);
57 amglue_add_constant(L_FAIL, logtype_t);
58 amglue_add_constant(L_STRANGE, logtype_t);
59 amglue_add_constant(L_CHUNK, logtype_t);
60 amglue_add_constant(L_CHUNKSUCCESS, logtype_t);
61 amglue_add_constant(L_STATS, logtype_t);
62 amglue_add_constant(L_MARKER, logtype_t);
63 amglue_add_constant(L_CONT, logtype_t);
64 amglue_copy_to_tag(logtype_t, constants);
65
66 amglue_add_enum_tag_fns(program_t);
67 amglue_add_constant(P_UNKNOWN, program_t);
68 amglue_add_constant(P_PLANNER, program_t);
69 amglue_add_constant(P_DRIVER, program_t);
70 amglue_add_constant(P_REPORTER, program_t);
71 amglue_add_constant(P_DUMPER, program_t);
72 amglue_add_constant(P_CHUNKER, program_t);
73 amglue_add_constant(P_TAPER, program_t);
74 amglue_add_constant(P_AMFLUSH, program_t);
75 amglue_add_constant(P_AMDUMP, program_t);
76 amglue_add_constant(P_AMIDXTAPED, program_t);
77 amglue_add_constant(P_AMFETCHDUMP, program_t);
78 amglue_add_constant(P_AMCHECKDUMP, program_t);
79 amglue_copy_to_tag(program_t, constants);
80
81 /* TODO: support for writing logfiles is omitted for the moment. */
82
83 %inline %{
84 /* open_ and close_logfile are both simple wrappers around fopen/fclose. */
85 typedef FILE loghandle;
86
87 static loghandle *open_logfile(char *filename) {
88     return fopen(filename, "r");
89 }
90 %}
91
92 %inline %{
93 static void close_logfile(loghandle *logfile) {
94     if (logfile) fclose(logfile);
95 }
96 %}
97
98 /* We fake the return type of get_logline, and use a typemap to
99  * slurp curstr, curprog, and curlog into a return value.  */
100 %{
101 typedef int LOGLINE_RETURN;
102 %}
103 %typemap(out) LOGLINE_RETURN {
104     if ($1 != 0) {
105         EXTEND(SP, 3);
106         $result = sv_2mortal(newSViv(curlog));
107         argvi++;
108         $result = sv_2mortal(newSViv(curprog));
109         argvi++;
110         $result = sv_2mortal(newSVpv(curstr, 0));
111         argvi++;
112     }
113     /* otherwise (end of logfile) return an empty list */
114 }
115 LOGLINE_RETURN get_logline(FILE *logfile);
116
117 %rename(log_add) log_add_;
118 %inline %{
119 static void log_add_(logtype_t typ, char *message)
120 {
121     log_add(typ, "%s", message);
122 }
123 %}
124
125 typedef struct {
126     %extend {
127         /* destructor */
128         ~find_result_t() {
129             find_result_t *selfp = self;
130             free_find_result(&selfp);
131         }
132     }
133
134     %immutable;
135     char *timestamp;
136     char *hostname;
137     char *diskname;
138     int level;
139     char *label;
140     off_t filenum;
141     char *status;
142     char *dump_status;
143     char *message;
144     int partnum;
145     int totalparts;
146     double sec;
147     off_t kb;
148     off_t orig_kb;
149     %mutable;
150 } find_result_t;
151
152 /* This typemap is used in a few functions.  It converts a linked list of find_result_t's
153  * into an array of same, de-linking the list in the process.  This gives ownership of the
154  * objects to perl, which is consistent with the C interface to this module.
155  */
156 %typemap(out) find_result_t * {
157     find_result_t *iter;
158     int len;
159
160     /* measure the list and make room on the perl stack */
161     for (len=0, iter=$1; iter; iter=iter->next) len++;
162     EXTEND(SP, len);
163
164     iter = $1;
165     while (iter) {
166         find_result_t *next;
167         /* Let SWIG take ownership of the object */
168         $result = SWIG_NewPointerObj(iter, $descriptor(find_result_t *), SWIG_OWNER | SWIG_SHADOW);
169         argvi++;
170
171         /* null out the 'next' field */
172         next = iter->next;
173         iter->next = NULL;
174         iter = next;
175     }
176 }
177
178 /* Similarly, on input we link an array full of find_result_t's.  The list is then
179  * unlinked on return.  Note that the array is supplied as an arrayref (since it's 
180  * usually the first argument).
181  */
182 %typemap(in) find_result_t * {
183     AV *av;
184     I32 len, i;
185     find_result_t *head = NULL, *tail = NULL;
186
187     if (!SvROK($input) || SvTYPE(SvRV($input)) != SVt_PVAV) {
188         SWIG_exception(SWIG_TypeError, "expected an arrayref of find_result_t's");
189     }
190
191     av = (AV *)SvRV($input);
192     len = av_len(av) + 1;
193
194     for (i = 0; i < len; i++) {
195         SV **val = av_fetch(av, i, 0);
196         find_result_t *r;
197
198         if (!val || SWIG_ConvertPtr(*val, (void **)&r, $descriptor(find_result_t *), 0) == -1) {
199             SWIG_exception(SWIG_TypeError, "array member is not a find_result_t");
200         }
201
202         if (!head) {
203             head = tail = r;
204         } else {
205             tail->next = r;
206             tail = r;
207         }
208
209         tail->next = NULL;
210     }
211
212     /* point to the head of that list */
213     $1 = head;
214 }
215
216 %typemap(freearg) find_result_t * {
217     find_result_t *iter = $1, *next;
218
219     /* undo all the links we added earlier */
220     while (iter) {
221         next = iter->next;
222         iter->next = NULL;
223         iter = next;
224     }
225 }
226
227 %typemap(out) char ** {
228     char **iter;
229     int len, i;
230     
231     /* measure the length of the array and make sure perl has enough room */
232     for (len=0, iter=$1; *iter; iter++) len++;
233     EXTEND(SP, len);
234
235     /* now copy it to the perl stack */
236     for (i=0, iter=$1; *iter; iter++, i++) {
237         $result = sv_2mortal(newSVpv(*iter, 0));
238         argvi++;
239     }
240 }
241
242 amglue_export_ok(
243     find_log search_logfile dumps_match
244     match_host match_disk match_datestamp match_level
245 );
246
247 char **find_log(void);
248
249 %rename(search_logfile) search_logfile_wrap;
250 %inline %{
251 static find_result_t *search_logfile_wrap(char *label, char *datestamp,
252                                    char *logfile, int add_missing_disks) {
253     find_result_t *rv = NULL;
254
255     /* We use a static variable to collect any unrecognized disks */
256     static disklist_t unrecognized_disks = { NULL, NULL };
257
258     search_logfile(&rv, label, datestamp, logfile, 
259         add_missing_disks? &unrecognized_disks : NULL);
260
261     return rv;
262 }
263 %}
264
265 %rename(search_holding_disk) search_holding_disk_wrap;
266 %inline %{
267 static find_result_t *search_holding_disk_wrap(void) {
268     find_result_t *rv = NULL;
269     static disklist_t unrecognized_disks = { NULL, NULL };
270     search_holding_disk(&rv, &unrecognized_disks);
271     return rv;
272 }
273 %}
274
275 find_result_t *dumps_match(find_result_t *output_find, char *hostname,
276                            char *diskname, char *datestamp, char *level, int ok);
277
278 find_result_t *dumps_match_dumpspecs(find_result_t *output_find,
279     amglue_dumpspec_list *dumpspecs,
280     gboolean ok);
281
282 /* these are actually available for clients as well, but they do not deserve
283  * their own perl module, so they're stuck here */
284 gboolean match_host(char *pat, char *value);
285 gboolean match_disk(char *pat, char *value);
286 gboolean match_datestamp(char *pat, char *value);
287 gboolean match_level(char *pat, char *value);
288
289 %immutable;
290 amanda_log_handler_t *amanda_log_trace_log;
291 %mutable;
292 amglue_export_ok(
293     $amanda_log_trace_log
294 );
295
296
297 amglue_export_ok(
298     find_all_logs find_latest_log
299 );
300
301 %perlcode %{
302
303 sub find_all_logs
304 {
305     my $logdir = shift @_ || config_dir_relative(getconf($CNF_LOGDIR));
306
307     opendir my $logdh, $logdir or die("can't read $logdir");
308     my @logfiles = sort grep { m{^log\.\d+\.\d+$} } readdir $logdh;
309
310     return @logfiles;
311 }
312
313 sub find_latest_log
314 {
315     my $logdir = shift @_;
316     my @logs = find_all_logs($logdir || ());
317     return $logs[-1];
318 }
319
320 %}