]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/DirPlugin.pm
rbd: add fallback default poolname 'rbd' to status
[pve-storage.git] / PVE / Storage / DirPlugin.pm
index 2cb55b832f23c0363e2651ece4d46aa589d25332..53eb6423c32320691a833231538c0dd1b942a8d9 100644 (file)
@@ -4,7 +4,9 @@ use strict;
 use warnings;
 
 use Cwd;
+use Encode qw(decode encode);
 use File::Path;
+use IO::File;
 use POSIX;
 
 use PVE::Storage::Plugin;
@@ -57,6 +59,7 @@ sub options {
        disable => { optional => 1 },
        maxfiles => { optional => 1 },
        'prune-backups' => { optional => 1 },
+       'max-protected-backups' => { optional => 1 },
        content => { optional => 1 },
        format => { optional => 1 },
        mkdir => { optional => 1 },
@@ -102,7 +105,10 @@ sub get_volume_notes {
     my $path = $class->filesystem_path($scfg, $volname);
     $path .= $class->SUPER::NOTES_EXT;
 
-    return PVE::Tools::file_get_contents($path) if -f $path;
+    if (-f $path) {
+       my $data = PVE::Tools::file_get_contents($path);
+       return eval { decode('UTF-8', $data, 1) } // $data;
+    }
 
     return '';
 }
@@ -119,7 +125,8 @@ sub update_volume_notes {
     $path .= $class->SUPER::NOTES_EXT;
 
     if (defined($notes) && $notes ne '') {
-       PVE::Tools::file_set_contents($path, $notes);
+       my $encoded = encode('UTF-8', $notes);
+       PVE::Tools::file_set_contents($path, $encoded);
     } else {
        unlink $path or $! == ENOENT or die "could not delete notes - $!\n";
     }
@@ -133,6 +140,14 @@ sub get_volume_attribute {
        return $class->get_volume_notes($scfg, $storeid, $volname);
     }
 
+    my ($vtype) = $class->parse_volname($volname);
+    return if $vtype ne 'backup';
+
+    if ($attribute eq 'protected') {
+       my $path = $class->filesystem_path($scfg, $volname);
+       return -e PVE::Storage::protection_file_path($path) ? 1 : 0;
+    }
+
     return;
 }
 
@@ -143,6 +158,27 @@ sub update_volume_attribute {
        return $class->update_volume_notes($scfg, $storeid, $volname, $value);
     }
 
+    my ($vtype) = $class->parse_volname($volname);
+    die "only backups support attribute '$attribute'\n" if $vtype ne 'backup';
+
+    if ($attribute eq 'protected') {
+       my $path = $class->filesystem_path($scfg, $volname);
+       my $protection_path = PVE::Storage::protection_file_path($path);
+
+       return if !((-e $protection_path) xor $value); # protection status already correct
+
+       if ($value) {
+           my $fh = IO::File->new($protection_path, O_CREAT, 0644)
+               or die "unable to create protection file '$protection_path' - $!\n";
+           close($fh);
+       } else {
+           unlink $protection_path or $! == ENOENT
+               or die "could not delete protection file '$protection_path' - $!\n";
+       }
+
+       return;
+    }
+
     die "attribute '$attribute' is not supported for storage type '$scfg->{type}'\n";
 }