From 5674e7fc1273800554c23ad6194e8a06dee851ae Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Sat, 7 Apr 2012 17:52:29 +0100 Subject: prune: do not prune beyond an optional base directory, and add a test Previously, prune("wiki/srcdir/sandbox/test.mdwn") could delete srcdir or even wiki, if they happened to be empty. This is rarely what you want: there's usually some base directory (destdir, srcdir, transientdir or another subdirectory of wikistatedir) beyond which you do not want to delete. --- IkiWiki/Render.pm | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'IkiWiki/Render.pm') diff --git a/IkiWiki/Render.pm b/IkiWiki/Render.pm index d6c1f4f4c..a90d202ee 100644 --- a/IkiWiki/Render.pm +++ b/IkiWiki/Render.pm @@ -262,12 +262,13 @@ sub render ($$) { } } -sub prune ($) { +sub prune ($;$) { my $file=shift; + my $up_to=shift; unlink($file); my $dir=dirname($file); - while (rmdir($dir)) { + while ((! defined $up_to || $dir =~ m{^\Q$up_to\E\/}) && rmdir($dir)) { $dir=dirname($dir); } } @@ -447,7 +448,7 @@ sub remove_del (@) { } foreach my $old (@{$oldrenderedfiles{$page}}) { - prune($config{destdir}."/".$old); + prune($config{destdir}."/".$old, $config{destdir}); } foreach my $source (keys %destsources) { @@ -537,7 +538,7 @@ sub remove_unrendered () { foreach my $file (@{$oldrenderedfiles{$page}}) { if (! grep { $_ eq $file } @{$renderedfiles{$page}}) { debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page)); - prune($config{destdir}."/".$file); + prune($config{destdir}."/".$file, $config{destdir}); } } } @@ -844,7 +845,7 @@ sub clean_rendered { remove_unrendered(); foreach my $page (keys %oldrenderedfiles) { foreach my $file (@{$oldrenderedfiles{$page}}) { - prune($config{destdir}."/".$file); + prune($config{destdir}."/".$file, $config{destdir}); } } } -- cgit v1.2.3