| 1 | |||
| 2 | package AI::MicroStructure::DrKnow; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use AI::MicroStructure::ObjectSet; | ||
| 7 | use AnyEvent::Subprocess::Easy qw(qx_nonblock); | ||
| 8 | use File::Temp qw(tempdir); | ||
| 9 | use Log::Log4perl qw(:easy); | ||
| 10 | use File::Spec::Functions; | ||
| 11 | use File::Spec; | ||
| 12 | use File::Path; | ||
| 13 | use File::Copy; | ||
| 14 | use File::Find; | ||
| 15 | use File::Basename; | ||
| 16 | use IPC::Run qw(run); | ||
| 17 | use Cwd; | ||
| 18 | |||
| 19 | our $VERSION = "0.16"; | ||
| 20 | |||
| 21 | sub new { | ||
| 22 | |||
| 23 | my($class, %options) = @_; | ||
| 24 | |||
| 25 | my $self = { | ||
| 26 | Know => undef, | ||
| 27 | tmpdir => undef, | ||
| 28 | Know_read_options => '', | ||
| 29 | Know_write_options => '', | ||
| 30 | Know_gnu_read_options => [], | ||
| 31 | dirs => 0, | ||
| 32 | max_cmd_line_args => 512, | ||
| 33 | ramdisk => undef, | ||
| 34 | %options, | ||
| 35 | }; | ||
| 36 | |||
| 37 | bless $self, $class; | ||
| 38 | |||
| 39 | $self->{Know} = bin_find("micro") unless defined $self->{Know}; | ||
| 40 | $self->{Know} = bin_find("pdftotext") unless defined $self->{Know}; | ||
| 41 | |||
| 42 | if( ! defined $self->{Know} ) { | ||
| 43 | LOGDIE "Know not found in PATH, please specify location"; | ||
| 44 | } | ||
| 45 | |||
| 46 | if(defined $self->{ramdisk}) { | ||
| 47 | my $rc = $self->ramdisk_mount( %{ $self->{ramdisk} } ); | ||
| 48 | if(!$rc) { | ||
| 49 | LOGDIE "Mounting ramdisk failed"; | ||
| 50 | } | ||
| 51 | $self->{tmpdir} = $self->{ramdisk}->{tmpdir}; | ||
| 52 | } else { | ||
| 53 | $self->{tmpdir} = tempdir($self->{tmpdir} ? | ||
| 54 | (DIR => $self->{tmpdir}) : ()); | ||
| 55 | } | ||
| 56 | |||
| 57 | $self->{Knowdir} = File::Spec->catfile($self->{tmpdir}, "Know"); | ||
| 58 | mkpath [$self->{Knowdir}], 0, 0755 or | ||
| 59 | LOGDIE "Cannot mkpath $self->{Knowdir} ($!)"; | ||
| 60 | |||
| 61 | $self->{objdir} = tempdir(); | ||
| 62 | |||
| 63 | return $self; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub Knowdir { | ||
| 67 | |||
| 68 | my($self) = @_; | ||
| 69 | |||
| 70 | return $self->{Knowdir}; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub read { | ||
| 74 | |||
| 75 | my($self, $Knowfile, @files) = @_; | ||
| 76 | |||
| 77 | my $cwd = getcwd(); | ||
| 78 | |||
| 79 | unless(File::Spec::Functions::file_name_is_absolute($Knowfile)) { | ||
| 80 | $Knowfile = File::Spec::Functions::rel2abs($Knowfile, $cwd); | ||
| 81 | } | ||
| 82 | |||
| 83 | chdir $self->{Knowdir} or | ||
| 84 | LOGDIE "Cannot chdir to $self->{Knowdir}"; | ||
| 85 | |||
| 86 | my $compr_opt = ""; | ||
| 87 | $compr_opt = "z" if $self->is_compressed($Knowfile); | ||
| 88 | |||
| 89 | my $cmd = [$self->{Know}, "${compr_opt}x$self->{Know_read_options}", | ||
| 90 | @{$self->{Know_gnu_read_options}}, | ||
| 91 | "-f", $Knowfile, @files]; | ||
| 92 | |||
| 93 | DEBUG "Running @$cmd"; | ||
| 94 | |||
| 95 | my $rc = run($cmd, \my($in, $out, $err)); | ||
| 96 | |||
| 97 | if(!$rc) { | ||
| 98 | ERROR "@$cmd failed: $err"; | ||
| 99 | chdir $cwd or LOGDIE "Cannot chdir to $cwd"; | ||
| 100 | return undef; | ||
| 101 | } | ||
| 102 | |||
| 103 | WARN $err if $err; | ||
| 104 | |||
| 105 | chdir $cwd or LOGDIE "Cannot chdir to $cwd"; | ||
| 106 | |||
| 107 | return 1; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub is_compressed { | ||
| 111 | |||
| 112 | my($self, $Knowfile) = @_; | ||
| 113 | |||
| 114 | return 1 if $Knowfile =~ /\.t?gz$/i; | ||
| 115 | |||
| 116 | # Sloppy check for gzip files | ||
| 117 | open FILE, "<$Knowfile" or die "Cannot open $Knowfile"; | ||
| 118 | binmode FILE; | ||
| 119 | my $read = sysread(FILE, my $two, 2, 0) or die "Cannot sysread"; | ||
| 120 | close FILE; | ||
| 121 | return 1 if | ||
| 122 | ord(substr($two, 0, 1)) eq 0x1F and | ||
| 123 | ord(substr($two, 1, 1)) eq 0x8B; | ||
| 124 | |||
| 125 | return 0; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub locate { | ||
| 129 | |||
| 130 | my($self, $rel_path) = @_; | ||
| 131 | |||
| 132 | my $real_path = File::Spec->catfile($self->{Knowdir}, $rel_path); | ||
| 133 | |||
| 134 | if(-e $real_path) { | ||
| 135 | DEBUG "$real_path exists"; | ||
| 136 | return $real_path; | ||
| 137 | } | ||
| 138 | DEBUG "$real_path doesn't exist"; | ||
| 139 | |||
| 140 | WARN "$rel_path not found in Knowball"; | ||
| 141 | return undef; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub add { | ||
| 145 | |||
| 146 | my($self, $rel_path, $path_or_stringref, $opts) = @_; | ||
| 147 | |||
| 148 | if($opts) { | ||
| 149 | if(!ref($opts) or ref($opts) ne 'HASH') { | ||
| 150 | LOGDIE "Option parameter given to add() not a hashref."; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | my $perm = $opts->{perm} if defined $opts->{perm}; | ||
| 155 | my $uid = $opts->{uid} if defined $opts->{uid}; | ||
| 156 | my $gid = $opts->{gid} if defined $opts->{gid}; | ||
| 157 | my $binmode = $opts->{binmode} if defined $opts->{binmode}; | ||
| 158 | |||
| 159 | my $Knowget = File::Spec->catfile($self->{Knowdir}, $rel_path); | ||
| 160 | my $Knowget_dir = dirname($Knowget); | ||
| 161 | |||
| 162 | if( ! -d $Knowget_dir ) { | ||
| 163 | if( ref($path_or_stringref) ) { | ||
| 164 | $self->add( dirname( $rel_path ), dirname( $Knowget_dir ) ); | ||
| 165 | } else { | ||
| 166 | $self->add( dirname( $rel_path ), dirname( $path_or_stringref ) ); | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | if(ref($path_or_stringref)) { | ||
| 171 | open FILE, ">$Knowget" or LOGDIE "Can't open $Knowget ($!)"; | ||
| 172 | if(defined $binmode) { | ||
| 173 | binmode FILE, $binmode; | ||
| 174 | } | ||
| 175 | print FILE $$path_or_stringref; | ||
| 176 | close FILE; | ||
| 177 | } elsif( -d $path_or_stringref ) { | ||
| 178 | # perms will be fixed further down | ||
| 179 | mkpath($Knowget, 0, 0755) unless -d $Knowget; | ||
| 180 | } else { | ||
| 181 | copy $path_or_stringref, $Knowget or | ||
| 182 | LOGDIE "Can't copy $path_or_stringref to $Knowget ($!)"; | ||
| 183 | } | ||
| 184 | |||
| 185 | if(defined $uid) { | ||
| 186 | chown $uid, -1, $Knowget or | ||
| 187 | LOGDIE "Can't chown $Knowget uid to $uid ($!)"; | ||
| 188 | } | ||
| 189 | |||
| 190 | if(defined $gid) { | ||
| 191 | chown -1, $gid, $Knowget or | ||
| 192 | LOGDIE "Can't chown $Knowget gid to $gid ($!)"; | ||
| 193 | } | ||
| 194 | |||
| 195 | if(defined $perm) { | ||
| 196 | chmod $perm, $Knowget or | ||
| 197 | LOGDIE "Can't chmod $Knowget to $perm ($!)"; | ||
| 198 | } | ||
| 199 | |||
| 200 | if(!defined $uid and | ||
| 201 | !defined $gid and | ||
| 202 | !defined $perm and | ||
| 203 | !ref($path_or_stringref)) { | ||
| 204 | perm_cp($path_or_stringref, $Knowget) or | ||
| 205 | LOGDIE "Can't perm_cp $path_or_stringref to $Knowget ($!)"; | ||
| 206 | } | ||
| 207 | |||
| 208 | 1; | ||
| 209 | } | ||
| 210 | |||
| 211 | ###################################### | ||
| 212 | sub perm_cp { | ||
| 213 | ###################################### | ||
| 214 | # Lifted from Ben Okopnik's | ||
| 215 | # http://www.linuxgazette.com/issue87/misc/tips/cpmod.pl.txt | ||
| 216 | |||
| 217 | my $perms = perm_get($_[0]); | ||
| 218 | perm_set($_[1], $perms); | ||
| 219 | } | ||
| 220 | |||
| 221 | ###################################### | ||
| 222 | sub perm_get { | ||
| 223 | ###################################### | ||
| 224 | my($filename) = @_; | ||
| 225 | |||
| 226 | my @stats = (stat $filename)[2,4,5] or | ||
| 227 | LOGDIE "Cannot stat $filename ($!)"; | ||
| 228 | |||
| 229 | return \@stats; | ||
| 230 | } | ||
| 231 | |||
| 232 | ###################################### | ||
| 233 | sub perm_set { | ||
| 234 | ###################################### | ||
| 235 | my($filename, $perms) = @_; | ||
| 236 | |||
| 237 | chown($perms->[1], $perms->[2], $filename) or | ||
| 238 | LOGDIE "Cannot chown $filename ($!)"; | ||
| 239 | chmod($perms->[0] & 07777, $filename) or | ||
| 240 | LOGDIE "Cannot chmod $filename ($!)"; | ||
| 241 | } | ||
| 242 | |||
| 243 | sub remove { | ||
| 244 | |||
| 245 | my($self, $rel_path) = @_; | ||
| 246 | |||
| 247 | my $Knowget = File::Spec->catfile($self->{Knowdir}, $rel_path); | ||
| 248 | |||
| 249 | rmtree($Knowget) or LOGDIE "Can't rmtree $Knowget ($!)"; | ||
| 250 | } | ||
| 251 | |||
| 252 | sub list_all { | ||
| 253 | |||
| 254 | my($self) = @_; | ||
| 255 | |||
| 256 | my @entries = (); | ||
| 257 | |||
| 258 | $self->list_reset(); | ||
| 259 | |||
| 260 | while(my $entry = $self->list_next()) { | ||
| 261 | push @entries, $entry; | ||
| 262 | } | ||
| 263 | |||
| 264 | return \@entries; | ||
| 265 | } | ||
| 266 | |||
| 267 | sub list_reset { | ||
| 268 | |||
| 269 | my($self) = @_; | ||
| 270 | |||
| 271 | my $list_file = File::Spec->catfile($self->{objdir}, "list"); | ||
| 272 | open FILE, ">$list_file" or LOGDIE "Can't open $list_file"; | ||
| 273 | |||
| 274 | my $cwd = getcwd(); | ||
| 275 | chdir $self->{Knowdir} or LOGDIE "Can't chdir to $self->{Knowdir} ($!)"; | ||
| 276 | |||
| 277 | find(sub { | ||
| 278 | my $entry = $File::Find::name; | ||
| 279 | $entry =~ s#^\./##; | ||
| 280 | my $type = (-d $_ ? "d" : | ||
| 281 | -l $_ ? "l" : | ||
| 282 | "f" | ||
| 283 | ); | ||
| 284 | print FILE "$type $entry\n"; | ||
| 285 | }, "."); | ||
| 286 | |||
| 287 | chdir $cwd or LOGDIE "Can't chdir to $cwd ($!)"; | ||
| 288 | |||
| 289 | close FILE; | ||
| 290 | |||
| 291 | $self->offset(0); | ||
| 292 | } | ||
| 293 | |||
| 294 | sub list_next { | ||
| 295 | |||
| 296 | my($self) = @_; | ||
| 297 | |||
| 298 | my $offset = $self->offset(); | ||
| 299 | |||
| 300 | my $list_file = File::Spec->catfile($self->{objdir}, "list"); | ||
| 301 | open FILE, "<$list_file" or LOGDIE "Can't open $list_file"; | ||
| 302 | seek FILE, $offset, 0; | ||
| 303 | |||
| 304 | { my $line = <FILE>; | ||
| 305 | |||
| 306 | return undef unless defined $line; | ||
| 307 | |||
| 308 | chomp $line; | ||
| 309 | my($type, $entry) = split / /, $line, 2; | ||
| 310 | redo if $type eq "d" and ! $self->{dirs}; | ||
| 311 | $self->offset(tell FILE); | ||
| 312 | return [$entry, File::Spec->catfile($self->{Knowdir}, $entry), | ||
| 313 | $type]; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | |||
| 317 | sub offset { | ||
| 318 | |||
| 319 | my($self, $new_offset) = @_; | ||
| 320 | |||
| 321 | my $offset_file = File::Spec->catfile($self->{objdir}, "offset"); | ||
| 322 | |||
| 323 | if(defined $new_offset) { | ||
| 324 | open FILE, ">$offset_file" or LOGDIE "Can't open $offset_file"; | ||
| 325 | print FILE "$new_offset\n"; | ||
| 326 | close FILE; | ||
| 327 | } | ||
| 328 | |||
| 329 | open FILE, "<$offset_file" or LOGDIE "Can't open $offset_file (Did you call list_next() without a previous list_reset()?)"; | ||
| 330 | my $offset = <FILE>; | ||
| 331 | chomp $offset; | ||
| 332 | return $offset; | ||
| 333 | close FILE; | ||
| 334 | } | ||
| 335 | |||
| 336 | sub write { | ||
| 337 | |||
| 338 | my($self, $Knowfile, $compress) = @_; | ||
| 339 | |||
| 340 | my $cwd = getcwd(); | ||
| 341 | chdir $self->{Knowdir} or LOGDIE "Can't chdir to $self->{Knowdir} ($!)"; | ||
| 342 | |||
| 343 | unless(File::Spec::Functions::file_name_is_absolute($Knowfile)) { | ||
| 344 | $Knowfile = File::Spec::Functions::rel2abs($Knowfile, $cwd); | ||
| 345 | } | ||
| 346 | |||
| 347 | my $compr_opt = ""; | ||
| 348 | $compr_opt = "z" if $compress; | ||
| 349 | |||
| 350 | opendir DIR, "." or LOGDIE "Cannot open $self->{Knowdir}"; | ||
| 351 | my @top_entries = grep { $_ !~ /^\.\.?$/ } readdir DIR; | ||
| 352 | closedir DIR; | ||
| 353 | |||
| 354 | my $cmd; | ||
| 355 | |||
| 356 | if(@top_entries > $self->{max_cmd_line_args}) { | ||
| 357 | my $filelist_file = $self->{tmpdir}."/file-list"; | ||
| 358 | open FLIST, ">$filelist_file" or | ||
| 359 | LOGDIE "Cannot open $filelist_file ($!)"; | ||
| 360 | for(@top_entries) { | ||
| 361 | print FLIST "$_\n"; | ||
| 362 | } | ||
| 363 | close FLIST; | ||
| 364 | $cmd = [$self->{Know}, "${compr_opt}cf$self->{Know_write_options}", | ||
| 365 | $Knowfile, "-T", $filelist_file]; | ||
| 366 | } else { | ||
| 367 | $cmd = [$self->{Know}, "${compr_opt}cf$self->{Know_write_options}", | ||
| 368 | $Knowfile, @top_entries]; | ||
| 369 | } | ||
| 370 | |||
| 371 | DEBUG "Running @$cmd"; | ||
| 372 | my $rc = run($cmd, \my($in, $out, $err)); | ||
| 373 | |||
| 374 | if(!$rc) { | ||
| 375 | ERROR "@$cmd failed: $err"; | ||
| 376 | chdir $cwd or LOGDIE "Cannot chdir to $cwd"; | ||
| 377 | return undef; | ||
| 378 | } | ||
| 379 | |||
| 380 | WARN $err if $err; | ||
| 381 | |||
| 382 | chdir $cwd or LOGDIE "Cannot chdir to $cwd"; | ||
| 383 | |||
| 384 | return 1; | ||
| 385 | } | ||
| 386 | |||
| 387 | sub DESTROY { | ||
| 388 | |||
| 389 | my($self) = @_; | ||
| 390 | |||
| 391 | $self->ramdisk_unmount() if defined $self->{ramdisk}; | ||
| 392 | |||
| 393 | rmtree($self->{objdir}) if defined $self->{objdir}; | ||
| 394 | rmtree($self->{tmpdir}) if defined $self->{tmpdir}; | ||
| 395 | } | ||
| 396 | |||
| 397 | ###################################### | ||
| 398 | sub bin_find { | ||
| 399 | ###################################### | ||
| 400 | my($exe) = @_; | ||
| 401 | |||
| 402 | my @paths = split /:/, $ENV{PATH}; | ||
| 403 | |||
| 404 | push @paths, | ||
| 405 | "/usr/bin", | ||
| 406 | "/bin", | ||
| 407 | "/usr/sbin", | ||
| 408 | "/opt/bin", | ||
| 409 | "/ops/csw/bin", | ||
| 410 | ; | ||
| 411 | |||
| 412 | for my $path ( @paths ) { | ||
| 413 | my $full = File::Spec->catfile($path, $exe); | ||
| 414 | return $full if -x $full; | ||
| 415 | } | ||
| 416 | |||
| 417 | return undef; | ||
| 418 | } | ||
| 419 | |||
| 420 | sub is_gnu { | ||
| 421 | |||
| 422 | my($self) = @_; | ||
| 423 | |||
| 424 | open PIPE, "$self->{Know} --version |" or | ||
| 425 | return 0; | ||
| 426 | |||
| 427 | my $output = join "\n", <PIPE>; | ||
| 428 | close PIPE; | ||
| 429 | |||
| 430 | return $output =~ /GNU/; | ||
| 431 | } | ||
| 432 | |||
| 433 | sub ramdisk_mount { | ||
| 434 | |||
| 435 | my($self, %options) = @_; | ||
| 436 | |||
| 437 | # mkdir -p /mnt/myramdisk | ||
| 438 | # mount -t tmpfs -o size=20m tmpfs /mnt/myramdisk | ||
| 439 | |||
| 440 | $self->{mount} = bin_find("mount") unless $self->{mount}; | ||
| 441 | $self->{umount} = bin_find("umount") unless $self->{umount}; | ||
| 442 | |||
| 443 | for (qw(mount umount)) { | ||
| 444 | if(!defined $self->{$_}) { | ||
| 445 | LOGWARN "No $_ command found in PATH"; | ||
| 446 | return undef; | ||
| 447 | } | ||
| 448 | } | ||
| 449 | |||
| 450 | $self->{ramdisk} = { %options }; | ||
| 451 | |||
| 452 | $self->{ramdisk}->{size} = "100m" unless | ||
| 453 | defined $self->{ramdisk}->{size}; | ||
| 454 | |||
| 455 | if(! defined $self->{ramdisk}->{tmpdir}) { | ||
| 456 | $self->{ramdisk}->{tmpdir} = tempdir( CLEANUP => 1 ); | ||
| 457 | } | ||
| 458 | |||
| 459 | my @cmd = ($self->{mount}, | ||
| 460 | "-t", "tmpfs", "-o", "size=$self->{ramdisk}->{size}", | ||
| 461 | "tmpfs", $self->{ramdisk}->{tmpdir}); | ||
| 462 | |||
| 463 | INFO "Mounting ramdisk: @cmd"; | ||
| 464 | my $rc = system( @cmd ); | ||
| 465 | |||
| 466 | if($rc) { | ||
| 467 | LOGWARN "Mount command '@cmd' failed: $?"; | ||
| 468 | LOGWARN "Note that this only works on Linux and as root"; | ||
| 469 | return; | ||
| 470 | } | ||
| 471 | |||
| 472 | $self->{ramdisk}->{mounted} = 1; | ||
| 473 | |||
| 474 | return 1; | ||
| 475 | } | ||
| 476 | |||
| 477 | sub ramdisk_unmount { | ||
| 478 | |||
| 479 | my($self) = @_; | ||
| 480 | |||
| 481 | return if !exists $self->{ramdisk}->{mounted}; | ||
| 482 | |||
| 483 | my @cmd = ($self->{umount}, $self->{ramdisk}->{tmpdir}); | ||
| 484 | |||
| 485 | INFO "Unmounting ramdisk: @cmd"; | ||
| 486 | |||
| 487 | my $rc = system( @cmd ); | ||
| 488 | |||
| 489 | if($rc) { | ||
| 490 | LOGWARN "Unmount command '@cmd' failed: $?"; | ||
| 491 | return; | ||
| 492 | } | ||
| 493 | |||
| 494 | delete $self->{ramdisk}; | ||
| 495 | return 1; | ||
| 496 | } | ||
| 497 | |||
| 498 | |||
| 499 | |||
| 500 | 1; | ||
| 501 | |||
| 502 | |||
| 503 | |||
| 504 | package main; | ||
| 505 | |||
| 506 | |||
| 507 | |||
| 508 | |||
| 509 | |||
| 510 | BEGIN{ | ||
| 511 | |||
| 512 | use MicroStructure::DrKnow; | ||
| 513 | use Data::Printer; | ||
| 514 | my ($Know_path && $real_path) = (); | ||
| 515 | my $arch = MicroStructure::DrKnow->new(); | ||
| 516 | |||
| 517 | |||
| 518 | # Open a Knowball, expand it into a temporary directory | ||
| 519 | $arch->read("archive.tgz"); | ||
| 520 | |||
| 521 | # Iterate over all entries in the archive | ||
| 522 | $arch->list_reset(); # Reset Iterator | ||
| 523 | # Iterate through archive | ||
| 524 | while(my $entry = $arch->list_next()) { | ||
| 525 | my($Know_path, $phys_path) = @$entry; | ||
| 526 | print "$Know_path\n"; | ||
| 527 | } | ||
| 528 | |||
| 529 | # Open a Knowball, expand it into a temporary directory | ||
| 530 | $arch->read("archive.tgz"); | ||
| 531 | |||
| 532 | # Iterate over all entries in the archive | ||
| 533 | $arch->list_reset(); # Reset Iterator | ||
| 534 | # Iterate through archive | ||
| 535 | while(my $entry = $arch->list_next()) { | ||
| 536 | my($Know_path, $phys_path) = @$entry; | ||
| 537 | print "$Know_path\n"; | ||
| 538 | next unless($Know_path && $real_path); | ||
| 539 | |||
| 540 | # Get a huge list with all entries | ||
| 541 | for my $entry (@{$arch->list_all()}) { | ||
| 542 | |||
| 543 | |||
| 544 | my($Know_path, $real_path) = @$entry; | ||
| 545 | print "Knowpath: $Know_path Tempfile: $real_path\n"; | ||
| 546 | } | ||
| 547 | |||
| 548 | |||
| 549 | # Find the physical location of a temporary file | ||
| 550 | my($tmp_path) = $arch->locate($Know_path); | ||
| 551 | |||
| 552 | # Create a Knowball | ||
| 553 | # $arch->write($Knowfile, $compress); | ||
| 554 | } | ||
| 555 | } | ||
| 556 | |||
| 557 | |||
| 558 | __END__ | ||
| 559 | |||
| 560 | =head1 NAME | ||
| 561 | |||
| 562 | MicroStructure::DrKnow - API wrapper around the 'Know' utility | ||
| 563 | |||
| 564 | =head1 SYNOPSIS | ||
| 565 | |||
| 566 | use MicroStructure::DrKnow; | ||
| 567 | |||
| 568 | my $arch = MicroStructure::DrKnow->new(); | ||
| 569 | |||
| 570 | # Open a Knowball, expand it into a temporary directory | ||
| 571 | $arch->read("archive.tgz"); | ||
| 572 | |||
| 573 | # Iterate over all entries in the archive | ||
| 574 | $arch->list_reset(); # Reset Iterator | ||
| 575 | # Iterate through archive | ||
| 576 | while(my $entry = $arch->list_next()) { | ||
| 577 | my($Know_path, $phys_path) = @$entry; | ||
| 578 | print "$Know_path\n"; | ||
| 579 | } | ||
| 580 | |||
| 581 | # Get a huge list with all entries | ||
| 582 | for my $entry (@{$arch->list_all()}) { | ||
| 583 | my($Know_path, $real_path) = @$entry; | ||
| 584 | print "Knowpath: $Know_path Tempfile: $real_path\n"; | ||
| 585 | } | ||
| 586 | |||
| 587 | # Add a new entry | ||
| 588 | $arch->add($logic_path, $file_or_stringref); | ||
| 589 | |||
| 590 | # Remove an entry | ||
| 591 | $arch->remove($logic_path); | ||
| 592 | |||
| 593 | # Find the physical location of a temporary file | ||
| 594 | my($tmp_path) = $arch->locate($Know_path); | ||
| 595 | |||
| 596 | # Create a Knowball | ||
| 597 | $arch->write($Knowfile, $compress); | ||
| 598 | |||
| 599 | =head1 DESCRIPTION | ||
| 600 | |||
| 601 | MicroStructure::DrKnow is an API wrapper around the 'Know' command line | ||
| 602 | utility. It never stores anything in memory, but works on temporary | ||
| 603 | directory structures on disk instead. It provides a mapping between | ||
| 604 | the logical paths in the Knowball and the 'real' files in the temporary | ||
| 605 | directory on disk. | ||
| 606 | |||
| 607 | It differs from Archive::Know in two ways: | ||
| 608 | |||
| 609 | =over 4 | ||
| 610 | |||
| 611 | =item * | ||
| 612 | |||
| 613 | MicroStructure::DrKnow doesn't hold anything in memory. Everything is | ||
| 614 | stored on disk. | ||
| 615 | |||
| 616 | =item * | ||
| 617 | |||
| 618 | MicroStructure::DrKnow is 100% compliant with the platform's C<Know> | ||
| 619 | utility, because it uses it internally. | ||
| 620 | |||
| 621 | =back | ||
| 622 | |||
| 623 | =head1 METHODS | ||
| 624 | |||
| 625 | =over 4 | ||
| 626 | |||
| 627 | =item B<my $arch = MicroStructure::DrKnow-E<gt>new()> | ||
| 628 | |||
| 629 | Constructor for the Know wrapper class. Finds the C<Know> executable | ||
| 630 | by searching C<PATH> and returning the first hit. In case you want | ||
| 631 | to use a different Know executable, you can specify it as a parameter: | ||
| 632 | |||
| 633 | my $arch = MicroStructure::DrKnow->new(Know => '/path/to/Know'); | ||
| 634 | |||
| 635 | Since C<MicroStructure::DrKnow> creates temporary directories to store | ||
| 636 | Know data, the location of the temporary directory can be specified: | ||
| 637 | |||
| 638 | my $arch = MicroStructure::DrKnow->new(tmpdir => '/path/to/tmpdir'); | ||
| 639 | |||
| 640 | Tremendous performance increases can be achieved if the temporary | ||
| 641 | directory is located on a ram disk. Check the "Using RAM Disks" | ||
| 642 | section below for details. | ||
| 643 | |||
| 644 | Additional options can be passed to the C<Know> command by using the | ||
| 645 | C<Know_read_options> and C<Know_write_options> parameters. Example: | ||
| 646 | |||
| 647 | my $arch = MicroStructure::DrKnow->new( | ||
| 648 | Know_read_options => "p" | ||
| 649 | ); | ||
| 650 | |||
| 651 | will use C<Know xfp archive.tgz> to extract the Knowball instead of just | ||
| 652 | C<Know xf archive.tgz>. Gnu Know supports even more options, these can | ||
| 653 | be passed in via | ||
| 654 | |||
| 655 | my $arch = MicroStructure::DrKnow->new( | ||
| 656 | Know_gnu_read_options => ["--numeric-owner"], | ||
| 657 | ); | ||
| 658 | |||
| 659 | By default, the C<list_*()> functions will return only file entries. | ||
| 660 | Directories will be suppressed. To have C<list_*()> | ||
| 661 | return directories as well, use | ||
| 662 | |||
| 663 | my $arch = MicroStructure::DrKnow->new( | ||
| 664 | dirs => 1 | ||
| 665 | ); | ||
| 666 | |||
| 667 | If more files are added to a Knowball than the command line can handle, | ||
| 668 | C<MicroStructure::DrKnow> will switch from using the command | ||
| 669 | |||
| 670 | Know cfv Knowfile file1 file2 file3 ... | ||
| 671 | |||
| 672 | to | ||
| 673 | |||
| 674 | Know cfv Knowfile -T filelist | ||
| 675 | |||
| 676 | where C<filelist> is a file containing all file to be added. The default | ||
| 677 | for this switch is 512, but it can be changed by setting the parameter | ||
| 678 | C<max_cmd_line_args>: | ||
| 679 | |||
| 680 | my $arch = MicroStructure::DrKnow->new( | ||
| 681 | max_cmd_line_args => 1024 | ||
| 682 | ); | ||
| 683 | |||
| 684 | =item B<$arch-E<gt>read("archive.tgz")> | ||
| 685 | |||
| 686 | C<read()> opens the given Knowball, expands it into a temporary directory | ||
| 687 | and returns 1 on success und C<undef> on failure. | ||
| 688 | The temporary directory holding the Know data gets cleaned up when C<$arch> | ||
| 689 | goes out of scope. | ||
| 690 | |||
| 691 | C<read> handles both compressed and uncompressed files. To find out if | ||
| 692 | a file is compressed or uncompressed, it tries to guess by extension, | ||
| 693 | then by checking the first couple of bytes in the Knowfile. | ||
| 694 | |||
| 695 | If only a limited number of files is needed from a Knowball, they | ||
| 696 | can be specified after the Knowball name: | ||
| 697 | |||
| 698 | $arch->read("archive.tgz", "path/file.dat", "path/sub/another.txt"); | ||
| 699 | |||
| 700 | The file names are passed unmodified to the C<Know> command, make sure | ||
| 701 | that the file paths match exactly what's in the Knowball, otherwise | ||
| 702 | C<read()> will fail. | ||
| 703 | |||
| 704 | =item B<$arch-E<gt>list_reset()> | ||
| 705 | |||
| 706 | Resets the list iterator. To be used before the first call to | ||
| 707 | B<$arch->list_next()>. | ||
| 708 | |||
| 709 | =item B<my($Know_path, $phys_path, $type) = $arch-E<gt>list_next()> | ||
| 710 | |||
| 711 | Returns the next item in the Knowfile. It returns a list of three scalars: | ||
| 712 | the relative path of the item in the Knowfile, the physical path | ||
| 713 | to the unpacked file or directory on disk, and the type of the entry | ||
| 714 | (f=file, d=directory, l=symlink). Note that by default, | ||
| 715 | MicroStructure::DrKnow won't display directories, unless the C<dirs> | ||
| 716 | parameter is set when running the constructor. | ||
| 717 | |||
| 718 | =item B<my $items = $arch-E<gt>list_all()> | ||
| 719 | |||
| 720 | Returns a reference to a (possibly huge) array of items in the | ||
| 721 | Knowfile. Each item is a reference to an array, containing two | ||
| 722 | elements: the relative path of the item in the Knowfile and the | ||
| 723 | physical path to the unpacked file or directory on disk. | ||
| 724 | |||
| 725 | To iterate over the list, the following construct can be used: | ||
| 726 | |||
| 727 | # Get a huge list with all entries | ||
| 728 | for my $entry (@{$arch->list_all()}) { | ||
| 729 | my($Know_path, $real_path) = @$entry; | ||
| 730 | print "Knowpath: $Know_path Tempfile: $real_path\n"; | ||
| 731 | } | ||
| 732 | |||
| 733 | If the list of items in the Knowfile is big, use C<list_reset()> and | ||
| 734 | C<list_next()> instead of C<list_all>. | ||
| 735 | |||
| 736 | =item B<$arch-E<gt>add($logic_path, $file_or_stringref, [$options])> | ||
| 737 | |||
| 738 | Add a new file to the Knowball. C<$logic_path> is the virtual path | ||
| 739 | of the file within the Knowball. C<$file_or_stringref> is either | ||
| 740 | a scalar, in which case it holds the physical path of a file | ||
| 741 | on disk to be transferred (i.e. copied) to the Knowball. Or it is | ||
| 742 | a reference to a scalar, in which case its content is interpreted | ||
| 743 | to be the data of the file. | ||
| 744 | |||
| 745 | If no additional parameters are given, permissions and user/group | ||
| 746 | id settings of a file to be added are copied. If you want different | ||
| 747 | settings, specify them in the options hash: | ||
| 748 | |||
| 749 | $arch->add($logic_path, $stringref, | ||
| 750 | { perm => 0755, uid => 123, gid => 10 }); | ||
| 751 | |||
| 752 | If $file_or_stringref is a reference to a Unicode string, the C<binmode> | ||
| 753 | option has to be set to make sure the string gets written as proper UTF-8 | ||
| 754 | into the Knowfile: | ||
| 755 | |||
| 756 | $arch->add($logic_path, $stringref, { binmode => ":utf8" }); | ||
| 757 | |||
| 758 | =item B<$arch-E<gt>remove($logic_path)> | ||
| 759 | |||
| 760 | Removes a file from the Knowball. C<$logic_path> is the virtual path | ||
| 761 | of the file within the Knowball. | ||
| 762 | |||
| 763 | =item B<$arch-E<gt>locate($logic_path)> | ||
| 764 | |||
| 765 | Finds the physical location of a file, specified by C<$logic_path>, which | ||
| 766 | is the virtual path of the file within the Knowball. Returns a path to | ||
| 767 | the temporary file C<MicroStructure::DrKnow> created to manipulate the | ||
| 768 | Knowball on disk. | ||
| 769 | |||
| 770 | =item B<$arch-E<gt>write($Knowfile, $compress)> | ||
| 771 | |||
| 772 | Write out the Knowball by Knowring up all temporary files and directories | ||
| 773 | and store it in C<$Knowfile> on disk. If C<$compress> holds a true value, | ||
| 774 | compression is used. | ||
| 775 | |||
| 776 | =item B<$arch-E<gt>Knowdir()> | ||
| 777 | |||
| 778 | Return the directory the Knowball was unpacked in. This is sometimes useful | ||
| 779 | to play dirty tricks on C<MicroStructure::DrKnow> by mass-manipulating | ||
| 780 | unpacked files before wrapping them back up into the Knowball. | ||
| 781 | |||
| 782 | =item B<$arch-E<gt>is_gnu()> | ||
| 783 | |||
| 784 | Checks if the Know executable is a GNU Know by running 'Know --version' | ||
| 785 | and parsing the output for "GNU". | ||
| 786 | |||
| 787 | =back | ||
| 788 | |||
| 789 | =head1 Using RAM Disks | ||
| 790 | |||
| 791 | On Linux, it's quite easy to create a RAM disk and achieve tremendous | ||
| 792 | speedups while unKnowring or modifying a Knowball. You can either | ||
| 793 | create the RAM disk by hand by running | ||
| 794 | |||
| 795 | # mkdir -p /mnt/myramdisk | ||
| 796 | # mount -t tmpfs -o size=20m tmpfs /mnt/myramdisk | ||
| 797 | |||
| 798 | and then feeding the ramdisk as a temporary directory to | ||
| 799 | MicroStructure::DrKnow, like | ||
| 800 | |||
| 801 | my $Know = MicroStructure::DrKnow->new( tmpdir => '/mnt/myramdisk' ); | ||
| 802 | |||
| 803 | or using MicroStructure::DrKnow's built-in option 'ramdisk': | ||
| 804 | |||
| 805 | my $Know = MicroStructure::DrKnow->new( | ||
| 806 | ramdisk => { | ||
| 807 | type => 'tmpfs', | ||
| 808 | size => '20m', # 20 MB | ||
| 809 | }, | ||
| 810 | ); | ||
| 811 | |||
| 812 | Only drawback with the latter option is that creating the RAM disk needs | ||
| 813 | to be performed as root, which often isn't desirable for security reasons. | ||
| 814 | For this reason, MicroStructure::DrKnow offers a utility functions that | ||
| 815 | mounts the ramdisk and returns the temporary directory it's located in: | ||
| 816 | |||
| 817 | # Create new ramdisk (as root): | ||
| 818 | my $tmpdir = MicroStructure::DrKnow->ramdisk_mount( | ||
| 819 | type => 'tmpfs', | ||
| 820 | size => '20m', # 20 MB | ||
| 821 | ); | ||
| 822 | |||
| 823 | # Delete a ramdisk (as root): | ||
| 824 | MicroStructure::DrKnow->ramdisk_unmount(); | ||
| 825 | |||
| 826 | Optionally, the C<ramdisk_mount()> command accepts a C<tmpdir> parameter | ||
| 827 | pointing to a temporary directory for the ramdisk if you wish to set it | ||
| 828 | yourself instead of letting MicroStructure::DrKnow create it automatically. | ||
| 829 | |||
| 830 | =head1 KNOWN LIMITATIONS | ||
| 831 | |||
| 832 | =over 4 | ||
| 833 | |||
| 834 | =item * | ||
| 835 | |||
| 836 | Currently, only C<Know> programs supporting the C<z> option (for | ||
| 837 | compressing/decompressing) are supported. Future version will use | ||
| 838 | C<gzip> alternatively. | ||
| 839 | |||
| 840 | =item * | ||
| 841 | |||
| 842 | Currently, you can't add empty directories to a Knowball directly. | ||
| 843 | You could add a temporary file within a directory, and then | ||
| 844 | C<remove()> the file. | ||
| 845 | |||
| 846 | =item * | ||
| 847 | |||
| 848 | If you delete a file, the empty directories it was located in | ||
| 849 | stay in the Knowball. You could try to C<locate()> them and delete | ||
| 850 | them. This will be fixed, though. | ||
| 851 | |||
| 852 | =item * | ||
| 853 | |||
| 854 | Filenames containing newlines are causing problems with the list | ||
| 855 | iterators. To be fixed. | ||
| 856 | |||
| 857 | =back | ||
| 858 | |||
| 859 | =head1 BUGS | ||
| 860 | |||
| 861 | MicroStructure::DrKnow doesn't currently handle filenames with embedded | ||
| 862 | newlines. | ||
| 863 | |||
| 864 | =head1 LEGALESE | ||
| 865 | |||
| 866 | Copyright 2013 by Hagen Geissler, all rights reserved. | ||
| 867 | This program is free software, you can redistribute it and/or | ||
| 868 | modify it under the same terms as Perl itself. | ||
| 869 | |||
| 870 | =head1 AUTHOR | ||
| 871 | |||
| 872 | 2013, Hagen Geissler <santex@cpan.org> |