source: ocsinventory-agent/trunk/fuentes/lib/Ocsinventory/Agent/AccountConfig.pm @ 468

Last change on this file since 468 was 468, checked in by mabarracus, 4 years ago

Copyt trusty code

File size: 1.9 KB
Line 
1package Ocsinventory::Agent::AccountConfig;
2use strict;
3use warnings;
4
5# AccountConfig read and write the setting for the client given by the server
6# This file will be overwrite and is not designed to be changed by the user
7
8# DESPITE ITS NAME, ACCOUNTCONFIG IS NOT A CONFIG FILE!
9
10sub new {
11    my (undef,$params) = @_;
12
13    my $self = {};
14    bless $self;
15
16    $self->{config} = $params->{config};
17    my $logger = $self->{logger} = $params->{logger};
18
19    # Configuration reading
20    $self->{xml} = {};
21
22    if ($self->{config}->{accountconfig}) {
23        if (! -f $self->{config}->{accountconfig}) {
24            $logger->debug ('accountconfig file: `'. $self->{config}->{accountconfig}.
25                " doesn't exist. I create an empty one");
26            $self->write();
27        } else {
28            eval {
29                $self->{xml} = XML::Simple::XMLin(
30                    $self->{config}->{accountconfig},
31                    SuppressEmpty => undef
32                );
33            };
34        }
35    }
36
37    $self;
38}
39
40sub get {
41    my ($self, $name) = @_;
42
43    my $logger = $self->{logger};
44
45    return $self->{xml}->{$name} if $name;
46    return $self->{xml};
47}
48
49sub set {
50    my ($self, $name, $value) = @_;
51
52    my $logger = $self->{logger};
53
54    $self->{xml}->{$name} = $value;
55    $self->write(); # save the change
56}
57
58
59sub write {
60    my ($self, $args) = @_;
61
62    my $logger = $self->{logger};
63
64    return unless $self->{config}->{accountconfig};
65    my $xml = XML::Simple::XMLout( $self->{xml} , RootName => 'CONF',
66        NoAttr => 1 );
67
68    my $fault;
69    if (!open CONF, ">".$self->{config}->{accountconfig}) {
70
71        $fault = 1;
72
73    } else {
74
75        print CONF $xml;
76        $fault = 1 if (!close CONF);
77
78    }
79
80    if (!$fault) {
81        $logger->debug ("ocsinv.conf updated successfully");
82    } else {
83
84        $logger->error ("Can't save setting change in `".$self->{config}->{accountconfig}."'");
85    }
86}
87
881;
Note: See TracBrowser for help on using the repository browser.