B
    2'[5                 @   sB  d dl mZmZmZ d dlmZ d dlmZmZm	Z	m
Z
mZmZ ddlmZ ddlmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' edd	d
Z(G dd de)Z*edddZ+edeeee*dfddZ,ede*dfddZ-ede*dfddZ.ede*dfddZ/dS )    )divisionprint_functionunicode_literals)contextmanager)byrefcastc_charc_size_tc_void_pPOINTER   )ffi)ArchiveEntrynew_archive_entry)OPEN_CALLBACKWRITE_CALLBACKCLOSE_CALLBACKVOID_CBREGULAR_FILEDEFAULT_UNIX_PERMISSIONARCHIVE_EOF	page_sizeentry_sourcepathentry_clearread_disk_newread_disk_open_wread_next_header2read_disk_descend	read_freewrite_header
write_datawrite_finish_entryentry_set_sizeentry_set_filetypeentry_set_permread_disk_set_behaviorFc             c   sB   t  }t|| |rt| t||  z
|V  W d t| X d S )N)r   r%   r   Zread_disk_set_standard_lookupr   r   )pathflagslookup	archive_p r*   /lib/python3.7/site-packages/libarchive/write.pynew_archive_read_disk   s    



r,   c               @   s2   e Zd Zdd Zdd Zdd ZeefddZd	S )
ArchiveWritec             C   s
   || _ d S )N)_pointer)selfr)   r*   r*   r+   __init__!   s    zArchiveWrite.__init__c             C   sN   | j }xB|D ]:}t||j x | D ]}t||t| q&W t| qW dS )z.Add the given entries to the archive.
        N)r.   r   Z_entry_pZ
get_blocksr    lenr!   )r/   entrieswrite_pentryblockr*   r*   r+   add_entries$   s    
zArchiveWrite.add_entriesc             O   s   | j }t|}|dkrd}t }td|}x|D ]}t|f|}xt||}	|	tkr\P |j	d|_t
| t|| |jrtt|d,}
x$|
|}|sP t||t| qW W dQ R X t| t| qHW W dQ R X q4W W dQ R X dS )zRead the given paths from disk and add them to the archive.

        The keyword arguments (`**kw`) are passed to `new_archive_read_disk`.
        r   i (  N/rb)r.   r   Zwrite_get_bytes_per_blockr   r   r,   r   r   pathnamelstripr   r   Zisregopenr   readr    r1   r!   r   )r/   pathskwr3   
block_sizeZentry_pr4   r&   Zread_prfdatar*   r*   r+   	add_files.   s0    





zArchiveWrite.add_filesc       
   	   C   s   | j }t r}td|}||_t|| t|| t|| t|| x"|D ]}	|	sVP t||	t	|	 qLW t
| t| W dQ R X dS )a#  "Add file from memory to archive.

        :param entry_path: where entry should be places in archive
        :type entry_path: str
        :param entry_size: entire size of entry
        :type entry_size: int
        :param entry_data: content of entry
        :type entry_data: iterable
        :param filetype: which type of file: normal, symlink etc.
        should entry be created as
        :type filetype: octal number
        :param permission: with which permission should entry be created
        :type permission: octal number
        N)r.   r   r   r9   r"   r#   r$   r   r    r1   r!   r   )
r/   Z
entry_pathZ
entry_sizeZ
entry_dataZfiletypeZ
permissionZarchive_pointerZarchive_entry_pointerZarchive_entrychunkr*   r*   r+   add_file_from_memoryN   s    





z!ArchiveWrite.add_file_from_memoryN)	__name__
__module____qualname__r0   r6   rC   r   r   rE   r*   r*   r*   r+   r-      s
   
"r-   N c             c   s   t  }tt d|  | |r0tt d| | |rTt|tsH|d}t || y|V  t | t | W n* t	k
r   t 
| t |  Y nX d S )NZwrite_set_format_Zwrite_add_filter_zutf-8)r   Z	write_newgetattr
isinstancebytesencodeZwrite_set_optionsZwrite_closeZ
write_free	ExceptionZ
write_fail)format_namefilter_nameoptionsr)   r*   r*   r+   new_archive_writeu   s     




rR   c          	   #   st    fdd}t |}	t|}
t|}t|||:}t|d t|| t|d |	|
| ||V  W d Q R X d S )Nc                s   t |tt| d } |S )Nr   )r   r   r   )r)   contextZbuffer_lengthrB   )
write_funcr*   r+   write_cb_internal   s    z(custom_writer.<locals>.write_cb_internalr   )r   r   r   rR   r   Zwrite_set_bytes_in_last_blockZwrite_set_bytes_per_blockZ
write_open)rU   rO   rP   Z	open_funcZ
close_funcr?   archive_write_classrQ   rV   Zopen_cbZwrite_cbZclose_cbr)   r*   )rU   r+   custom_writer   s    rX   c          	   c   s2   t |||}t||  ||V  W d Q R X d S )N)rR   r   Zwrite_open_fd)fdrO   rP   rW   rQ   r)   r*   r*   r+   	fd_writer   s    rZ   c          	   c   s2   t |||}t||  ||V  W d Q R X d S )N)rR   r   Zwrite_open_filename_w)filepathrO   rP   rW   rQ   r)   r*   r*   r+   file_writer   s    r\   c          	   c   sN   t |||8}tt }t| t}t||t| | ||V  W d Q R X d S )N)rR   r   r	   r   r
   r   Zwrite_open_memoryr1   )ZbufrO   rP   rW   rQ   r)   ZusedZbuf_pr*   r*   r+   memory_writer   s
    

r]   )r   F)NrI   )0Z
__future__r   r   r   
contextlibr   Zctypesr   r   r   r	   r
   r   rI   r   r4   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r,   objectr-   rR   rX   rZ   r\   r]   r*   r*   r*   r+   <module>   s.    `
V