]> git.donarmstrong.com Git - perltidy.git/commitdiff
added option -mfs=n, --maximum-file-size-mb=n
authorSteve Hancock <perltidy@users.sourceforge.net>
Mon, 19 Oct 2020 15:20:13 +0000 (08:20 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Mon, 19 Oct 2020 15:20:13 +0000 (08:20 -0700)
CHANGES.md
bin/perltidy
lib/Perl/Tidy.pm

index 30214a48d8c9da5b9379d5f333d5e1551e073994..d2e5e97abdc43e0fb15b4e1408f2c09935a71b73 100644 (file)
@@ -2,6 +2,12 @@
 
 ## 2020 10 01.02
 
+    - Add flag -mfs=n, --maximum-file-size-mb=n.  This parameter is provided to
+      avoid causing system problems by accidentally attempting to format an 
+      extremely large data file. The default is n=10.  The command to increase 
+      the limit to 20 MB for example would be  -mfs=20.  This only applies to
+      files specified by filename on the command line.
+
     - Add flag -xci, --extended-continuation-indentation, regarding issue git #28
       This flag causes continuation indentation to "extend" deeper into structures.
       If you use B<-ci=n> and B<-i=n> with the same value of B<n> you will probably
index cf0940149c25789e098ebc724c3b0272c67aa9c6..161c22fb44e8e51a0ea156b528e6220c9bfa321a 100755 (executable)
@@ -3930,6 +3930,18 @@ increasing size, when multiple files are being processed.  This is useful
 during program development, when large numbers of files with varying sizes are
 processed, because it can reduce virtual memory usage. 
 
+B<--maximum-file-size-mb=n> or B<-mfs=n> specifies the maximum file size in
+megabytes that perltidy will attempt to format. This parameter is provided to
+avoid causing system problems by accidentally attempting to format an extremely
+large data file. Most perl scripts are less than about 2 MB in size. The
+integer B<n> has a default value of 10, so perltidy will skip formatting files
+which have a size greater than 10 MB.  The command to increase the limit to 20
+MB for example would be
+   
+  perltidy -mfs=20
+
+This only applies to files specified by filename on the command line. 
+
 B<-DEBUG>  will write a file with extension F<.DEBUG> for each input file 
 showing the tokenization of all lines of code.
 
index 3a8c35e93b4e532ad6e5c28328ba64350616d9fa..a34099142864018bb0856eb5f034c9a72993a91a 100644 (file)
@@ -906,6 +906,18 @@ EOM
                 next;
             }
 
+           # And avoid formatting extremely large files. Since perltidy reads
+           # files into memory, trying to process an extremely large file
+           # could cause system problems.
+            my $size_in_mb = ( -s $input_file ) / ( 1024 * 1024 );
+            if ( $size_in_mb > $rOpts->{'maximum-file-size-mb'} ) {
+                $size_in_mb = sprintf( "%0.1f", $size_in_mb );
+                Warn(
+"skipping file: $input_file: size $size_in_mb MB exceeds limit $rOpts->{'maximum-file-size-mb'}; use -mfs=i to change\n"
+                );
+                next;
+            }
+
             unless ( ( -T $input_file ) || $rOpts->{'force-read-binary'} ) {
                 Warn(
                     "skipping file: $input_file: Non-text (override with -f)\n"
@@ -2369,6 +2381,7 @@ sub generate_options {
     $add_option->( 'version',                         'v',    '' );
     $add_option->( 'memoize',                         'mem',  '!' );
     $add_option->( 'file-size-order',                 'fso',  '!' );
+    $add_option->( 'maximum-file-size-mb',            'mfs',  '=i' );
 
     #---------------------------------------------------------------------
 
@@ -2511,6 +2524,7 @@ sub generate_options {
       maximum-consecutive-blank-lines=1
       maximum-fields-per-table=0
       maximum-line-length=80
+      maximum-file-size-mb=10
       memoize
       minimum-space-to-comment=4
       nobrace-left-and-indent