
    |;iC                         d dl mZ d dlmZmZ d dlmZ d dlZddlm	Z	 ddl
mZ  G d d	e      Zed
        Zd Z G d d      Z G d de      Z G d de      Zy)    )contextmanager)create_string_buffer	string_at)IntEnumN   )ffi)ArchiveErrorc                   D    e Zd ZdxZZdxZZdxZZdxZ	Z
dxZZdxZZdxZZy)	FileType        @   `           N)__name__
__module____qualname__
NAMED_PIPEAE_IFIFOCHAR_DEVICEAE_IFCHR	DIRECTORYAE_IFDIRBLOCK_DEVICEAE_IFBLKREGULAR_FILEAE_IFREGSYMBOLINK_LINKAE_IFLNKSOCKET	AE_IFSOCK     C/home/ubuntu/myenv/lib/python3.12/site-packages/libarchive/entry.pyr   r   
   sH    !))JX!))KX!))IX!))LX!))LX!))NX!))FYr%   r   c               #      K   t        j                         } 	 |  t        j                  |        y # t        j                  |        w xY wwN)r   	entry_new
entry_free)entry_ps    r&   new_archive_entryr,      s3     mmoG wws   A2 AA		Ac                 P    |rt        |       t        |      dz  z   S t        |       S )zC return float of seconds.nanos when nanos set, or seconds when not g    eA)floatint)secondsnanoss     r&   format_timer2      s'    W~e| ;;;w<r%   c                   N   e Zd ZdZd@dZd Zd ZdAdZed        Z	e	j                  d        Z	ed	        Zej                  d
        Zed        Zej                  d        Zed        Zej                  d        Zed        Zej                  d        Zej                   fdZed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zej                  d        ZdBdZed        Zej                  d         ZdBd!Zed"        Z e j                  d#        Z dBd$Z!ed%        Z"e"j                  d&        Z"dBd'Z#ed(        Z$e$j                  d)        Z$ed*        Z%e%j                  d+        Z% ee$jL                  e$jN                  d,-      Z(e(Z) ee%jL                  e%jN                  d.-      Z*ed/        Z+e+j                  d0        Z+ed1        Z,e,j                  d2        Z,ed3        Z-ed4        Z.e.j                  d5        Z.ed6        Z/e/j                  d7        Z/ed8        Z0e0j                  d9        Z0ed:        Z1e1j                  d;        Z1ed<        Z2e2j                  d=        Z2d> Z3d? Z4y)CArchiveEntry)
_archive_p_entry_pheader_codecNc                 |    || _         t        j                         | _        || _        |r | j
                  di | yy)zAllocate memory for an `archive_entry` struct.

        The `header_codec` is used to decode and encode file paths and other
        attributes.

        The `**attributes` are passed to the `modify` method.
        Nr$   )r5   r   r)   r6   r7   modify)self	archive_pr7   
attributess       r&   __init__zArchiveEntry.__init__(   s9     $(DKK%*% r%   c                 B    t        j                  | j                         y)zFree the C structN)r   r*   r6   r:   s    r&   __del__zArchiveEntry.__del__6   s    t}}%r%   c                     | j                   S )zReturns the file's path)pathnamer?   s    r&   __str__zArchiveEntry.__str__:   s    }}r%   c                 `    |r|| _         |j                         D ]  \  }}t        | ||        y)a  Convenience method to modify the entry's attributes.

        Args:
            filetype (int): the file's type, see the `FileType` class for values
            pathname (str): the file's path
            linkpath (str): the other path of the file, if the file is a link
            size (int | None): the file's size, in bytes
            perm (int): the file's permissions in standard Unix format, e.g. 0o640
            uid (int): the file owner's numerical identifier
            gid (int): the file group's numerical identifier
            uname (str | bytes): the file owner's name
            gname (str | bytes): the file group's name
            atime (int | Tuple[int, int] | float | None):
                the file's most recent access time,
                either in seconds or as a tuple (seconds, nanoseconds)
            mtime (int | Tuple[int, int] | float | None):
                the file's most recent modification time,
                either in seconds or as a tuple (seconds, nanoseconds)
            ctime (int | Tuple[int, int] | float | None):
                the file's most recent metadata change time,
                either in seconds or as a tuple (seconds, nanoseconds)
            birthtime (int | Tuple[int, int] | float | None):
                the file's creation time (for archive formats that support it),
                either in seconds or as a tuple (seconds, nanoseconds)
            rdev (int | Tuple[int, int]): device number, if the file is a device
            rdevmajor (int): major part of the device number
            rdevminor (int): minor part of the device number
            stored_digests (dict[str, bytes]): hashes of the file's contents
        N)r7   itemssetattr)r:   r7   r<   namevalues        r&   r9   zArchiveEntry.modify>   s7    <  ,D%++- 	'KD%D$&	'r%   c                 @    t        j                  | j                        S r(   )r   entry_filetyper6   r?   s    r&   filetypezArchiveEntry.filetypea   s    !!$--00r%   c                 D    t        j                  | j                  |       y r(   )r   entry_set_filetyper6   r:   rH   s     r&   rK   zArchiveEntry.filetypee   s    t}}e4r%   c                 @    t        j                  | j                        S r(   )r   	entry_uidr6   r?   s    r&   uidzArchiveEntry.uidi       }}T]]++r%   c                 D    t        j                  | j                  |       y r(   )r   entry_set_uidr6   )r:   rQ   s     r&   rQ   zArchiveEntry.uidm       $---r%   c                 @    t        j                  | j                        S r(   )r   	entry_gidr6   r?   s    r&   gidzArchiveEntry.gidq   rR   r%   c                 D    t        j                  | j                  |       y r(   )r   entry_set_gidr6   )r:   rX   s     r&   rX   zArchiveEntry.gidu   rU   r%   c                     t        j                  | j                        }|s?t        j                  | j                        }|	 |j	                  | j
                        }|S |S # t        $ r Y |S w xY wr(   )r   entry_uname_wr6   entry_unamedecoder7   UnicodeError)r:   unames     r&   r`   zArchiveEntry.unamey   n    !!$--0OODMM2E !LL):):;E u $    A# #	A0/A0c                     t        |t              s|j                  | j                        }| j                  dk(  r!t	        j
                  | j                  |       y t	        j                  | j                  |       y Nzutf-8)
isinstancebytesencoder7   r   entry_update_uname_utf8r6   entry_copy_unamerN   s     r&   r`   zArchiveEntry.uname   V    %'LL!2!23E'''u=  6r%   c                     t        j                  | j                        }|s?t        j                  | j                        }|	 |j	                  | j
                        }|S |S # t        $ r Y |S w xY wr(   )r   entry_gname_wr6   entry_gnamer^   r7   r_   )r:   gnames     r&   rn   zArchiveEntry.gname   ra   rb   c                     t        |t              s|j                  | j                        }| j                  dk(  r!t	        j
                  | j                  |       y t	        j                  | j                  |       y rd   )re   rf   rg   r7   r   entry_update_gname_utf8r6   entry_copy_gnamerN   s     r&   rn   zArchiveEntry.gname   rj   r%   c              #      K   | j                   }|st        d      t        |      }t        j                  }	  ||||      }|dk(  r	 t        | _        y|j
                  d|  .w)zRead the file's content, keeping only one chunk in memory at a time.

        Don't do anything like `list(entry.get_blocks())`, it would silently fail.

        Args:
            block_size (int): the buffer's size, in bytes
        z&this entry isn't linked to any contentr   N)r5   	TypeErrorr   r   	read_datarawConsumedArchiveEntry	__class__)r:   
block_sizer;   bufreadrs         r&   
get_blockszArchiveEntry.get_blocks   sl      OO	DEE":.}}YZ0AAv- ''!A,	 s   A$A&c                 &    | j                   dz  dk(  S )N   r   rK   r?   s    r&   isblkzArchiveEntry.isblk       }}x'833r%   c                 &    | j                   dz  dk(  S )Nr~   r   r   r?   s    r&   ischrzArchiveEntry.ischr   r   r%   c                 &    | j                   dz  dk(  S )Nr~   r   r   r?   s    r&   isdirzArchiveEntry.isdir   r   r%   c                 &    | j                   dz  dk(  S )Nr~   r   r   r?   s    r&   isfifozArchiveEntry.isfifo   r   r%   c                     t        t        j                  | j                        xs t        j                  | j                              S r(   )boolr   entry_hardlink_wr6   entry_hardlinkr?   s    r&   islnkzArchiveEntry.islnk   s7    C((7 6&&t}}57 	7r%   c                 &    | j                   dz  dk(  S )Nr~   r   r   r?   s    r&   issymzArchiveEntry.issym   r   r%   c                 &    | j                   dz  dk(  S )Nr~   r   r   r?   s    r&   isregzArchiveEntry.isreg   r   r%   c                     | j                   S r(   )r   r?   s    r&   isfilezArchiveEntry.isfile   s    zzr%   c                 &    | j                   dz  dk(  S )Nr~   r   r   r?   s    r&   issockzArchiveEntry.issock   r   r%   c                 n    | j                   xs( | j                  xs | j                  xs | j                  S r(   )r   r   r   r   r?   s    r&   isdevzArchiveEntry.isdev   s'    zzETZZE4;;E$++Er%   c                     t        j                  | j                        sy t        j                  | j                        }t        j                  | j                        }t        ||      S r(   )r   entry_atime_is_setr6   entry_atimeentry_atime_nsecr2   r:   sec_valnsec_vals      r&   atimezArchiveEntry.atime   J    %%dmm4//$--0''67H--r%   c                 J   | t        j                  | j                         y t        |t              r| j                  |       y t        |t              r | j
                  |  y t        j                  |      \  }}| j                  t	        |      t	        |dz               y Ni ʚ;)	r   entry_unset_atimer6   re   r/   	set_atimetuplemathmodfr:   rH   r0   fractions       r&   r   zArchiveEntry.atime   v    =!!$--0s#NN5!u%DNNE" $		% 0GXNN3w<X-E)FGr%   c                 D    t        j                  | j                  ||      S )zFKept for backward compatibility. `entry.atime = ...` is supported now.)r   entry_set_atimer6   r:   timestamp_sectimestamp_nsecs      r&   r   zArchiveEntry.set_atime       ""4==-PPr%   c                     t        j                  | j                        sy t        j                  | j                        }t        j                  | j                        }t        ||      S r(   )r   entry_mtime_is_setr6   entry_mtimeentry_mtime_nsecr2   r   s      r&   mtimezArchiveEntry.mtime   r   r%   c                 J   | t        j                  | j                         y t        |t              r| j                  |       y t        |t              r | j
                  |  y t        j                  |      \  }}| j                  t	        |      t	        |dz               y r   )	r   entry_unset_mtimer6   re   r/   	set_mtimer   r   r   r   s       r&   r   zArchiveEntry.mtime   r   r%   c                 D    t        j                  | j                  ||      S )zFKept for backward compatibility. `entry.mtime = ...` is supported now.)r   entry_set_mtimer6   r   s      r&   r   zArchiveEntry.set_mtime  r   r%   c                     t        j                  | j                        sy t        j                  | j                        }t        j                  | j                        }t        ||      S r(   )r   entry_ctime_is_setr6   entry_ctimeentry_ctime_nsecr2   r   s      r&   ctimezArchiveEntry.ctime  r   r%   c                 J   | t        j                  | j                         y t        |t              r| j                  |       y t        |t              r | j
                  |  y t        j                  |      \  }}| j                  t	        |      t	        |dz               y r   )	r   entry_unset_ctimer6   re   r/   	set_ctimer   r   r   r   s       r&   r   zArchiveEntry.ctime  r   r%   c                 D    t        j                  | j                  ||      S )zFKept for backward compatibility. `entry.ctime = ...` is supported now.)r   entry_set_ctimer6   r   s      r&   r   zArchiveEntry.set_ctime$  r   r%   c                     t        j                  | j                        sy t        j                  | j                        }t        j                  | j                        }t        ||      S r(   )r   entry_birthtime_is_setr6   entry_birthtimeentry_birthtime_nsecr2   r   s      r&   	birthtimezArchiveEntry.birthtime(  sL    ))$--8%%dmm4++DMM:7H--r%   c                 J   | t        j                  | j                         y t        |t              r| j                  |       y t        |t              r | j
                  |  y t        j                  |      \  }}| j                  t	        |      t	        |dz               y r   )	r   entry_unset_birthtimer6   re   r/   set_birthtimer   r   r   r   s       r&   r   zArchiveEntry.birthtime0  s|    =%%dmm4s#u%u%D& $		% 0GXs7|SM1I-JKr%   c                 D    t        j                  | j                  ||      S )zJKept for backward compatibility. `entry.birthtime = ...` is supported now.)r   entry_set_birthtimer6   r   s      r&   r   zArchiveEntry.set_birthtime<  s     &&MM=.
 	
r%   c                     t        j                  | j                        }|s?t        j                  | j                        }|	 |j	                  | j
                        }|S |S # t        $ r Y |S w xY wr(   )r   entry_pathname_wr6   entry_pathnamer^   r7   r_   r:   paths     r&   rB   zArchiveEntry.pathnameB  sp    ##DMM2%%dmm4D;;t'8'89D t $ rb   c                     t        |t              s|j                  | j                        }| j                  dk(  r!t	        j
                  | j                  |       y t	        j                  | j                  |       y rd   )re   rf   rg   r7   r   entry_update_pathname_utf8r6   entry_copy_pathnamerN   s     r&   rB   zArchiveEntry.pathnameN  sV    %'LL!2!23E'**4==%@##DMM59r%   c                    | j                   r@t        j                  | j                        xs_ t        j                  | j                        n?t        j
                  | j                        xs t        j                  | j                        }t        |t              r	 |j                  | j                        }|S |S # t        $ r Y |S w xY wr(   )r   r   entry_symlink_wr6   entry_symlinkr   r   re   rf   r^   r7   r_   r   s     r&   linkpathzArchiveEntry.linkpathW  s      ##DMM2 1!!$--0$$T]]3 2""4==1 	 dE"{{4#4#45 t   s   B= =	C
	C
c                     t        |t              s|j                  | j                        }| j                  dk(  r!t	        j
                  | j                  |       y t	        j                  | j                  |       y rd   )re   rf   rg   r7   r   entry_update_link_utf8r6   entry_copy_linkrN   s     r&   r   zArchiveEntry.linkpathi  sV    %'LL!2!23E'&&t}}e<u5r%   zalias of pathname)doczalias of linkpathc                     t        j                  | j                        rt        j                  | j                        S y r(   )r   entry_size_is_setr6   
entry_sizer?   s    r&   sizezArchiveEntry.sizew  s,      />>$--00 0r%   c                     | t        j                  | j                         y t        j                  | j                  |       y r(   )r   entry_unset_sizer6   entry_set_sizerN   s     r&   r   zArchiveEntry.size|  s-    =  /t}}e4r%   c                 @    t        j                  | j                        S r(   )r   
entry_moder6   r?   s    r&   modezArchiveEntry.mode      ~~dmm,,r%   c                 D    t        j                  | j                  |       y r(   )r   entry_set_moder6   rN   s     r&   r   zArchiveEntry.mode      4==%0r%   c                 \    t        j                  | j                        j                         S )z.The file's mode as a string, e.g. '?rwxrwx---')r   entry_strmoder6   stripr?   s    r&   strmodezArchiveEntry.strmode  s"    
   /5577r%   c                 @    t        j                  | j                        S r(   )r   
entry_permr6   r?   s    r&   permzArchiveEntry.perm  r   r%   c                 D    t        j                  | j                  |       y r(   )r   entry_set_permr6   rN   s     r&   r   zArchiveEntry.perm  r   r%   c                 @    t        j                  | j                        S r(   )r   
entry_rdevr6   r?   s    r&   rdevzArchiveEntry.rdev  r   r%   c                     t        |t              rGt        j                  | j                  |d          t        j
                  | j                  |d          y t        j                  | j                  |       y )Nr   r   )re   r   r   entry_set_rdevmajorr6   entry_set_rdevminorentry_set_rdevrN   s     r&   r   zArchiveEntry.rdev  sP    eU###DMM58<##DMM58<t}}e4r%   c                 @    t        j                  | j                        S r(   )r   entry_rdevmajorr6   r?   s    r&   	rdevmajorzArchiveEntry.rdevmajor      ""4==11r%   c                 D    t        j                  | j                  |       y r(   )r   r   r6   rN   s     r&   r   zArchiveEntry.rdevmajor      u5r%   c                 @    t        j                  | j                        S r(   )r   entry_rdevminorr6   r?   s    r&   	rdevminorzArchiveEntry.rdevminor  r   r%   c                 D    t        j                  | j                  |       y r(   )r   r   r6   rN   s     r&   r  zArchiveEntry.rdevminor  r  r%   c                 h    t         j                  D ci c]  }|| j                  |       c}S c c}w )a  The file's hashes stored in the archive.

        libarchive only supports reading and writing digests from and to 'mtree'
        files. Setting the digests requires at least version 3.8.0 of libarchive
        (released in May 2025). It also requires including the names of the
        digest algorithms in the string of options passed to the archive writer
        (e.g. `file_writer(archive_path, 'mtree', options='md5,rmd160,sha256')`).
        )r   DIGEST_ALGORITHMSget_stored_digest)r:   rG   s     r&   stored_digestszArchiveEntry.stored_digests  s0     @C?T?TUtd,,T22UUUs   /c                 X    |j                         D ]  \  }}| j                  ||        y r(   )rE   set_stored_digest)r:   valuesrG   rH   s       r&   r	  zArchiveEntry.stored_digests  s+    !<<> 	0KD%""4/	0r%   c           
         t         j                  |   }	 t        j                  | j                  |j                        }t        ||j                        S # t
        $ r4 t        dt        j                          dt         j                   d      d t        $ r7 t        dt        j                          dt         j                   d| d      d w xY w)N#the libarchive being used (version , path z') doesn't support reading entry digests) doesn't support  digests)r   r  entry_digestr6   libarchive_idAttributeErrorNotImplementedErrorversion_numberlibarchive_pathr	   r   bytes_length)r:   algorithm_name	algorithmptrs       r&   r  zArchiveEntry.get_stored_digest  s    )).9		""4==)2I2IJC i4455  	%5c6H6H6J5K7&&''NP   	%5c6H6H6J5K7&&''9.9IS 	s   *A A;Cc           	          t         j                  |   }|j                  }t        |      |k7  rt	        d| dt        |             	 t        j
                  | j                  |j                  |t         j                  z  j                  |            }|dk  r6t        dt        j                          dt         j                   d| d      d y # t        $ r4 t        dt        j                          dt         j                   d      d w xY w)	Nzinvalid input digest: expected z bytes, got r  r  z') doesn't support writing entry digestsr   r  r  )r   r  r  len
ValueErrorentry_set_digestr6   r  c_ubytefrom_buffer_copyr  r  r  r  )r:   r  rH   r  expected_lengthretcodes         r&   r  zArchiveEntry.set_stored_digest  s(   )).9	#00u:(1/1B C5zl$ 
	**'' 3;;.@@GG Q;%5c6H6H6J5K7&&''9.9IS    	%5c6H6H6J5K7&&''NP 	s   	AC =Drd   r(   )r   )5r   r   r   	__slots__r=   r@   rC   r9   propertyrK   setterrQ   rX   r`   rn   r   	page_sizer|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rB   r   fgetfsetr   rG   linknamer   r   r   r   r   r   r  r	  r  r  r$   r%   r&   r4   r4   $   s   :I&&!'F 1 1 __5 5 , , 	ZZ. . , , 	ZZ. . 	 	 \\7 7 	 	 \\7 7 %(MM .( 4 4 4 4 4 4 4 4 7 7 4 4 4 4   4 4 F F . . \\	H 	HQ . . \\	H 	HQ . . \\	H 	HQ . . 	L 	L
 	 	 __: :  " __6 6 HMM8==6IJDDx}}:MNH1 1 
[[5 5 - - 
[[1 1 8 8 - - 
[[1 1 - - 
[[5 5 2 2 6 6 2 2 6 6 	V 	V 0 06 r%   r4   c                       e Zd ZdZd Zy)rv   r$   c                     t        d      )Nz/the content of this entry has already been readrs   r:   kws     r&   r|   zConsumedArchiveEntry.get_blocks  s    IJJr%   Nr   r   r   r$  r|   r$   r%   r&   rv   rv     s    IKr%   rv   c                       e Zd ZdZd Zy)PassedArchiveEntryr$   c                     t        d      )Nz7this entry is passed, it's too late to read its contentr-  r.  s     r&   r|   zPassedArchiveEntry.get_blocks  s    QRRr%   Nr0  r$   r%   r&   r2  r2    s    ISr%   r2  )
contextlibr   ctypesr   r   enumr   r    r   	exceptionr	   r   r,   r2   r4   rv   r2  r$   r%   r&   <module>r9     se    % 2    #*w *    J JZK< KS Sr%   