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: //usr/lib/python3.9/site-packages/ipalib/__pycache__/sysrestore.cpython-39.pyc
a

}�f]6�@s�ddlmZddlZddlZddlZddlZddlZddlmZddl	Z	e	j
r\ddlmZ
nddlm
Z
ddlmZddlmZe	j
r�eZe�e�ZejZdZd	ZGd
d�d�ZGdd
�d
�ZdS)�)�absolute_importN)�sha256)�ConfigParser)�SafeConfigParser)�tasks)�pathszsysrestore.indexzsysrestore.statec@s`eZdZdZeefdd�Zdd�Zdd�Zdd	�Z	d
d�Z
dd
d�Zdd�Zdd�Z
dd�ZdS)�	FileStorez.Class for handling backup and restore of filescCs4||_tj�|j|�|_t��|_i|_|��dS)z�Create a _StoreFiles object, that uses @path as the
        base directory.

        The file @path/sysrestore.index is used to store information
        about the original location of the saved files.
        N)	�_path�os�path�join�_index�randomZRandom�files�_load)�selfrZ
index_file�r�5/usr/lib/python3.9/site-packages/ipalib/sysrestore.py�__init__=s

zFileStore.__init__cCsht�d|j�i|_tdd�}t|_|�|j�|��D]*}|dkr8|�	|�D]\}}||j|<qNq8dS)zvLoad the file list from the index file. @files will
        be an empty dictionary if the file doesn't exist.
        zLoading Index file from '%s'N�Z
interpolationr)
�logger�debugr
rr�str�optionxform�read�sections�items)r�p�section�key�valuerrrrLs
zFileStore._loadcCs�t�d|j�t|j�dkrDt�d�tj�|j�r@t�|j�dSt	dd�}t
|_|�d�|j�
�D]\}}|�d|t
|��qht|jd��}|�|�Wd�n1s�0YdS)zkSave the file list to @_index. If @files is an empty
        dict, then @_index should be removed.
        zSaving Index File to '%s'rz  -> no files, removing fileNrr�w)rrr
�lenrr
r�exists�removerrr�add_sectionr�set�open�write)rrrr �frrr�save^s


zFileStore.savec
Cs�t�d|�tj�|�s td��tj�|�s<t�d|�dStj�|�\}}t|d�� }t	|�
����}Wd�n1s|0Ydj||d�}tj�
|j|�}tj�|�r�t�d|�dSt�||�t�|�}d	}	|	j||d
�|j|<|��dS)z�Create a copy of the file at @path - as long as an exact copy
        does not already exist - which will be restored to its
        original location by restore_files().
        z)Backing up system configuration file '%s'�Absolute path requiredz(  -> Not backing up - '%s' doesn't existN�rbz{hexhash}-{bcppath})ZhexhashZbcppathz1  -> Not backing up - already have a copy of '%s'z1{stat.st_mode},{stat.st_uid},{stat.st_gid},{path})�statr)rrr
r�isabs�
ValueError�isfile�splitr'rrZ	hexdigest�formatrr	r#�shutil�copy2r-rr*)
rrZ_reldirZ
backupfiler)Z	cont_hash�filename�backup_pathr-�templaterrr�backup_filets..��
zFileStore.backup_filec	Cs@d}|j��D],\}}|�dd�\}}}}||krd}q<q|S)z�Checks whether file at @path was added to the file store

        Returns #True if the file exists in the file store, #False otherwise
        F�,�T)rrr1)	rr�resultZ_keyr �_mode�_uid�_gid�filepathrrr�has_file�szFileStore.has_fileNcCs:|durt�d|�nt�d||�tj�|�s8td��|durTtj�|�sTtd��d}d}d}d}|j��D],\}}|�dd�\}}}}	|	|krn|}q�qn|s�td��tj�	|j
|�}
tj�|
�s�t�d	|
�d
S|dur�|}t�
|
|�t�|
�t�|t|�t|��t�|t|��t�|�|j|=|��dS)a<Restore the copy of a file at @path to its original
        location and delete the copy.

        Takes optional parameter @new_path which specifies the
        location where the file is to be restored.

        Returns #True if the file was restored, #False if there
        was no backup file to restore
        Nz(Restoring system configuration file '%s'z0Restoring system configuration file '%s' to '%s'r+zAbsolute new path requiredr9r:�No such file name in the index�'  -> Not restoring - '%s' doesn't existFT)rrr
rr.r/rrr1rr	r#r3�copyr$�chown�int�chmodr�restore_contextr*)rr�new_path�mode�uid�gidr5rr r?r6rrr�restore_file�sL���

zFileStore.restore_filecCs�t|j�dkrdS|j��D]�\}}|�dd�\}}}}tj�|j|�}tj�|�sbt	�
d|�qt�||�t�
|�t�|t|�t|��t�|t|��t�|�qi|_|��dS)z�Restore the files in the inbdex to their original
        location and delete the copy.

        Returns #True if the file was restored, #False if there
        was no backup file to restore
        rFr9r:rBT)r"rrr1r
rrr	r#rrr3rCr$rDrErFrrGr*)rr5r rIrJrKrr6rrr�restore_all_files�s$�
zFileStore.restore_all_filescCst|j�dkS)zReturn True or False if there are any files in the index

        Can be used to determine if a program is configured.
        r)r"r)rrrr�	has_filesszFileStore.has_filesc
Cs�t�d|�tj�|�s td��d}|j��D],\}}|�dd�\}}}}||kr.|}q\q.|shtd��tj�	|j
|�}	tj�|	�s�t�d|	�dSzt�|	�Wn6t
y�}
zt�d	|	t|
��WYd}
~
n
d}
~
00|j|=|��d
S)z�Remove file at path @path from list of backed up files.

        Does not remove any files from the filesystem.

        Returns #True if the file was untracked, #False if there
        was no backup file to restore
        z)Untracking system configuration file '%s'r+Nr9r:rArBFzError removing %s: %sT)rrr
rr.r/rrr1rr	r#�unlink�	Exception�errorrr*)rrr5rr r<r=r>r?r6�errr�untrack_files0	�(zFileStore.untrack_file)N)�__name__�
__module__�__qualname__�__doc__�SYSRESTORE_PATH�SYSRESTORE_INDEXFILErrr*r8r@rLrMrNrSrrrrr:s$
;#rc@sVeZdZdZeefdd�Zdd�Zdd�Zdd	�Z	d
d�Z
dd
�Zdd�Zdd�Z
dS)�	StateFileafA metadata file for recording system state which can
    be backed up and later restored.
    StateFile gets reloaded every time to prevent loss of information
    recorded by child processes. But we do not solve concurrency
    because there is no need for it right now.
    The format is something like:

    [httpd]
    running=True
    enabled=False
    cCs"tj�||�|_i|_|��dS)a�Create a StateFile object, loading from @path.

        The dictionary @modules, a member of the returned object,
        is where the state can be modified. @modules is indexed
        using a module name to return another dictionary containing
        key/value pairs with the saved state of that module.

        The keys in these latter dictionaries are arbitrary strings
        and the values may either be strings or booleans.
        N)r
rrr	�modulesr)rrZ
state_filerrrrBszStateFile.__init__cCs�t�d|j�i|_tdd�}t|_|�|j�|��D]R}i|j|<|�	|�D]8\}}|td�krjd}n|td�krzd}||j||<qPq8dS)zwLoad the modules from the file @_path. @modules will
        be an empty dictionary if the file doesn't exist.
        zLoading StateFile from '%s'NrTF)
rrr	r[rrrrrr)rr�modulerr rrrrSs

zStateFile._loadcCs�t�d|j�t|j�D]}t|j|�dkr|j|=qt|j�dkrnt�d�tj�|j�rjt�	|j�dSt
dd�}t|_|j�
�D]6\}}|�|�|�
�D]\}}|�||t|��q�q�t|jd��}|�|�Wd�n1s�0YdS)ziSave the modules to @_path. If @modules is an empty
        dict, then @_path should be removed.
        zSaving StateFile to '%s'rz  -> no modules, removing fileNrr!)rrr	�listr[r"r
rr#r$rrrrr%r&r'r()rr\rZvalsrr r)rrrr*hs"



zStateFile.savecCsXt|tttf�std��|��||jvr4i|j|<||jvrL||j||<|��dS)z�Backup an item of system state from @module, identified
        by the string @key and with the value @value. @value may be
        a string or boolean.
        z7Only strings, booleans or unicode strings are supportedN)�
isinstancer�bool�unicoder/rr[r*�rr\rr rrr�backup_state�s


zStateFile.backup_statecCs(|��||jvrdS|j|�|d�S)z�Return the value of an item of system state from @module,
        identified by the string @key.

        If the item doesn't exist, #None will be returned, otherwise
        the original string or boolean value is returned.
        N)rr[�get�rr\rrrr�	get_state�s
zStateFile.get_statecCs8|��z|j||=Wnty*Yn
0|��dS)z�Delete system state from @module, identified by the string
        @key.

        If the item doesn't exist, no change is done.
        N)rr[�KeyErrorr*rdrrr�delete_state�szStateFile.delete_statecCs$|�||�}|dur |�||�|S)a Return the value of an item of system state from @module,
        identified by the string @key, and remove it from the backed
        up system state.

        If the item doesn't exist, #None will be returned, otherwise
        the original string or boolean value is returned.
        N)rergrarrr�
restore_state�s	zStateFile.restore_statecCs
||jvS)z�Return True or False if there is any state stored for @module.

        Can be used to determine if a service is configured.
        )r[)rr\rrr�	has_state�szStateFile.has_stateN)rTrUrVrWrX�SYSRESTORE_STATEFILErrr*rbrergrhrirrrrrZ5srZ)Z
__future__rZloggingr
Zos.pathr3rZhashlibrZsixZPY3ZconfigparserrrZipaplatform.tasksrZipaplatform.pathsrrr`Z	getLoggerrTrZTMPrXrYrjrrZrrrr�<module>s*
|