@@ -616,7 +616,7 @@ def _eof_ack_pdu_done(self) -> None:
616616 ):
617617 self ._start_deferred_lost_segment_handling ()
618618 return
619- self ._checksum_verify ()
619+ assert self ._params . fp . crc32 is not None
620620 self .states .step = TransactionStep .TRANSFER_COMPLETION
621621
622622 def _start_transaction_first_packet_file_data (self , fd_pdu : FileDataPdu ) -> None :
@@ -902,7 +902,13 @@ def _deferred_lost_segment_handling(self) -> None:
902902 and not self ._params .acked_params .metadata_missing
903903 ):
904904 # We are done and have received everything.
905- self ._checksum_verify ()
905+ assert self ._params .fp .crc32 is not None
906+ if self ._checksum_verify (self ._params .fp .progress , self ._params .fp .crc32 ):
907+ self ._params .finished_params .delivery_code = DeliveryCode .DATA_COMPLETE
908+ self ._params .finished_params .condition_code = ConditionCode .NO_ERROR
909+ else :
910+ self ._params .finished_params .delivery_code = DeliveryCode .DATA_INCOMPLETE
911+ self ._params .finished_params .condition_code = ConditionCode .FILE_CHECKSUM_FAILURE
906912 self .states .step = TransactionStep .TRANSFER_COMPLETION
907913 self ._params .acked_params .deferred_lost_segment_detection_active = False
908914 return
@@ -923,6 +929,7 @@ def _deferred_lost_segment_handling(self) -> None:
923929 and self ._params .acked_params .nak_activity_counter + 1
924930 == self ._params .remote_cfg .nak_timer_expiration_limit
925931 ):
932+ self ._params .finished_params .delivery_code = DeliveryCode .DATA_INCOMPLETE
926933 self ._declare_fault (ConditionCode .NAK_LIMIT_REACHED )
927934 return
928935 # This is not the first NAK issuance and the timer expired.
@@ -983,8 +990,18 @@ def _handle_cancel_eof(self, eof_pdu: EofPdu) -> None:
983990 eof_pdu .condition_code ,
984991 EntityIdTlv (self ._params .remote_cfg .entity_id .as_bytes ),
985992 )
986- # Store this as progress for the checksum calculation.
987- self ._params .fp .progress = self ._params .fp .file_size
993+ # Store this as progress for the checksum calculation as well.
994+ self ._params .fp .progress = eof_pdu .file_size
995+ if self ._params .acked_params .metadata_missing :
996+ self ._params .finished_params .delivery_code = DeliveryCode .DATA_INCOMPLETE
997+ return
998+ if self ._params .fp .file_size == 0 :
999+ # Empty file, no file data PDU.
1000+ self ._params .finished_params .delivery_code = DeliveryCode .DATA_COMPLETE
1001+ return
1002+ if self ._checksum_verify (self ._params .fp .progress , self ._params .fp .crc32 ):
1003+ self ._params .finished_params .delivery_code = DeliveryCode .DATA_COMPLETE
1004+ return
9881005 self ._params .finished_params .delivery_code = DeliveryCode .DATA_INCOMPLETE
9891006
9901007 def _handle_no_error_eof (self ) -> bool :
@@ -1004,15 +1021,12 @@ def _handle_no_error_eof(self) -> bool:
10041021 )
10051022 if (
10061023 self .transmission_mode == TransmissionMode .UNACKNOWLEDGED
1007- and not self ._checksum_verify ()
1024+ and not self ._checksum_verify (self . _params . fp . progress , self . _params . fp . crc32 ) # type: ignore
10081025 ):
1009- if (
1010- self ._declare_fault (ConditionCode .FILE_CHECKSUM_FAILURE )
1011- != FaultHandlerCode .IGNORE_ERROR
1012- ):
1013- return False
10141026 self ._start_check_limit_handling ()
10151027 return False
1028+ self ._params .finished_params .delivery_code = DeliveryCode .DATA_COMPLETE
1029+ self ._params .finished_params .condition_code = ConditionCode .NO_ERROR
10161030 return True
10171031
10181032 def _start_deferred_lost_segment_handling (self ) -> None :
@@ -1035,27 +1049,21 @@ def _prepare_eof_ack_packet(self) -> None:
10351049 )
10361050 self ._add_packet_to_be_sent (ack_pdu )
10371051
1038- def _checksum_verify (self ) -> bool :
1039- file_delivery_complete = False
1052+ def _checksum_verify (self , verify_len : int , expected_crc32 : bytes ) -> bool :
10401053 if (
10411054 self ._params .checksum_type == ChecksumType .NULL_CHECKSUM
10421055 or self ._params .fp .metadata_only
10431056 ):
1044- file_delivery_complete = True
1045- else :
1046- crc32 = self .user .vfs .calculate_checksum (
1047- self ._params .checksum_type ,
1048- self ._params .fp .file_name ,
1049- self ._params .fp .progress ,
1050- )
1051- if crc32 == self ._params .fp .crc32 :
1052- file_delivery_complete = True
1053- else :
1054- self ._declare_fault (ConditionCode .FILE_CHECKSUM_FAILURE )
1055- if file_delivery_complete :
1056- self ._params .finished_params .delivery_code = DeliveryCode .DATA_COMPLETE
1057- self ._params .finished_params .condition_code = ConditionCode .NO_ERROR
1058- return file_delivery_complete
1057+ return True
1058+ crc32 = self .user .vfs .calculate_checksum (
1059+ self ._params .checksum_type ,
1060+ self ._params .fp .file_name ,
1061+ verify_len ,
1062+ )
1063+ if crc32 == expected_crc32 :
1064+ return True
1065+ self ._declare_fault (ConditionCode .FILE_CHECKSUM_FAILURE )
1066+ return False
10591067
10601068 def _file_transfer_complete_transition (self ) -> None :
10611069 if self .transmission_mode == TransmissionMode .UNACKNOWLEDGED :
@@ -1126,8 +1134,11 @@ def _add_packet_to_be_sent(self, packet: GenericPduPacket) -> None:
11261134 def _check_limit_handling (self ) -> None :
11271135 assert self ._params .check_timer is not None
11281136 assert self ._params .remote_cfg is not None
1137+ assert self ._params .fp .crc32 is not None
11291138 if self ._params .check_timer .timed_out ():
1130- if self ._checksum_verify ():
1139+ if self ._checksum_verify (self ._params .fp .progress , self ._params .fp .crc32 ):
1140+ self ._params .finished_params .delivery_code = DeliveryCode .DATA_COMPLETE
1141+ self ._params .finished_params .condition_code = ConditionCode .NO_ERROR
11311142 self ._file_transfer_complete_transition ()
11321143 return
11331144 if self ._params .current_check_count + 1 >= self ._params .remote_cfg .check_limit :
0 commit comments