§
    d¦   ã                   óL  — d dl Z d dlZd dlZd dlZd dlmZ ddlmZ ddlm	Z	 ddl
mZ  e	¦   «         Z G d„ d¦  «        Z G d	„ d
e¦  «        Z G d„ de¦  «        Z G d„ de¦  «        Zej        eej        eiZ G d„ de¦  «        Z G d„ de¦  «        Z G d„ de¦  «        ZdS )é    N)Úcompare_digesté   )ÚIntegrityError)Úcreate_logger)ÚStreamingXXH64c                   óN   — e Zd Zd„ Zd„ Zd„ Zej        fd„Zd„ Z	d
d„Z
d„ Zd	„ ZdS )ÚFileLikeWrapperc                 ó8   — | j                              ¦   «          | S ©N)ÚfdÚ	__enter__©Úselfs    ú</usr/lib/python3/dist-packages/borg/crypto/file_integrity.pyr   zFileLikeWrapper.__enter__   s   € ØŒ×ÒÑÔÐØˆó    c                 ó>   — | j                              |||¦  «         d S r   )r   Ú__exit__)r   Úexc_typeÚexc_valÚexc_tbs       r   r   zFileLikeWrapper.__exit__   s"   € ØŒ×Ò˜ 7¨FÑ3Ô3Ð3Ð3Ð3r   c                 ó4   — | j                              ¦   «         S r   )r   Útellr   s    r   r   zFileLikeWrapper.tell   s   € ØŒw�|Š|‰~Œ~Ðr   c                 ó8   — | j                              ||¦  «        S r   )r   Úseek)r   ÚoffsetÚwhences      r   r   zFileLikeWrapper.seek   s   € ØŒw�|Š|˜F FÑ+Ô+Ð+r   c                 ó6   — | j                              |¦  «        S r   )r   Úwrite©r   Údatas     r   r   zFileLikeWrapper.write   s   € ØŒw�}Š}˜TÑ"Ô"Ð"r   Nc                 ó6   — | j                              |¦  «        S r   )r   Úread)r   Úns     r   r"   zFileLikeWrapper.read   s   € ØŒw�|Š|˜A‰ŒÐr   c                 ó8   — | j                              ¦   «          d S r   )r   Úflushr   s    r   r%   zFileLikeWrapper.flush"   s   € ØŒ�Š‰Œˆˆˆr   c                 ó4   — | j                              ¦   «         S r   )r   Úfilenor   s    r   r'   zFileLikeWrapper.fileno%   s   € ØŒw�~Š~ÑÔÐr   r   )Ú__name__Ú
__module__Ú__qualname__r   r   r   ÚioÚSEEK_SETr   r   r"   r%   r'   © r   r   r	   r	      s�   € € € € € ðð ð ð4ð 4ð 4ðð ð ð #%¤+ð ,ð ,ð ,ð ,ð#ð #ð #ðð ð ð ðð ð ð ð  ð  ð  ð  r   r	   c                   ó`   ‡ — e Zd ZdZdZdZd„ Zˆ fd„Zˆ fd„Zdˆ fd„	Z	d„ Z
defd	„Zdd„Zˆ xZS )ÚFileHashingWrappera'  
    Wrapper for file-like objects that computes a hash on-the-fly while reading/writing.

    WARNING: Seeks should only be used to query the size of the file, not
    to skip data, because skipped data isn't read and not hashed into the digest.

    Similarly skipping while writing to create sparse files is also not supported.

    Data has to be read/written in a symmetric fashion, otherwise different
    digests will be generated.

    Note: When used as a context manager read/write operations outside the enclosed scope
    are illegal.
    Nc                 óT   — || _         || _        |                      ¦   «         | _        d S r   )r   ÚwritingÚFACTORYÚhash)r   Ú
backing_fdr   s      r   Ú__init__zFileHashingWrapper.__init__<   s#   € ØˆŒØˆŒØ—L’L‘N”NˆŒ	ˆ	ˆ	r   c                 óz   •— |€|                       ¦   «          t          ¦   «                              |||¦  «         d S r   )Úhash_lengthÚsuperr   )r   r   r   r   Ú	__class__s       €r   r   zFileHashingWrapper.__exit__A   s=   ø€ ØÐØ×ÒÑÔÐÝ‰Œ×Ò˜ 7¨FÑ3Ô3Ð3Ð3Ð3r   c                 ó~   •— t          ¦   «                              |¦  «        }| j                             |¦  «         |S )zI
        Write *data* to backing file and update internal state.
        )r8   r   r3   Úupdate)r   r    r#   r9   s      €r   r   zFileHashingWrapper.writeF   s5   ø€ õ ‰GŒG�MŠM˜$ÑÔˆØŒ	×Ò˜ÑÔÐØˆr   c                 ó~   •— t          ¦   «                              |¦  «        }| j                             |¦  «         |S )zf
        Read *data* from backing file (*n* has the usual meaning) and update internal state.
        )r8   r"   r3   r;   )r   r#   r    r9   s      €r   r"   zFileHashingWrapper.readN   s3   ø€ õ ‰wŒw�|Š|˜A‰ŒˆØŒ	×Ò˜ÑÔÐØˆr   c                 ó4   — | j                              ¦   «         S )zn
        Return current digest bytes as hex-string.

        Note: this can be called multiple times.
        )r3   Ú	hexdigestr   s    r   r>   zFileHashingWrapper.hexdigestV   s   € ð Œy×"Ò"Ñ$Ô$Ð$r   r    c                 ó:   — | j                              |¦  «         d S r   )r3   r;   r   s     r   r;   zFileHashingWrapper.update^   s   € ØŒ	×Ò˜ÑÔÐÐÐr   Fc                 óà   — |r |                       dt          j        ¦  «         | j                             t          |                      ¦   «         ¦  «                             ¦   «         ¦  «         d S )Nr   )r   r+   ÚSEEK_ENDr3   r;   Ústrr   Úencode)r   Úseek_to_ends     r   r7   zFileHashingWrapper.hash_lengtha   sX   € Øð 	&à�IŠI�a�œÑ%Ô%Ð%ØŒ	×Ò�˜TŸYšY™[œ[Ñ)Ô)×0Ò0Ñ2Ô2Ñ3Ô3Ð3Ð3Ð3r   r   ©F)r(   r)   r*   Ú__doc__Ú	ALGORITHMr2   r5   r   r   r"   r>   Úbytesr;   r7   Ú__classcell__©r9   s   @r   r/   r/   )   sÒ   ø€ € € € € ðð ð €IØ€Gð#ð #ð #ð
4ð 4ð 4ð 4ð 4ð
ð ð ð ð ðð ð ð ð ð ð%ð %ð %ð˜5ð ð ð ð ð4ð 4ð 4ð 4ð 4ð 4ð 4ð 4r   r/   c                   ó    — e Zd ZdZej        ZdS )ÚSHA512FileHashingWrapperÚSHA512N)r(   r)   r*   rG   ÚhashlibÚsha512r2   r-   r   r   rL   rL   h   s   € € € € € Ø€IØŒn€G€G€Gr   rL   c                   ó   — e Zd ZdZeZdS )ÚXXH64FileHashingWrapperÚXXH64N)r(   r)   r*   rG   r   r2   r-   r   r   rQ   rQ   m   s   € € € € € Ø€IØ€G€G€Gr   rQ   c                   ó   — e Zd ZdZdS )ÚFileIntegrityErrorzFile failed integrity check: {}N)r(   r)   r*   rF   r-   r   r   rT   rT   x   s   € € € € € Ø)Ð)Ð)Ð)r   rT   c                   ó^   — e Zd Zdd„Zd„ Zdd„Zededefd„¦   «         Zdd	„Z	d
„ Z
defd„ZdS )ÚIntegrityCheckedFileNc                 óR  — || _         || _        |rdnd}|pt          ||¦  «        | _        |d u | _        i | _        t          }|s/|                      ||¦  «        }|r|\  }	| _        t          |	         } || j        |¬¦  «        x| _	        | _
        |                      |¦  «         d S )NÚwbÚrb)r4   r   )Úpathr1   ÚopenÚfile_fdÚfile_openedÚdigestsrQ   Úload_integrity_dataÚSUPPORTED_ALGORITHMSr   ÚhasherÚhash_filename)
r   rZ   r   ÚfilenameÚoverride_fdÚintegrity_dataÚmodeÚhash_clsÚalgorithm_and_digestsÚ	algorithms
             r   r5   zIntegrityCheckedFile.__init__}   sÄ   € ØˆŒ	ØˆŒØÐ&ˆtˆt $ˆØ"Ð6¥d¨4°Ñ&6Ô&6ˆŒØ&¨$Ð.ˆÔØˆŒå*ˆàð 	;Ø$(×$<Ò$<¸TÀ>Ñ$RÔ$RÐ!Ø$ð ;Ø*?Ñ'�	˜4œ<Ý/°	Ô:�ð
 !) °D´LÈÐ NÑ NÔ NÐNˆŒ�$”+Ø×Ò˜8Ñ$Ô$Ð$Ð$Ð$r   c                 ó6   — |�|                       ||¦  «        S d S r   )Úparse_integrity_data©r   rZ   re   s      r   r_   z(IntegrityCheckedFile.load_integrity_data“   s&   € ØÐ%Ø×,Ò,¨T°>ÑBÔBÐBð &Ð%r   c                 ó"  — t           j                             |p| j        ¦  «        }| j                             dt          |¦  «        z                       ¦   «         ¦  «         | j                             |                     ¦   «         ¦  «         d S )Nú%10d)ÚosrZ   Úbasenamera   r;   ÚlenrC   )r   rc   s     r   rb   z"IntegrityCheckedFile.hash_filename—   ss   € õ ”7×#Ò# HÐ$9°´	Ñ:Ô:ˆØŒ×Ò˜F¥S¨¡]¤]Ñ2×:Ò:Ñ<Ô<Ñ=Ô=Ð=ØŒ×Ò˜8Ÿ?š?Ñ,Ô,Ñ-Ô-Ð-Ð-Ð-r   rZ   r    c                 óF  — 	 t          j        |¦  «        }|d         }|t          vrt                               d||¦  «         d S |d         }|d          ||fS # t
          t          t          f$ r0}t                               d||¦  «         t          |¦  «        ‚d }~ww xY w)Nri   z3Cannot verify integrity of %s: Unknown algorithm %rr^   Úfinalz)Could not parse integrity data for %s: %s)	ÚjsonÚloadsr`   ÚloggerÚwarningÚ
ValueErrorÚ	TypeErrorÚKeyErrorrT   )ÚclsrZ   r    re   ri   r^   Úes          r   rk   z)IntegrityCheckedFile.parse_integrity_data¢   sµ   € ð	+Ý!œZ¨Ñ-Ô-ˆNà& {Ô3ˆIØÕ 4Ð4Ð4Ý—’ÐTÐVZÐ\eÑfÔfÐfØ�Ø$ YÔ/ˆGà�GÔÐØ˜gÐ%Ð%øÝ�I¥xÐ0ð 	+ð 	+ð 	+Ý�NŠNÐFÈÈaÑPÔPÐPÝ$ TÑ*Ô*Ð*øøøøð	+øøøs   ‚AA ÁA ÁB Á0+BÂB Fc                 ó  — | j         s	| j        sd S | j                             dt	          |¦  «        z                       ¦   «         ¦  «         | j                             |                     ¦   «         ¦  «         | j                             |¬¦  «         | j                             ¦   «         }| j         r|| j        |<   d S | j        r=t          | j         	                    |d¦  «        |¦  «        st          | j        ¦  «        ‚d S d S )Nrn   )rD   Ú )r1   r^   ra   r;   rq   rC   r7   r>   r   ÚgetrT   rZ   )r   ÚpartnameÚis_finalÚdigests       r   Ú	hash_partzIntegrityCheckedFile.hash_part³   s  € ØŒ|ð 	 D¤Lð 	ØˆFØŒ×Ò˜F¥S¨¡]¤]Ñ2×:Ò:Ñ<Ô<Ñ=Ô=Ð=ØŒ×Ò˜8Ÿ?š?Ñ,Ô,Ñ-Ô-Ð-ØŒ×Ò¨HÐÑ5Ô5Ð5Ø”×&Ò&Ñ(Ô(ˆØŒ<ð 	0Ø%+ˆDŒL˜Ñ"Ð"Ð"ØŒ\ð 	0¥.°´×1AÒ1AÀ(ÈBÑ1OÔ1OÐQWÑ"XÔ"Xð 	0Ý$ T¤YÑ/Ô/Ð/ð	0ð 	0ð 	0ð 	0r   c                 óæ  — |d u}	 |s|                       dd¬¦  «         | j                             |||¦  «         | j        r| j                             ¦   «          n&# | j        r| j                             ¦   «          w w xY w|rd S | j        r;|                      t          j	        | j        j
        | j        dœ¦  «        ¦  «         d S | j        r"t                               d| j        ¦  «         d S d S )Nrs   T)r�   )ri   r^   zVerified integrity of %s)rƒ   ra   r   r]   r\   Úcloser1   Ústore_integrity_datart   ÚdumpsrG   r^   rv   ÚdebugrZ   )r   r   r   r   Ú	exceptions        r   r   zIntegrityCheckedFile.__exit__¿   s+  € Ø DÐ(ˆ	ð	%Øð 7Ø—’˜w°�Ñ6Ô6Ð6ØŒK× Ò  ¨7°FÑ;Ô;Ð;àÔð %Ø”×"Ò"Ñ$Ô$Ð$øøð Ôð %Ø”×"Ò"Ñ$Ô$Ð$Ð$ð%øøøàð 	ØˆFØŒ<ð 	@Ø×%Ò%¥d¤jØ!œ[Ô2Øœ<ð2ð 2ñ 'ô 'ñ ô ð ð ð ð Œ\ð 	@Ý�LŠLÐ3°T´YÑ?Ô?Ð?Ð?Ð?ð	@ð 	@s   †5A Á#A?c                 ó   — || _         d S r   )re   r   s     r   r†   z)IntegrityCheckedFile.store_integrity_dataÒ   s   € Ø"ˆÔÐÐr   )NNNr   rE   )r(   r)   r*   r5   r_   rb   ÚclassmethodrB   rk   rƒ   r   r†   r-   r   r   rV   rV   |   s½   € € € € € ð%ð %ð %ð %ð,Cð Cð Cð	.ð 	.ð 	.ð 	.ð ð+¨ð +°3ð +ð +ð +ñ „[ð+ð 
0ð 
0ð 
0ð 
0ð@ð @ð @ð&#¨ð #ð #ð #ð #ð #ð #r   rV   c                   ó^   ‡ — e Zd Zdˆ fd„	Zd„ Zed„ ¦   «         Zed„ ¦   «         Zde	fd„Z
ˆ xZS )	ÚDetachedIntegrityCheckedFileNc                 ó@  •— t          ¦   «                              ||||¦  «         |pt          j                             |¦  «        }t          j                             |¦  «        }|                      t          j                             ||¦  «        ¦  «        | _        d S r   )	r8   r5   ro   rZ   rp   ÚdirnameÚintegrity_file_pathÚjoinÚoutput_integrity_file)r   rZ   r   rc   rd   Ú
output_dirr9   s         €r   r5   z%DetachedIntegrityCheckedFile.__init__×   s{   ø€ Ý‰Œ×Ò˜˜u h°Ñ<Ô<Ð<ØÐ5�rœw×/Ò/°Ñ5Ô5ˆÝ”W—_’_ TÑ*Ô*ˆ
Ø%)×%=Ò%=½b¼g¿lºlÈ:ÐW_Ñ>`Ô>`Ñ%aÔ%aˆÔ"Ð"Ð"r   c                 óN   — |r
J d¦   «         ‚|                       | j        ¦  «        S )NzCCannot pass explicit integrity_data to DetachedIntegrityCheckedFile)Úread_integrity_filerZ   rl   s      r   r_   z0DetachedIntegrityCheckedFile.load_integrity_dataÝ   s-   € Ø!ÐhÐhÐ#hÑhÔhÐhØ×'Ò'¨¬	Ñ2Ô2Ð2r   c                 ó   — | dz   S )Nz
.integrityr-   )rZ   s    r   r�   z0DetachedIntegrityCheckedFile.integrity_file_pathá   s   € à�lÑ"Ð"r   c                 ó”  — 	 t          |                      |¦  «        ¦  «        5 }|                      ||                     ¦   «         ¦  «        cd d d ¦  «         S # 1 swxY w Y   d S # t          $ r t
                               d|¦  «         Y d S t          $ r0}t
                               d||¦  «         t          |¦  «        ‚d }~ww xY w)NzNo integrity file found for %sz(Could not read integrity file for %s: %s)
r[   r�   rk   r"   ÚFileNotFoundErrorrv   ÚinfoÚOSErrorrw   rT   )r{   rZ   r   r|   s       r   r•   z0DetachedIntegrityCheckedFile.read_integrity_fileå   s)  € ð	+Ý�c×-Ò-¨dÑ3Ô3Ñ4Ô4ð A¸Ø×/Ò/°°b·g²g±i´iÑ@Ô@ðAð Að Að Añ Aô Að Að Að Að Að Að Aøøøð Að Að Að Að Að Aøå ð 	@ð 	@ð 	@Ý�KŠKÐ8¸$Ñ?Ô?Ð?Ð?Ð?Ð?Ýð 	+ð 	+ð 	+Ý�NŠNÐEÀtÈQÑOÔOÐOÝ$ TÑ*Ô*Ð*øøøøð	+øøøs@   ‚"A& ¤(AÁA& ÁAÁA& Á AÁ!A& Á&%CÂ	CÂ+CÃCr    c                 óŒ   — t          | j        d¦  «        5 }|                     |¦  «         d d d ¦  «         d S # 1 swxY w Y   d S )NÚw)r[   r’   r   )r   r    r   s      r   r†   z1DetachedIntegrityCheckedFile.store_integrity_datað   s…   € Ý�$Ô,¨cÑ2Ô2ð 	°bØ�HŠH�T‰NŒNˆNð	ð 	ð 	ñ 	ô 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	øøøð 	ð 	ð 	ð 	ð 	ð 	s   –9¹=Á =)NN)r(   r)   r*   r5   r_   Ústaticmethodr�   r‹   r•   rB   r†   rI   rJ   s   @r   r�   r�   Ö   s£   ø€ € € € € ðbð bð bð bð bð bð3ð 3ð 3ð ð#ð #ñ „\ð#ð ð+ð +ñ „[ð+ð¨ð ð ð ð ð ð ð ð r   r�   )rN   r+   rt   ro   Úhmacr   Úhelpersr   rv   r   Úalgorithms.checksumsr   r	   r/   rL   rQ   rG   r`   rT   rV   r�   r-   r   r   ú<module>r¡      sÓ  ðØ €€€Ø 	€	€	€	Ø €€€Ø 	€	€	€	Ø Ð Ð Ð Ð Ð à $Ð $Ð $Ð $Ð $Ð $Ø "Ð "Ð "Ð "Ð "Ð "Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1à	ˆ‰Œ€ð ð  ð  ð  ð  ñ  ô  ð  ð6<4ð <4ð <4ð <4ð <4˜ñ <4ô <4ð <4ð~ð ð ð ð Ð1ñ ô ð ð
ð ð ð ð Ð0ñ ô ð ð Ô&Ð(@ØÔ%Ð'>ðÐ ð*ð *ð *ð *ð *˜ñ *ô *ð *ðW#ð W#ð W#ð W#ð W#˜?ñ W#ô W#ð W#ðtð ð ð ð Ð#7ñ ô ð ð ð r   