From cebd63a999e9e9526da56f87a750f0812a256569 Mon Sep 17 00:00:00 2001
From: Mark Tompsett <mtompset@hotmail.com>
Date: Wed, 15 Apr 2015 01:02:08 -0400
Subject: [PATCH] [Signed-off] Bug 10625: Inventory/Stocktaking tool cannot
 handle windows file uploads
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The current code uses
    $barcode = <fh>;
logic. This reads until \n, as far as I can tell.
EOL is indicated by \n, \r, and \r\n depending on OS and software.
So, to this end, rather than File::Slurp (which is a potential
memory hog, which is already an issue with no filters), a loop
to pre-read the barcodes was written.

This loop includes:
    $barcode =~ s/\r/\n/g;
    $barcode =~ s/\n\n/\n/g;
    $barcode =~ s/\n$//;
    $barcode =~ s/^\n//;
    my @data = split(/\n/, $barcode);
    push @uploadedbarcodes,@data;

So, that means that lines ending in \n would have it stripped
and pushed into the uploaded barcodes array.

Lines ending in \r would likely be read as one giant block,
have everything converted to single \n's and then using a split,
the set of barcodes are pushed into the uploaded barcodes array.

Lines ending in \r\n would get that stripped and pushed into the
uploaded barcodes array.

It is then the uploaded barcodes array that is looped over for
validating the barcodes.

TEST PLAN
---------
1) Create a file with at least three different barcodes.

---
I tested with the test file provided and with a file where some lines have CR and other lines have CR NL (inserted with Win text editor).
Worked as expected.
Signed-off-by: Marc Véron <veron@veron.ch>
---
 tools/inventory.pl |   12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/tools/inventory.pl b/tools/inventory.pl
index b8cc31d..f09abf9 100755
--- a/tools/inventory.pl
+++ b/tools/inventory.pl
@@ -161,6 +161,7 @@ if ( $uploadbarcodes && length($uploadbarcodes) > 0 ) {
     my $count = 0;
 
     my @barcodes;
+    my @uploadedbarcodes;
 
     my $sth = $dbh->column_info(undef,undef,"items","barcode");
     my $barcode_def = $sth->fetchall_hashref('COLUMN_NAME');
@@ -169,8 +170,15 @@ if ( $uploadbarcodes && length($uploadbarcodes) > 0 ) {
     my $err_data=0;
     my $lines_read=0;
     binmode($uploadbarcodes, ":encoding(UTF-8)");
-    while (my $barcode=<$uploadbarcodes>){
-        $barcode =~ s/\r?\n$//;
+    while (my $barcode=<$uploadbarcodes>) {
+        $barcode =~ s/\r/\n/g;
+        $barcode =~ s/\n\n/\n/g;
+        $barcode =~ s/\n$//;
+        $barcode =~ s/^\n//;
+        my @data = split(/\n/,$barcode);
+        push @uploadedbarcodes, @data;
+    }
+    for my $barcode (@uploadedbarcodes) {
         next unless $barcode;
         ++$lines_read;
         if (length($barcode)>$barcode_size) {
-- 
1.7.10.4