[pve-devel] r5491 - in pve-common/trunk/data: . PVE

svn-commits at proxmox.com svn-commits at proxmox.com
Tue Feb 8 10:53:12 CET 2011


Author: dietmar
Date: 2011-02-08 10:53:11 +0100 (Tue, 08 Feb 2011)
New Revision: 5491

Modified:
   pve-common/trunk/data/ChangeLog
   pve-common/trunk/data/PVE/INotify.pm
   pve-common/trunk/data/PVE/JSONSchema.pm
Log:


Modified: pve-common/trunk/data/ChangeLog
===================================================================
--- pve-common/trunk/data/ChangeLog	2011-02-08 04:48:40 UTC (rev 5490)
+++ pve-common/trunk/data/ChangeLog	2011-02-08 09:53:11 UTC (rev 5491)
@@ -1,3 +1,11 @@
+2011-02-08  Proxmox Support Team  <support at proxmox.com>
+
+	* PVE/INotify.pm (update_file): use PVE::Tools, changed interface
+	(update_etc_resolv_conf): do not touch other options (like
+	'sortlist' and 'options'),
+
+	* PVE/JSONSchema.pm (pve_verify_ipv4): register IPv4 format.
+
 2011-02-02  Proxmox Support Team  <support at proxmox.com>
 
 	* PVE/Tools.pm (next_vnc_port): moved from qemu-server

Modified: pve-common/trunk/data/PVE/INotify.pm
===================================================================
--- pve-common/trunk/data/PVE/INotify.pm	2011-02-08 04:48:40 UTC (rev 5490)
+++ pve-common/trunk/data/PVE/INotify.pm	2011-02-08 09:53:11 UTC (rev 5491)
@@ -10,6 +10,7 @@
 use File::Basename;
 use Fcntl qw(:DEFAULT :flock);
 use PVE::SafeSyslog;
+use PVE::Tools;
 use Storable qw(dclone);            
 use Linux::Inotify2;
 use base 'Exporter';
@@ -110,7 +111,7 @@
 	my $fh = IO::File->new($tmpname, O_WRONLY|O_CREAT, $perm);
 	die "unable to open file '$tmpname' - $!\n" if !$fh;
 
-	$res = &$writer ($filename, $fh, $data);
+	$res = &$writer($filename, $fh, $data);
 
 	die "closing file '$tmpname' failed - $!\n" unless close $fh;
     };
@@ -152,40 +153,31 @@
 
     my $lkfn = "$filename.lock";
 
-    if (!open (FLCK, ">>$lkfn")) {
-	die "unable to open lock file '$lkfn' - $?";
-    }
+    my $timeout = 10;
 
-    if (!flock (FLCK, LOCK_EX)) {
-	close (FLCK);
-	die "unable to aquire lock for file '$lkfn' - $?";
-    }
+    my $fd;
 
-    my $newdata;
+    my $code = sub {
 
-    eval {
+	$fd = IO::File->new ($filename, "r");
+	
+	my $new = &$update($filename, $fd, $data, @args);
 
-	my $olddata = read_file ($filename);
-
-	if ($data) {
-	    my $res = &$update ($filename, $olddata, $data, @args);
-	    if (defined ($res)) {
-		$newdata = write_file ($filename, $res);
-	    } else {
-		$newdata = $olddata;
-	    }
+	if (defined($new)) {
+	    PVE::Tools::file_set_contents($filename, $new, $ccinfo->{perm});
 	} else {
-	    $newdata = $olddata;
+	    unlink $filename;
 	}
     };
 
+    PVE::Tools::lock_file($lkfn, $timeout, "update_file", $code);
     my $err = $@;
 
-    close (FLCK);
+    close($fd) if defined($fd);
 
     die $err if $err;
 
-    return $newdata;
+    return undef;
 }
 
 sub discard_changes {
@@ -264,7 +256,7 @@
 	$diff = ccache_compute_diff ($filename, $shadow);
     }
 
-    my $res = &$parser ($filename, $fd);
+    my $res = &$parser($filename, $fd);
 
     if (!$ccinfo->{nocache}) {
 	$ccinfo->{version} = $cver;
@@ -538,43 +530,50 @@
 
     my $res = {};
 
+    my $nscount = 0;
     while (my $line = <$fh>) {
 	chomp $line;
-	if ($line =~ m/^search\s+(\S+)\s*/) {
-	    $res->{search} = $1;
+	if ($line =~ m/^(search|domain)\s+(\S+)\s*/) {
+	    $res->{search} = $2;
 	} elsif ($line =~ m/^nameserver\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*/) {
-	    push @{$res->{nameservers}}, $1;
+	    $nscount++;
+	    if ($nscount <= 3) {
+		$res->{"dns$nscount"} = $1;
+	    }
 	}
     }
 
     return $res;
 }
 
-sub write_etc_resolv_conf {
-    my ($filename, $fh, $resolv) = @_;
+sub update_etc_resolv_conf {
+    my ($filename, $fh, $resolv, @args) = @_;
 
-    my $data = "search $resolv->{search}\n";
+    my $data = "";
 
-    my $written = {};
-    my $nslist = [];
+    $data = "search $resolv->{search}\n"
+	if $resolv->{search};
 
-    foreach my $ns (@{$resolv->{nameservers}}) {
-	if ($ns ne '0.0.0.0' && !$written->{$ns}) {
+    my $written = {};
+    foreach my $k ("dns1", "dns2", "dns3") {
+	my $ns = $resolv->{$k};
+	if ($ns && $ns ne '0.0.0.0' && !$written->{$ns}) {
 	    $written->{$ns} = 1;
 	    $data .= "nameserver $ns\n";
-	    push @$nslist, $ns;
 	}
     }
 
-    die "write failed: $!" unless print $fh $data;
-
-    $resolv->{nameservers} = $nslist;
-    return $resolv;
+    while (my $line = <$fh>) {
+	next if $line =~ m/^(search|domain|nameserver)\s+/;
+	$data .= $line
+    }
+    
+    return $data;
 }
 
 register_file ('resolvconf', "/etc/resolv.conf", 
-	       \&read_etc_resolv_conf, 
-	       \&write_etc_resolv_conf);
+	       \&read_etc_resolv_conf, undef, 
+	       \&update_etc_resolv_conf);
 
 
 

Modified: pve-common/trunk/data/PVE/JSONSchema.pm
===================================================================
--- pve-common/trunk/data/PVE/JSONSchema.pm	2011-02-08 04:48:40 UTC (rev 5490)
+++ pve-common/trunk/data/PVE/JSONSchema.pm	2011-02-08 09:53:11 UTC (rev 5491)
@@ -106,6 +106,20 @@
     return $node;
 }
 
+register_format('ipv4', \&pve_verify_ipv4);
+sub pve_verify_ipv4 {
+    my ($ipv4, $noerr) = @_;
+
+   if ($ipv4 !~ m/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ ||
+       !(($1 > 0) && ($1 < 255) &&
+	 ($2 <= 255) && ($3 <= 255) && 
+	 ($4 > 0) && ($4 < 255)))  {
+	   return undef if $noerr;
+	die "value does not look like a valid IP address\n";
+    }
+    return $ipv4;
+}
+
 sub check_format {
     my ($format, $value) = @_;
 



More information about the pve-devel mailing list