o
    +ke                     @   s  d dl Z d dlZd dlZddlT ddlmZ ddlmZmZ ddl	m
Z
 ddlmZ dd	lmZ e je jed
Zdd Zdd Zdd Zdd Ze dd Zejddgddd Zdd Ze dd Zdd Zejje  d d!d"d# Z ejje  d d!d$d% Z!dS )&    N   )*)
KeyfileKey)AtticRepositoryUpgraderAtticKeyfileKey)get_keys_dir)
Repository   )are_hardlinks_supportedzattic.tar.gzc                    sX    fdd}t | d}|j|||d W d   n1 s w   Y  tj| S )zo
    extract <tarfname> tar archive to <path>, all stuff starting with <what>.

    return path to <what>.
    c                 3   s"    | D ]}|j  r|V  qd S N)name
startswith)memberstarinfowhat ;usr/lib/python3.10/site-packages/borg/testsuite/upgrader.pyfiles   s   zuntar.<locals>.filesr)r   N)tarfileopen
extractallospathjoin)Ztarfnamer   r   r   tfr   r   r   untar   s
   r   c                 C   s>   t t| ddd}| W  d   S 1 sw   Y  dS )z
    utility function to check if borg can open a repository

    :param path: the path to the repository
    :returns: if borg can check the repository
    TF)Z	exclusivecreateN)r   strcheck)r   
repositoryr   r   r   
repo_valid$   s   $r"   c                 C   sT   t jt t j| }t|}| tj	W  d   S 1 s#w   Y  dS )z
    check that the new keyfile is alright

    :param path: the path to the key file
    :returns: if the file starts with the borg magic string
    N)
r   r   r   r   basenamer   readr   r   ZFILE_ID)r   keyfilefr   r   r   	key_valid0   s   


$r'   c                 C   s   t tt| dS )z
    create an attic repo with some stuff in it

    :param dir: path to the repository to be created
    :returns: path to attic repository
    repo)r   	ATTIC_TARr   )dirr   r   r   make_attic_repo=   s   r+   c                 C   s   t | S r   )r+   )tmpdirr   r   r   
attic_repoH   s   r-   TF)paramsc                 C   s   | j S r   )param)Zrequestr   r   r   inplaceM   s   r0   c                 C   s   | }t tj t| W d   n1 sw   Y  t|dd}| dd |j D }W d   n1 s9w   Y  |j|d|d |j	dd t|sRJ dS )a  test segment conversion

    this will load the given attic repository, list all the segments
    then convert them one at a time. we need to close the repo before
    conversion otherwise we have errors from borg

    :param attic_repo: a populated attic repository (fixture)
    NFr   c                 S   s   g | ]\}}|qS r   r   ).0ifilenamer   r   r   
<listcomp>`   s    z)test_convert_segments.<locals>.<listcomp>dryrunr0   r7   )
pytestraisesr   AtticRepositoryr"   r   ioZsegment_iteratorZconvert_segmentsZconvert_cache)r-   r0   	repo_pathr!   segmentsr   r   r   test_convert_segmentsR   s   	
r?   c                 C   sB   t tt| d}|d| |d| |dd tj|dS )z
    create an attic key file from the given repo, in the keys
    subdirectory of the given tmpdir

    :param tmpdir: a temporary directory (a builtin fixture)
    :returns: path to key file
    keysZATTIC_KEYS_DIRZBORG_KEYS_DIRZATTIC_PASSPHRASEtestr(   )r   r)   r   Zsetenvr   r   r   )r,   ZmonkeypatchZkeys_dirr   r   r   attic_key_filef   s
   	rB   c                 C   sd   |}t |rJ t| dd}t|}tj|dd W d   n1 s%w   Y  t |s0J dS )au  test key conversion

    test that we can convert the given key to a properly formatted
    borg key. assumes that the ATTIC_KEYS_DIR and BORG_KEYS_DIR have
    been properly populated by the attic_key_file fixture.

    :param attic_repo: path to an attic repository (fixture defined above)
    :param attic_key_file: path to an attic key file (fixture defined above)
    Fr1   r8   N)r'   r   r   Zfind_key_fileZconvert_keyfiles)r-   rB   Zkeyfile_pathr!   r%   r   r   r   	test_keys~   s   

rC   zhardlinks not supported)reasonc                    s  | }t tj t| W d   n1 sw   Y  dd   fdd}||}t|ddE}tt |j	d|d}|rO|du sEJ ||j
|ksNJ n|sSJ ||j
||ks^J d	tjvrn |jt@ d
ksnJ W d   n1 sxw   Y  t|sJ t|sJ dS )a[  test all conversion steps

    this runs everything. mostly redundant test, since everything is
    done above. yet we expect a NotImplementedError because we do not
    convert caches yet.

    :param attic_repo: path to an attic repository (fixture defined above)
    :param attic_key_file: path to an attic key file (fixture defined above)
    Nc                 S   s   t t j| dddS )Ndata0)r   statr   r   r   r   r   r   stat_segment   s   z&test_convert_all.<locals>.stat_segmentc                    s
    | j S r   )st_inorH   rI   r   r   first_inode   s   
z%test_convert_all.<locals>.first_inodeFr1   r6   ZBORG_TESTS_IGNORE_MODESr   )r9   r:   r   r;   r"   r   r   umaskZUMASK_DEFAULTupgrader   environst_moder'   )r-   rB   r0   r=   rL   Z
orig_inoder!   Zbackupr   rK   r   test_convert_all   s*   


rQ   c                 C   s   t | d}t|d}|d W d   n1 sw   Y  t | d}t|| tj|dd|d |sHt|j	t|j	ksGJ nt|j	t|j	ksVJ t|d	}|
 d
ksdJ W d   dS 1 sow   Y  dS )ab  test that we handle hard links properly

    that is, if we are in "inplace" mode, hardlinks should *not*
    change (ie. we write to the file directly, so we do not rewrite the
    whole file, and we do not re-create the file).

    if we are *not* in inplace mode, then the inode should change, as
    we are supposed to leave the original inode alone.awbs   aXXXNb   a   b)r0   rbs   bXXX)r   r   r   writer   linkr   Zheader_replacerG   rJ   r$   )r,   r0   rR   tmprT   r   r   r   test_hardlink   s   
"r[   )"r   r   r9   	constantsZ
crypto.keyr   Zupgraderr   r   helpersr   r!   r    r
   r   r   dirname__file__r)   r   r"   r'   r+   Zfixturer-   r0   r?   rB   rC   ZmarkZskipifrQ   r[   r   r   r   r   <module>   s4    



)