
    |;i	+              	       ^   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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" edd	       Z# G d
 d      Z$edd       Z%edddee$dddfd       Z&ede$dddfd       Z'ede$dddfd       Z(ede$dddfd       Z)y)    )contextmanager)byrefcastc_charc_size_tc_void_pPOINTER)joinN   )ffi)ArchiveEntryFileType)OPEN_CALLBACKWRITE_CALLBACKCLOSE_CALLBACK
NO_OPEN_CBNO_CLOSE_CB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read_disk_set_behaviorc              #      K   t               }t        ||       |rt        j                  |       t	        ||        	 | t        |       y # t        |       w xY wwN)r   r    r   read_disk_set_standard_lookupr   r   )pathflagslookup	archive_ps       C/home/ubuntu/myenv/lib/python3.12/site-packages/libarchive/write.pynew_archive_read_diskr)      sL     I9e,)))4Y%)	)s   :AA AAAc                   T    e Zd ZddZd ZdddddddZd	 Zej                  d
fdZ	y)ArchiveWritec                      || _         || _        y r"   )_pointerheader_codec)selfr'   r.   s      r(   __init__zArchiveWrite.__init__!   s    !(    c           	          | j                   }|D ]N  }t        ||j                         |j                         D ]  }t	        ||t        |              t        |       P y)z.Add the given entries to the archive.
        N)r-   r   _entry_p
get_blocksr   lenr   )r/   entrieswrite_pentryblocks        r(   add_entrieszArchiveWrite.add_entries%   sZ     -- 	(E%..1))+ 77E3u:67w'		(r1   r   FNT)r%   r&   pathname	recursivesymlink_modec          
         | j                   }t        j                  |      }	|	dk  rd}	d}
|r	 t        t        d|       }
t        | j                        }|j                  }|D ]>  }t        |||      5 }|
r |
|       	 t        ||      }|t        k(  rn|j                  }|rB||k(  r|}n:|j                  |      sJ t        ||t        |      d j!                  d            }|j!                  d      |_        |r |j"                  d
i | t%        |       t'        ||       |j(                  rKt+        t-        |      d	      5 }	 |j/                  |	      }|snt1        ||t        |             +	 ddd       t3        |       t5        |       |snddd       A y# t        $ r t        d|d      dw xY w# 1 sw Y   MxY w# 1 sw Y   vxY w)a  Read files through the OS and add them to the archive.

        Args:
            paths (str): the paths of the files to add to the archive
            flags (int):
                passed to the C function `archive_read_disk_set_behavior`;
                use the `libarchive.flags.READDISK_*` constants
            lookup (bool):
                when True, the C function `archive_read_disk_set_standard_lookup`
                is called to enable the lookup of user and group names
            pathname (str | None):
                the path of the file in the archive, defaults to the source path
            recursive (bool):
                when False, if a path in `paths` is a directory,
                only the directory itself is added.
            symlink_mode (Literal['hybrid', 'logical', 'physical'] | None):
                how symbolic links should be handled; see `man archive_read_disk`
                for meanings
            attributes (dict): passed to `ArchiveEntry.modify()`

        Raises:
            ArchiveError: if a file doesn't exist or can't be accessed, or if
                          adding it to the archive fails
        r   i (  Nread_disk_set_symlink_zsymlink_mode value z is invalid)r.   /rb )r-   r   write_get_bytes_per_blockgetattrAttributeError
ValueErrorr   r.   r3   r)   r   r   r;   
startswithr
   r5   lstripmodifyr   r   isregopenr   readr   r   r   )r/   r%   r&   r;   r<   r=   paths
attributesr7   
block_sizeset_symlink_moder8   entry_pr$   read_pr
entry_pathfdatas                      r(   	add_fileszArchiveWrite.add_files/   s   8 --227;
?J#*1,@$  $*;*;<.. !	D&tUF;  v#$V,)&':AK'!&J%-)1J#-#8#8#>>#>)- ( *3t9: 6 = =c B*J &0%6%6s%;EN!$2z2%f- '2{{!"27";TB Ea"#'(vvj'9'+$) *7D#d) D	 #$ %*	E 'w/($;    !	 "  ),)9E>E E/   s0   F( 6CG-G9#G(GG
GG	c                 (     | j                   |fi |S )z"Single-path alias of `add_files()`)rW   )r/   r$   kws      r(   add_filezArchiveWrite.add_file   s    t~~d)b))r1   i  c           	      L   | j                   }t        |t              r|f}n't        |t              rt	        dt        |      z        t        d||||| j                  d|}t        ||j                         |D ]  }	|	s nt        ||	t        |	              t        |       y)a  "Add file from memory to archive.

        Args:
            entry_path (str | bytes): the file's path
            entry_size (int): the file's size, in bytes
            entry_data (bytes | Iterable[bytes]): the file's content
            filetype (int): see `libarchive.entry.ArchiveEntry.modify()`
            permission (int): see `libarchive.entry.ArchiveEntry.modify()`
            other_attributes: see `libarchive.entry.ArchiveEntry.modify()`
        z"entry_data: expected bytes, got %r)r;   sizefiletypepermr.   NrB   )r-   
isinstancebytesstr	TypeErrortyper   r.   r   r3   r   r5   r   )
r/   rT   
entry_size
entry_datar]   
permissionother_attributesarchive_pointerr8   chunks
             r(   add_file_from_memoryz!ArchiveWrite.add_file_from_memory   s     --j%($J
C(4tJ7GG   
j8$*;*;
 

 	_enn5 	;Es5z:	;
 	?+r1   )utf-8)
__name__
__module____qualname__r0   r:   rW   rZ   r   REGULAR_FILErj   rB   r1   r(   r+   r+      s8    )( eddPd* &&5$,r1   r+    c              #   @  K   t        j                         }	  t        j                  |       |       |r t        j                  |      |       |r'd|vr#| dk(  rt	        j
                  d       ||rdndz  }|r7t        |t              s|j                  d      }t        j                  ||       |r8t        |t              s|j                  d      }	 t        j                  ||       | t        j                  |       t        j                   |       t&        d	        }y # t        $ r3 t        dt        j                          dt         j                   d      w xY w# t"        $ r, t        j$                  |       t        j                   |        w xY ww)
N
encryptionzipzThe default encryption scheme of zip archives is weak. Use `options='encryption=$type'` to specify the encryption type you want to use. The supported values are 'zipcrypt' (the weak default), 'aes128' and 'aes256'.z,encryptionrk   z#the libarchive being used (version z, path z) doesn't support encryptionc                 B    t        j                  | j                  d      S )N)r   filter_bytesr-   )r/   s    r(   bytes_writtenz(new_archive_write.<locals>.bytes_written   s    r22r1   )r   	write_newget_write_format_functionget_write_filter_functionwarningswarnr_   r`   encodewrite_set_optionswrite_set_passphraserE   NotImplementedErrorversion_numberlibarchive_pathwrite_close
write_free	Exception
write_failproperty)format_namefilter_nameoptions
passphraser'   rw   s         r(   new_archive_writer      s{    I!2%%k29=6C))+6yA,g5e#A }\AGgu-!..1!!)W5j%0'..w7
((J? 	"y! 3 3 " )9#:L:L:N9O P//00LN   y!y!s5   FB=E& D' ,.E& F'<E##E& &5FFrk   c
              #   `   K    fd}
|rt        |      nt        }t        |
      }|rt        |      nt        }t        ||||      5 }t        j                  |d       t        j                  ||       t        j                  |d|||        |||	       ddd       y# 1 sw Y   yxY ww)zCreate an archive and send it in chunks to the `write_func` function.

    For formats and filters, see `WRITE_FORMATS` and `WRITE_FILTERS` in the
    `libarchive.ffi` module.
    c                 R    t        |t        t        |z              d   } |      S )Nr   )r   r	   r   )r'   contextbuffer_lengthrV   
write_funcs        r(   write_cb_internalz(custom_writer.<locals>.write_cb_internal   s(    GWVf_56q9$r1   r   N)
r   r   r   r   r   r   r   write_set_bytes_in_last_blockwrite_set_bytes_per_block
write_open)r   r   r   	open_func
close_funcrO   archive_write_classr   r   r.   r   open_cbwrite_cbclose_cbr'   s   `              r(   custom_writerr      s       +4mI&G/0H-7~j)[H	;W%
' ;*3)))Q7%%i<y$8D!)\::; ; ;s   AB.AB"	B."B+'B.c              #      K   t        ||||      5 }t        j                  ||         |||       ddd       y# 1 sw Y   yxY ww)zCreate an archive and write it into a file descriptor.

    For formats and filters, see `WRITE_FORMATS` and `WRITE_FILTERS` in the
    `libarchive.ffi` module.
    N)r   r   write_open_fd)fdr   r   r   r   r   r.   r'   s           r(   	fd_writerr      sM      
;W%
' ;*3)R(!)\::; ; ;   A"<	AAAc              #      K   t        ||||      5 }t        j                  ||         |||       ddd       y# 1 sw Y   yxY ww)zCreate an archive and write it into a file.

    For formats and filters, see `WRITE_FORMATS` and `WRITE_FILTERS` in the
    `libarchive.ffi` module.
    N)r   r   write_open_filename_w)filepathr   r   r   r   r   r.   r'   s           r(   file_writerr     sM      
;W%
' ;*3!!)X6!)\::; ; ;r   c              #      K   t        ||||      5 }t        t                     }t        | t              }	t        j                  ||	t        |       |        |||       ddd       y# 1 sw Y   yxY ww)zCreate an archive and write it into a buffer.

    For formats and filters, see `WRITE_FORMATS` and `WRITE_FILTERS` in the
    `libarchive.ffi` module.
    N)r   r   r   r   r   r   write_open_memoryr5   )
bufr   r   r   r   r   r.   r'   usedbuf_ps
             r(   memory_writerr     sm      
;W%
' ;*3XZ S(#iC$?!)\::; ; ;s   A6AA*!	A6*A3/A6)r   F)Nrp   N)*
contextlibr   ctypesr   r   r   r   r   r	   	posixpathr
   r{   rp   r   r8   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   r   r   rB   r1   r(   <module>r      s    % C C    )     	 	J, J,Z '3 '3T )-t	$bT	; ;8 !%$bT; ;  '+$bT; ;  "&$bT; ;r1   