s3_close Subroutine

public subroutine s3_close(unit, iostat)

Close an S3 file handle.

Closes an open S3 file handle. For write mode, this uploads the buffered content to S3. The file handle is released and can be reused.

@param[in] unit The unit number to close @param[out] iostat Status code: 0 on success, -1 on error

Warning

For write mode, upload errors will be reflected in iostat.

Example

call s3_close(unit, iostat)
if (iostat /= 0) then
    print *, 'Error closing file'
end if

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: unit
integer, intent(out) :: iostat

Source Code

    subroutine s3_close(unit, iostat)
        integer, intent(in) :: unit
        integer, intent(out) :: iostat
        logical :: success

        iostat = 0

        if (unit < 1 .or. unit > MAX_FILES) then
            iostat = -1
            return
        end if

        if (.not. files(unit)%is_open) then
            iostat = -1
            return
        end if

        ! If writing, upload the buffer
        if (files(unit)%is_write .and. allocated(files(unit)%buffer)) then
            success = s3_put_object(files(unit)%key, files(unit)%buffer)
            if (.not. success) iostat = -1
        end if

        ! Clean up
        files(unit)%is_open = .false.
        files(unit)%key = ''
        files(unit)%position = 1
        files(unit)%is_write = .false.
        if (allocated(files(unit)%buffer)) deallocate(files(unit)%buffer)
    end subroutine s3_close