/usr/share/perl5/Octopussy/Storage.pm is in octopussy 1.0.6-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | # $HeadURL$
# $Revision: 579 $
# $Date: 2012-06-24 02:11:57 +0100 (Sun, 24 Jun 2012) $
# $Author: sebthebert $
=head1 NAME
Octopussy::Storage - Octopussy Storage module
=cut
package Octopussy::Storage;
use strict;
use warnings;
use Readonly;
use List::MoreUtils qw(any firstval);
use AAT::Utils qw( ARRAY NOT_NULL );
use AAT::XML;
use Octopussy::Device;
use Octopussy::FS;
Readonly my $FILE_STORAGES => 'storages';
Readonly my $XML_ROOT => 'octopussy_storages';
=head1 FUNCTIONS
=head2 Add($conf_storage)
Add a new Storage
=cut
sub Add
{
my $conf_storage = shift;
my @storages = ();
my $file = Octopussy::FS::File($FILE_STORAGES);
my $conf = AAT::XML::Read($file);
if (any { $_->{s_id} eq $conf_storage->{s_id} } ARRAY($conf->{storage}))
{
return ('_MSG_STORAGE_ALREADY_EXISTS');
}
push @{$conf->{storage}}, $conf_storage;
AAT::XML::Write($file, $conf, $XML_ROOT);
return (undef);
}
=head2 Remove($storage)
Removes Storage '$storage'
=cut
sub Remove
{
my $storage = shift;
my $file = Octopussy::FS::File($FILE_STORAGES);
my $conf = AAT::XML::Read($file);
my @storages = grep { $_->{s_id} ne $storage } ARRAY($conf->{storage});
$conf->{storage} = \@storages;
AAT::XML::Write($file, $conf, $XML_ROOT);
return (undef);
}
=head2 Default()
=cut
sub Default
{
my $conf = AAT::XML::Read(Octopussy::FS::File($FILE_STORAGES));
return (undef) if (!defined $conf);
return (
{
incoming => $conf->{default_incoming},
unknown => $conf->{default_unknown},
known => $conf->{default_known}
}
);
}
=head2 Default_Set($conf_new)
=cut
sub Default_Set
{
my $conf_new = shift;
my $file = Octopussy::FS::File($FILE_STORAGES);
my $conf = AAT::XML::Read($file);
$conf->{default_incoming} = $conf_new->{incoming};
$conf->{default_unknown} = $conf_new->{unknown};
$conf->{default_known} = $conf_new->{known};
AAT::XML::Write($file, $conf, $XML_ROOT);
return ($file);
}
=head2 List()
Get list of storages
Returns:
@storages - Array of storages names
=cut
sub List
{
my @storages =
AAT::XML::File_Array_Values(Octopussy::FS::File($FILE_STORAGES),
'storage', 's_id');
return (@storages);
}
=head2 Configuration($storage)
Get the configuration for the storage '$storage'
Parameters:
$storage - Name of the storage
Returns:
\%conf - Hashref of the storage configuration
=cut
sub Configuration
{
my $storage = shift;
my $conf = AAT::XML::Read(Octopussy::FS::File($FILE_STORAGES));
return (
{name => 'DEFAULT', directory => Octopussy::FS::Directory('data_logs')})
if ($storage eq 'DEFAULT');
foreach my $s (ARRAY($conf->{storage}))
{
return ($s) if ($s->{s_id} eq $storage);
}
return (undef);
}
=head2 Configurations($sort)
Get the configuration for all storages
Parameters:
$sort - selected field to sort configurations
Returns:
@configurations - Array of Hashref storage configurations
=cut
sub Configurations
{
my $sort = shift || 's_id';
my (@configurations, @sorted_configurations) = ((), ());
my @storages = List();
my $dir_default = Octopussy::FS::Directory('data_logs');
push @sorted_configurations, {s_id => 'DEFAULT', directory => $dir_default};
foreach my $s (@storages)
{
my $conf = Configuration($s);
if (defined $conf->{s_id})
{
push @configurations, $conf;
}
}
foreach my $c (sort { $a->{$sort} cmp $b->{$sort} } @configurations)
{
push @sorted_configurations, $c;
}
return (@sorted_configurations);
}
=head2 Directory($storage)
Returns directory for Storage '$storage'
=cut
sub Directory
{
my $storage = shift;
return (undef) if (!defined $storage);
my $conf = AAT::XML::Read(Octopussy::FS::File($FILE_STORAGES));
return (Octopussy::FS::Directory('data_logs')) if ($storage eq 'DEFAULT');
my $dir = firstval { $_->{s_id} eq $storage } ARRAY($conf->{storage});
return ($dir->{directory});
}
=head2 Directory_Service($device, $service)
Returns directory for Device '$device' Service '$service' Logs
=cut
sub Directory_Service
{
my ($device, $service) = @_;
my $storage = Default();
my $dconf = Octopussy::Device::Configuration($device);
my $dir =
Directory($dconf->{"storage_$service"})
|| Directory($dconf->{'storage_known'})
|| Directory($storage->{known});
return ($dir);
}
=head2 Directory_Incoming($device)
Returns directory for Device '$device' Incoming Logs
=cut
sub Directory_Incoming
{
my $device = shift;
my $storage = Default();
my $dconf = Octopussy::Device::Configuration($device);
my $dir = Directory($dconf->{storage_incoming})
|| Directory($storage->{incoming});
return ($dir);
}
=head2 Directory_Unknown($device)
Returns directory for Device '$device' Unknown Logs
=cut
sub Directory_Unknown
{
my $device = shift;
my $storage = Default();
my $dconf = Octopussy::Device::Configuration($device);
my $dir = Directory($dconf->{storage_unknown})
|| Directory($storage->{unknown});
return ($dir);
}
=head2 Valid_Name($name)
Checks that '$name' is valid for a Storage name
=cut
sub Valid_Name
{
my $name = shift;
return (1) if ((NOT_NULL($name)) && ($name =~ /^[a-z][a-z0-9_-]*$/i));
return (0);
}
1;
=head1 AUTHOR
Sebastien Thebert <octo.devel@gmail.com>
=cut
|