[xen-tools-dev] [PATCH 10/17] Move old log files rather than removing them.

Stéphane Jourdois sjourdois at gmail.com
Sun Jul 25 14:12:42 CEST 2010


---
 bin/xen-create-image |   30 +++++++++++++++++++++++++-----
 1 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/bin/xen-create-image b/bin/xen-create-image
index 576fb1a..5d05a68 100755
--- a/bin/xen-create-image
+++ b/bin/xen-create-image
@@ -2275,16 +2275,36 @@ sub setupLogFile
     mkdir( "/var/log/xen-tools", 0750 ) if ( !-d "/var/log/xen-tools" );
 
     #
-    #  Trash any existing for this run logfile.
+    #  Move any existing for this run logfile.
+    #  (Hint: read from the end to understand how this works).
     #
-    open( TRASH, ">", "/var/log/xen-tools/$CONFIG{'hostname'}.log" );
-    print TRASH "";
-    close(TRASH);
+    my $logname = "/var/log/xen-tools/$CONFIG{'hostname'}.log";
+    map {
+        (my $new = $_) =~ s/(?<=\.)\d+(?=\.log$)/$& + 1/e;
+        mv $_, $new; # increment file number
+    } sort {
+        $a =~ /\.(\d+)\.log$/;
+        my $aa = $1;
+        $b =~ /\.(\d+)\.log$/;
+        my $bb = $1;
+        $bb <=> $aa; # sort in reverse order
+    } grep /\.\d+\.log$/, # we only care in numeric filenames
+    glob( "/var/log/xen-tools/$CONFIG{'hostname'}.*.log" );
+
+    # Move the non-numeric filename also
+    mv $logname, "/var/log/xen-tools/$CONFIG{'hostname'}.0.log"
+        if -f $logname;
+
+    #
+    #  Now create an empty file.
+    #
+    open STUB, '>', $logname;
+    close STUB;
 
     #
     #  Make sure the logfile is 0640 - avoid leaking root passwords.
     #
-    chmod( oct("0640"), "/var/log/xen-tools/$CONFIG{'hostname'}.log" );
+    chmod( oct("0640"), $logname );
 }
 
 
-- 
1.7.2



More information about the xen-tools-dev mailing list