BackupPC-users

Re: [BackupPC-users] Suggested improvements to MakeFileLink routine in Lib.pm

2011-01-23 12:28:04
Subject: Re: [BackupPC-users] Suggested improvements to MakeFileLink routine in Lib.pm
From: "Jeffrey J. Kosowsky" <backuppc AT kosowsky DOT org>
To: "General list for user discussion, questions and support" <backuppc-users AT lists.sourceforge DOT net>
Date: Sun, 23 Jan 2011 12:25:26 -0500
Jeffrey J. Kosowsky wrote at about 01:09:10 -0500 on Sunday, January 23, 2011:
 > It seems there are some extra tests and computation that are not
 > necessary. I will show my suggested changes with +/- style diff
 > Am I missing something?
 > 
 > 
 >  sub MakeFileLink
 >  {
 >      my($bpc, $name, $d, $newFile, $compress) = @_;
 >      my($i, $rawFile);
 >  
 >      return -1 if ( !-f $name );
 > +     return -2 if ( !defined($rawFile = $bpc->MD52Path($d, $compress)) );
 >      for ( $i = -1 ; ; $i++ ) {
 > -        return -2 if ( !defined($rawFile = $bpc->MD52Path($d, $compress)) );
 >          $rawFile .= "_$i" if ( $i >= 0 );
 >          if ( -f $rawFile ) {
 >              if ( (stat(_))[3] < $bpc->{Conf}{HardLinkMax}
 >                      && !compare($name, $rawFile) ) {
 >                  unlink($name);
 >                  return -3 if ( !link($rawFile, $name) );
 >                  return 1;
 >              }
 > -         } elsif ( $newFile && -f $name && (stat($name))[3] == 1 ) {
 > +         } elsif ( $newFile && -f $name && (stat($_))[3] == 1 ) {
 > -             my($newDir);
 > -             ($newDir = $rawFile) =~ s{(.*)/.*}{$1};
 > +                     $rawfile =~ m{(.*)/}
 > -             mkpath($newDir, 0, 0777) if ( !-d $newDir );
 > +             mkpath($1, 0, 0777) if ( !-d $1 );
 >              return -4 if ( !link($name, $rawFile) );
 >              return 2;
 >          } else {
 >              return 0;
 >          }
 >      }
 >  }
 > 

One more improvement suggestion...
Since the file is unlinked before making the new link there is a
danger that you delete the file but then fail to make the link
resulting in data loss.

A suggested solution would be to rename first so the file can be
restored in case linking fails:

             if ( (stat(_))[3] < $bpc->{Conf}{HardLinkMax}
                     && !$compare->($name, $rawFile) ) {
                                 my $tempname = mktemp("$name.XXXXXXXXXXXXXXX");
                                 return -5 unless rename($name, $tempname);
                                 unless(link($rawFile, $name)) {
                                         rename($tempname, $name); #Restore
                                         return -3;
                                 }
                 unlink($tempname);
                 return 1;
             }

------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires 
February 28th, so secure your free ArcSight Logger TODAY! 
http://p.sf.net/sfu/arcsight-sfd2d
_______________________________________________
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>