From 2cebf24d44684fb9be94edde0e4a2d93b7c1cce7 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Mon, 19 Oct 2020 08:20:13 -0700 Subject: [PATCH] added option -mfs=n, --maximum-file-size-mb=n --- CHANGES.md | 6 ++++++ bin/perltidy | 12 ++++++++++++ lib/Perl/Tidy.pm | 14 ++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 30214a48..d2e5e97a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 you will probably diff --git a/bin/perltidy b/bin/perltidy index cf094014..161c22fb 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -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 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. diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index 3a8c35e9..a3409914 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -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 -- 2.39.5