HEX
Server: LiteSpeed
System: Linux shams.tasjeel.ae 5.14.0-611.5.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Nov 11 08:09:09 EST 2025 x86_64
User: infowars (1469)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: //lib64/python3.9/site-packages/borg/__pycache__/cache.cpython-39.opt-1.pyc
a

HZ�hs��@s.ddlZddlZddlZddlZddlZddlmZddlmZddl	m
Z
ddlmZe�Zed�Z
ddlmZmZdd	lmZmZmZdd
lmZddlmZddlmZdd
lmZmZddlmZmZmZmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#m$Z$ddlm%Z%m&Z&ddlm'Z'ddlm(Z(ddl)m*Z*m+Z+ddl,m-Z-ddl.m/Z/m0Z0m1Z1ddl2m3Z3ddl4m5Z5ddl6m7Z7ddl8m9Z9edd�Z:Gd d!�d!�Z;d"d#�Z<d$d%�Z=d6d&d'�Z>d(d)�Z?d*d+�Z@Gd,d-�d-�ZAGd.d/�d/�ZBGd0d1�d1�ZCGd2d3�d3eC�ZDGd4d5�d5eC�ZEdS)7�N)�	unhexlify)�
namedtuple)�perf_counter�)�
create_loggerzborg.debug.files_cache)�CACHE_README�FILES_CACHE_MODE_DISABLED)�
ChunkIndex�ChunkIndexEntry�CacheSynchronizer)�Location)�Error)�Manifest)�
get_cache_dir�get_security_dir)�
int_to_bigint�
bigint_to_int�
bin_to_hex�parse_stringified_list)�format_file_size)�safe_ns)�yes)�remove_surrogates)�ProgressIndicatorPercent�ProgressIndicatorMessage)�set_ec�EXIT_WARNING)�safe_unlink)�msgpack)�ArchiveItem�ChunkListEntry)�PlaintextKey)�IntegrityCheckedFile�DetachedIntegrityCheckedFile�FileIntegrityError)�Lock)�SaveFile)�cache_if_remote)�LIST_SCAN_LIMIT�FileCacheEntryzage inode size cmtime chunk_idsc@s�eZdZdZdd�Zeddd��Zdd�Zd	d
�Zdd�Z	dd
d�Z
ddd�Zddd�Zdddd�dd�Z
ddd�Zdd�ZdS) �SecurityManageras
    Tracks repositories. Ensures that nothing bad happens (repository swaps,
    replay attacks, unknown repositories etc.).

    This is complicated by the Cache being initially used for this, while
    only some commands actually use the Cache, which meant that other commands
    did not perform these checks.

    Further complications were created by the Cache being a cache, so it
    could be legitimately deleted, which is annoying because Borg didn't
    recognize repositories after that.

    Therefore a second location, the security database (see get_security_dir),
    was introduced which stores this information. However, this means that
    the code has to deal with a cache existing but no security DB entry,
    or inconsistencies between the security DB and the cache which have to
    be reconciled, and also with no cache existing but a security DB entry.
    cCsV||_t|j�|_t|�|_tj�|jd�|_tj�|jd�|_	tj�|jd�|_
dS)Nzkey-type�locationzmanifest-timestamp)�
repositoryr�id_str�dir�	cache_dir�os�path�join�
key_type_file�
location_file�manifest_ts_file)�selfr,�r7�0/usr/lib64/python3.9/site-packages/borg/cache.py�__init__?s
zSecurityManager.__init__NcCs(|pt|j�}tj�|�r$t�|�dS)z:destroy the security dir for ``repository`` or at ``path``N)rr-r0r1�exists�shutil�rmtree�r,r1r7r7r8�destroyGszSecurityManager.destroycCstdd�|j|j|jfD��S)Ncss|]}tj�|�VqdS�N)r0r1r:)�.0�fr7r7r8�	<genexpr>Os�z(SecurityManager.known.<locals>.<genexpr>)�allr3r4r5�r6r7r7r8�knownNs�zSecurityManager.knownc
Cs�|��sdSzFt|j��(}|��}|t|j�kWd�WS1sF0YWn0ty�}zt�d|�WYd}~n
d}~00dS)NFz&Could not read/parse key type file: %s)	rE�openr3�read�str�TYPE�OSError�logger�warning)r6�key�fd�type�excr7r7r8�key_matchesRs2zSecurityManager.key_matchescCs�t�d|jj|j�|jj��}t�d|�t�dt|j��t�d|j	�t
|j��}|�|�Wd�n1sv0Yt
|j
�� }|�t|j��Wd�n1s�0Yt
|j��}|�|j	�Wd�n1s�0YdS)Nz#security: saving state for %s to %szsecurity: current location   %szsecurity: key type           %szsecurity: manifest timestamp %s)rK�debugr,r-r.�	_location�canonical_pathrHrI�	timestampr&r4�writer3r5)r6�manifestrMZcurrent_locationrNr7r7r8�save\s(.zSecurityManager.savec
CsZzBt|j��}|��}Wd�n1s*0Yt�d|�WnVtyft�d|j�d}Yn4ty�}zt�d|�d}WYd}~n
d}~00|r�|jr�||jkr�|j}t�d|�|j	j
��}|�rV||k�rVd�||�d}t
|ddd	d
d��st���t�d�t|j��}|�|�Wd�n1�s>0Y|�rV|��dS)
Nz#security: read previous location %rz-security: previous location file %s not found�)Could not read previous location file: %sz.security: using previous_location of cache: %rzDWarning: The repository at location {} was previously located at {}
zDo you want to continue? [yN] �	Aborting.�Invalid answer, aborting.FZ BORG_RELOCATED_REPO_ACCESS_IS_OK�Z	false_msgZinvalid_msgZretryZenv_var_overridez<security: updating location stored in cache and security dir)rFr4rGrKrR�FileNotFoundErrorrJrL�previous_locationr,rSrT�formatr�Cache�RepositoryAccessAbortedr&rVrX)r6�cache_configrNr^rPZrepository_location�msgr7r7r8�assert_location_matchesis:&���
*z'SecurityManager.assert_location_matchesc
Cs�zBt|j��}|��}Wd�n1s*0Yt�d|�WnVtyft�d|j�d}Yn4ty�}zt�d|�d}WYd}~n
d}~00|r�t||j	p�d�}t�d|�|r�||j	kr�t
|t�r�t�
��nt���dS)Nz$security: read manifest timestamp %rz.security: manifest timestamp file %s not found�rYz4security: determined newest manifest timestamp as %s)rFr5rGrKrRr]rJrL�maxrU�
isinstancer!r`�RepositoryIDNotUnique�RepositoryReplay)r6rWrMrbrNrUrPr7r7r8�assert_no_manifest_replay�s"&

z)SecurityManager.assert_no_manifest_replaycCsD|r&|jdur&|jt|j�kr&t���|��r@|�|�s@t���dSr?)�key_typerHrIr`�EncryptionMethodMismatchrErQ)r6rMrbr7r7r8�assert_key_type�szSecurityManager.assert_key_typeT)rb�warn_if_unencrypted�	lock_waitcCs�|�|||�|r"|�|||�nVt|j|d�}|��rl|�|�|||�Wd�qx1s`0Yn|�||�t�d�dS)N�roz/security: repository checks ok, allowing access)�assert_access_unknown�_assert_secure�CacheConfigr,r:rKrR)r6rWrMrbrnror7r7r8�
assert_secure�s.zSecurityManager.assert_securecCsF|�|�|�||�|�|||�|��sBt�d�|�||�dS)Nz3security: remembering previously unknown repository)rdrmrjrErKrRrX)r6rWrMrbr7r7r8rr�s

zSecurityManager._assert_securecCsb|js^|��s^d}|p(t|ddddd�}|rV|r>t�d�n
t�d�|�||�nt���dS)	NziWarning: Attempting to access a previously unknown unencrypted repository!
Do you want to continue? [yN] rZr[FZ*BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OKr\zIsecurity: remembering unknown unencrypted repository (explicitly allowed)z-security: initializing unencrypted repository)Zlogically_encryptedrErrKrRrXr`�CacheInitAbortedError)r6rnrWrMrcZallow_accessr7r7r8rq�s�
z%SecurityManager.assert_access_unknown)N)N)N)N)N)�__name__�
__module__�__qualname__�__doc__r9�staticmethodr>rErQrXrdrjrmrtrrrqr7r7r7r8r*+s

 


r*cCst|�}|j||j|d�dS)Nrp)r*rtrM)r,rWroZsmr7r7r8rt�srtcCs�|j��}t|�}t|�}|j|jkr�|j|jkr�|j|jkr�|j|jkr�|jr�|jr�|j�d�r�|j�d�r�|jdd�|jdd�kr�|S|SdS)Nz/~/z/./�)	rSrTr�proto�user�host�portr1�
startswith)Zcache_locationr,Z
repo_locationZrlZclr7r7r8� recanonicalize_relative_location�s 
0��
�
��r�cCs|ptj�t�|j�Sr?)r0r1r2rr-r=r7r7r8r/�sr/cCstj�dd�}|rd|SdS)NZBORG_FILES_CACHE_SUFFIXre�files.�files)r0�environ�get��suffixr7r7r8�files_cache_name�sr�cCsdd�t�|�D�dS)NcSs"g|]}|dks|�d�r|�qS)r�r�)r��r@�fnr7r7r8�
<listcomp>��z-discover_files_cache_name.<locals>.<listcomp>r)r0�listdir)r1r7r7r8�discover_files_cache_name�sr�c@s`eZdZddd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	ddd�Z
dd�Zdd�ZdS)rsNcCs4||_t||�|_tj�|jd�|_d|_||_dS)N�config)r,r/r1r0r2�config_path�lockro)r6r,r1ror7r7r8r9�s
zCacheConfig.__init__cCs|��|Sr?)rFrDr7r7r8�	__enter__�szCacheConfig.__enter__cCs|��dSr?��close�r6�exc_typeZexc_valZexc_tbr7r7r8�__exit__�szCacheConfig.__exit__cCstj�|j�Sr?)r0r1r:r�rDr7r7r8r:szCacheConfig.existscCs�tjdd�}|�d�|�ddd�|�dd|jj�|�ddd�|�d�|�ddd�t|j��}|�|�Wd�n1s�0YdS)	N�Z
interpolation�cache�version�1r,rWre�	integrity)	�configparser�ConfigParser�add_section�setr,r-r&r�rV)r6r�rNr7r7r8�creates

zCacheConfig.createcCs.ttj�|jd�d|jd���|_|��dS)Nr�T)�	exclusive�timeout)r%r0r1r2ro�acquirer��loadrDr7r7r8rFs"zCacheConfig.opencCs�tjdd�|_t|j��}|j�|�Wd�n1s:0Y|�|j�|j�dd�|_t	|j�dd��|_
|jjdddd�|_|jjdddd�|_t
t|jjddd	d���|_t
t|jjdd
d	d���|_zBt|j�d��|_|j�dd�|j�d�k�ri|_t�d�Wn&tj�y<t�d
�i|_Yn0|jjdddd�}|�rft||j�|_nd|_|j�
dd|jj���dS)Nr�r�r,rWrU)�fallbackrk�ignored_featuresre�mandatory_featuresr�zHCache integrity data not available: old Borg version modified the cache.zTCache integrity: No integrity data found (files, chunks). Cache is from old version.r^)r�r��_configrFr�Z	read_file�_check_upgrader��idr�manifest_idrUrkr�rr�r��dict�itemsr��poprKrL�NoSectionErrorrRr�r,r^rSrT)r6rNr^r7r7r8r�s.*
zCacheConfig.loadcCs�|r�|j�dd|j�|j�dd|j�|j�ddd�|j��|j�ddd�|j��|j�d�sp|j�d�|j	�
�D]\}}|j�d||�qz|j�dd|j�|r�|j�ddt|j��t
|j��}|j�|�Wd�n1s�0YdS)	Nr�rWrUr��,r�r�rk)r�r�r-rUr2r�r�Zhas_sectionr�r�r�rHrIr&r�rV)r6rWrM�file�integrity_datarNr7r7r8rX3szCacheConfig.savecCs|jdur|j��d|_dSr?)r��releaserDr7r7r8r�Cs

zCacheConfig.closecCshz8|j�dd�}d}||kr6|��td|||f��Wn*tjyb|��td|�d�Yn0dS)Nr�r�rz0%s has unexpected cache version %d (wanted: %d).z#%s does not look like a Borg cache.)r�Zgetintr��	Exceptionr�r�)r6r�Z
cache_versionZwanted_versionr7r7r8r�Hs�
zCacheConfig._check_upgrade)NN)NN)
rvrwrxr9r�r�r:r�rFr�rXr�r�r7r7r7r8rs�s
 
rsc	@s�eZdZdZGdd�de�ZGdd�de�ZGdd�de�ZGdd	�d	e�ZGd
d�de�Z	e
dd
d��Ze
ddd��Zdddddde
ddf	dd�ZdS)r`zClient Side cache
    c@seZdZdZdS)zCache.RepositoryIDNotUniquez`Cache is newer than repository - do you have multiple, independently updated repos with same ID?N�rvrwrxryr7r7r7r8rhXsrhc@seZdZdZdS)zCache.RepositoryReplayz�Cache, or information obtained from the security directory is newer than repository - this is either an attack or unsafe (multiple repos with same ID)Nr�r7r7r7r8ri[sric@seZdZdZdS)zCache.CacheInitAbortedErrorzCache initialization abortedNr�r7r7r7r8ru^sruc@seZdZdZdS)zCache.RepositoryAccessAbortedzRepository access abortedNr�r7r7r7r8raasrac@seZdZdZdS)zCache.EncryptionMethodMismatchzLRepository encryption method changed since last access, refusing to continueNr�r7r7r7r8rldsrlNcCs(t||�}ttj�|d�dd���dS)Nr�T)r�)r/r%r0r1r2�
break_lockr=r7r7r8r�gs
zCache.break_lockcCsH|ptj�t�|j�}tj�|d�}tj�|�rDt�|�t�|�dS)z3destroy the cache for ``repository`` or at ``path``r�N)	r0r1r2rr-r:�remover;r<)r,r1r�r7r7r8r>ls

z
Cache.destroyTFc
s�����������	�
fdd�}
������fdd�}|	s@|
�St����}|��r�|�|j�jk}Wd�n1sz0Y|r�t�d�|
�St�d�|�S)Nc
st�����	�
�����d�S)N)r,rMrWr1�syncrn�progress�iecro�
cache_mode�consider_part_files)�
LocalCacher7�r�r�r�rMrorWr1r�r,r�rnr7r8�localys�zCache.__new__.<locals>.localcst������d�S)N)r,rMrWror�r�)�
AdHocCacher7)r�r�rMrorWr,r7r8�adhoc~s�zCache.__new__.<locals>.adhocz%Cache: choosing local cache (in sync)zKCache: choosing ad-hoc cache (local cache does not exist or is not in sync))rsr:r�r�rKrR)�clsr,rMrWr1r�rnr�roZpermit_adhoc_cacher�r�r�r�r�rbZ
cache_in_syncr7r�r8�__new__us *

z
Cache.__new__)N)N)rvrwrxryr
rhrirurarlrzr�r>rr�r7r7r7r8r`Us�r`c@sHeZdZdZddd�Zdd�Zedgd��Zd	d
�Zdd�Z	d
d�Z
dS)�CacheStatsMixinz�All archives:   {0.total_size:>20s} {0.total_csize:>20s} {0.unique_csize:>20s}

                       Unique chunks         Total chunks
Chunk index:    {0.total_unique_chunks:20d} {0.total_chunks:20d}FcCs||_i|_dSr?)r��
pre12_meta)r6r�r7r7r8r9�szCacheStatsMixin.__init__cCs|j�|���Sr?)�
str_formatr_�format_tuplerDr7r7r8�__str__�szCacheStatsMixin.__str__�Summary)�
total_size�total_csize�unique_size�unique_csize�total_unique_chunks�total_chunkscCs�ddlm}|j��\}}}}}}d\}}|jjD]@}||j|j|j||jd�}	|	j	|dd�}
||
j
7}||
j7}q2|�||||||��
�}
|
S)Nr)�Archive)rr)r�F)Zwant_unique)�archiver��chunksZ	summarizerW�archivesr,rMr�Z
calc_statsZosize�csizer��_asdict)r6r�r�r�r�r�r�r��archive_namer��statsr7r7r8r��s�
�zCacheStatsMixin.statscCs8|��}dD]}t|||jd�||<q|jfi|��S)N)r�r�r��r�)r�rr�r�)r6r�Zfieldr7r7r8r��szCacheStatsMixin.format_tuplecCs|��dS)Nr�)r�rDr7r7r8�chunks_stored_size�sz"CacheStatsMixin.chunks_stored_sizeN)F)rvrwrxr�r9r�rr�r�r�r�r7r7r7r8r��s
r�c@s�eZdZdZdddddeddfdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd/d#d$�Zd0d%d&�Zd1d'd(�Zd2d)d*�Zd+d,�Zd-d.�ZdS)3r�z0
    Persistent, local (client-side) cache.
    NTFcCstj||d�||_||_||_||_|	|_|
|_d|_d|_	t
||�|_t|�|_
t|j|j|�|_tj�|j�s�|j
�|||�|��|��zT|j
j|||jd�|��s�|��|��|r�|jj|jjkr�|��|��Wn|���Yn0dS)aO
        :param warn_if_unencrypted: print warning if accessing unknown unencrypted repository
        :param lock_wait: timeout for lock acquisition (int [s] or None [wait forever])
        :param sync: do :meth:`.sync`
        :param cache_mode: what shall be compared in the file stat infos vs. cached stat infos comparison
        r�NF)rb)r�r9r,rMrWr�r�r�rU�
txn_activer/r1r*�security_managerrsrbr0r:rqr�rFrt�check_cache_compatibility�
wipe_cache�update_compatibilityr�r�r��commitr�)r6r,rMrWr1r�rnr�ror�r�r�r7r7r8r9�s6	
zLocalCache.__init__cCs|Sr?r7rDr7r7r8r��szLocalCache.__enter__cCs|��dSr?r�r�r7r7r8r��szLocalCache.__exit__cCst�|j�ttj�|jd�d��}|�t�Wd�n1sB0Y|j��t	��tj�|jd��t�tj�|jd��t
tj�|jt��dd��Wd�n1s�0Yt
tj�|jd�d	d��"}tj
|j|d
d�Wd�n1s�0YdS)z0Create a new empty cache at `self.path`
        ZREADME�wNr��chunks.archive.dT�Zbinary�
pre12-metaF���indent)r0�makedirsr1rFr2rVrrbr�r	r&r��json�dumpr��r6rNr7r7r8r��s(
zLocalCache.createc	Cs�|j��ttj�|jd�d|jj�d�d��}t�	|�|_
Wd�n1sP0Yd|jvrld|_n|�
�zDttj�|jd���}t�|�|_Wd�n1s�0YWnttjfy�Yn0dS)Nr�F�r1rVr��dr�)rbr�r"r0r1r2r�r�r	rGr�r�r��_read_filesrFr�r�r]ZJSONDecodeErrorr�r7r7r8�_do_opens
�*
.zLocalCache._do_opencCs2tj�|j�std|j��|j��|��dS)Nz"%s Does not look like a Borg cache)r0r1�isdirr�rbrF�rollbackrDr7r7r8rFs
zLocalCache.opencCsfttj�|jd�d��"}tj|j|dd�Wd�n1s>0Y|jdurb|j��d|_dS)Nr�r�r�r�)	rFr0r1r2r�r�r�rbr�r�r7r7r8r� s
0

zLocalCache.closec
Cs�i|_d|_t�d�t�d�d}z�ttj�|jt	��d|j
j�t	��d���}t
jdd�}|�d�}|slq�|�|�z8|D].\}}t|�}t
�|j|jdd	��|j|<q|Wq\ttfy�}z$d
t|�}WYd}~q�WYd}~q\d}~00q\Wd�n1�s0YWnbt�yF}zdt|�}WYd}~n:d}~0t�yv}	zdt|	�}WYd}	~	n
d}	~	00|du�r�t�|�t�d
�i|_t�dt|j��dS)NzReading files cache ...zFILES-CACHE-LOAD: starting...Fr�T)Zuse_listir)�agez#The files cache seems invalid. [%s]z#The files cache can't be read. [%s]z"The files cache is corrupted. [%s]z:Continuing without files cache - expect lower performance.z.FILES-CACHE-LOAD: finished, %d entries loaded.)r��_newest_cmtimerKrR�files_cache_loggerr"r0r1r2r�rbr�r�rZUnpackerrG�feedr)�packb�_replacer��	TypeError�
ValueErrorrHrJr$rL�len)
r6rcrN�u�data�	path_hash�item�entryrP�fier7r7r8r�(s>

�

$H "


zLocalCache._read_filescCstdd�}tj�|jd�}t�|�|�d�t�tj�|jd�|�|�d�t�tj�|jd�|�|�d�zt�tj�|jt��|�WnJt	y�t
tj�|t��d	d
��Wd�n1s�0YYn0t�tj�|jd�tj�|jd��d	|_|�
�dS)Nzcache.begin_transaction��msgid�txn.tmpz.Initializing cache transaction: Reading configr�z.Initializing cache transaction: Reading chunksr�z-Initializing cache transaction: Reading filesTr��
txn.active)rr0r1r2�mkdir�outputr;�copyr�r]r&�renamer��finish)r6�pi�txn_dirr7r7r8�	begin_txnIs$




$�zLocalCache.begin_txncCs|js
dS|j�|j|j�tdd�}|jdu�rF|jdurBd|_tt	j
�dd��}|�d�t
�d�tt	j�|jt��d	d
��|}d}|j��D]\\}}tt�|��}|jdkr�t|j�|jks�|jdkr�|j|kr�t�||f|�|d7}q�Wd�n1�s0Yt
�d
|�t
�d|j�t
�d|�|j|jjt�<|�d�tt	j�|jd�d	d
��}|j�|�Wd�n1�s�0Y|j|jjd<|�d�|j�|j|j�t	� t	j�|jd�t	j�|jd��t!�"t	j�|jd��d|_|�#�dS)zCommit transaction
        Nzcache.commitrl����ZBORG_FILES_CACHE_TTL�zSaving files cachezFILES-CACHE-SAVE: starting...T�r1rVrrz>FILES-CACHE-KILL: removed all old entries with age >= TTL [%d]zCFILES-CACHE-KILL: removed all current entries with newest cmtime %dz7FILES-CACHE-SAVE: finished, %d remaining entries saved.zSaving chunks cacher�zSaving cache configrrF)$r�r�rXrWrMrr�r��intr0r�r�rr�rRr"r1r2r�r�r)r�unpackbr�r�cmtime�packr�rbr�r�rVrr;r<r	)r6r
ZttlrNZentry_countr�r�r�r7r7r8r�]sJ



��*
,
�zLocalCache.commitcCs�tj�tj�|jd��r.t�tj�|jd��tj�|jd�}tj�|�r�t�tj�|d�|j�t�tj�|d�|j�t�tj�|t|��|j�t�|tj�|jd��tj�tj�|jd��r�t�tj�|jd��d|_	|�
�dS)z3Roll back partial and aborted transactions
        rrr�r�FN)r0r1r:r2r;r<rr�rr�r�)r6rr7r7r8r��szLocalCache.rollbackcs`tj��jd��t��d�
d�d�d�
d�	d�fdd�	���fdd���fdd	���fd
d��d �fd
d�	����	�
fdd����
���fdd����fdd�����fdd���fdd���������	�
��
����fdd�}��fdd�}�j�tjjf���	�t
�j�jd��0�|�tj�
���_|�j��_Wd�n1�sR0YdS)!a�Re-synchronize chunks cache with repository.

        Maintains a directory with known backup archive indexes, so it only
        needs to fetch infos from repo and build a chunk index once per backup
        archive.
        If out of sync, missing archive indexes get added, outdated indexes
        get removed and a new master chunks index is built by merging all
        archive indexes.
        r�rrecst|�}tj��||�}|Sr?)rr0r1r2)r�r�Zid_hexr1)�archive_pathr7r8�mkpath�szLocalCache.sync.<locals>.mkpathcs6�jr,t���}dd�|D�dd�|D�BSt�SdS)NcSs h|]}t|�dkrt|��qS)�@)r�rr�r7r7r8�	<setcomp>�r�z;LocalCache.sync.<locals>.cached_archives.<locals>.<setcomp>cSs2h|]*}t|�dkr|�d�rt|dd���qS)�H�.compactNr)r��endswithrr�r7r7r8r�r�)�do_cacher0r�r�)Zfns�rr6r7r8�cached_archives�s
�z(LocalCache.sync.<locals>.cached_archivescsdd��jj��D�S)NcSsh|]
}|j�qSr7)r�)r@�infor7r7r8r�r�z9LocalCache.sync.<locals>.repo_archives.<locals>.<setcomp>)rWr��listr7rDr7r8�
repo_archives�sz&LocalCache.sync.<locals>.repo_archivescs|D]}�|�qdSr?r7)�idsr�)�cleanup_cached_archiver7r8�cleanup_outdated�sz)LocalCache.sync.<locals>.cleanup_outdatedTcs�z$t��|��t��|�d�Wnty6Yn0|s@dSz,t��|dd��t��|dd�d�Wnty~Yn0dS)Nz
.integrityrr�)r0�unlinkr])r��cleanup_compact)rr7r8r!�sz/LocalCache.sync.<locals>.cleanup_cached_archivecs�|��}g}t��rT|D]8}��|�}|rF||j|jd�}|||<q|�|�qn|}t|�j�|��D]@\}}||jt|�d�}|||<|�|<�d7��t|�7�qjdS)z�
            Archives created with AdHocCache will have csize=0 in all chunk list entries whose
            chunks were already in the repository.

            Scan *chunk_idx* for entries where csize=0 and fill in the correct information.
            )r�rN)	Zzero_csize_idsr�r�r�r��append�zipr,�get_many)�	chunk_idxZall_missing_idsZ	fetch_ids�id_Zalready_fetched_entryr�r�)�chunks_fetched_size_index�decrypted_repository�fetched_bytes_for_csize�fetched_chunks_for_csizer7r8�fetch_missing_csize�s 


z,LocalCache.sync.<locals>.fetch_missing_csizec
s�|�|�\}}|�|dt|�|��jj|dd�\}}}t|d�}|jdkrTtd��t|�}t	|j
|�|j
��D]>\}	\}}|�|	dt|�|��t|�7��d7�|�|�qp�j
rȈ|��||�dS)NrT)Zforce_tam_not_required)Z
internal_dictz Unknown archive metadata version)r��addr�rMZunpack_and_verify_archiverr�r�rr&r�r'r�r)
�
archive_idr+r(r�r�r�Zverified�_r�Zitem_id)r.�processed_item_metadata_bytes�processed_item_metadata_chunksr6�write_archive_indexr7r8�fetch_and_build_idx�s

 z,LocalCache.sync.<locals>.fetch_and_build_idxcs��|��7��|dd�}�|dd�}zDt|dt|�dd��}|�|�Wd�n1s\0YWnty�t|�Yn0t�||�dS)Nrr�z.tmpT)r1rV�filename)Zcompactr#rrVr�rr0r)r0r(r�Zfn_tmprN)�"compact_chunks_archive_saved_spacerr7r8r4s
�,z,LocalCache.sync.<locals>.write_archive_indexcs�|�}t�d|�z�zRt|ddd��}tj|dd�}Wd�n1sL0Y�|dd�|WWSty�t|dd��}t�|�}Wd�n1s�0YYn0WnFt�y�}z,t�d||��|�tt	�WYd}~dSd}~00t�
d	|��|��||�|S)
Nz-Reading cached archive chunk index for %s ...rFrT)Zpermit_compact)r$z1Cached archive chunk index of %s is corrupted: %sz6Found non-compact index for %s, converting to compact.)rKrr#r	rGr]r$�errorrrrR)r0r�Zarchive_chunk_idx_pathrN�archive_chunk_idxr)r!rr4r7r8�read_archive_indexs(,2
z+LocalCache.sync.<locals>.read_archive_indexcs0i}�jj��D]}|j|vr|j||j<q|Sr?)rWr�rr��name)�archive_idsZ
archive_namesrrDr7r8�get_archive_ids_to_names/s

z1LocalCache.sync.<locals>.get_archive_ids_to_namesc		s�t�d���}��}t�dt|�t|�t||�t||��|���||�t�
j�}|�r��
jsndnt|d�}tt|�dddd�}�|�}|��D]�\}}|j	t
|�gd��
j�r ||vr�||�}|dur�|�|�||v�r
t�d	|�t�}�|�|�t�d
�|�|�q�|�p.t|d�}t�d|��|�|�q��
j�sZ�|�|�
�t�dt����t�d
t�	��
�t�dt���t�d�|S)NzSynchronizing chunks cache...zIArchives: %d, w/ cached Idx: %d, w/ outdated Idx: %d, w/o cached Idx: %d.�Zusableg�������?z3%3.0f%% Syncing chunks cache. Processing archive %sz
cache.sync)�total�steprcr)rz.Fetching and building archive index for %s ...z$Merging into master chunks index ...z!Fetching archive index for %s ...zlCache sync: had to fetch %s (%d chunks) because no archive had a csize set for them (due to --no-cache-sync)z0Cache sync: processed %s (%d chunks) of metadataz;Cache sync: compact chunks.archive.d storage saved %s byteszDone.)rKrr��clearr,rr	rr��showrr��merger	rRr)	r(Z
cached_idsr<Zmaster_index_capacityr
Zarchive_ids_to_namesr0r�r9)rr"r7r+r5r.r,r-r=r2r3r:rr6r7r8�create_master_idx;s^
�

�



���
z*LocalCache.sync.<locals>.create_master_idxcspzt�tj��jd��WnYn0zt�tj��jd��WnYn0zt���WnYn0dS)z?bring old cache dirs into the desired state (cleanup and adapt)zchunks.archivezchunks.archive.tmpN)r0r#r1r2rr7rr7r8�legacy_cleanupnsz'LocalCache.sync.<locals>.legacy_cleanup)Zdecrypted_cacheN)re)T)r0r1r2r	rWZcheck_repository_compatibilityrZ	OperationZREADrr'r,rMr�rr�)r6rDrEr7)rrr*r!r"r7r+r5r.r,r-r=rr2r3r:rr6r4r8r��s2

!&3zLocalCache.synccCs*tj}|jj|@rdS|jj|ks&dSdS)NFT)r�SUPPORTED_REPO_FEATURESrbr�r�)r6�my_featuresr7r7r8r��sz$LocalCache.check_cache_compatibilitycCs�t�d�tj�|jd�}tj�|�rRt�tj�|jd��t�tj�|jd��t	�|_
ttj�|jt��dd��Wd�n1s�0Yd|j
_|j
j�ddd�t�|j
_t�|j
_dS)Nz9Discarding incompatible cache and forcing a cache rebuildr�Tr�rer�rW)rKrLr0r1r2r�r;r<r�r	r�r&r�rbr�r�r�r�r�)r6rr7r7r8r��s

zLocalCache.wipe_cachecCsZ|j��}tj}t�}|��D]\}}|�|�q|jj�||�|jj	�||@�dSr?)
rWZget_all_mandatory_featuresrrFr�r��updaterbr�r�)r6Zoperation_to_features_maprGZ
repo_featuresZ	operationZfeaturesr7r7r8r��s
zLocalCache.update_compatibilityc
Cs�|js|��t|�}|�||�}|r6|s6|�||�S|j�|�}t|�}	|jj|||d�|j	�
|d||	�|�||	|�t|||	�S)N��waitr)
r�rr��
seen_chunk�chunk_increfrM�encryptr,�putr�r/rHr �
r6r��chunkr�Z	overwriterJ�size�refcountr�r�r7r7r8�	add_chunk�szLocalCache.add_chunkcCsJ|j�|tddd��\}}}|durF|durF||krFtd|||f��|S)Nrz@chunk has same id [%r], but different size (stored: %d new: %d)!)r�r�r
r�)r6r�rQrRZstored_sizer1r7r7r8rK�s�zLocalCache.seen_chunkcCs>|js|��|j�|�\}}}|j||d|d�t|||�S�NF��part)r�rr��increfrHr �r6r�r�rQrV�count�_sizer�r7r7r8rL�s
zLocalCache.chunk_increfcCsr|js|��|j�|�\}}}|dkrX|j|=|jj||d�|j||d|d�n|j||d|d�dS�NrrITrUF)r�rr��decrefr,�deleterH�r6r�r�rJrVrYrQr�r7r7r8�chunk_decref�szLocalCache.chunk_decrefcCs2t�|j�sdS|j}d|vr,t�d�dSd|vrBt�d�dS|j�|�}|sbt�d|�dStt	�
|��}d|vr�|j|jkr�t�d|�d	Sd
|vr�|j
|jkr�t�d|�d	Sd|vr�t|j�|jkr�t�d
|�d	Sd|v�rt|j�|jk�rt�d|�d	St	�|j|jdd��|j|<d|jfS)aj
        Check if we know the file that has this path_hash (know == it is in our files cache) and
        whether it is unchanged (the size/inode number/cmtime is same for stuff we check in this cache_mode).

        :param hashed_path: the file's path as we gave it to hash(hashed_path)
        :param path_hash: hash(hashed_path), to save some memory in the files cache
        :param st: the file's stat() result
        :return: known, ids (known is True if we have infos about this file in the cache,
                             ids is the list of chunk ids IF the file has not changed, otherwise None).
        �FNr�zUNKNOWN: files cache disabled�rzUNKNOWN: rechunking enforcedz*UNKNOWN: no file metadata in cache for: %r�sz(KNOWN-CHANGED: file size has changed: %r)TN�iz0KNOWN-CHANGED: file inode number has changed: %r�cz)KNOWN-CHANGED: file ctime has changed: %r�mz)KNOWN-CHANGED: file mtime has changed: %rr)�inoder�T)�stat�S_ISREG�st_moder�r�rRr�r�r)rrrQ�st_sizerf�st_inorr�st_ctime_ns�st_mtime_nsr�r��	chunk_ids)r6�hashed_pathr��str�r�r7r7r8�file_known_and_unchanged�s8

	z#LocalCache.file_known_and_unchangedc	Cs�t�|j�sdS|j}d|vr,t�d�dSd|vrDd}t|j�}n&d|vr\d}t|j�}nd}t|j�}t	d|j
|jt|�|d�}t
�|�|j|<t|jp�d|�|_t�d	|jd
t|j�d�||�dS)Nr�z*FILES-CACHE-NOUPDATE: files cache disabledrd�ctimere�mtimer)r�rfrQrrnz)FILES-CACHE-UPDATE: put %r [has %s] <- %rz[%d entries])rn)rgrhrir�r�rRrrlrmr)rkrjrrr�r�rfr�r�r�rn)	r6ror�rpr r�Zcmtime_typeZ	cmtime_nsr�r7r7r8�
memorize_files*

�zLocalCache.memorize_file)FT)N)NF)TF)rvrwrxryrr9r�r�r�r�rFr�r�rr�r�r�r�r�r�rSrKrLr_rqrtr7r7r7r8r��s4�
,!)s


	

4r�c@s�eZdZdZdZddd�Zdd	�Zd
d�ZdZdZ	d
d�Z
dd�Zd dd�Zd!dd�Z
d"dd�Zd#dd�Zdd�Zdd�Zdd�ZdS)$r�a�
    Ad-hoc, non-persistent cache.

    Compared to the standard LocalCache the AdHocCache does not maintain accurate reference count,
    nor does it provide a files cache (which would require persistence). Chunks that were not added
    during the current AdHocCache lifetime won't have correct size/csize set (0 bytes) and will
    have an infinite reference count (MAX_VALUE).
    z�All archives:                unknown              unknown              unknown

                       Unique chunks         Total chunks
Chunk index:    {0.total_unique_chunks:20d}             unknownTNFcCs\tj||d�||_||_||_||_d|_t|�|_|jj	|||d�i|_
t�d�dS)Nr�Frpz1Note: --no-cache-sync is an experimental feature.)
r�r9r,rMrWr��_txn_activer*r�rtr�rKrL)r6r,rMrWrnror�r�r7r7r8r95s
zAdHocCache.__init__cCs|Sr?r7rDr7r7r8r�FszAdHocCache.__enter__cCsdSr?r7r�r7r7r8r�IszAdHocCache.__exit__r�cCst�d�dS)Nz$UNKNOWN: files cache not implementedr`)r�rR)r6ror�rpr7r7r8rqOs
z#AdHocCache.file_known_and_unchangedcCsdSr?r7)r6ror�rpr r7r7r8rtSszAdHocCache.memorize_filec
Cs�|js|��t|�}|�||�}|r6|j|||d�S|j�|�}t|�}	|jj|||d�|j	�
|d||	�|�||	|�t|||	�S)N�rQrIr)
rurr�rKrLrMrMr,rNr�r/rHr rOr7r7r8rSVszAdHocCache.add_chunkcCsL|js|��|j�|tddd��}|jrF|rF|jsF|j|d�|j|<|jS)Nrrv)rurr�r�r
rRrQr�)r6r�rQr�r7r7r8rKeszAdHocCache.seen_chunkcCsF|js|��|j�|�\}}}|p&|}|j||d|d�t|||�SrT)rurr�rWrHr rXr7r7r8rLpszAdHocCache.chunk_increfcCsr|js|��|j�|�\}}}|dkrX|j|=|jj||d�|j||d|d�n|j||d|d�dSr[)rurr�r\r,r]rHr^r7r7r8r_{szAdHocCache.chunk_decrefcCs&|js
dS|j�|j|j�d|_dS�NF)rur�rXrWrMrDr7r7r8r��szAdHocCache.commitcCsd|_|`dSrw)rur�rDr7r7r8r��szAdHocCache.rollbackc
	Cs�d|_t|j�}t|dd�|_t|ddd�}t�}d}d}|jjt|d�}|d	7}|sZq�|j	t|�d
�|d}t
tjddd�}|D]}||j|<q�q<|j|jj
=t�|p�d
}	|��t�d||	|t|d|	��dS)NTg�������?r>z!Downloading chunk list... %3.0f%%zcache.download_chunks)r?rcrr)�limit�markerr)Zincrease���)rRrQr�g{�G�z�?zBAdHocCache: downloaded %d chunk IDs in %.2f s (%d requests), ~%s/s�")rur�r,r	r�rrrr(rBr
Z	MAX_VALUErWZMANIFEST_IDr	rKrRr)
r6Z
num_chunksr
Zt0Znum_requestsry�resultZ
init_entryr)Zdurationr7r7r8r�s0
��zAdHocCache.begin_txn)TNFF)FT)N)NF)TF)rvrwrxryr�r9r�r�r�r�rqrtrSrKrLr_r�r�rr7r7r7r8r�%s$	�




r�)N)Fr�r�r0r;rgZbinasciir�collectionsr�timerrKrr�Z	constantsrrZ	hashindexr	r
rZhelpersrr
rrrrrrrrrrrrrrrrrr�rr Z
crypto.keyr!Zcrypto.file_integrityr"r#r$Zlockingr%�platformr&Zremoter'r,r(r)r*rtr�r/r�r�rsr`r�r�r�r7r7r7r8�<module>sb
%
dA0c