§
    cÌcì*  ã                   ód   — d Z ddlZddlZddlZddlmZmZmZm	Z	m
Z
 dZdZdZ G d„ d¦  «        ZdS )	s   ### bzr hashcache v5
é    Né   )Ú
atomicfileÚerrorsÚfiltersÚosutilsÚtraceé   é   c                   ó\   — e Zd ZdZdZ	 	 dd„Zd„ Zd„ Zd„ Zdd„Z	d	„ Z
d
„ Zd„ Zd„ Zdd„ZdS )Ú	HashCachea{  Cache for looking up file SHA-1.

    Files are considered to match the cached value if the fingerprint
    of the file has not changed.  This includes its mtime, ctime,
    device number, inode number, and size.  This should catch
    modifications or replacement of the file by a new one.

    This may not catch modifications that do not change the file's
    size and that occur within the resolution window of the
    timestamps.  To handle this we specifically do not cache files
    which have changed since the start of the present second, since
    they could undetectably change again.

    This scheme may fail if the machine's clock steps backwards.
    Don't do that.

    This does not canonicalize the paths passed in; that should be
    done by the caller.

    _cache
        Indexed by path, points to a two-tuple of the SHA-1 of the file.
        and its fingerprint.

    stat_count
        number of times files have been statted

    hit_count
        number of times files have been retrieved from the cache, avoiding a
        re-read

    miss_count
        number of misses (times files have been completely re-read)
    FNc                 óè   — t          |t          ¦  «        st          d¦  «        ‚|| _        d| _        d| _        d| _        d| _        d| _        d| _	        i | _
        || _        || _        || _        dS )aI  Create a hash cache in base dir, and set the file mode to mode.

        :param content_filter_stack_provider: a function that takes a
            path (relative to the top of the tree) and a file-id as
            parameters and returns a stack of ContentFilters.
            If None, no content filtering is performed.
        z#Base dir for hashcache must be textr   N)Ú
isinstanceÚstrÚ
ValueErrorÚrootÚ	hit_countÚ
miss_countÚ
stat_countÚdanger_countÚremoved_countÚupdate_countÚ_cacheÚ_modeÚ_cache_file_nameÚ_filter_provider)Úselfr   Úcache_file_nameÚmodeÚcontent_filter_stack_providers        ú6/usr/lib/python3/dist-packages/breezy/bzr/hashcache.pyÚ__init__zHashCache.__init__U   s~   € õ ˜$¥Ñ$Ô$ð 	DÝÐBÑCÔCÐCØˆŒ	ØˆŒØˆŒØˆŒØˆÔØˆÔØˆÔØˆŒØˆŒ
Ø /ˆÔØ =ˆÔÐÐó    c                 ó   — | j         S ©N)r   ©r   s    r    r   zHashCache.cache_file_namel   s   € ØÔ$Ð$r"   c                 ó4   — | j         rd| _        i | _         dS dS )zJDiscard all cached information.

        This does not reset the counters.TN)r   Úneeds_writer%   s    r    ÚclearzHashCache.clearo   s+   € ð Œ;ð 	Ø#ˆDÔØˆDŒKˆKˆKð	ð 	r"   c                 ó@  — d„ }t          | j                             ¦   «         |¬¦  «        D ]q\  }}t          j        | j        |¦  «        }|                      |¦  «        }| xj        dz  c_        |r|d         |k    r| xj        dz  c_        d| _	        | j        |= ŒrdS )zÃScan all files and remove entries where the cache entry is obsolete.

        Obsolete entries are those where the file has been modified or deleted
        since the entry was inserted.
        c                 ó*   — | d         d         d         S )Nr	   é   © )Úpath_and_caches    r    Úinode_orderz#HashCache.scan.<locals>.inode_order~   s   € Ø! !Ô$ QÔ'¨Ô*Ð*r"   )Úkeyr	   TN)
Úsortedr   Úitemsr   Úpathjoinr   Ú_fingerprintr   r   r'   )r   r.   ÚpathÚ	cache_valÚabspathÚfps         r    ÚscanzHashCache.scanw   s½   € ð	+ð 	+ð 	+å% d¤k×&7Ò&7Ñ&9Ô&9¸{ÐKÑKÔKð 		&ð 		&‰OˆD�)ÝÔ& t¤y°$Ñ7Ô7ˆGØ×"Ò" 7Ñ+Ô+ˆBØˆOŒO˜qÑ ˆOŒOàð &˜ 1œ¨Ò+Ð+àÐ"Ô" aÑ'Ð"Ô"Ø#'�Ô Ø”K Ð%øð		&ð 		&r"   c                 ó  — t          j        | j        |¦  «        }| xj        dz  c_        |                      ||¦  «        }|s*|| j        v r| xj        dz  c_        d| _        | j        |= dS || j        v r| j        |         \  }}nd\  }}||k    r| xj        dz  c_        |S | xj	        dz  c_	        |t                   }t          j        |¦  «        r7| j        €g }n|                      |¬¦  «        }|                      ||¦  «        }	nit          j        |¦  «        r<t          j        |¦  «        }
t          j        |
                     d¦  «        ¦  «        }	nt'          j        d||fz  ¦  «        ‚|                      ¦   «         }|t,                   |k    s|t.                   |k    r2| xj        dz  c_        |r| xj        dz  c_        d| _        | j        |= n#| xj        dz  c_        d| _        |	|f| j        |<   |	S )z#Return the sha1 of a file.
        r	   TN©NN)r4   zUTF-8z#file %r: unknown file stat mode: %o)r   r2   r   r   r3   r   r   r'   r   r   ÚFP_MODE_COLUMNÚstatÚS_ISREGr   Ú_really_sha1_fileÚS_ISLNKÚreadlinkÚ
sha_stringÚencoder   ÚBzrErrorÚ_cutoff_timeÚFP_MTIME_COLUMNÚFP_CTIME_COLUMNr   r   )r   r4   Ú
stat_valuer6   Úfile_fpÚ
cache_sha1Úcache_fpr   r   ÚdigestÚtargetÚcutoffs               r    Úget_sha1zHashCache.get_sha1‹   s@  € õ Ô" 4¤9¨dÑ3Ô3ˆØˆŒ˜1ÑˆŒØ×#Ò# G¨ZÑ8Ô8ˆàð 	à�t”{Ð"Ð"ØÐ"Ô" aÑ'Ð"Ô"Ø#'�Ô Ø”K Ð%Ø�4à�4”;ÐÐØ#'¤;¨tÔ#4Ñ ˆJ˜˜à#-Ñ ˆJ˜à�wÒÐØˆNŒN˜aÑˆNŒNØÐàˆŒ˜1ÑˆŒà•~Ô&ˆÝŒ<˜ÑÔð 	5ØÔ$Ð,Ø��à×/Ò/°TÐ/Ñ:Ô:�Ø×+Ò+¨G°WÑ=Ô=ˆFˆFÝŒ\˜$ÑÔð 	5ÝÔ% gÑ.Ô.ˆFÝÔ'¨¯ª°gÑ(>Ô(>Ñ?Ô?ˆFˆFå”/Ð"GØ%,¨d Oñ#4ñ 5ô 5ð 5ð
 ×"Ò"Ñ$Ô$ˆØ•?Ô# vÒ-Ð-Ø�?Ô+¨vÒ5Ð5ð ÐÔ Ñ"ÐÔØð &ØÐ"Ô" aÑ'Ð"Ô"Ø#'�Ô Ø”K Ð%øð
 ÐÔ Ñ"ÐÔØ#ˆDÔØ!'¨Ð 1ˆDŒK˜ÑØˆr"   c                 ó8   — t          j        ||¦  «        d         S )z5Calculate the SHA1 of a file by reading the full textr	   )Ú_mod_filtersÚinternal_size_sha_file_byname)r   r6   r   s      r    r>   zHashCache._really_sha1_fileÒ   s   € åÔ9¸'À7ÑKÔKÈAÔNÐNr"   c                 ó  — t          j        |                      ¦   «         d| j        ¬¦  «        5 }|                     t
          ¦  «         | j                             ¦   «         D ]\  }}|                     d¦  «        d|d         dg}| 	                    d|d         z  ¦  «         | 	                    d	¦  «         |                     d
 
                    |¦  «        ¦  «         Œ€d| _        ddd¦  «         dS # 1 swxY w Y   dS )z Write contents of cache to file.Úwb)Únew_modeúutf-8ó   // r   ó    s   %d %d %d %d %d %dr	   ó   
r"   FN)r   Ú
AtomicFiler   r   ÚwriteÚCACHE_HEADERr   r1   rB   ÚappendÚjoinr'   )r   Úoutfr4   ÚcÚ	line_infos        r    rZ   zHashCache.writeÖ   s=  € åÔ" 4×#7Ò#7Ñ#9Ô#9¸4Ø,0¬Jð8ñ 8ô 8ð 		%Ø;?Ø�JŠJ•|Ñ$Ô$Ð$àœ;×,Ò,Ñ.Ô.ð 0ð 0‘��aØ!Ÿ[š[¨Ñ1Ô1°6¸1¸Q¼4ÀÐF�	Ø× Ò Ð!5¸¸!¼Ñ!<Ñ=Ô=Ð=Ø× Ò  Ñ'Ô'Ð'Ø—
’
˜3Ÿ8š8 IÑ.Ô.Ñ/Ô/Ð/Ð/Ø$ˆDÔð		%ð 		%ð 		%ñ 		%ô 		%ð 		%ð 		%ð 		%ð 		%ð 		%ð 		%ð 		%øøøð 		%ð 		%ð 		%ð 		%ð 		%ð 		%s   ¯B;C7Ã7C;Ã>C;c           
      óÞ  — i | _         |                      ¦   «         }	 t          |dd¬¦  «        }nB# t          $ r5}t	          j        d|t          |¦  «        ¦  «         d| _        Y d}~dS d}~ww xY w|5  |                     ¦   «         }|t          k    r*t	          j        d|¦  «         d| _        	 ddd¦  «         dS |D �]}| 
                    d¦  «        }|d|…                              d	¦  «        }|| j         v rt	          j        d
|z  ¦  «         ŒV|dz  }||d…                              d¦  «        }t          |¦  «        dk    rt	          j        d|z  ¦  «         Œ£|d         }	t          |	¦  «        dk    rt	          j        d|	z  ¦  «         ŒÖt          t!          t"          |dd…         ¦  «        ¦  «        }
|	|
f| j         |<   �Œ	 ddd¦  «         n# 1 swxY w Y   d| _        dS )z›Reinstate cache from file.

        Overwrites existing cache.

        If the cache file has the wrong version marker, this just clears
        the cache.Úrbièý  )Ú	bufferingzfailed to open %s: %sTNz<cache header marker not found at top of %s; discarding cacherV   rU   zduplicated path %r in cacher+   rW   é   zbad line in hashcache: %rr   é(   zbad sha1 in hashcache: %rr	   F)r   r   ÚopenÚOSErrorr   Úmutterr   r'   Úreadliner[   ÚindexÚdecodeÚwarningÚsplitÚlenÚtupleÚmapÚint)r   ÚfnÚinfÚeÚhdrÚlÚposr4   ÚfieldsÚsha1r7   s              r    ÚreadzHashCache.readç   sm  € ð ˆŒà×!Ò!Ñ#Ô#ˆð	Ý�r˜4¨5Ð1Ñ1Ô1ˆCˆCøÝð 	ð 	ð 	ÝŒLÐ0°"µc¸!±f´fÑ=Ô=Ð=à#ˆDÔØˆFˆFˆFˆFˆFøøøøð		øøøð ð 	/ð 	/Ø—,’,‘.”.ˆCØ•lÒ"Ð"Ý”ð 1Ø24ñ6ô 6ð 6à#'�Ô Øð	/ð 	/ð 	/ñ 	/ô 	/ð 	/ð 	/ð 	/ð ð /ñ /�Ø—g’g˜f‘o”o�Ø˜˜#˜”w—~’~ gÑ.Ô.�Ø˜4œ;Ð&Ð&Ý”MÐ"?À$Ñ"FÑGÔGÐGØà�q‘�Ø˜3˜4˜4œŸš tÑ,Ô,�Ý�v‘;”; !Ò#Ð#Ý”MÐ"=ÀÑ"AÑBÔBÐBØà˜a”y�Ý�t‘9”9 ’?�?Ý”MÐ"=ÀÑ"DÑEÔEÐEØå�3�s F¨1¨2¨2¤JÑ/Ô/Ñ0Ô0�à%)¨2 J�”˜DÑ!Ñ!ð)/ð	/ð 	/ð 	/ñ 	/ô 	/ð 	/ð 	/ð 	/ð 	/ð 	/ð 	/øøøð 	/ð 	/ð 	/ð 	/ð< !ˆÔÐÐs-   �0 °
A/º*A*Á*A/Á4=GÂ>DGÇGÇ"Gc                 óH   — t          t          j        ¦   «         ¦  «        dz
  S )zœReturn cutoff time.

        Files modified more recently than this time are at risk of being
        undetectably modified and so can't be cached.
        r+   )rq   Útimer%   s    r    rD   zHashCache._cutoff_time  s   € õ •4”9‘;”;ÑÔ !Ñ#Ð#r"   c                 ó
  — |€'	 t          j        |¦  «        }n# t          $ r Y d S w xY wt          j        |j        ¦  «        rd S |j        t          |j        ¦  «        t          |j	        ¦  «        |j
        |j        |j        fS r$   )ÚosÚlstatrg   r<   ÚS_ISDIRÚst_modeÚst_sizerq   Úst_mtimeÚst_ctimeÚst_inoÚst_dev)r   r6   rG   s      r    r3   zHashCache._fingerprint!  s™   € ØÐðÝœX gÑ.Ô.�
�
øÝð ð ð à�t�tðøøøõ Œ<˜
Ô*Ñ+Ô+ð 	Ø�4ð Ô"¥C¨
Ô(;Ñ$<Ô$<Ý�JÔ'Ñ(Ô(¨*Ô*;ØÔ! :Ô#5ð7ð 	7s   „ ™
'¦'r:   r$   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r'   r!   r   r(   r8   rN   r>   rZ   rz   rD   r3   r,   r"   r    r   r   1   sÚ   € € € € € ð ð  ðB €Kà37Ø/3ð>ð >ð >ð >ð.%ð %ð %ðð ð ð&ð &ð &ð(Eð Eð Eð EðNOð Oð Oð%ð %ð %ð"0!ð 0!ð 0!ðd$ð $ð $ð7ð 7ð 7ð 7ð 7ð 7r"   r   )r[   r~   r<   r|   Ú r   r   r   rP   r   r   rE   rF   r;   r   r,   r"   r    ú<module>rŒ      sº   ðð: )€à 	€	€	€	Ø €€€Ø €€€ðð ð ð ð ð ð ð ð ð ð ð ð ð ð €Ø€Ø€ð}7ð }7ð }7ð }7ð }7ñ }7ô }7ð }7ð }7ð }7r"   