/
usr
/
libexec
/
kcare
/
python
/
kcarectl
/
__pycache__
/
New Directory
Upload File
HOME
a @�j�! � @ s� d dl Z d dlZd dlZd dlZd dlZddlmZmZ ddd�Zd Zd d� Z dd � Zdd� Zddd�Zdd� Zdd� Zdd� Zdd� ZdS )� N� )� log_utils� process_utilsF�w� c C s@ |rt | �} tj�| �}|r2tj�|�s2t�|� |dur@|}n(zt�| �j}W n tyf |}Y n0 t j ||tj�| �d dd�} z�| �d} |r�tj�| �r�t |�r�t| | j� t�| �� |� | �|� | �� t�| �� � W d � n1 s�0 Y t�| j| � W n" t�y2 t�| j� � Y n0 t|� dS )a� Replace fname with content so that after a reset it has either the old or the new content. The rename gives fname a new inode. preserve_attrs also keeps what an admin may have set on the old one (owner, ACLs, xattrs, SELinux label) and writes through a symlink that only root could have placed. Hard links can't survive a rename. The file keeps the mode of the one it replaces, or gets create_mode when there is none. force_mode sets the mode whatever the old file had: for a secret that an earlier version left readable (create_mode alone would never tighten it). The temp file is created 0600 and gets the final mode before the content is written; with preserve_attrs the old file's attributes are copied onto it first, so it carries them (the old mode, if wider) until that fchmod. A failure of the directory fsync after the rename is only logged: the new content is in place. N�.F)�mode�dir�prefix�delete)�resolve_trusted_link�os�path�dirname�exists�makedirs�stat�st_mode� Exception�tempfile�NamedTemporaryFile�basename�isfile�is_trusted_dir� copy_attrs�name�fchmod�fileno�write�flush�fsync�rename�unlink� fsync_path)�fname�content� ensure_dirr Zcreate_mode�preserve_attrs� force_mode�dnamer Ztmp_file�f� r+ �./usr/libexec/kcare/python/kcarectl/fs_utils.py�atomic_write s2 ,r- �( c C sJ zt �| �}W n ty" Y dS 0 |jdt �� fv oH|jtjtjB @ S )z�Is path owned by root, or the user running us, and writable by nobody else? Only path itself is checked: its ancestors are assumed to be root-controlled. Fr )r r �OSError�st_uid�geteuidr �S_IWGRP�S_IWOTH)r �str+ r+ r, r F s r c C sn t t�D ]`}tj�| �s | S tj�| �}t�| �jdt�� fvsLt |�sT| S tj� |t�| ��} q| S )a? Follow path through symlinks that only root, or the user running us, could have placed. Writing through a link as root lands wherever the link points, so a link that someone else could plant (they own it, or own or can write to its directory) is not followed: the rename then replaces the link itself. r )�range�MAX_SYMLINKSr r �islinkr �lstatr0 r1 r �join�readlink)r �_r) r+ r+ r, r T s r c C s� t jdddd�}d\}}|rt|ddd| |g}zt j|d d d �\}}}W n, tyr } zt|�}W Y d}~n d}~0 0 |dkr�tjd �| |�dd� dS )a@ Copy src's owner, mode, ACLs, xattrs and SELinux label to dst, where cp can. cp is the one way to copy all of them on every supported Python. With --preserve=all it doesn't fail on an xattr or a label the filesystem can't hold. If cp itself fails, dst keeps the attributes it has and the write goes on. �cp)z/usr/binz/binF)� raise_exc)Nzcp not foundz--attributes-onlyz--preserve=allz--T)�catch_stdout�catch_stderrNr z)Failed to keep the attributes of {0}: {1})� print_msg)r �find_cmd�run_commandr/ �strr �logwarn�format)�src�dstr<