]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei_saved_search: canonicalized relative save paths
authorEric Wong <e@80x24.org>
Wed, 11 Aug 2021 11:26:17 +0000 (11:26 +0000)
committerEric Wong <e@80x24.org>
Wed, 11 Aug 2021 21:50:08 +0000 (21:50 +0000)
Storing relative paths with '..' in them can be expensive to
resolve when running 'lei up', so prefer storing canonicalized
absolute paths.  We only do this for paths with '..' in them,
though, since this can lose symlink info.

lib/PublicInbox/LeiSavedSearch.pm
t/lei-q-save.t

index cfbf68c3491a9405480fa42e4f270de90e36fa92..2a0e9321b3354051d3d10df6cf72014268374d13 100644 (file)
@@ -115,9 +115,16 @@ sub up { # updating existing saved search via "lei up"
 sub new { # new saved search "lei q --save"
        my ($cls, $lei) = @_;
        my $self = bless { ale => $lei->ale }, $cls;
+       require File::Path;
        my $dst = $lei->{ovv}->{dst};
+
+       # canonicalize away relative paths into the config
+       if ($lei->{ovv}->{fmt} eq 'maildir' &&
+                       $dst =~ m!(?:/*|\A)\.\.(?:/*|\z)! && !-d $dst) {
+               File::Path::make_path($dst);
+               $lei->{ovv}->{dst} = $dst = $lei->abs_path($dst);
+       }
        my $dir = lss_dir_for($lei, \$dst);
-       require File::Path;
        File::Path::make_path($dir); # raises on error
        $self->{-cfg} = {};
        my $f = $self->{'-f'} = "$dir/lei.saved-search";
index b1ca4e92ab656ea1ff6792d05b57bf870e895814..eada2dd48a99c4cdbc23ef4aa414935db815c34b 100644 (file)
@@ -202,5 +202,14 @@ test_lei(sub {
 
        lei_ok([qw(edit-search), $v2s], { VISUAL => 'cat', EDITOR => 'cat' });
        like($lei_out, qr/^\[lei/sm, 'edit-search can cat');
+
+       lei_ok('-C', "$home/v2s",
+               qw(q -q --save -o ../s m:testmessage@example.com));
+       lei_ok qw(ls-search);
+       unlike $lei_out, qr{/\.\./s$}sm, 'relative path not in ls-search';
+       like $lei_out, qr{^\Q$home\E/s$}sm,
+               'absolute path appears in ls-search';
+       lei_ok qw(up ../s -C), "$home/v2s", \'relative lei up';
+       lei_ok qw(up), "$home/s", \'absolute lei up';
 });
 done_testing;