]> git.proxmox.com Git - pve-common.git/blob - data/PVE/Tools.pm
33f0de1a217715395c521caf77b218673d9fe51e
[pve-common.git] / data / PVE / Tools.pm
1 package PVE::Tools;
2
3 use strict;
4 use POSIX;
5 use IO::Socket::INET;
6 use IO::Select;
7 use File::Basename;
8 use File::Path qw(make_path);
9 use IO::File;
10 use IPC::Open3;
11 use Fcntl qw(:DEFAULT :flock);
12 use base 'Exporter';
13 use URI::Escape;
14 use Encode;
15 use Digest::SHA1;
16 use Text::ParseWords;
17 use String::ShellQuote;
18
19 our @EXPORT_OK = qw(
20 lock_file
21 run_command
22 file_set_contents
23 file_get_contents
24 file_read_firstline
25 split_list
26 template_replace
27 safe_print
28 trim
29 extract_param
30 );
31
32 my $pvelogdir = "/var/log/pve";
33 my $pvetaskdir = "$pvelogdir/tasks";
34
35 mkdir $pvelogdir;
36 mkdir $pvetaskdir;
37
38 # flock: we use one file handle per process, so lock file
39 # can be called multiple times and succeeds for the same process.
40
41 my $lock_handles = {};
42
43 sub lock_file {
44 my ($filename, $timeout, $code, @param) = @_;
45
46 my $res;
47
48 $timeout = 10 if !$timeout;
49
50 eval {
51
52 local $SIG{ALRM} = sub { die "got timeout (can't lock '$filename')\n"; };
53
54 alarm ($timeout);
55
56 if (!$lock_handles->{$$}->{$filename}) {
57 $lock_handles->{$$}->{$filename} = new IO::File (">>$filename") ||
58 die "can't open lock file '$filename' - $!\n";
59 }
60
61 if (!flock ($lock_handles->{$$}->{$filename}, LOCK_EX|LOCK_NB)) {
62 print STDERR "trying to aquire lock...";
63 if (!flock ($lock_handles->{$$}->{$filename}, LOCK_EX)) {
64 print STDERR " failed\n";
65 die "can't aquire lock for '$filename' - $!\n";
66 }
67 print STDERR " OK\n";
68 }
69 alarm (0);
70
71 $res = &$code(@param);
72 };
73
74 my $err = $@;
75
76 alarm (0);
77
78 if ($lock_handles->{$$}->{$filename}) {
79 my $fh = $lock_handles->{$$}->{$filename};
80 $lock_handles->{$$}->{$filename} = undef;
81 close ($fh);
82 }
83
84 if ($err) {
85 $@ = $err;
86 return undef;
87 }
88
89 $@ = undef;
90
91 return $res;
92 }
93
94 sub file_set_contents {
95 my ($filename, $data, $perm) = @_;
96
97 $perm = 0644 if !defined($perm);
98
99 my $tmpname = "$filename.tmp.$$";
100
101 eval {
102 my $fh = IO::File->new($tmpname, O_WRONLY|O_CREAT, $perm);
103 die "unable to open file '$tmpname' - $!\n" if !$fh;
104 die "unable to write '$tmpname' - $!\n" unless print $fh $data;
105 die "closing file '$tmpname' failed - $!\n" unless close $fh;
106 };
107 my $err = $@;
108
109 if ($err) {
110 unlink $tmpname;
111 die $err;
112 }
113
114 if (!rename($tmpname, $filename)) {
115 my $msg = "close (rename) atomic file '$filename' failed: $!\n";
116 unlink $tmpname;
117 die $msg;
118 }
119 }
120
121 sub file_get_contents {
122 my ($filename, $max) = @_;
123
124 my $fh = IO::File->new($filename, "r") ||
125 die "can't open '$filename' - $!\n";
126
127 my $content = safe_read_from($fh, $max);
128
129 close $fh;
130
131 return $content;
132 }
133
134 sub file_read_firstline {
135 my ($filename) = @_;
136
137 my $fh = IO::File->new ($filename, "r");
138 return undef if !$fh;
139 my $res = <$fh>;
140 chomp $res;
141 $fh->close;
142 return $res;
143 }
144
145 sub safe_read_from {
146 my ($fh, $max, $oneline) = @_;
147
148 $max = 32768 if !$max;
149
150 my $br = 0;
151 my $input = '';
152 my $count;
153 while ($count = sysread($fh, $input, 8192, $br)) {
154 $br += $count;
155 die "input too long - aborting\n" if $br > $max;
156 if ($oneline && $input =~ m/^(.*)\n/) {
157 $input = $1;
158 last;
159 }
160 }
161 die "unable to read input - $!\n" if !defined($count);
162
163 return $input;
164 }
165
166 sub run_command {
167 my ($cmd, %param) = @_;
168
169 my $old_umask;
170
171 $cmd = [ $cmd ] if !ref($cmd);
172
173 my $cmdstr = cmd2string($cmd);
174
175 my $errmsg;
176 my $laststderr;
177 my $timeout;
178 my $oldtimeout;
179 my $pid;
180
181 my $outfunc;
182 my $errfunc;
183 my $logfunc;
184 my $input;
185 my $output;
186
187 eval {
188
189 foreach my $p (keys %param) {
190 if ($p eq 'timeout') {
191 $timeout = $param{$p};
192 } elsif ($p eq 'umask') {
193 umask($param{$p});
194 } elsif ($p eq 'errmsg') {
195 $errmsg = $param{$p};
196 } elsif ($p eq 'input') {
197 $input = $param{$p};
198 } elsif ($p eq 'output') {
199 $output = $param{$p};
200 } elsif ($p eq 'outfunc') {
201 $outfunc = $param{$p};
202 } elsif ($p eq 'errfunc') {
203 $errfunc = $param{$p};
204 } elsif ($p eq 'logfunc') {
205 $logfunc = $param{$p};
206 } else {
207 die "got unknown parameter '$p' for run_command\n";
208 }
209 }
210
211 if ($errmsg) {
212 my $origerrfunc = $errfunc;
213 $errfunc = sub {
214 if ($laststderr) {
215 if ($origerrfunc) {
216 &$origerrfunc("$laststderr\n");
217 } else {
218 print STDERR "$laststderr\n" if $laststderr;
219 }
220 }
221 $laststderr = shift;
222 };
223 }
224
225 my $reader = $output && $output =~ m/^>&/ ? $output : IO::File->new();
226 my $writer = $input && $input =~ m/^<&/ ? $input : IO::File->new();
227 my $error = IO::File->new();
228
229 # try to avoid locale related issues/warnings
230 my $lang = $param{lang} || 'C';
231
232 my $orig_pid = $$;
233
234 eval {
235 local $ENV{LC_ALL} = $lang;
236
237 # suppress LVM warnings like: "File descriptor 3 left open";
238 local $ENV{LVM_SUPPRESS_FD_WARNINGS} = "1";
239
240 $pid = open3($writer, $reader, $error, @$cmd) || die $!;
241
242 # if we pipe fron STDIN, open3 closes STDIN, so we we
243 # a perl warning "Filehandle STDIN reopened as GENXYZ .. "
244 # as soon as we open a new file.
245 # to avoid that we open /dev/null
246 if (!ref($writer) && !defined(fileno(STDIN))) {
247 POSIX::close(0);
248 open(STDIN, "</dev/null");
249 }
250 };
251
252 my $err = $@;
253
254 # catch exec errors
255 if ($orig_pid != $$) {
256 warn "ERROR: $err";
257 POSIX::_exit (1);
258 kill ('KILL', $$);
259 }
260
261 die $err if $err;
262
263 local $SIG{ALRM} = sub { die "got timeout\n"; } if $timeout;
264 $oldtimeout = alarm($timeout) if $timeout;
265
266 if (ref($writer)) {
267 print $writer $input if defined $input;
268 close $writer;
269 }
270
271 my $select = new IO::Select;
272 $select->add($reader) if ref($reader);
273 $select->add($error);
274
275 my $outlog = '';
276 my $errlog = '';
277
278 my $starttime = time();
279
280 while ($select->count) {
281 my @handles = $select->can_read(1);
282
283 foreach my $h (@handles) {
284 my $buf = '';
285 my $count = sysread ($h, $buf, 4096);
286 if (!defined ($count)) {
287 my $err = $!;
288 kill (9, $pid);
289 waitpid ($pid, 0);
290 die $err;
291 }
292 $select->remove ($h) if !$count;
293 if ($h eq $reader) {
294 if ($outfunc || $logfunc) {
295 eval {
296 $outlog .= $buf;
297 while ($outlog =~ s/^([^\010\r\n]*)(\r|\n|(\010)+|\r\n)//s) {
298 my $line = $1;
299 &$outfunc($line) if $outfunc;
300 &$logfunc($line) if $logfunc;
301 }
302 };
303 my $err = $@;
304 if ($err) {
305 kill (9, $pid);
306 waitpid ($pid, 0);
307 die $err;
308 }
309 } else {
310 print $buf;
311 *STDOUT->flush();
312 }
313 } elsif ($h eq $error) {
314 if ($errfunc || $logfunc) {
315 eval {
316 $errlog .= $buf;
317 while ($errlog =~ s/^([^\010\r\n]*)(\r|\n|(\010)+|\r\n)//s) {
318 my $line = $1;
319 &$errfunc($line) if $errfunc;
320 &$logfunc($line) if $logfunc;
321 }
322 };
323 my $err = $@;
324 if ($err) {
325 kill (9, $pid);
326 waitpid ($pid, 0);
327 die $err;
328 }
329 } else {
330 print STDERR $buf;
331 *STDERR->flush();
332 }
333 }
334 }
335 }
336
337 &$outfunc($outlog) if $outfunc && $outlog;
338 &$logfunc($outlog) if $logfunc && $outlog;
339
340 &$errfunc($errlog) if $errfunc && $errlog;
341 &$logfunc($errlog) if $logfunc && $errlog;
342
343 waitpid ($pid, 0);
344
345 if ($? == -1) {
346 die "failed to execute\n";
347 } elsif (my $sig = ($? & 127)) {
348 die "got signal $sig\n";
349 } elsif (my $ec = ($? >> 8)) {
350 if (!($ec == 24 && ($cmdstr =~ m|^(\S+/)?rsync\s|))) {
351 if ($errmsg && $laststderr) {
352 my $lerr = $laststderr;
353 $laststderr = undef;
354 die "$lerr\n";
355 }
356 die "exit code $ec\n";
357 }
358 }
359
360 alarm(0);
361 };
362
363 my $err = $@;
364
365 alarm(0);
366
367 if ($errmsg && $laststderr) {
368 &$errfunc(undef); # flush laststderr
369 }
370
371 umask ($old_umask) if defined($old_umask);
372
373 alarm($oldtimeout) if $oldtimeout;
374
375 if ($err) {
376 if ($pid && ($err eq "got timeout\n")) {
377 kill (9, $pid);
378 waitpid ($pid, 0);
379 die "command '$cmdstr' failed: $err";
380 }
381
382 if ($errmsg) {
383 die "$errmsg: $err";
384 } else {
385 die "command '$cmdstr' failed: $err";
386 }
387 }
388
389 return undef;
390 }
391
392 sub split_list {
393 my $listtxt = shift || '';
394
395 return split (/\0/, $listtxt) if $listtxt =~ m/\0/;
396
397 $listtxt =~ s/[,;]/ /g;
398 $listtxt =~ s/^\s+//;
399
400 my @data = split (/\s+/, $listtxt);
401
402 return @data;
403 }
404
405 sub trim {
406 my $txt = shift;
407
408 return $txt if !defined($txt);
409
410 $txt =~ s/^\s+//;
411 $txt =~ s/\s+$//;
412
413 return $txt;
414 }
415
416 # simple uri templates like "/vms/{vmid}"
417 sub template_replace {
418 my ($tmpl, $data) = @_;
419
420 my $res = '';
421 while ($tmpl =~ m/([^{]+)?({([^}]+)})?/g) {
422 $res .= $1 if $1;
423 $res .= ($data->{$3} || '-') if $2;
424 }
425 return $res;
426 }
427
428 sub safe_print {
429 my ($filename, $fh, $data) = @_;
430
431 return if !$data;
432
433 my $res = print $fh $data;
434
435 die "write to '$filename' failed\n" if !$res;
436 }
437
438 sub debmirrors {
439
440 return {
441 'at' => 'ftp.at.debian.org',
442 'au' => 'ftp.au.debian.org',
443 'be' => 'ftp.be.debian.org',
444 'bg' => 'ftp.bg.debian.org',
445 'br' => 'ftp.br.debian.org',
446 'ca' => 'ftp.ca.debian.org',
447 'ch' => 'ftp.ch.debian.org',
448 'cl' => 'ftp.cl.debian.org',
449 'cz' => 'ftp.cz.debian.org',
450 'de' => 'ftp.de.debian.org',
451 'dk' => 'ftp.dk.debian.org',
452 'ee' => 'ftp.ee.debian.org',
453 'es' => 'ftp.es.debian.org',
454 'fi' => 'ftp.fi.debian.org',
455 'fr' => 'ftp.fr.debian.org',
456 'gr' => 'ftp.gr.debian.org',
457 'hk' => 'ftp.hk.debian.org',
458 'hr' => 'ftp.hr.debian.org',
459 'hu' => 'ftp.hu.debian.org',
460 'ie' => 'ftp.ie.debian.org',
461 'is' => 'ftp.is.debian.org',
462 'it' => 'ftp.it.debian.org',
463 'jp' => 'ftp.jp.debian.org',
464 'kr' => 'ftp.kr.debian.org',
465 'mx' => 'ftp.mx.debian.org',
466 'nl' => 'ftp.nl.debian.org',
467 'no' => 'ftp.no.debian.org',
468 'nz' => 'ftp.nz.debian.org',
469 'pl' => 'ftp.pl.debian.org',
470 'pt' => 'ftp.pt.debian.org',
471 'ro' => 'ftp.ro.debian.org',
472 'ru' => 'ftp.ru.debian.org',
473 'se' => 'ftp.se.debian.org',
474 'si' => 'ftp.si.debian.org',
475 'sk' => 'ftp.sk.debian.org',
476 'tr' => 'ftp.tr.debian.org',
477 'tw' => 'ftp.tw.debian.org',
478 'gb' => 'ftp.uk.debian.org',
479 'us' => 'ftp.us.debian.org',
480 };
481 }
482
483 sub kvmkeymaps {
484 return {
485 'dk' => ['Danish', 'da', 'qwerty/dk-latin1.kmap.gz', 'dk', 'nodeadkeys'],
486 'de' => ['German', 'de', 'qwertz/de-latin1-nodeadkeys.kmap.gz', 'de', 'nodeadkeys' ],
487 'de-ch' => ['Swiss-German', 'de-ch', 'qwertz/sg-latin1.kmap.gz', 'ch', 'de_nodeadkeys' ],
488 'en-gb' => ['United Kingdom', 'en-gb', 'qwerty/uk.kmap.gz' , 'gb', 'intl' ],
489 'en-us' => ['U.S. English', 'en-us', 'qwerty/us-latin1.kmap.gz', 'us', 'intl' ],
490 'es' => ['Spanish', 'es', 'qwerty/es.kmap.gz', 'es', 'nodeadkeys'],
491 #'et' => [], # Ethopia or Estonia ??
492 'fi' => ['Finnish', 'fi', 'qwerty/fi-latin1.kmap.gz', 'fi', 'nodeadkeys'],
493 #'fo' => ['Faroe Islands', 'fo', ???, 'fo', 'nodeadkeys'],
494 'fr' => ['French', 'fr', 'azerty/fr-latin1.kmap.gz', 'fr', 'nodeadkeys'],
495 'fr-be' => ['Belgium-French', 'fr-be', 'azerty/be2-latin1.kmap.gz', 'be', 'nodeadkeys'],
496 'fr-ca' => ['Canada-French', 'fr-ca', 'qwerty/cf.kmap.gz', 'ca', 'fr-legacy'],
497 'fr-ch' => ['Swiss-French', 'fr-ch', 'qwertz/fr_CH-latin1.kmap.gz', 'ch', 'fr_nodeadkeys'],
498 #'hr' => ['Croatia', 'hr', 'qwertz/croat.kmap.gz', 'hr', ??], # latin2?
499 'hu' => ['Hungarian', 'hu', 'qwertz/hu.kmap.gz', 'hu', undef],
500 'is' => ['Icelandic', 'is', 'qwerty/is-latin1.kmap.gz', 'is', 'nodeadkeys'],
501 'it' => ['Italian', 'it', 'qwerty/it2.kmap.gz', 'it', 'nodeadkeys'],
502 'jp' => ['Japanese', 'ja', 'qwerty/jp106.kmap.gz', 'jp', undef],
503 'lt' => ['Lithuanian', 'lt', 'qwerty/lt.kmap.gz', 'lt', 'std'],
504 #'lv' => ['Latvian', 'lv', 'qwerty/lv-latin4.kmap.gz', 'lv', ??], # latin4 or latin7?
505 'mk' => ['Macedonian', 'mk', 'qwerty/mk.kmap.gz', 'mk', 'nodeadkeys'],
506 'nl' => ['Dutch', 'nl', 'qwerty/nl.kmap.gz', 'nl', undef],
507 #'nl-be' => ['Belgium-Dutch', 'nl-be', ?, ?, ?],
508 'no' => ['Norwegian', 'no', 'qwerty/no-latin1.kmap.gz', 'no', 'nodeadkeys'],
509 'pl' => ['Polish', 'pl', 'qwerty/pl.kmap.gz', 'pl', undef],
510 'pt' => ['Portuguese', 'pt', 'qwerty/pt-latin1.kmap.gz', 'pt', 'nodeadkeys'],
511 'pt-br' => ['Brazil-Portuguese', 'pt-br', 'qwerty/br-latin1.kmap.gz', 'br', 'nodeadkeys'],
512 #'ru' => ['Russian', 'ru', 'qwerty/ru.kmap.gz', 'ru', undef], # dont know?
513 'si' => ['Slovenian', 'sl', 'qwertz/slovene.kmap.gz', 'si', undef],
514 #'sv' => [], Swedish ?
515 #'th' => [],
516 #'tr' => [],
517 };
518 }
519
520 sub extract_param {
521 my ($param, $key) = @_;
522
523 my $res = $param->{$key};
524 delete $param->{$key};
525
526 return $res;
527 }
528
529 sub next_vnc_port {
530
531 for (my $p = 5900; $p < 6000; $p++) {
532
533 my $sock = IO::Socket::INET->new (Listen => 5,
534 LocalAddr => 'localhost',
535 LocalPort => $p,
536 ReuseAddr => 1,
537 Proto => 0);
538
539 if ($sock) {
540 close ($sock);
541 return $p;
542 }
543 }
544
545 die "unable to find free vnc port";
546 };
547
548 # NOTE: NFS syscall can't be interrupted, so alarm does
549 # not work to provide timeouts.
550 # from 'man nfs': "Only SIGKILL can interrupt a pending NFS operation"
551 # So the spawn external 'df' process instead of using
552 # Filesys::Df (which uses statfs syscall)
553 sub df {
554 my ($path, $timeout) = @_;
555
556 my $cmd = [ 'df', '-P', '-B', '1', $path];
557
558 my $res = {
559 total => 0,
560 used => 0,
561 avail => 0,
562 };
563
564 my $parser = sub {
565 my $line = shift;
566 if (my ($fsid, $total, $used, $avail) = $line =~
567 m/^(\S+.*)\s+(\d+)\s+(\d+)\s+(\d+)\s+\d+%\s.*$/) {
568 $res = {
569 total => $total,
570 used => $used,
571 avail => $avail,
572 };
573 }
574 };
575 eval { run_command($cmd, timeout => $timeout, outfunc => $parser); };
576 warn $@ if $@;
577
578 return $res;
579 }
580
581 # UPID helper
582 # We use this to uniquely identify a process.
583 # An 'Unique Process ID' has the following format:
584 # "UPID:$node:$pid:$pstart:$startime:$dtype:$id:$user"
585
586 sub upid_encode {
587 my $d = shift;
588
589 return sprintf("UPID:%s:%08X:%08X:%08X:%s:%s:%s:", $d->{node}, $d->{pid},
590 $d->{pstart}, $d->{starttime}, $d->{type}, $d->{id},
591 $d->{user});
592 }
593
594 sub upid_decode {
595 my ($upid, $noerr) = @_;
596
597 my $res;
598 my $filename;
599
600 # "UPID:$node:$pid:$pstart:$startime:$dtype:$id:$user"
601 if ($upid =~ m/^UPID:([A-Za-z][[:alnum:]\-]*[[:alnum:]]+):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8}):([^:\s]+):([^:\s]*):([^:\s]+):$/) {
602 $res->{node} = $1;
603 $res->{pid} = hex($2);
604 $res->{pstart} = hex($3);
605 $res->{starttime} = hex($4);
606 $res->{type} = $5;
607 $res->{id} = $6;
608 $res->{user} = $7;
609
610 my $subdir = substr($4, 7, 8);
611 $filename = "$pvetaskdir/$subdir/$upid";
612
613 } else {
614 return undef if $noerr;
615 die "unable to parse worker upid '$upid'\n";
616 }
617
618 return wantarray ? ($res, $filename) : $res;
619 }
620
621 sub upid_open {
622 my ($upid) = @_;
623
624 my ($task, $filename) = upid_decode($upid);
625
626 my $dirname = dirname($filename);
627 make_path($dirname);
628
629 my $wwwid = getpwnam('www-data') ||
630 die "getpwnam failed";
631
632 my $perm = 0640;
633
634 my $outfh = IO::File->new ($filename, O_WRONLY|O_CREAT|O_EXCL, $perm) ||
635 die "unable to create output file '$filename' - $!\n";
636 chown $wwwid, -1, $outfh;
637
638 return $outfh;
639 };
640
641 sub upid_read_status {
642 my ($upid) = @_;
643
644 my ($task, $filename) = upid_decode($upid);
645 my $fh = IO::File->new($filename, "r");
646 return "unable to open file - $!" if !$fh;
647 my $maxlen = 1024;
648 sysseek($fh, -$maxlen, 2);
649 my $readbuf = '';
650 my $br = sysread($fh, $readbuf, $maxlen);
651 close($fh);
652 if ($br) {
653 return "unable to extract last line"
654 if $readbuf !~ m/\n?(.+)$/;
655 my $line = $1;
656 if ($line =~ m/^TASK OK$/) {
657 return 'OK';
658 } elsif ($line =~ m/^TASK ERROR: (.+)$/) {
659 return $1;
660 } else {
661 return "unexpected status";
662 }
663 }
664 return "unable to read tail (got $br bytes)";
665 }
666
667 # useful functions to store comments in config files
668 sub encode_text {
669 my ($text) = @_;
670
671 # all control and hi-bit characters, and ':'
672 my $unsafe = "^\x20-\x39\x3b-\x7e";
673 return uri_escape(Encode::encode("utf8", $text), $unsafe);
674 }
675
676 sub decode_text {
677 my ($data) = @_;
678
679 return Encode::decode("utf8", uri_unescape($data));
680 }
681
682 sub random_ether_addr {
683
684 my $rand = Digest::SHA1::sha1_hex(rand(), time());
685
686 my $mac = '';
687 for (my $i = 0; $i < 6; $i++) {
688 my $ss = hex(substr($rand, $i*2, 2));
689 if (!$i) {
690 $ss &= 0xfe; # clear multicast
691 $ss |= 2; # set local id
692 }
693 $ss = sprintf("%02X", $ss);
694
695 if (!$i) {
696 $mac .= "$ss";
697 } else {
698 $mac .= ":$ss";
699 }
700 }
701
702 return $mac;
703 }
704
705 sub shellquote {
706 my $str = shift;
707
708 return String::ShellQuote::shell_quote($str);
709 }
710
711 sub cmd2string {
712 my ($cmd) = @_;
713
714 die "no arguments" if !$cmd;
715
716 return $cmd if !ref($cmd);
717
718 my @qa = ();
719 foreach my $arg (@$cmd) { push @qa, shellquote($arg); }
720
721 return join (' ', @qa);
722 }
723
724 # split an shell argument string into an array,
725 sub split_args {
726 my ($str) = @_;
727
728 return $str ? [ Text::ParseWords::shellwords($str) ] : [];
729 }
730
731 sub dump_logfile {
732 my ($filename, $start, $limit) = @_;
733
734 my $lines = [];
735 my $count = 0;
736
737 my $fh = IO::File->new($filename, "r");
738 if (!$fh) {
739 $count++;
740 push @$lines, { n => $count, t => "unable to open file - $!"};
741 return ($count, $lines);
742 }
743
744 $start = 0 if !$start;
745 $limit = 50 if !$limit;
746
747 my $line;
748 while (defined($line = <$fh>)) {
749 next if $count++ < $start;
750 next if $limit <= 0;
751 chomp $line;
752 push @$lines, { n => $count, t => $line};
753 $limit--;
754 }
755
756 close($fh);
757
758 # HACK: ExtJS store.guaranteeRange() does not like empty array
759 # so we add a line
760 if (!$count) {
761 $count++;
762 push @$lines, { n => $count, t => "no content"};
763 }
764
765 return ($count, $lines);
766 }
767
768 1;