BackupPC-users

Re: [BackupPC-users] *BUMP* *BUMP* Re: BackupPC perl code hacking question... (Craig any chance you might have a suggestion?)

2011-07-19 21:31:51
Subject: Re: [BackupPC-users] *BUMP* *BUMP* Re: BackupPC perl code hacking question... (Craig any chance you might have a suggestion?)
From: Holger Parplies <wbppc AT parplies DOT de>
To: "Jeffrey J. Kosowsky" <backuppc AT kosowsky DOT org>
Date: Wed, 20 Jul 2011 03:29:00 +0200
Hi,

sorry for not replying earlier. In case you're still wondering (otherwise for
the archives) ...

Jeffrey J. Kosowsky wrote on 2011-02-07 14:15:05 -0500 [[BackupPC-users] *BUMP* 
*BUMP* Re: BackupPC perl code hacking question... (Craig any chance you might 
have a suggestion?)]:
> Let me rewrite my earlier posting to be more clear so maybe someone
> can help me.

Well, let's rearrange your message so it makes sense (did I ever mention that
I don't like top posting? ;-).

>  > Jeffrey J. Kosowsky wrote at about 12:53:28 -0500 on Monday, December 13, 
> 2010:
>  >  > For reasons I can explain later, I am trying to set
>  >  > $Conf{RsyncdPasswd} in the main routine of BackupPC_dump (I am
>  >  > actually trying to do something a bit more complex but this is easier
>  >  > to understand).
>  >  > 
>  >  > Now since %Conf = $bpc->Conf(),

You are aware that this is a hash copy operation, right?

>  >  > I would have thought that for example
>  >  > setting $Conf{RsyncPasswd} = "mypasswd" would then be pushed down to
>  >  > all the routines called directly or indirectly from BackupPC_dump.

Well, it is as long as they use the "my %Conf" from BackupPC_dump and not
$bpc->Conf(). You modified the copy, not the original hash in the
BackupPC::Lib object.

>  >  > However, in Rsync.pm where the value of $Conf{RsyncPasswd} is actually
>  >  > used, the value remains at ''.

Yes, because Rsync.pm gets a reference to the unmodified BackupPC::Lib object's
hash (BackupPC::Xfer::Protocol, line 59, sub new, "conf => $bpc->{Conf}").

>  >  > (Of course setting the paramter the "normal" way within a config file
>  >  > works and shows up as set in Rsync.pm)

That is because the code in BackupPC::Lib that reads the config file saves the
values.

>  >  > I'm sure I must be missing something about how perl inherits and/or
>  >  > overwrites variables... but I am stumped here...

It's really simple. If you get a reference to a hash ($conf = \%conf), then
you modify the original, if you get a copy (%conf = %conf_orig), you don't.
What makes things complicated here is that you need to follow the code around
through various modules and subs, and that each copy of %conf is named the
same :-).

> Here is a simplified version of my actual command
> 
> $Conf{DumpPreUserCmd} = "&{sub {\$args[1]{RsyncdPasswd} = `ssh
> -x mybackuypclient get_rsyncd_secret`}}";
> 
> This uses the fact that $args[1]=$Conf

Actually, it's \%Conf (a reference, not a copy), so modifying *BackupPC_dump's
copy* works. $Conf would be a scalar, which might coincidentally contain a
reference to a hash. Using an element of a hash is "$Conf{Foo}", using an
element of a hash a scalar is pointing to is "$Conf->{Foo}". You might even
have both visible at the same time, but only if you are either a bad
programmer or enjoy confusing people.

        my $Conf = $bpc->Conf();
        my %Conf = (XferMethod => "snailmail");
        print $Conf{XferMethod}, "\n";      # probably prints "rsync"
        print $Conf->{XferMethod}, "\n";    # prints "snailmail"

> So, that \$args[1]{RsyncdPasswd} is equivalent to

... a syntax error? ;-)

$args [1] should be a reference to BackupPC_dump's %Conf, and 
$args [1] -> {RsyncdPasswd} should reference the corresponding entry (as an
lvalue, so you can assign to it). "->" between braces "[]"/"{}" is implied and
may be left out. I'm not sure how the reference operator binds in your example.
As it seems to work, let's ignore it for now.

> $Conf{RsyncdPasswd}.

... in BackupPC_dump (because that is what was passed in).

> [...]
> So, my question is is there any way to dynamically set Conf parameters
> along the lines I am trying to do?

Well, you'd have to modify the original hash.

        $bpc->{Conf}->{RsyncdPasswd} = `ssh ...`;

That is not strictly legal, but it should work. Note that this change would
*not* propagate to any copies previously made (like the "my %Conf" in
BackupPC_dump). Using references to $bpc->{Conf} everywhere instead of copies
would probably make things much easier, but on the downside, it would mean you
can't make local modifications to $Conf->{...} that are not meant to propagate
to the rest of the code (or, put differently, you could more easily
accidentally clobber BackupPC::Lib's state). You'll notice that
BackupPC::Xfer::Protocol (as quoted above) actually *does* use a reference.

Hope that helps.

Regards,
Holger

------------------------------------------------------------------------------
10 Tips for Better Web Security
Learn 10 ways to better secure your business today. Topics covered include:
Web security, SSL, hacker attacks & Denial of Service (DoS), private keys,
security Microsoft Exchange, secure Instant Messaging, and much more.
http://www.accelacomm.com/jaw/sfnl/114/51426210/
_______________________________________________
BackupPC-users mailing list
BackupPC-users AT lists.sourceforge DOT net
List:    https://lists.sourceforge.net/lists/listinfo/backuppc-users
Wiki:    http://backuppc.wiki.sourceforge.net
Project: http://backuppc.sourceforge.net/

<Prev in Thread] Current Thread [Next in Thread>
  • Re: [BackupPC-users] *BUMP* *BUMP* Re: BackupPC perl code hacking question... (Craig any chance you might have a suggestion?), Holger Parplies <=