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/jessl.f90 | 2 +- common/protocol.f90 | 27 ++++++++++++++++++--------- common/request.f90 | 12 +++++++++--- common/utilities.F90 | 44 +++++++++++++++++++++++++++++++++----------- 4 files changed, 61 insertions(+), 24 deletions(-) (limited to 'common') diff --git a/common/jessl.f90 b/common/jessl.f90 index a03df09..5ad90fd 100644 --- a/common/jessl.f90 +++ b/common/jessl.f90 @@ -208,7 +208,7 @@ contains implicit none type(c_ptr)::ssl - character, dimension(:), intent(out)::buf + character(len=1), dimension(:), intent(out)::buf integer::ssl_read integer::bufsize diff --git a/common/protocol.f90 b/common/protocol.f90 index 992283c..46838b8 100644 --- a/common/protocol.f90 +++ b/common/protocol.f90 @@ -179,7 +179,8 @@ contains else - write(unit_number, '(A1)', advance='no') buffer(i) + !write(unit_number, '(A1)', advance='no') buffer(i) + write(unit_number) buffer(i) end if @@ -251,35 +252,43 @@ contains if(conn%code == CONNECTION_OPEN) then - successes(1) = send_string(conn%ssl, trim(url), trimming=.false.) - successes(2) = send_string(conn%ssl, ";token="//trim(token), trimming=.false.) + successes(1) = send_string(conn%ssl, trim(url), trimming=.false., allow_trailing_null=.false.) + successes(2) = send_string(conn%ssl, ";token="//trim(token), & + trimming=.false., allow_trailing_null=.false.) if(present(is_plain_text)) then if(is_plain_text) then - successes(3) = send_string(conn%ssl, ";mime=text/plain", trimming=.false.) + successes(3) = send_string(conn%ssl, ";mime=text/plain", & + trimming=.false., allow_trailing_null=.false.) else - successes(3) = send_string(conn%ssl, ";mime=application/octet-stream", trimming=.false.) + successes(3) = send_string(conn%ssl, ";mime=application/octet-stream", & + trimming=.false., allow_trailing_null=.false.) end if else - successes(3) = send_string(conn%ssl, ";mime=application/octet-stream", trimming=.false.) + successes(3) = send_string(conn%ssl, ";mime=application/octet-stream", & + trimming=.false., allow_trailing_null=.false.) end if write(file_length_text, '(I14)') file_length file_length_text = adjustl(file_length_text) - successes(4) = send_string(conn%ssl, ";size="//trim(file_length_text)//c_carriage_return//c_new_line, trimming=.false.) + + successes(4) = send_string(conn%ssl, & + ";size="//trim(file_length_text)//c_carriage_return//c_new_line, & + trimming=.false., & + allow_trailing_null=.false.) if(all(successes, 1)) then total_written = 0 bytes_read = read_into_buffer(unit_number, buffer) - Print *, "bytes read for sending: ", bytes_read + !Print *, "bytes read for sending: ", bytes_read do while(bytes_read > 0) bytes_written = ssl_write(conn%ssl, buffer(1:bytes_read)) total_written = total_written + bytes_written bytes_read = read_into_buffer(unit_number, buffer) - Print *, "bytes read for sending now: ", bytes_read, " and so far, we wrote", total_written + !Print *, "bytes read for sending now: ", bytes_read, " and so far, we wrote", total_written end do if(total_written >= file_length) then diff --git a/common/request.f90 b/common/request.f90 index d7b3120..fe4d970 100644 --- a/common/request.f90 +++ b/common/request.f90 @@ -218,7 +218,7 @@ contains end subroutine get_server_from_url - function send_string(ssl, str, trimming) result(success) + function send_string(ssl, str, trimming, allow_trailing_null) result(success) use iso_c_binding use jessl implicit none @@ -226,12 +226,13 @@ contains logical::success type(c_ptr)::ssl character(*), intent(in)::str - logical, intent(in), optional::trimming + logical, intent(in), optional::trimming, allow_trailing_null integer::start_send integer::chars_sent_this_time, chars_sending integer::i integer::string_length + logical::end_with_null character, dimension(bufsize)::buffer @@ -245,6 +246,11 @@ contains string_length = len_trim(str) end if + end_with_null = .true. + if(present(allow_trailing_null)) then + end_with_null = allow_trailing_null + end if + success = .true. start_send = 1 do while(start_send <= string_length) @@ -259,7 +265,7 @@ contains end do ! A null character seems necessary at the end of the request - if(i >= string_length) then + if(i >= string_length .and. end_with_null) then chars_sending = chars_sending + 1 buffer(chars_sending) = c_null_char end if 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