5 Bweb - A Bacula web interface
6 Bacula® - The Network Backup Solution
8 Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
10 The main author of Bweb is Eric Bollengier.
11 The main author of Bacula is Kern Sibbald, with contributions from
12 many others, a complete list can be found in the file AUTHORS.
13 This program is Free Software; you can redistribute it and/or
14 modify it under the terms of version three of the GNU Affero General Public
15 License as published by the Free Software Foundation and included
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Affero General Public License for more details.
23 You should have received a copy of the GNU Affero General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 Bacula® is a registered trademark of Kern Sibbald.
29 The licensor of Bacula is the Free Software Foundation Europe
30 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
31 Switzerland, email:ftf@fsfeurope.org.
36 use POSIX qw/strftime/;
39 use Digest::MD5 qw(md5_hex);
40 use File::Basename qw/basename dirname/;
42 my $conf = new Bweb::Config(config_file => $Bweb::config_file);
44 my $bweb = new Bweb(info => $conf);
47 my $arg = $bweb->get_form('where', 'jobid', 'pathid', 'filenameid');
48 my $where = $arg->{where} || '/';
49 my $jobid = $arg->{jobid};
50 my $pathid = $arg->{pathid};
51 my $fnid = $arg->{filenameid};
52 my $jobid_url = "jobid=$jobid";
55 my $batch = CGI::param("mode") || '';
57 my $md5_rep = md5_hex("$where:$jobid:$pathid:$fnid") ;
58 my $base_url = '/bweb/fv' ;
59 my $base_fich = $conf->{fv_write_path};
61 if ($jobid and $batch eq 'batch') {
62 my $root = fv_get_root_pathid($where);
64 fv_compute_size($jobid, $root);
70 $bweb->display_begin();
71 $bweb->display_job_zoom($jobid);
74 $bweb->error("Can't get where or jobid");
79 unless ($base_fich and -w $base_fich) {
80 $bweb->error("fv_write_path ($base_fich) is not writable." .
81 " See Bweb configuration.");
86 if (-f "$base_fich/$md5_rep.png" and -f "$base_fich/$md5_rep.tpl")
88 $bweb->display({}, "$base_fich/$md5_rep.tpl");
93 my $r = $bweb->dbh_selectrow_hashref("SELECT PurgedFiles AS ok FROM Job WHERE JobId = $jobid");
94 if (!$r || $r->{ok}) {
95 $bweb->error("File information for job $jobid has been pruned from catalog");
100 $r = $bweb->dbh_selectrow_hashref("SELECT JobId AS ok FROM brestore_knownjobid WHERE JobId = $jobid");
101 if (!$r || !$r->{ok}) { # TODO: compute information
102 $bweb->error("Path information for job $jobid has not been updated in the catalog");
103 $bweb->display_end();
107 # if it's a file, display it
108 if ($fnid and $pathid)
110 my $attribs = fv_get_file_attribute_from_id($jobid, $pathid, $fnid);
111 if ($attribs->{found}) {
112 $bweb->display($attribs, 'fv_file_attribs.tpl');
113 $bweb->display_end();
117 } elsif ($where ne '/') {
119 my $attribs = fv_get_file_attribute($jobid, $where);
120 if ($attribs->{found}) {
121 $bweb->display($attribs, 'fv_file_attribs.tpl');
122 $bweb->display_end();
131 $where = fv_get_root_path($pathid);
134 if ($where !~ m!/$!) {
135 $where = $where . "/" ;
138 $root = fv_get_root_pathid($where);
142 $bweb->error("Can't find $where in catalog");
143 $bweb->display_end();
147 my $total = fv_compute_size($jobid, $root);
149 my $url_action = "bfileview.pl?opt_level=$opt_level" ;
150 my $top = new CCircle(
152 base_url => "$url_action;pathid=$root;$jobid_url;here=$where",
155 fv_display_rep($top, $total, $root, $opt_level) ;
157 $top->draw_labels() ;
158 $top->set_title(Bweb::human_size($total)) ;
160 open(OUT, ">$base_fich/$md5_rep.png") or die "$base_fich/$md5_rep.png $!";
161 # make sure we are writing to a binary stream
163 # Convert the image to PNG and print it on standard output
164 print OUT $CCircle::gd->png;
167 open(OUT, ">$base_fich/$md5_rep.tpl") or die "$base_fich/$md5_rep.tpl $!";
169 <form action='$url_action' method='get'>
171 <input title='jobids' type='hidden' name='jobid' value='$jobid'>
172 <input title='directory' type='text' name='where' value='$where'/>
173 <input type='submit' size='256' name='go' value='go'/>
179 print OUT $top->get_imagemap($where, "$base_url/$md5_rep.png") ;
182 $bweb->display({}, "$base_fich/$md5_rep.tpl");
183 $bweb->display_end();
187 my ($ccircle, $max, $rep, $level) = @_ ;
188 return if ($max < 1);
191 my $dirs = fv_list_dirs($jobid, $rep); # 0: pathid, 1: pathname
193 foreach my $dir (@{$dirs})
195 my $size = fv_compute_size($jobid, $dir->[0]);
198 my $per = $size * 100 / $max;
199 # can't use pathname when using utf or accent
201 $ccircle->{base_url} =~ s/pathid=\d+;/pathid=$dir->[0];/;
203 my $chld = $ccircle->add_part($per,
204 basename($dir->[1]) . '/',
206 . sprintf(' %.0f%% ', $per)
207 . Bweb::human_size($size)
210 if ($chld && $level > 0) {
211 fv_display_rep($chld, $size, $dir->[0], $level - 1) ;
215 # 0: filenameid, 1: filename, 2: size
216 my $files = fv_get_big_files($jobid, $rep, 3*100/$max, $max_file/($level+1));
217 foreach my $f (@{$files}) {
218 $ccircle->{base_url} =~ s/pathid=\d+;(filenameid=\d+)?/pathid=$rep;filenameid=$f->[0];/;
220 $ccircle->add_part($f->[2] * 100 / $max,
222 $f->[1] . "\n" . Bweb::human_size($f->[2]));
227 $ccircle->add_part(($max - $sum) * 100 / $max,
229 "other\n" . Bweb::human_size($max - $sum));
232 $ccircle->finalize() ;
237 my ($jobid, $rep) = @_;
239 my $size = fv_get_size($jobid, $rep);
244 $size = fv_get_files_size($jobid, $rep);
246 my $dirs = fv_list_dirs($jobid, $rep);
247 foreach my $dir (@{$dirs}) {
248 $size += fv_compute_size($jobid, $dir->[0]);
251 fv_update_size($jobid, $rep, $size);
257 my ($jobid, $rep) = @_;
259 my $ret = $bweb->dbh_selectall_arrayref("
261 ( SELECT Path FROM Path WHERE PathId = P.PathId) AS Path
264 FROM brestore_pathvisibility
265 INNER JOIN brestore_pathhierarchy USING (PathId)
274 sub fv_get_file_attribute
276 my ($jobid, $full_name) = @_;
278 if ($full_name eq '/') {
282 my $filename = $bweb->dbh_quote(basename($full_name));
283 my $path = $bweb->dbh_quote(dirname($full_name) . "/");
285 my $attr = $bweb->dbh_selectrow_hashref("
288 base64_decode_lstat(8, LStat) AS size,
289 base64_decode_lstat(11, LStat) AS atime,
290 base64_decode_lstat(12, LStat) AS mtime,
291 base64_decode_lstat(13, LStat) AS ctime
293 FROM File JOIN Filename USING (FilenameId)
294 JOIN Path USING (PathId)
295 WHERE Name = $filename
300 $attr->{filename} = $full_name;
301 $attr->{size} = Bweb::human_size($attr->{size});
302 foreach my $d (qw/atime ctime mtime/) {
303 $attr->{$d} = strftime('%F %H:%M', localtime($attr->{$d}));
309 sub fv_get_file_attribute_from_id
311 my ($jobid, $pathid, $filenameid) = @_;
313 my $attr = $bweb->dbh_selectrow_hashref("
316 base64_decode_lstat(8, LStat) AS size,
317 base64_decode_lstat(11, LStat) AS atime,
318 base64_decode_lstat(12, LStat) AS mtime,
319 base64_decode_lstat(13, LStat) AS ctime,
320 Path.Path || Filename.Name AS filename
322 FROM File INNER JOIN Filename USING (FilenameId)
323 INNER JOIN Path USING (PathId)
324 WHERE FilenameId = $filenameid
329 $attr->{size} = Bweb::human_size($attr->{size});
330 foreach my $d (qw/atime ctime mtime/) {
331 $attr->{$d} = strftime('%F %H:%M', localtime($attr->{$d}));
338 my ($jobid, $rep) = @_;
340 my $ret = $bweb->dbh_selectrow_hashref("
342 FROM brestore_pathvisibility
350 sub fv_get_files_size
352 my ($jobid, $rep) = @_;
354 my $ret = $bweb->dbh_selectrow_hashref("
355 SELECT sum(base64_decode_lstat(8,LStat)) AS size
366 my ($jobid, $rep, $min, $limit) = @_;
367 $limit = int($limit);
369 my $ret = $bweb->dbh_selectall_arrayref("
370 SELECT FilenameId AS filenameid, Name AS name, size
372 SELECT FilenameId, base64_decode_lstat(8,LStat) AS size
376 ) AS S INNER JOIN Filename USING (FilenameId)
387 my ($jobid, $rep, $size) = @_;
389 my $nb = $bweb->dbh_do("
390 UPDATE brestore_pathvisibility SET Size = $size
398 sub fv_get_root_pathid
401 $path = $bweb->dbh_quote($path);
402 my $ret = $bweb->dbh_selectrow_hashref("SELECT PathId FROM Path WHERE Path = $path");
403 return $ret->{pathid};
409 my $ret = $bweb->dbh_selectrow_hashref("SELECT Path FROM Path WHERE PathId = $pathid");
416 CREATE OR REPLACE FUNCTION base64_decode_lstat(int4, varchar) RETURNS int8 AS $$
423 size := split_part($2, ' ', $1);
424 b64 := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
426 FOR i IN 1..length(size) LOOP
427 val := val + (strpos(b64, substr(size, i, 1))-1) * (64^(length(size)-i));
431 $$ language 'plpgsql';
433 ALTER TABLE brestore_pathvisibility ADD Size int8;
437 ALTER TABLE brestore_pathvisibility ADD Files int4;