* Changes in preparation for 3.2.0beta1...
[BackupPC.git] / lib / BackupPC / Attrib.pm
index f5c497e..2b4b36a 100644 (file)
@@ -12,7 +12,7 @@
 #   Craig Barratt  <cbarratt@users.sourceforge.net>
 #
 # COPYRIGHT
 #   Craig Barratt  <cbarratt@users.sourceforge.net>
 #
 # COPYRIGHT
-#   Copyright (C) 2001  Craig Barratt
+#   Copyright (C) 2001-2009  Craig Barratt
 #
 #   This program is free software; you can redistribute it and/or modify
 #   it under the terms of the GNU General Public License as published by
 #
 #   This program is free software; you can redistribute it and/or modify
 #   it under the terms of the GNU General Public License as published by
@@ -30,7 +30,7 @@
 #
 #========================================================================
 #
 #
 #========================================================================
 #
-# Version 1.5.0, released 2 Aug 2002.
+# Version 3.2.0beta0, released 5 April 2009.
 #
 # See http://backuppc.sourceforge.net.
 #
 #
 # See http://backuppc.sourceforge.net.
 #
@@ -43,6 +43,7 @@ use strict;
 use Carp;
 use File::Path;
 use BackupPC::FileZIO;
 use Carp;
 use File::Path;
 use BackupPC::FileZIO;
+use Encode qw/from_to/;
 require Exporter;
 
 use vars qw( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS );
 require Exporter;
 
 use vars qw( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS );
@@ -59,6 +60,7 @@ use constant BPC_FTYPE_DIR      => 5;
 use constant BPC_FTYPE_FIFO     => 6;
 use constant BPC_FTYPE_SOCKET   => 8;
 use constant BPC_FTYPE_UNKNOWN  => 9;
 use constant BPC_FTYPE_FIFO     => 6;
 use constant BPC_FTYPE_SOCKET   => 8;
 use constant BPC_FTYPE_UNKNOWN  => 9;
+use constant BPC_FTYPE_DELETED  => 10;
 
 my @FILE_TYPES = qw(
                   BPC_FTYPE_FILE
 
 my @FILE_TYPES = qw(
                   BPC_FTYPE_FILE
@@ -70,6 +72,7 @@ my @FILE_TYPES = qw(
                   BPC_FTYPE_FIFO
                   BPC_FTYPE_SOCKET
                   BPC_FTYPE_UNKNOWN
                   BPC_FTYPE_FIFO
                   BPC_FTYPE_SOCKET
                   BPC_FTYPE_UNKNOWN
+                 BPC_FTYPE_DELETED
              );
 
 #
              );
 
 #
@@ -85,6 +88,8 @@ my @FileType2Text = (
     "fifo",
     "?",
     "socket",
     "fifo",
     "?",
     "socket",
+    "?",
+    "deleted",
 );
 
 #
 );
 
 #
@@ -139,13 +144,18 @@ sub set
 {
     my($a, $fileName, $attrib) = @_;
 
 {
     my($a, $fileName, $attrib) = @_;
 
-    $a->{files}{$fileName} = $attrib;
+    if ( !defined($attrib) ) {
+       delete($a->{files}{$fileName});
+    } else {
+       $a->{files}{$fileName} = $attrib;
+    }
 }
 
 sub get
 {
     my($a, $fileName) = @_;
 }
 
 sub get
 {
     my($a, $fileName) = @_;
-    return $a->{files}{$fileName};
+    return $a->{files}{$fileName} if ( defined($fileName) );
+    return $a->{files};
 }
 
 sub fileType2Text
 }
 
 sub fileType2Text
@@ -189,6 +199,8 @@ sub read
     my($data);
 
     $file = $a->fileName($dir, $file);
     my($data);
 
     $file = $a->fileName($dir, $file);
+    from_to($file, "utf8", $a->{charsetLegacy})
+                    if ( $a->{charsetLegacy} ne "" );
     my $fd = BackupPC::FileZIO->open($file, 0, $a->{compress});
     if ( !$fd ) {
        $a->{_errStr} = "Can't open $file";
     my $fd = BackupPC::FileZIO->open($file, 0, $a->{compress});
     if ( !$fd ) {
        $a->{_errStr} = "Can't open $file";
@@ -202,9 +214,9 @@ sub read
     }
     (my $magic, $data) = unpack("N a*", $data);
     if ( $magic != $a->{type} ) {
     }
     (my $magic, $data) = unpack("N a*", $data);
     if ( $magic != $a->{type} ) {
-       $a->{_errStr} = sprintf("Wrong magic number in $file"
+       $a->{_errStr} = sprintf("Wrong magic number in %s"
                                . " (got 0x%x, expected 0x%x)",
                                . " (got 0x%x, expected 0x%x)",
-                                   $magic, $a->{type});
+                                   $file, $magic, $a->{type});
        $fd->close;
        return;
     }
        $fd->close;
        return;
     }
@@ -231,17 +243,27 @@ sub read
            }
        }
        (my $fileName, $data) = unpack("a$len a*", $data);
            }
        }
        (my $fileName, $data) = unpack("a$len a*", $data);
+
+        from_to($fileName, $a->{charsetLegacy}, "utf8")
+                        if ( $a->{charsetLegacy} ne "" );
        my $nFldsW = @FldsUnixW;
        my $nFldsN = @FldsUnixN;
        if ( length($data) < 5 * $nFldsW + 4 * $nFldsN ) {
            $fd->read(\$newData, 65536);
            $data .= $newData;
        }
        my $nFldsW = @FldsUnixW;
        my $nFldsN = @FldsUnixN;
        if ( length($data) < 5 * $nFldsW + 4 * $nFldsN ) {
            $fd->read(\$newData, 65536);
            $data .= $newData;
        }
-       (
-            @{$a->{files}{$fileName}}{@FldsUnixW},
-            @{$a->{files}{$fileName}}{@FldsUnixN},
-            $data
-        ) = unpack("w$nFldsW N$nFldsN a*", $data);
+        eval {
+           (
+               @{$a->{files}{$fileName}}{@FldsUnixW},
+               @{$a->{files}{$fileName}}{@FldsUnixN},
+               $data
+            ) = unpack("w$nFldsW N$nFldsN a*", $data);
+        };
+        if ( $@ ) {
+            $a->{_errStr} = "unpack: Can't read attributes for $fileName from $file ($@)";
+            $fd->close;
+            return;
+        }
         if ( $a->{files}{$fileName}{$FldsUnixN[-1]} eq "" ) {
             $a->{_errStr} = "Can't read attributes for $fileName"
                           . " from $file";
         if ( $a->{files}{$fileName}{$FldsUnixN[-1]} eq "" ) {
             $a->{_errStr} = "Can't read attributes for $fileName"
                           . " from $file";
@@ -289,7 +311,13 @@ sub write
     my($data) = $a->writeData;
 
     $file = $a->fileName($dir, $file);
     my($data) = $a->writeData;
 
     $file = $a->fileName($dir, $file);
-    mkpath($dir, 0, 0777) if ( !-d $dir );
+    if ( !-d $dir ) {
+        eval { mkpath($dir, 0, 0777) };
+        if ( $@ ) {
+            $a->{_errStr} = "Can't create directory $dir";
+            return;
+        }
+    }
     my $fd = BackupPC::FileZIO->open($file, 1, $a->{compress});
     if ( !$fd ) {
        $a->{_errStr} = "Can't open/write to $file";
     my $fd = BackupPC::FileZIO->open($file, 1, $a->{compress});
     if ( !$fd ) {
        $a->{_errStr} = "Can't open/write to $file";