From 05b91a7ca0aace044621d8db1e82f4772181d893 Mon Sep 17 00:00:00 2001 From: Jeffrey Armstrong Date: Wed, 14 Apr 2021 10:54:27 -0400 Subject: Fixed binary transfers to maintain file integrity. Added globbed file upload capabilities. Fixed memory overrun issues due to problems parsing ls output. --- common/utilities.F90 | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) (limited to 'common/utilities.F90') diff --git a/common/utilities.F90 b/common/utilities.F90 index ddc0c79..981dee3 100644 --- a/common/utilities.F90 +++ b/common/utilities.F90 @@ -334,7 +334,7 @@ contains character(DIR_LIST_STRING_LENGTH), dimension(:), pointer::res character(80)::line character(len=:), pointer::tempfile - integer::dcount, total_count, unum, ierr, i + integer::dcount, total_count, unum, ierr, i, n tempfile => generate_temporary_filename() res => null() @@ -344,36 +344,58 @@ contains open(newunit=unum, file=tempfile, action='read') - ! First line is "total ###" - read(unum, '(A)', iostat=ierr) line - dcount = 0 total_count = 0 + + ! Count directories first read(unum, '(A)', iostat=ierr) line do while(ierr == 0) + if(line(1:1) == 'd') then dcount = dcount + 1 end if - total_count = total_count + 1 + + ! ls puts a nonsense entry first. harmless, but we don't want it + if(line(1:6) /= "total ") then + total_count = total_count + 1 + end if + read(unum, '(A)', iostat=ierr) line end do close(unum) - if((total_count - dcount) > 0) then - allocate(res(total_count - dcount)) + n = total_count - dcount + !print *, "Total: ", total_count, "Dirs:", dcount, "Files:", n + + if(n > 0) then + allocate(res(n)) + !print *, "Size: ", size(res) ! Now call ls, but group directories first call execute_command_line("ls --group-directories-first "//trim(directory)//" > "//trim(tempfile), & wait=.true.) open(newunit=unum, file=tempfile, action='read') + i = 0 + + ! First, skip directories + do while(i < dcount) + read(unum, '(A)', iostat=ierr) line + if(line(1:6) /= "total ") then + i = i + 1 + end if + end do + + ! Now we can read files i = 0 read(unum, '(A)', iostat=ierr) line - do while(ierr == 0 .and. i <= total_count) - i = i + 1 - if(i > dcount) then - res(i-dcount) = trim(line) + do while(ierr == 0 .and. i < n) + if(line(1:6) /= "total ") then + i = i + 1 + res(i) = trim(line) + + !print *, i, trim(res(i))//"|" end if read(unum, '(A)', iostat=ierr) line -- cgit v1.2.3