texinfo-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[no subject]


From: Patrice Dumas
Date: Sat, 13 Apr 2024 09:59:39 -0400 (EDT)

branch: master
commit b010b3db98be4575404c4d72cd86b10aaab46bbf
Author: Patrice Dumas <pertusus@free.fr>
AuthorDate: Sat Apr 13 13:37:30 2024 +0200

    * tp/Texinfo/Common.pm (output_files_initialize)
    (output_files_open_out, output_files_opened_files), tp/texi2any.pl
    (_exit, %opened_files, handle_errors): use a hash instead of an array
    for opened files, to avoid unlinling twice files opened (and closed)
    twice.
---
 ChangeLog            |  8 ++++++++
 tp/Texinfo/Common.pm | 20 ++++++++++++--------
 tp/texi2any.pl       | 42 +++++++++++++++++++++---------------------
 3 files changed, 41 insertions(+), 29 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3500c5ef18..d9302efb2f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2024-04-13  Patrice Dumas  <pertusus@free.fr>
+
+       * tp/Texinfo/Common.pm (output_files_initialize)
+       (output_files_open_out, output_files_opened_files), tp/texi2any.pl
+       (_exit, %opened_files, handle_errors): use a hash instead of an array
+       for opened files, to avoid unlinling twice files opened (and closed)
+       twice.
+
 2024-04-13  Patrice Dumas  <pertusus@free.fr>
 
        * tp/Texinfo/Common.pm, tp/texi2any.pl: change in comments for a more
diff --git a/tp/Texinfo/Common.pm b/tp/Texinfo/Common.pm
index d7d91a94ea..6e23c96727 100644
--- a/tp/Texinfo/Common.pm
+++ b/tp/Texinfo/Common.pm
@@ -635,7 +635,7 @@ sub locate_init_file($$$)
 # document both in POD and in HTML Customization API.
 sub output_files_initialize
 {
-  return {'unclosed_files' => {}, 'opened_files' => []};
+  return {'unclosed_files' => {}, 'opened_files' => {}};
 }
 
 sub output_files_disable_output_encoding($$)
@@ -703,14 +703,13 @@ sub output_files_open_out($$$;$$)
       warn "BUG: already open: $file_path\n";
     } else {
       # FIXME check that this file has not already been registered
-      # as opened_file?  If not, it probably has been overwritten and
-      # will be unlink'ed twice if the main program aborts.
+      # as opened_file?  If not, it probably has been overwritten.
       # It is not possible to use the file name twice in converters
       # for regular output as files are only closed when all the output
       # units have been written.  It could be possible in HTML with js
       # scripts licence files set to the same name as an output unit, which
       # is not possible in the default case.
-      push @{$self->{'opened_files'}}, $file_path;
+      $self->{'opened_files'}->{$file_path} = 1;
     }
     $self->{'unclosed_files'}->{$file_path} = $filehandle;
   }
@@ -736,13 +735,18 @@ sub output_files_register_closed($$)
 # consistency of the API and clarity of the code.
 #
 # see the description of $SELF in comment above output_files_open_out.
-sub output_files_opened_files($)
+# The $RESULT_OPENED_FILES argument should be an input output hash
+# reference to put opened files in.
+sub output_files_opened_files($$)
 {
   my $self = shift;
+  my $result_opened_files = shift;
+
   if (defined($self->{'opened_files'})) {
-    return @{$self->{'opened_files'}};
-  } else {
-    return ();
+    foreach my $opened_file (keys(%{$self->{'opened_files'}})) {
+    # TODO warn if already exists
+      $result_opened_files->{$opened_file} = 1;
+    }
   }
 }
 
diff --git a/tp/texi2any.pl b/tp/texi2any.pl
index 53e13cc1e9..04132e494b 100755
--- a/tp/texi2any.pl
+++ b/tp/texi2any.pl
@@ -1222,9 +1222,9 @@ sub _exit($$)
   my $opened_files = shift;
 
   if ($error_count and $opened_files and !get_conf('FORCE')) {
-    while (@$opened_files) {
-      my $opened_file = shift (@$opened_files);
+    foreach my $opened_file (keys(%$opened_files)) {
       unlink ($opened_file);
+      delete $opened_files->{$opened_file};
     }
   }
   exit (1) if ($error_count and (!get_conf('FORCE')
@@ -1437,7 +1437,7 @@ if (defined($ENV{TEXINFO_XS_EXTERNAL_FORMATTING})
 }
 
 my $file_number = -1;
-my @opened_files = ();
+my %opened_files;
 my %main_unclosed_files;
 my $error_count = 0;
 # main processing
@@ -1504,13 +1504,13 @@ while(@input_files) {
   }
   # object registering errors and warnings
   if (!defined($document) or $format eq 'parse') {
-    handle_errors($parser->errors(), $error_count, \@opened_files);
+    handle_errors($parser->errors(), $error_count, \%opened_files);
     goto NEXT;
   }
 
   my $document_information = $document->global_information();
   if (get_conf('TRACE_INCLUDES')) {
-    handle_errors($parser->errors(), $error_count, \@opened_files);
+    handle_errors($parser->errors(), $error_count, \%opened_files);
     my $included_file_paths = $document_information->{'included_files'};
     if (defined($included_file_paths)) {
       foreach my $included_file (@$included_file_paths) {
@@ -1596,19 +1596,19 @@ while(@input_files) {
                             $macro_expand_file_name, $error_message));
       $error_macro_expand_file = 1;
     }
-    push @opened_files, Texinfo::Common::output_files_opened_files(
-                                      $macro_expand_files_information);
+    Texinfo::Common::output_files_opened_files(
+                    $macro_expand_files_information, \%opened_files);
 
     # we do not need to go through unclosed files of
     # $macro_expand_files_information as we know that the file is
     # already closed if needed.
     if ($error_macro_expand_file) {
       $error_count++;
-      _exit($error_count, \@opened_files);
+      _exit($error_count, \%opened_files);
     }
   }
   if (get_conf('DUMP_TEXI') or $formats_table{$format}->{'texi2dvi_format'}) {
-    handle_errors($parser->errors(), $error_count, \@opened_files);
+    handle_errors($parser->errors(), $error_count, \%opened_files);
     goto NEXT;
   }
 
@@ -1694,7 +1694,7 @@ while(@input_files) {
   push @$errors, @$document_errors;
 
   _handle_errors($errors);
-  _exit($error_count, \@opened_files);
+  _exit($error_count, \%opened_files);
 
   if ($format eq 'structure') {
     goto NEXT;
@@ -1746,9 +1746,9 @@ while(@input_files) {
 
   # FIXME it is unlikely, but possible that a file registered with
   # MACRO_EXPAND is registered again in a converter
-  push @opened_files, Texinfo::Common::output_files_opened_files(
-                              $converter->output_files_information());
-  handle_errors($converter_registrar->errors(), $error_count, \@opened_files);
+  Texinfo::Common::output_files_opened_files(
+       $converter->output_files_information(), \%opened_files);
+  handle_errors($converter_registrar->errors(), $error_count, \%opened_files);
   my $converter_unclosed_files
        = Texinfo::Common::output_files_unclosed_files(
                                $converter->output_files_information());
@@ -1762,7 +1762,7 @@ while(@input_files) {
           warn(sprintf(__("%s: error on closing %s: %s\n"),
                            $real_command_name, $unclosed_file, $!));
           $error_count++;
-          _exit($error_count, \@opened_files);
+          _exit($error_count, \%opened_files);
         }
       }
     }
@@ -1802,15 +1802,15 @@ while(@input_files) {
       $error_internal_links_file = 1;
     }
 
-    push @opened_files, Texinfo::Common::output_files_opened_files(
-                                      $internal_links_files_information);
+    Texinfo::Common::output_files_opened_files(
+               $internal_links_files_information, \%opened_files);
     # we do not need to go through unclosed files of
     # $internal_links_files_information as we know that the file is
     # already closed if needed.
 
     if ($error_internal_links_file) {
       $error_count++;
-      _exit($error_count, \@opened_files);
+      _exit($error_count, \%opened_files);
     }
   }
 
@@ -1880,8 +1880,8 @@ while(@input_files) {
       $error_sort_element_count_file = 1;
     }
 
-    push @opened_files, Texinfo::Common::output_files_opened_files(
-                                      $sort_elem_files_information);
+    Texinfo::Common::output_files_opened_files(
+                    $sort_elem_files_information, \%opened_files);
 
     $converter_element_count->destroy();
     # we do not need to go through unclosed files of
@@ -1890,7 +1890,7 @@ while(@input_files) {
 
     if ($error_sort_element_count_file) {
       $error_count++;
-      _exit($error_count, \@opened_files);
+      _exit($error_count, \%opened_files);
     }
   }
 
@@ -1904,7 +1904,7 @@ foreach my $unclosed_file (keys(%main_unclosed_files)) {
     warn(sprintf(__("%s: error on closing %s: %s\n"),
                      $real_command_name, $unclosed_file, $!));
     $error_count++;
-    _exit($error_count, \@opened_files);
+    _exit($error_count, \%opened_files);
   }
 }
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]