<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># vim:cindent:ts=2:sw=2:et:fdm=marker:cms=\ #\ %s
#
# $Id: DB.pm 223 2011-02-28 23:34:22Z robert $
#

package Debian::DocBase::DB;

use strict;
use warnings;

use Debian::DocBase::Common;
use Debian::DocBase::Utils;
use Debian::DocBase::Gettext;
use YAML::Tiny;

my $filesdb  = undef;
my $statusdb = undef;

sub new { # {{{
    my $class   = shift;
    my $dbfile  = shift;
    my $enckey  = shift;
    my $self    = {
        YAML    =&gt; YAML::Tiny-&gt;new,
        DB      =&gt; {},
        FILE    =&gt; $dbfile,
        ENCKEY  =&gt; $enckey,
        CHANGED =&gt; 0
    };
    bless($self, $class);
    $self-&gt;_Init();
    return $self;
} # }}}

sub _Init() { #  {{{
  my $self = shift;
#  # read-only access for `install-docs --status or --dump-db' run as non-root user
#  my $readonly = $&gt; != 0;
  my $file = $self-&gt;{'FILE'};
  if (-f $file)
  {
    eval { $self-&gt;{'YAML'} = YAML::Tiny-&gt;read ($file) } 
      or Fatal($ERR_DATABASE, _g("Cannot read file `%s': %s."), $file, $@);
  }
  else
  {
    $self-&gt;{'YAML'}-&gt;[0] = {};
  }
  $self-&gt;{'DB'} = \%{$self-&gt;{'YAML'}-&gt;[0]};
  
} # }}} 

 sub PutData($$$) { # {{{
    my ($self, $key, $data)  = @_;
    $self-&gt;{'DB'}-&gt;{$self-&gt;EncodeKey($key)}   = $data;
    $self-&gt;{'CHANGED'} = 1;
} # }}}

sub GetData($$) { # {{{
  my ($self, $key) = @_;
  return $self-&gt;{'DB'}-&gt;{$self-&gt;EncodeKey($key)}
} # }}}

sub GetDBKeys() { # {{{
  my $self = shift;
  my @keys = grep { ! m|^/internal/| } keys %{$self-&gt;{'DB'}};   
  map { $_ = $self-&gt;DecodeKey($_) } @keys if $self-&gt;{'ENCKEY'};
  return @keys;
} # }}} 

sub _SaveDB() { # {{{ 
  my $self = shift;
  my $file = $self-&gt;{'FILE'};
  Debug(_g("Saving `%s' (%d)."), $file, $self-&gt;{'CHANGED'});
  return unless $self-&gt;{'CHANGED'};
  my $readonly = $&gt; != 0;
  Fatal($ERR_PROCESSING, _g("Needs to be root for this operation.")) if $readonly;
  (my $newfile = $file) =~ s/^[^\.]+/$&amp;-new/g;
  (my $oldfile = $file) =~ s/^[^\.]+/$&amp;-old/g;


  eval { $self-&gt;{'YAML'}-&gt;write($newfile) }  
    or Fatal($ERR_DATABASE, _g("Cannot save file `%s': %s."), $newfile, $@);
  unlink $oldfile if -f $oldfile;
  rename $file, $oldfile if -f $file;
  rename $newfile, $file 
    or Fatal($ERR_DATABASE, _g("Cannot rename file `%s' to `%s': %s."), $newfile, $file, $!);
  $self-&gt;{'CHANGED'} = 0;
} # }}}

sub RemoveData($$) # {{{
{
  my ($self, $key) = @_;
  delete $self-&gt;{'DB'}-&gt;{$self-&gt;EncodeKey($key)};
  $self-&gt;{'CHANGED'} = 1;
} # }}}

sub Exists($) { # {{{
  my ($self, $key) = @_;
  my $data = $self-&gt;{'DB'}-&gt;{$self-&gt;EncodeKey($key)};
  return $data &amp;&amp; %{$data};
} # }}}

sub DumpDB($) { # {{{
  my $self = shift;
  my $db   = $self-&gt;{'DB'};

  Inform(_g("Contents of file `%s':")."\n", $self-&gt;{'FILE'});
  print STDOUT YAML::Tiny::Dump($db);
} # }}}

sub EncodeKey($$) { # {{{
  my ($self, $key) = @_;
  return $key unless $self-&gt;{'ENCKEY'};
  $key =~ s/\/+/\//go;
  $key =~ s/^~/~~/o;
  $key =~ s/^$CONTROL_DIR/~1/o;
  $key =~ s/^$LOCAL_CONTROL_DIR/~2/o;
  return $key;
 } # }}}

 sub DecodeKey($$) { # {{{ 
  my ($self, $key) = @_;
  return $key unless $self-&gt;{'ENCKEY'};

  $key =~ s/^~1/$CONTROL_DIR/o;
  $key =~ s/^~2/$LOCAL_CONTROL_DIR/o;
  $key =~ s/^~~/~/o;
  return $key;
} # }}}

### STATIC FUNCTIONS
sub GetFilesDB() { # {{{
  $filesdb     = Debian::DocBase::DB-&gt;new($DB_FILES, 1) unless $filesdb;
  return $filesdb;
} # }}} 

sub GetStatusDB() { # {{{
  $statusdb     = Debian::DocBase::DB-&gt;new($DB_STATUS, 0) unless $statusdb;
  return $statusdb;
} # }}} 

sub SaveDatabases()
{
  IgnoreSignals();
  $statusdb-&gt;_SaveDB() if $statusdb;
  $filesdb-&gt;_SaveDB()  if $filesdb;
  RestoreSignals();
}
1
</pre></body></html>