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. --- player/tasks.f90 | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 73 insertions(+), 8 deletions(-) (limited to 'player/tasks.f90') diff --git a/player/tasks.f90 b/player/tasks.f90 index 87b7d66..2487e73 100644 --- a/player/tasks.f90 +++ b/player/tasks.f90 @@ -69,30 +69,84 @@ contains end function shell - function upload(url, source_filename) + function upload_glob(url, mask) result(res) + use utilities + implicit none + + logical::res + character(*), intent(in)::url + character(*), intent(in)::mask + + character(DIR_LIST_STRING_LENGTH), dimension(:), pointer::files + logical, dimension(:), allocatable::statuses + integer::i + + ! We can cheat by using the get_files_in_directory function since it + ! is merely calling ls/dir, which will resolve the glob + files => get_files_in_directory(mask) + + !allocate(files(2)) + !files(1) = "/tmp/example/jagcdenc2.zip" + !files(2) = "/tmp/example/jagenc2.zip" + + if(associated(files)) then + + allocate(statuses(size(files))) + do i = 1, size(files) + statuses(i) = upload(url, files(i)) + end do + + res = all(statuses) + + deallocate(statuses) + deallocate(files) + + else + + res = .false. + + end if + + end function upload_glob + + recursive function upload(url, source_filename) result(res) use config, only: token, captain use gemini_protocol, only: titan_post_url, STATUS_SUCCESS implicit none - logical::upload + logical::res character(*), intent(in)::url character(*), intent(in)::source_filename character(len=:), allocatable::mod_url integer(kind=8)::file_size - integer::unit_number, istatus, url_length + integer::unit_number, istatus, url_length, i + + ! Check for globbing - sloppy, but still... + if(index(source_filename, "*") > 0 .or. index(source_filename, "?") > 0) then + res = upload_glob(url, source_filename) + return + + end if + + ! If we're here, we have a single filename to upload inquire(file=source_filename, size=file_size) open(newunit=unit_number, file=trim(source_filename), status='UNKNOWN', & access='STREAM', form='UNFORMATTED', iostat=istatus) if(index(url, "://") > 0) then - allocate(character(len=len_trim(url)) :: mod_url) + ! Leave room for a possble filename + url_length = len_trim(url) + len_trim(source_filename) + + allocate(character(len=url_length) :: mod_url) mod_url = url else - url_length = len_trim(url) + len_trim(captain) + 16 + ! Leave room for a possble filename + url_length = len_trim(url) + len_trim(source_filename) + len_trim(captain) + 16 + allocate(character(len=url_length) :: mod_url) mod_url = "titan://"//trim(captain) if(url(1:1) == "/") then @@ -102,12 +156,23 @@ contains end if end if + ! If the URL ends in a slash, it's a folder, add the filename base. + if(mod_url(len_trim(mod_url):len_trim(mod_url)) == "/") then + i = max(index(source_filename, "/", back=.true.), index(source_filename, "\", back=.true.)) + if(i > 0) then + mod_url = trim(mod_url)//source_filename(i+1:len_trim(source_filename)) + else + mod_url = trim(mod_url)//trim(source_filename) + end if + end if + if(istatus == 0) then + Print *, "Writing "//trim(mod_url) istatus = titan_post_url(mod_url, unit_number, file_size, token) - upload = (istatus == STATUS_SUCCESS) + res = (istatus == STATUS_SUCCESS) close(unit_number) else - upload = .false. + res = .false. end if deallocate(mod_url) @@ -145,7 +210,7 @@ contains end if open(newunit=unit_number, file=trim(destination_filename), status='UNKNOWN', & - access='STREAM', form='FORMATTED', iostat=istatus) + access='STREAM', iostat=istatus) ! , form='FORMATTED', if(istatus == 0) then istatus = request_url(mod_url, unit_number, mimetype) -- cgit v1.2.3