HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/states/__pycache__/file.cpython-310.pyc
o

�N�g���@s"dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlmZddlmZmZddlmZmZddlmZddlZddlZddlZddlZddlZddlZddlZddl Zddl!Zddl"Zddl#Zddl$Zddl%Zddl&Zddl'm(Z(ddl)m*Z*dd	l+m,Z-dd
l.m/Z/ej0j1�2�r�ddl3Zddl4Zddl5Zej0j1�2�r�ddl6Z6ddl7Z8e�9e:�Z;dZ<e=�Z>dd
iZ?dd�Z@dd�ZAdd�ZBdd�ZCdd�ZDd�dd�ZEdd�ZF					d�dd�ZGd�dd �ZHd!d"�ZId#d$�ZJd%d&�ZKd'd(�ZL											d�d)d*�ZM					d�d+d,�ZNd�d-d.�ZOd/d0�ZPd1d2�ZQd3d4�ZRd5d6�ZSd7d8�ZTd9d:�ZUd;d<�ZVd�d=d>�ZW	d�d?d@�ZX	d�dBdC�ZYd�dDdE�ZZdFdG�Z[dHdI�Z\dJdK�Z]dLdM�Z^							d�dNdO�Z_					d�dPdQ�Z`													d�dRdS�ZadTdU�Zb								V	W		Xd�dYdZ�Zcd[d\�Zdd]d^�Ze		_		X									X		_	X	X		_	_			X	`		a	X	X							X		X	d�dbdc�Zfgdd�Zgdedf�Zhd�dgdh�Zi														X					X	d�didj�Zj	X										X			_									Xd�dkdl�Zkd�dmdn�Zl							X			X				d�dodp�Zm		q	r				s	X		d�dtdu�Zn				v				X		r			d�dwdx�Zo	y	z			A					_			s	X			d�d{d|�Zpd�d~d�Zqd�d�d��Zr					A					Xd�d�d��Zs					A					d�d�d��Zt								_			d�d�d��Zud�d�d��Zv								d�d�d�Zwd�d�d��Zxd�d��Zy						_		X	X			a			d�d�d��Zzd�d�d��Z{d�d��Z|			�	�d�d�d��Z}								d�d�d��Z~	_			�	d�d�d��Zd�d�d��Z�d�d��Z�d�d�d��Z�dS)�a�!
Operations on regular files, special files, directories, and symlinks
=====================================================================

Salt States can aggressively manipulate files on a system. There are a number
of ways in which files can be managed.

Regular files can be enforced with the :mod:`file.managed
<salt.states.file.managed>` state. This state downloads files from the salt
master and places them on the target system. Managed files can be rendered as a
jinja, mako, or wempy template, adding a dynamic component to file management.
An example of :mod:`file.managed <salt.states.file.managed>` which makes use of
the jinja templating system would look like this:

.. code-block:: jinja

    /etc/http/conf/http.conf:
      file.managed:
        - source: salt://apache/http.conf
        - user: root
        - group: root
        - mode: 644
        - attrs: ai
        - template: jinja
        - defaults:
            custom_var: "default value"
            other_var: 123
    {% if grains['os'] == 'Ubuntu' %}
        - context:
            custom_var: "override"
    {% endif %}

It is also possible to use the :mod:`py renderer <salt.renderers.py>` as a
templating option. The template would be a Python script which would need to
contain a function called ``run()``, which returns a string. All arguments
to the state will be made available to the Python script as globals. The
returned string will be the contents of the managed file. For example:

.. code-block:: python

    def run():
        lines = ['foo', 'bar', 'baz']
        lines.extend([source, name, user, context])  # Arguments as globals
        return '\n\n'.join(lines)

.. note::

    The ``defaults`` and ``context`` arguments require extra indentation (four
    spaces instead of the normal two) in order to create a nested dictionary.
    :ref:`More information <nested-dict-indentation>`.

If using a template, any user-defined template variables in the file defined in
``source`` must be passed in using the ``defaults`` and/or ``context``
arguments. The general best practice is to place default values in
``defaults``, with conditional overrides going into ``context``, as seen above.

The template will receive a variable ``custom_var``, which would be accessed in
the template using ``{{ custom_var }}``. If the operating system is Ubuntu, the
value of the variable ``custom_var`` would be *override*, otherwise it is the
default *default value*

The ``source`` parameter can be specified as a list. If this is done, then the
first file to be matched will be the one that is used. This allows you to have
a default file on which to fall back if the desired file does not exist on the
salt fileserver. Here's an example:

.. code-block:: jinja

    /etc/foo.conf:
      file.managed:
        - source:
          - salt://foo.conf.{{ grains['fqdn'] }}
          - salt://foo.conf.fallback
        - user: foo
        - group: users
        - mode: 644
        - attrs: i
        - backup: minion

.. note::

    Salt supports backing up managed files via the backup option. For more
    details on this functionality please review the
    :ref:`backup_mode documentation <file-state-backups>`.

The ``source`` parameter can also specify a file in another Salt environment.
In this example ``foo.conf`` in the ``dev`` environment will be used instead.

.. code-block:: yaml

    /etc/foo.conf:
      file.managed:
        - source:
          - 'salt://foo.conf?saltenv=dev'
        - user: foo
        - group: users
        - mode: '0644'
        - attrs: i

.. warning::

    When using a mode that includes a leading zero you must wrap the
    value in single quotes. If the value is not wrapped in quotes it
    will be read by YAML as an integer and evaluated as an octal.

The ``names`` parameter, which is part of the state compiler, can be used to
expand the contents of a single state declaration into multiple, single state
declarations. Each item in the ``names`` list receives its own individual state
``name`` and is converted into its own low-data structure. This is a convenient
way to manage several files with similar attributes.

.. code-block:: yaml

    salt_master_conf:
      file.managed:
        - user: root
        - group: root
        - mode: '0644'
        - names:
          - /etc/salt/master.d/master.conf:
            - source: salt://saltmaster/master.conf
          - /etc/salt/minion.d/minion-99.conf:
            - source: salt://saltmaster/minion.conf

.. note::

    There is more documentation about this feature in the :ref:`Names declaration
    <names-declaration>` section of the :ref:`Highstate docs <states-highstate>`.

Special files can be managed via the ``mknod`` function. This function will
create and enforce the permissions on a special file. The function supports the
creation of character devices, block devices, and FIFO pipes. The function will
create the directory structure up to the special file if it is needed on the
minion. The function will not overwrite or operate on (change major/minor
numbers) existing special files with the exception of user, group, and
permissions. In most cases the creation of some special files require root
permissions on the minion. This would require that the minion to be run as the
root user. Here is an example of a character device:

.. code-block:: yaml

    /var/named/chroot/dev/random:
      file.mknod:
        - ntype: c
        - major: 1
        - minor: 8
        - user: named
        - group: named
        - mode: 660

Here is an example of a block device:

.. code-block:: yaml

    /var/named/chroot/dev/loop0:
      file.mknod:
        - ntype: b
        - major: 7
        - minor: 0
        - user: named
        - group: named
        - mode: 660

Here is an example of a fifo pipe:

.. code-block:: yaml

    /var/named/chroot/var/log/logfifo:
      file.mknod:
        - ntype: p
        - user: named
        - group: named
        - mode: 660

Directories can be managed via the ``directory`` function. This function can
create and enforce the permissions on a directory. A directory statement will
look like this:

.. code-block:: yaml

    /srv/stuff/substuf:
      file.directory:
        - user: fred
        - group: users
        - mode: 755
        - makedirs: True

If you need to enforce user and/or group ownership or permissions recursively
on the directory's contents, you can do so by adding a ``recurse`` directive:

.. code-block:: yaml

    /srv/stuff/substuf:
      file.directory:
        - user: fred
        - group: users
        - mode: 755
        - makedirs: True
        - recurse:
          - user
          - group
          - mode

As a default, ``mode`` will resolve to ``dir_mode`` and ``file_mode``, to
specify both directory and file permissions, use this form:

.. code-block:: yaml

    /srv/stuff/substuf:
      file.directory:
        - user: fred
        - group: users
        - file_mode: 744
        - dir_mode: 755
        - makedirs: True
        - recurse:
          - user
          - group
          - mode

Symlinks can be easily created; the symlink function is very simple and only
takes a few arguments:

.. code-block:: yaml

    /etc/grub.conf:
      file.symlink:
        - target: /boot/grub/grub.conf

Recursive directory management can also be set via the ``recurse``
function. Recursive directory management allows for a directory on the salt
master to be recursively copied down to the minion. This is a great tool for
deploying large code and configuration systems. A state using ``recurse``
would look something like this:

.. code-block:: yaml

    /opt/code/flask:
      file.recurse:
        - source: salt://code/flask
        - include_empty: True

A more complex ``recurse`` example:

.. code-block:: jinja

    {% set site_user = 'testuser' %}
    {% set site_name = 'test_site' %}
    {% set project_name = 'test_proj' %}
    {% set sites_dir = 'test_dir' %}

    django-project:
      file.recurse:
        - name: {{ sites_dir }}/{{ site_name }}/{{ project_name }}
        - user: {{ site_user }}
        - dir_mode: 2775
        - file_mode: '0644'
        - template: jinja
        - source: salt://project/templates_dir
        - include_empty: True

Retention scheduling can be applied to manage contents of backup directories.
For example:

.. code-block:: yaml

    /var/backups/example_directory:
      file.retention_schedule:
        - strptime_format: example_name_%Y%m%dT%H%M%S.tar.bz2
        - retain:
            most_recent: 5
            first_of_hour: 4
            first_of_day: 14
            first_of_week: 6
            first_of_month: 6
            first_of_year: all

�N��defaultdict)�Iterable�Mapping)�date�datetime)�zip_longest)�CommandExecutionError)�DeserializationError)�get_accumulator_dir)�OrderedDictz^([[:space:]]*){0}[[:space:]]?�copy_�copycCs*t|t�r|���d�Stdd�|D��S)z;
    Check if source or sources is http, https or ftp.
    �zhttp:zhttps:zftp:cSsg|]	}|���d��qS)r)�lower�
startswith)�.0�s�r�D/opt/saltstack/salt/lib/python3.10/site-packages/salt/states/file.py�
<listcomp>Wsz#_http_ftp_check.<locals>.<listcomp>)�
isinstance�strrr�any)�sourcerrr�_http_ftp_checkQs
rcCstj�ttd�t�S)z'
    Return accumulator data path.
    Zcachedir)�os�path�join�_get_accumulator_dir�__opts__Z__instance_id__rrrr�_get_accumulator_filepathZsr!cCs"dd�}|t��}|d|dfS)Nc	Ssviid�}z(tjj�|d��}tj�|�}|r|n|Wd�WS1s&wYWdSttfy:|YSw)N��accumulators�accumulators_deps�rb)�salt�utils�files�fopen�payload�load�OSError�	NameError)r�ret�f�loadedrrr�_deserializebs

(��z(_load_accumulators.<locals>._deserializer#r$)r!)r1r0rrr�_load_accumulatorsas

r2cCsj||d�}z%tjj�t�d��}tj�||�Wd�WdS1s#wYWdSty4YdSw)Nr"zw+b)r&r'r(r)r!r*�dumpr-)r#r$Zaccumm_datar/rrr�_persist_accummulatorsqs
&��r4cCsrd}|rtd|�}|dkr|d|�d�7}|r*td|�}|dkr*|d|�d�7}|r7tdr7t�|�dS|S)	zF
    Checks if the named user and group are present on the minion
    ��file.user_to_uid�User z is not available �file.group_to_gid�Group z is not available�test)�__salt__r �log�warning)�user�group�err�uid�gidrrr�_check_user|s
rCcCsRtjtj}}||||||vrdS|dur'|�|��|�}||kr'dSdS)a2
    Performs basic sanity checks on a relative path.

    Requires POSIX-compatible paths (i.e. the kind obtained through
    cp.list_master or other such calls).

    Ensures that the path does not contain directory transversal, and
    that it does not exceed a stated maximum depth (if specified).
    FNT)�	posixpath�sep�pardir�strip�count)�relpath�maxdepthrErFZ
path_depthrrr�_is_valid_relpath�srKcCstj�|�tjtjj��S)zd
    Converts a path from the form received via salt master to the OS's native
    path format.
    )rr�normpath�replacerDrE�rrrr�_salt_to_os_path�srOFc
sd�fdd����������fdd�}t�}	t�}
t��t��t��tjj�|�\�}|dur1t}��tj�s<�tj�t	d|��}|rQt	d|��}
|||
�}|D]P}|�
�sZqStjj�t�
|���}t|�d�slqStjj�|���svqS�|�}tj�|�}��|�|�vr�|
�|���|�tjjj||d	�}|	�||f�qS|r�t	d
|��}|D]F}t�
|��}t|�d�s�q�tjj�|���s�q��|�}|r�d}|
D]}|�|tjd�r�t�d
|�d}nq�|r�q�|
�|���|�q�t|	�}�D]/\}}|	D](\}}t���tj�|���}t�|�}||k�r)|�|�|�||f����qq�||
��fS)z?
    Generate the list of files managed by a recurse state
    cstj��t|��S�N)rrrrO)Zmaster_relpath��namerr�	full_path�sz-_gen_recurse_managed_files.<locals>.full_pathcs�|��D]E\}}t�|��}t|�d�sqtjj�|���sqt|�}|D]}|�	|t
j�r:t�
d|�|�|�q%��||f����|��q����|S)N�rJz/** skipping file ** %s, it intersects a symlink)�itemsrDrIrKr&r'�stringutils�check_include_exclude�listrrrEr<�debug�remove�add�update)�	filenames�symlinks�lname�ltarget�srelpathZ
_filenames�filename)�exclude_patrS�include_pat�keep�managed_symlinksrJ�srcpath�vdirrr�process_symlinks�s(��
�
z4_gen_recurse_managed_files.<locals>.process_symlinksNzcp.list_masterzcp.list_master_symlinksrT��saltenv�cp.list_master_dirsFrz4** skipping empty dir ** %s, it intersects a symlinkT)�setr&r'�url�parse�__env__�endswithrDrEr;rG�data�decoderIrKrVrWrr�dirnamer[�createrr<rYrX�pathlib�Path�append�pop�index)rRr�
keep_symlinksrdrcrJ�
include_empty�kwargsriZ
managed_filesZmanaged_directories�senvZfns_r^�fn_Zrelname�destrt�srcZmdirsZmdirZmdest�islink�linkZnew_managed_filesZlink_src_relpath�_Z	file_destZfile_srcZsymlink_full_pathZfile_dest_full_pathr)	rcrSrdrerfrJrRrgrhr�_gen_recurse_managed_files�s�

�


���


����r�cs2t��dd�}�fdd������fdd��dd�}t�}t|t�r�d	d
�|D�}|D]a}tD]\}|d|dksA|d
|dkr�|d}	|d}
tj�|	�r�||	|�r�|
dkrntdi|��d}t�	d|	|�|�
|�q/�r~t���|	��|�
��q/|�
||	��q/|�|	�q/q+t�	dt|��t|�S)z~
    Generate the list of files that need to be kept when a dir based function
    like directory or recurse has a clean.
    cSs4tj�|�}tj�|�}tj�||�}|�tj�S)zB
        Check whether ``path`` is child of ``directory``
        )rr�abspathrIrrF)r�	directoryZrelativerrr�	_is_childOsz"_gen_keep_files.<locals>._is_childcslt�}tj�|�r4��|d�\}}|�|�|D]}|�tj�||��q|D]}|�tj�||��q'|S�N)rr)rmrr�isdir�getr[r)r�_ret�dirsr(�_name)�walk_drr�_add_current_pathZs
z*_gen_keep_files.<locals>._add_current_pathcsbtj�|�r-���|����|d�\}}|D]}tj�||�}���|���||�qdSdSr�)rrr�r\r�r)rRr.r�r�Z_d�p�r��_process_by_walk_dr�Zwalk_retrrr�es�z+_gen_keep_files.<locals>._process_by_walk_dcSsvt�}tj�|�r9tjj�|�D](\}}}|�|�|D]}|�tj�||��q|D]}|�tj�||��q+q|SrP)	rmrrr�r&r'�os_walkr[r)rRr.�rootr�r(rrr�_processns
�z!_gen_keep_files.<locals>._processcS�g|]}d|vr|�qS��filer)r�comprrrr{�z#_gen_keep_files.<locals>.<listcomp>rRr��__id__�fun�recurse�zKeep from %s: %sz&Files to keep from required states: %sNr)rmrrXZ__lowstate__rrr�r�r<rYr\r[)rR�requirer�r�r�reZrequired_filesr��low�fnr�Zfkeeprr�r�_gen_keep_filesGs<	
 

�
��r�cCsNd}d}tj�|�sd}d|�d�}||fStj�|�s#d}|�d�}||fS)NTr5F�Specified file � is not an absolute pathz: file not found)rr�isabs�exists)rRr.�msgrrr�_check_file�s�
r�cCs�t�}|�|�t|t�rH|D]8}tj�|�sqtj�tj�|��}|�|�	tj�tj�	|��}|�|�tj�
|�\}}|�tj�sFnq(q|S)zX
    Compile a list of valid keep files (and directories).
    Used by _clean_dir()
    )
rmr[rrXrrr��normcaser�rt�
splitdrive�lstriprE)r�re�	real_keepr�driverrrr�_find_keep_files�s"



�r�csttj����t�|��t������fdd�}tjj���D]\}}}t�	||�D]}|tj�
||��q)qt��S)z�
    Clean out all of the files and directories in a directory (root) while
    preserving the files in a list (keep) and part of exclude_pat
    cs|tj�|��vr:tjj�tj�|��d��sdS��|�t	ds<zt�
|�WdSty9td|�YdSwdSdS)Nr:�file.remove)
rrr�r&r'rVrWrIr[r rZr,r;)Znfn�rcr��removedr�rr�_delete_not_kept�s�
��z$_clean_dir.<locals>._delete_not_kept)rrr�r�rmr&r'r��	itertools�chainrrX)r�rercr��rootsr�r(rRrr�r�
_clean_dir�s
�r�cCsd|d<||d<|S)NF�result�commentr)r.�err_msgrrr�_error�sr�c!
si}|s|r(|	dus|rJ�tt�|	��}
i}|
D]}|d|df||d<q|�rzt|�}WnttfyK}zd|�|fWYd}~Sd}~wwd|vrRd}d|vrXd}d|vr`d}d}d	|v}d
|v}|
D]�\�}}|r�|D]q}i}tj��|�}td|d|
�}|dur�||�	d�ks�||�	d�ks�||d<|dur�||�	d�ks�||�	d
�ks�||d<t
jj�
|�	d��}t
jj�
|�}|dur�||kr�|
s�|�	d�dks�t
jj��s�||d<|r�|||<qs|�r|D]}tj��|�}t|||||
�}|�r|||<q�qj|�st�||||
�}|�r||�<|�rKt�||������fdd�}|
D] \�}}|D]
}|�||���q1|D]
}|�||���q>�q*tj����sXddi|�<|�r�dg}|D]}||��D]\}} |�|�d|�d| �d���qh�q`dd�|�|fSdd��d�|fS)�;
    Check what changes need to be made on a directory
    N��rFr>r?�mode�ignore_files�ignore_dirs�
file.statsrArB�typer�csFtj��|�}|�vr
iStjj�tj�|��d��siS|ddiiS)Nr�zRemoved due to clean)rrrr&r'rVrWrI)�fnamer�rcrerRr�rr�_check_changes:s�z(_check_directory.<locals>._check_changesr��newz%The following files will be changed:
�: z - �
r5T�The directory � is in the correct state)rX�_depth_limited_walk�_get_recurse_set�	TypeError�
ValueErrorrrrr;r�r&r'r(�normalize_mode�platform�is_linux�_check_dir_metar�r\r�rUrx)!rRr>r?r��dir_mode�	file_mode�cleanr�rc�	max_depth�follow_symlinks�
children_only�changes�walk_lr��i�recurse_set�exc�check_files�
check_dirsr�r(r�Zfchanger�stats�smodeZname_r��commentsr�key�valrr�r�_check_directory�s����
��
��� �r�cCsftj�|�s
|ddii}ntjjj|di|||||dd�	d}|r*dd|�d	�|fSdd
|�d�|fS)r�r�r�r�T)	Zobj_nameZobj_typer.�owner�grant_perms�
deny_perms�inheritance�resetZ	test_moder�NzThe directory "z" will be changedr�r�)rrr�r&r'�win_daclZcheck_perms)rR�	win_owner�	win_perms�win_deny_perms�win_inheritance�win_perms_resetr�rrr�_check_directory_winWs$�
�r�cCs�z
td|d|�}Wntyi}Ynwi}|s d|d<|S|dur5||dkr5||�d�kr5||d<|durJ||dkrJ||�d�krJ||d<tjj�|d	�}tjj�|�}|duru||kru|sq|�d
�dksqtjj��su||d	<|S)z1
    Check the changes in directory metadata
    r�Nr�r�r>rAr?rBr�r�r�)	r;r	r�r&r'r(r�r�r�)rRr>r?r�r�r�r�r�rrrr�ws0�""�
�r�cCs�ddd|id�}tj�|�sd|�d�|d<|Std|d	d
�}|dur.t|�t|d�ks<|durKt|�t|d�krKd
|��|d<d|i|d<|Sd|d<d|�d�|d<|S)z?
    Check to see if a file needs to be updated or created
    Nr5r�)r�r�r��File � is set to be createdr�r�F�r��atime�mtimez Times set to be updated on file �touchedr�Tr�z! exists and has the correct times)rrr�r;r)rRr�r�r.r�rrr�_check_touch�s ���r�cCsBtjj��rtjj�|�}||fStd|dd�td|dd�fS)N�
file.get_userFr��file.get_group)r&r'r��
is_windowsr�Z	get_ownerr;)rr�rrr�_get_symlink_ownership�s�r�cCs0t|�\}}tjj��r||kS||ko||kS)zM
    Check if the symlink ownership matches the specified user and group
    )r�r&r'r�r�)rr>r?r��cur_user�	cur_grouprrr�_check_symlink_ownership�srcCsjtjj��rz
tjj�||�WntyYnwz
td|||�Wn	ty-Ynwt	||||�S)z\
    Set the ownership of a symlink and return a boolean indicating
    success/failure
    �file.lchown)
r&r'r�r�r�Z	set_ownerr	r;r,r)rr>r?r�rrr�_set_symlink_ownership�s��rc	Cs�i}tj�|�std|�s||d<dd|�d|�d�|fStd|�rhtd|�|kr;||d<dd	|�d
|��|fSd}d|�d
�}t||||�scd}djt|��|d<|dj||gt|��R�7}|||fS|rsdd�||�|fSdd�|�|fS)z$
    Check the symlink function
    �file.is_linkr�NzSymlink � to � is set for creation�
file.readlink�change�Link �  target is set to be changed to TzThe symlink z is presentz{}:{}�	ownershipzK, but the ownership of the symlink would be changed from {2}:{3} to {0}:{1}zVThe file or directory {} is set for removal to make way for a new symlink targeting {}FzSFile or directory exists where the symlink {} should be. Did you mean to use force?)rrr�r;r�formatr�)	rR�target�forcer>r?r�r�r�r�rrr�_symlink_check�sN�����
��rcCsTtd|ddd�}d|vrdS|d}td|ddd�}d|vr"dS|d}||kS)zF
    Check to see if the inodes match for the name and the target
    r�NFr��inode�r;)rRr�resZname_iZtarget_irrr�_hardlink_samesrcCsi}tj�|�sd|�d�}d||fStj�|�r#d|��}d||fStj�|�r3d|��}d||fStj�|�sKd|�d|�d�}||d	<d
||fStd|�rtt||�rcd|�d
|��}d||fSd|�d|��}||d<d
||fS|r�d�||�}d
||fSd�|�}d||fS)z%
    Check the hardlink function
    �Target � for hard link does not existFz#Unable to hard link from directory z!Unable to hard link to directory z
Hard link rrr�N�file.is_hardlinkzThe hard link z is presently targetting Trr	rzXThe file or directory {} is set for removal to make way for a new hard link targeting {}zUFile or directory exists where the hard link {} should be. Did you mean to use force?)rrr�r�r;rr)rRrr
r�r�rrr�_hardlink_checks<








�
�
rcCs&|r|Sd|vrt�d�|dS|S)z�
    Convert owner to user, since other config management tools use owner,
    no need to punish people coming from other systems.
    PLEASE DO NOT DOCUMENT THIS! WE USE USER, NOT OWNER!!!!
    r�zBUse of argument owner found, "owner" is invalid, please use "user")r<r=)r}r>rrr�_test_ownerIs�rc	Csp|durg}|durg}|r|rddgfS|r|rddgfS|r(dd||fgfSddtt||dt|����fS)zb
    Silly little function to give us a standard tuple list for sources and
    source_hashes
    NFz)source and sources are mutually exclusivez4source_hash and source_hashes are mutually exclusiveTr5)rXr�len�r�source_hash�sources�
source_hashesrrr�_unify_sources_and_hashesZs

 r�jinjac	KsHdiddgd�}|durt|d�Sg}|D]�\}}|r|ni}	|r&|	�|�td|df|t|	d�|��}
t�d	|
|�|
r�d}tjj�	|
d
��}|�
�}tjj�|�}|�
d�}Wd�n1scwY|s�d�|
|�}
t�|
�||d<t||
�S|�d�|��qd
|��}
t�|
�||d<t||
�S||d<|S)z�
    Iterate a list of sources and process them as templates.
    Returns a list of 'chunks' containing the rendered templates.
    �_get_template_textsTr5)rRr�r�r�rrNz1_get_template_texts called with empty source_listzcp.get_template)�templaterk�contextz-cp.get_template returned %s (Called with: %s)r%z-Failed to read rendered template file {} ({})rRzFailed to load template file rr)r�r\r;rpr<rYr&r'r(r)�readrV�
to_unicode�
splitlinesrrxr)�source_listr �defaultsr!r}r.ZtxtlrrZtmpctxZrndrd_templ_fnZtmplines�fp_r�rrrrtsX	�

������


rcCs�t|t�rtjjj||d�g}|St|t�r|g}|St|t�r@t|t�s@g}|D]}t|t�r6|�	|�q)|�	t|��q)|St|�g}|S)z-
    ensure ``arg`` is a list of strings
    ��encoding)
r�bytesr&r'rVr#rrrrx)�argr)r.�itemrrr�_validate_str_list�s

�
�

�r-cCstd|dd�S)Nr�Fr�rrNrrr�_get_shortcut_ownership�sr.cCst|�}||kS)zD
    Check if the shortcut ownership matches the specified user
    )r.)rr>r�rrr�_check_shortcut_ownership�sr/cCs0z	td||�Wn	tyYnwt||�S)z]
    Set the ownership of a shortcut and return a boolean indicating
    success/failure
    r)r;r,r/)rr>rrr�_set_shortcut_ownership�s�
r0cCs�i}tj�|�s||d<dd|�d|�d�|fStj�|�r�tjj���Utj	�
d�}	|	�|�}
|
j�
�|�
�kg}|durF|�|
j|k�|durV|�|
j�
�|�
�k�|durb|�|
j|k�|durr|�|
j�
�|�
�k�Wd�n1s|wYt|�s�||d<dd�||�|fSd	}d
|�d�}
t||�s�d}t|��|d<|
d
�|t|��7}
||
|fS|r�dd�||�|fSdd�|�|fS)z%
    Check the shortcut function
    r�Nz
Shortcut "�" to "z" is set for creation�
WScript.Shellrz1Shortcut "{}" target is set to be changed to "{}"TzThe shortcut "z" is presentr
zD, but the ownership of the shortcut would be changed from {1} to {0}z[The link or directory "{}" is set for removal to make way for a new shortcut targeting "{}"FzVLink or directory exists where the shortcut "{}" should be. Did you mean to use force?)rrr��isfiler&r'�winapi�Com�win32com�client�Dispatch�CreateShortcut�
TargetPathrrx�	Arguments�WorkingDirectory�Description�IconLocation�allrr/r.)rRr�	arguments�working_dir�description�
icon_locationr
r>r��shell�scut�state_checksr�r�rrr�_shortcut_check�sn��
�����
�
��rGc
Csftjj��r)tj�|�\}}	tj�|�st|��|r|n|}t	d|||||d�St	d||||d�S)a�
    Helper function for creating directories when the ``makedirs`` option is set
    to ``True``. Handles Unix and Windows based systems

    .. versionadded:: 2017.7.8

    Args:
        name (str): The directory path to create
        user (str): The linux user to own the directory
        group (str): The linux group to own the directory
        dir_mode (str): The linux mode to apply to the directory
        win_owner (str): The Windows user to own the directory
        win_perms (dict): A dictionary of grant permissions for Windows
        win_deny_perms (dict): A dictionary of deny permissions for Windows
        win_inheritance (bool): True to inherit permissions on Windows

    Returns:
        bool: True if successful, otherwise False on Windows
        str: Error messages on failure on Linux
        None: On successful creation on Linux

    Raises:
        CommandExecutionError: If the drive is not mounted on Windows
    �
file.makedirs�rr�r�r�r�)rr>r?r�)
r&r'r�r�rrr�r�r	r;)
rRr>r?r�r�r�r�r�r�rrrr�	_makedirss"��rJc
Ks�tj�|�}tjj�|�}t||d�}|iddd�}|s!t|d�S|dur)t	d}tjj
��r;|dur9t�
d|�|}|durTd	tvrRtd
td	|��dd��}n|}g}	td
|�}
td|�}|
dkro|	�d|�d��|dkr||	�d|�d��tj�|�s�|	�d|�d��tj�|�s�|	�d|�d��|	r�d�|	�}t|	�dkr�|d7}t||�St	dr�t|||�\}
}}|
|d<||d<||d<|Stddg||g�D]\}}tj�|�r�d|�d|��}t||�Sq�tj�|�s�d |�d!�}t||�Stj�tj�|���s$|�rtd"||||d#�nt|d$�tj�|���Stj�|��rJtd%|��sJ|�rAt�|�d&|dd'<n	t|d(|�d)��Std%|��r�t||��red|d<d*�||�|d<|St�|�z	td+||�Wn"t�y�}zd,|d<d-�|||�|d<|WYd}~Sd}~wwd|d<d.|�d/|��|d<||dd0<|Stj�|��s�z	td+||�Wn"t�y�}zd,|d<d1�|||�|d<|WYd}~Sd}~wwd|d<d2|�d/|��|d<||dd0<|S)3a�
    Create a hard link
    If the file already exists and is a hard link pointing to any location other
    than the specified target, the hard link will be replaced. If the hard link
    is a regular file or directory then the state will return False. If the
    regular file is desired to be replaced with a hard link pass force: True

    name
        The location of the hard link to create
    target
        The location that the hard link points to
    force
        If the name of the hard link exists and force is set to False, the
        state will fail. If force is set to True, the file or directory in the
        way of the hard link file will be deleted to make room for the hard
        link, unless backupname is set, when it will be renamed
    makedirs
        If the location of the hard link does not already have a parent directory
        then the state will fail, setting makedirs to True will allow Salt to
        create the parent directory
    user
        The user to own any directories made if makedirs is set to true. This
        defaults to the user salt is running as on the minion
    group
        The group ownership set on any directories made if makedirs is set to
        true. This defaults to the group salt is running as on the minion. On
        Windows, this is ignored
    dir_mode
        If directories are to be created, passing this option specifies the
        permissions for those directories.
    �r>Tr5�rRr�r�r�z"Must provide name to file.hardlinkNr>�GThe group argument for %s has been ignored as this is a Windows system.�	user.info�file.gid_to_grouprBrr6r8r7� does not existr9r�r��Specified target �. r��.r:r�r�r��to�fromzUnable to hard link z directory rrrH�r>r?r�z)Directory {} for hard link is not presentrz(File for hard link was forcibly replaced�forcedz File exists where the hard link �
 should bez0Target of hard link {} is already pointing to {}z	file.linkFz.Unable to set target of hard link {} -> {}: {}zSet target of hard link � -> r�z+Unable to create new hard link {} -> {}: {}zCreated new hard link )rr�
expanduserr&r'r(r�rr�r r�r�r<r=r;r�rxr�rrrrr�r�rtrr3rZrr	)rRrr
�makedirsr>r?r�r}r.�preflight_errorsrArBr��tresult�tcomment�tchanges�	directionr,�Errr�hardlinkSs�)
��

�

��	
�
�������rbcKs6tj�|�}|iddd�}|st|d�Stjj�|�}t||d�}|rC|dus+|durCt	d|�rCt
|�\}}|dur=|}|durC|}|durKtd}tjj�
�rvt	d	|�s`t	d
�}|s`d}|durj|rh|nd}|durtt�d|�|}|dur�d	t	vr�t	d
t	d	|��dd��}n|}g}tjj�
��rz	tjj�|�Wnty�}z|�d|�d��WYd}~nd}~ww|	r�|	D]&}z	tjj�|�Wq�ty�}z|�d|�d��WYd}~q�d}~ww|
�r|
D]'}z	tjj�|�Wq�t�y}z|�d|�d��WYd}~q�d}~wwn(t	d|�}t	d|�}|dk�r0|�d|�d��|dk�r>|�d|�d��tj�|��sN|�d|�d��|�rfd�|�}t|�dk�ra|d7}t||�St||||||�\}}}tj�tj�|���s�|�r�td�r�|dtj�|��d�7}nIzt||||||	|
|d�Wn:t�y�}zt|d|j�d��WYd}~Sd}~wwtd�r�|d �tj�|��7}nt|d!�tj�|���Std�r�||d"<||d#<||d$<|St	d|��r�tj�t	d%|��tj�|�k�rt	d&|��n*t||||��r2tjj�
��r'd'�||�|d#<|Sd(�|||�|d#<|St ||||��rgtjj�
��rQd)�||�|d#<||d$d*<|Sd+�|||�|d#<|�d,|��|d$d*<|Sd-|d"<tjj�
��r�|d#d.�||�7<|S|d#d/�|||�7<|Stj�!|��r8|du�rtj�|��s�|tj�"|�k�r�tj�tj�tj�|��|�}nt|d0�|��Stj�#|��r�|�s�t|d1�|||��St	d&|�zt	d2|||
d3�WnOt$�y}zi|d$<tj%d4||dd5�t|d6�|||��WYd}~Sd}~ww|�s8|�s8tj�&|��r"d7n
tj�|��r+d8nd9}t||�d:|�d;��Szt	d<||||d=�Wn$tt'f�yh}zd-|d"<d>�|||�|d#<|WYd}~Sd}~wwd?|�d@|��|d#<||d$dA<t||||��s�t ||||��s�d-|d"<|d#dB�||�7<|S)Ca�
    Create a symbolic link (symlink, soft link)

    If the file already exists and is a symlink pointing to any location other
    than the specified target, the symlink will be replaced. If an entry with
    the same name exists then the state will return False. If the existing
    entry is desired to be replaced with a symlink pass force: True, if it is
    to be renamed, pass a backupname.

    name
        The location of the symlink to create

    target
        The location that the symlink points to

    force
        If the name of the symlink exists and is not a symlink and
        force is set to False, the state will fail. If force is set to
        True, the existing entry in the way of the symlink file
        will be deleted to make room for the symlink, unless
        backupname is set, when it will be renamed

        .. versionchanged:: 3000
            Force will now remove all types of existing file system entries,
            not just files, directories and symlinks.

    backupname
        If the name of the symlink exists and is not a symlink, it will be
        renamed to the backupname. If the backupname already
        exists and force is False, the state will fail. Otherwise, the
        backupname will be removed first.
        An absolute path OR a basename file/directory name must be provided.
        The latter will be placed relative to the symlink destination's parent
        directory.

    makedirs
        If the location of the symlink does not already have a parent directory
        then the state will fail, setting makedirs to True will allow Salt to
        create the parent directory

    user
        The user to own the file, this defaults to the user salt is running as
        on the minion unless the link already exists and
        ``inherit_user_and_group`` is set

    group
        The group ownership set for the file, this defaults to the group salt
        is running as on the minion unless the link already exists and
        ``inherit_user_and_group`` is set. On Windows, this is ignored

    mode
        The permissions to set on this file, aka 644, 0775, 4664. Not supported
        on Windows.

        The default mode for new files and directories corresponds umask of salt
        process. For existing files and directories it's not enforced.

    win_owner
        The owner of the symlink and directories if ``makedirs`` is True. If
        this is not passed, ``user`` will be used. If ``user`` is not passed,
        the account under which Salt is running will be used.

        .. versionadded:: 2017.7.7

    win_perms
        A dictionary containing permissions to grant

        .. versionadded:: 2017.7.7

    win_deny_perms
        A dictionary containing permissions to deny

        .. versionadded:: 2017.7.7

    win_inheritance
        True to inherit permissions from parent, otherwise False

        .. versionadded:: 2017.7.7

    atomic
        Use atomic file operation to create the symlink.

        .. versionadded:: 3006.0

    disallow_copy_and_unlink
        Only used if ``backupname`` is used and the name of the symlink exists
        and is not a symlink. If set to ``True``, the operation is offloaded to
        the ``file.rename`` execution module function. This will use
        ``os.rename`` underneath, which will fail in the event that ``src`` and
        ``dst`` are on different filesystems. If ``False`` (the default),
        ``shutil.move`` will be used in order to fall back on a "copy then
        unlink" approach, which is required for moving across filesystems.

        .. versionadded:: 3006.0

    inherit_user_and_group
        If set to ``True``, the link already exists, and either ``user`` or
        ``group`` are not set, this parameter will inform Salt to pull the user
        and group information from the existing link and use it where ``user``
        or ``group`` is not set. The ``user`` and ``group`` parameters will
        override this behavior.

        .. versionadded:: 3006.0
    Tr5rLz!Must provide name to file.symlinkrKNrr>rN�user.current�SYSTEM��The group argument for %s has been ignored as this is a Windows system. Please use the `win_*` parameters to set permissions in Windows.rOrBrr7rPr6r8r9r�r�rRr�rSr:r�z will be created�rRr>r?r�r�r�r�r��Drive � is not mappedz(
Directory {} for symlink is not presentz'Directory {} for symlink is not presentr�r�r�rr�z%Symlink {} is present and owned by {}z(Symlink {} is present and owned by {}:{}z!Set ownership of symlink {} to {}r
z$Set ownership of symlink {} to {}:{}�:Fz+Failed to set ownership of symlink {} to {}z.Failed to set ownership of symlink {} to {}:{}z6Backupname must be an absolute path or a file name: {}zESymlink & backup dest exists and Force not set. {} -> {} - backup: {}z	file.move)�disallow_copy_and_unlinkz#Encountered error renaming %s to %s)�exc_infoz(Unable to rename {} to backup {} -> : {}ZFileZ	DirectoryzFile system entryz exists where the symlink rXzfile.symlink)r
�atomicz)Unable to create new symlink {} -> {}: {}zCreated new symlink rYr�z*, but was unable to set ownership to {}:{})(rrrZr�r&r'r(r�rr;r�r r�r�r<r=r��
win_functionsZget_sid_from_namer	rxr�rrrr�rtrJ�messagerrLrrr��basename�lexists�	ExceptionrYr3r,)rRrr
�
backupnamer[r>r?r�r�r�r�r�rlrjZinherit_user_and_groupr}r.r�r�r\r�Z
name_checkrArBr�r]r^r_Z
fs_entry_typerrr�symlinks�z
�
�
���������




�

�
 ��

�
��
��!��������������
���
�
������
�������rsc
Ks�tj�|�}|iddd�}|st|d�Stj�|�s#t|d|�d��S|dkr,t|d�Stj�|�s8tj�|�r�td	rPd
|d<||dd
<d|�d�|d<|Sztd|dd�d|��|d<||dd
<|WSt	y�}zt||��WYd
}~Sd
}~wwtj�
|�r�td	r�d
|d<||dd
<d|�d�|d<|Sztd|dd�d|��|d<||dd
<|WSty�t|d|���YSwd|�d�|d<|S)a�
    Make sure that the named file or directory is absent. If it exists, it will
    be deleted. This will work to reverse any of the functions in the file
    state module. If a directory is supplied, it will be recursively deleted.

    If only the contents of the directory need to be deleted but not the directory
    itself, use :mod:`file.directory <salt.states.file.directory>` with ``clean=True``

    name
        The path which should be deleted
    Tr5rLz Must provide name to file.absentr�r��/zRefusing to make "/" absentr:Nr�r�r�r�� is set for removalr�r��r
z
Removed file �
Directory �Removed directory �Failed to remove directory � is not present)rrrZr�r�r3r�r r;r	r�r,)rRr}r.r�rrr�absent�sN

���r{r��ORTc 
s�tj�|�}|iddd�}
|	��}	|	dvrd}	t�d�|
r-|
��}
|
dvr-d}
t�d	�tj�|�s<t|
d
|�d��Stj�	|�sJt|
|�d��Sgd
�}t
|t�rY|��|vr[d}|��}t
|t�rmtj
jj|dd�}g}t��}|durzdg}g�|D]
}��t�|��q~g�|p�gD]
}��t�|��q���fdd�}tj|d|d�D]�\}}}||D]�}d}d}d}tj�||�}z�tj�|�r�|s�d}t�|�}nt�|�}t�|j�r�|}n|j}|dkr�|j}n|dkr�|j}n|j}t |t�!|��}|}|�r|}|
�r|
dv�r|
dk�r|j"|k}n||k}n|	dk�r.||k�o,|j"|k}n
||k�p7|j"|k}|�rI||d��rI|�rI|�|�Wq�t#�yTYq�t$�yct�d|�Yq�wq�|�r�t%d�r�d|
d<|�d�|
d <d!|i|
d"<|
Sg|
d"d!<z|D]}t&d#|�|
d"d!�|��q�Wnt'�y�}zt|
|��WYd}~Sd}~wwd$�(t)|�|�|
d <|
Sd%|��|
d <|
S)&aJ

    .. versionchanged:: 3005,3006.0

    Remove unwanted files based on specific criteria.

    The default operation uses an OR operation to evaluate age and size, so a
    file that is too large but is not old enough will still get tidied. If
    neither age nor size is given all files which match a pattern in matches
    will be removed.

    NOTE: The regex patterns in this function are used in ``re.match()``, so
    there is an implicit "beginning of string" anchor (``^``) in the regex and
    it is unanchored at the other end unless explicitly entered (``$``).

    name
        The directory tree that should be tidied

    age
        Maximum age in days after which files are considered for removal

    matches
        List of regular expressions to restrict what gets removed.  Default: ['.*']

    rmdirs
        Whether or not it's allowed to remove directories

    size
        Maximum allowed file size. Files greater or equal to this size are
        removed. Doesn't apply to directories or symbolic links

    exclude
        List of regular expressions to filter the ``matches`` parameter and better
        control what gets removed.

        .. versionadded:: 3005

    full_path_match
        Match the ``matches`` and ``exclude`` regex patterns against the entire
        file path instead of just the file or directory name. Default: ``False``

        .. versionadded:: 3005

    followlinks
        This module will not descend into subdirectories which are pointed to by
        symbolic links. If you wish to force it to do so, you may give this
        option the value ``True``. Default: ``False``

        .. versionadded:: 3005

    time_comparison
        Default: ``atime``. Options: ``atime``/``mtime``/``ctime``. This value
        is used to set the type of time comparison made using ``age``. The
        default is to compare access times (atime) or the last time the file was
        read. A comparison by modification time (mtime) uses the last time the
        contents of the file was changed. The ctime parameter is the last time
        the contents, owner,  or permissions of the file were changed.

        .. versionadded:: 3005

    age_size_logical_operator
        This parameter can change the default operation (OR) to an AND operation
        to evaluate age and size. In that scenario, a file that is too large but
        is not old enough will NOT get tidied. A file will need to fulfill BOTH
        conditions in order to be tidied. Accepts ``OR`` or ``AND``.

        .. versionadded:: 3006.0

    age_size_only
        This parameter can trigger the reduction of age and size conditions
        which need to be satisfied down to ONLY age or ONLY size. By default,
        this parameter is ``None`` and both conditions will be evaluated using
        the logical operator defined in ``age_size_logical_operator``. The
        parameter can be set to ``age`` or ``size`` in order to restrict
        evaluation down to that specific condition. Path matching and
        exclusions still apply.

        .. versionadded:: 3006.0

    rmlinks
        Whether or not it's allowed to remove symbolic links

        .. versionadded:: 3006.0

    .. code-block:: yaml

        cleanup:
          file.tidied:
            - name: /tmp/salt_test
            - rmdirs: True
            - matches:
              - foo
              - b.*r
    Tr5rL)�ANDr|r|z=Logical operator must be 'AND' or 'OR'. Defaulting to 'OR'...)�age�sizeNzOage_size_only parameter must be 'age' or 'size' if set. Defaulting to 'None'...r�r�z& does not exist or is not a directory.)r��ctimer�r�)Z
handle_metricz.*cs:�D]}|�|�r�D]}|�|�rdSqdSqdS)NFT)�match)rR�progZ_ex�ZexesZprogsrr�_matcheses

��ztidied.<locals>._matchesF)�top�topdown�followlinksrr�r�>rr~r~r}rQz+Unable to read %s due to permissions error.r:r�z is set for tidyr�r�r�r�z1Removed {} files or directories from directory {}z!Nothing to remove from directory )*rrrZ�upperr<r=rr�r�r�rrr&r'rVZhuman_to_bytesr�todayrx�re�compile�walkrr��lstat�stat�S_ISDIR�st_mode�st_size�st_ctime�st_mtime�st_atime�abs�
fromtimestamp�days�FileNotFoundError�PermissionErrorr r;r	rr) rRr~�matchesZrmdirsr�excludeZfull_path_matchr�Ztime_comparisonZage_size_logical_operatorZ
age_size_onlyZrmlinksr}r.Z	poss_compZtodeleter��regexr�r�r�r(�elemZmyageZmysizeZdeletemerZmystatZmytimestamprbZcompare_age_sizer�rr�r�tidied�s�l
�






���6
�����r�cKsZtj�|�}|iddd�}|st|d�Stj�|�s#t|d|�d��Sd|�d�|d	<|S)
ai
    Verify that the named file or directory is present or exists.
    Ensures pre-requisites outside of Salt's purview
    (e.g., keytabs, private keys, etc.) have been previously satisfied before
    deployment.

    This function does not create the file if it doesn't exist, it will return
    an error.

    name
        Absolute path which must exist
    Tr5rLz Must provide name to file.exists�Specified path rP�Path � existsr��rrrZr�r��rRr}r.rrrr��s

r�cKsZtj�|�}|iddd�}|st|d�Stj�|�r#t|d|�d��Sd|�d�|d	<|S)
z�
    Verify that the named file or directory is missing, this returns True only
    if the named file is missing but does not remove the file if it is present.

    name
        Absolute path which must NOT exist
    Tr5rLz!Must provide name to file.missingr�r�r�z is missingr�r�r�rrr�missing�s
r�r5ri�strictc'EKs�
d|'vr	|'�d�tj�|�}id|dd�}(|st|(d�S|dur,tjj��r,t|(d�S|dur;tjj��r;t|(d�S|durJtjj�	�sJt|(d	�S|re|�
d
d�})|�
dd�}*|�
dd�}+|�
d
d�},nd})}*}+},z|��dk}-|-rxd}Wnty�d}-Ynwtjj
�|�}tdd�|||fD��}.|r�|.dkr�t|(d�S|-r�|.dkr�t|(d�S|.dkr�t|(d�S|dur�t|�s�|r�t�d�|s�|.dkr�|
r�d}
t�d|�d|'vr�|(�dg��d�|du�r2t|t��rg}/|D] }0td|0t|d�}1|1tu�r
t|(d|0�d��S|/�|1�q�tj�|/�}2nrtd|t|d�}2|2tu�r1t|(d|�d��SnZ|du�r�t|t��rjg}/|D]!}3td |3t|d�}1|1tu�r\t|(d!|1�d��S|/�|1��qAtj�|/�}2n"td |t|d�}2|2tu�r�t|(d!|�d��Sn
|du�r�|}2nd}2|2du�r:|�s�|2�s�|�r�d"|��}4n|�r�d#|��}4nd$}4t|(d%�|4��Sz9t|2|d&�}5|5�s�t|(d'�WSd}|5D]}6|6��D]}7||7�d(��d)�tj7}�q͐q�|�s�|�d(��d)�}Wnt�y|	�r�t|(d*�YS|2}Ynw|	�r:td+||	||t d,�}t|t!��s:d-|v�r#|d-|(d-<nd|(d-<d.|v�r4|d.|(d.<|(Sd/|(d.<|(St"|'|d0�}tjj���r`| du�rS|�rQ|nd} |du�r^t�d1|�|}|�sttj�#|��std2|�d3�|(d.<|(St$||�}8|8�r�t|(|8�Stj�%|��s�t|(d4|�d5��Stj�&|��r�d6|�d7�|(d.<d|(d-<|(S|du�r�i}nt|t'��s�t|(d8�S|�r�t|t'��s�t|(d9�S|du�rD|	�sD|�rD|�sDtj�(|��rD|
�rDz:td:||t �\}}d}9|�r|�rt)j*�+|�j,d;v�rtj�%|��std<||||t |%d=�}9td>||9d?�}:Wnt-t.f�y7};ztj/d@|;t0j1dA�WYd};~;nd};~;ww|9�rD|9dB|:k�rDd}
|
�s�tj�(|��r�i}<tjj���retdC||(| |!|"|#|$dD�}(ntdC||(||||||)|*|+|,dE�\}(}<t2dF�r�|�r�t|<t'��r�dG|<v�r�||<dGk�r�dH�|||<dG�|(d.<|(Sd2|�dI�|(d.<|(S|(dJ�s�|(d-�r�d2|�dK�|(d.<|(St3�\}=}>||=v�r�|�s�i}|=||dL<z�t2dF�r�dMtv�r=tdM|||||||||	||t |||-f|)|*|+|,|%|dN�|'��|(dJ<tjj���r=ztdC||(| |!|"|#|$dD�}(Wn(t-�y<}?zt|(dJt4��s2|?j5�6dO��r2||(dJdP<WYd}?~?nd}?~?wwt|(dJt4��rV|(dJ\|(d-<|(d.<i|(dJ<|(WS|(dJ�r�d|(d-<dQ|�dR�|(d.<|(d.dS7<dT|(dJv�r|�sdU|(dJdT<|(WSd|(d-<dQ|�dV�|(d.<|(WStd:||t �\}}Wn t-�y�}?zd|(d-<dW|?��|(d.<|(WYd}?~?Sd}?~?wwztdX||	|||||||t |||f
|%|&dY�|'��\}@}9}AWn&t7�y}?zi|(dJ<t�8t9�:��t|(dW|?���WYd}?~?Sd}?~?wwd}B|�r�tjj
j;||dZ�}Btd[|��rBz	td\||B�Wn!t7�yA}?zt|(d]|�d^|B�d_|?���WYd}?~?Sd}?~?wwz+td`|B|@|(||9||||t ||
|	||||||-f| |!|"|#|$|||)|*|+|,|&da�|'��}(WnQt7�y�}?zDi|(dJ<t�8t9�:��tjj
�<|B�|�s�|@�s�|�r�t)j*�+|�j,dbk�r�tdc|t �}@|@�r�tjj
�<|@�t|(dd|?���WYd}?~?Sd}?~?ww|(dJ�r�id|dd�}(i}Cdet=v�r�t=de|Cde<t>||Bfi|C��}Dt|Dt'��r�|(�?|D�tjj
�<|B�|(S|B}@nid|dd�}(|A�r|du�rt|(|A�Sz�z[td`||@|(||9||||t ||
|	||||||-f| |!|"|#|$|||)|*|+|,|&da�|'��WW|B�rBtjj
�<|B�|�sg|@�s\|�r\t)j*�+|�j,dbk�r\tdc|t �}@|@�rhtjj
�<|@�SSSt7�y�}?zJi|(dJ<t�8t9�:��t|(dW|?���WYd}?~?W|B�r�tjj
�<|B�|�s�|@�s�|�r�t)j*�+|�j,dbk�r�tdc|t �}@|@�r�tjj
�<|@�SSSd}?~?ww|B�r�tjj
�<|B�|�s�|@�s�|�r�t)j*�+|�j,dbk�r�tdc|t �}@|@�r�tjj
�<|@�www)falZ
    Manage a given file, this function allows for a file to be downloaded from
    the salt master and potentially run through a templating system.

    name
        The location of the file to manage, as an absolute path.

    source
        The source file to download to the minion, this source file can be
        hosted on either the salt master server (``salt://``), the salt minion
        local file system (``/``), or on an HTTP or FTP server (``http(s)://``,
        ``ftp://``).

        Both HTTPS and HTTP are supported as well as downloading directly
        from Amazon S3 compatible URLs with both pre-configured and automatic
        IAM credentials. (see s3.get state documentation)
        File retrieval from Openstack Swift object storage is supported via
        swift://container/object_path URLs, see swift.get documentation.
        For files hosted on the salt file server, if the file is located on
        the master in the directory named spam, and is called eggs, the source
        string is salt://spam/eggs. If source is left blank or None
        (use ~ in YAML), the file will be created as an empty file and
        the content will not be managed. This is also the case when a file
        already exists and the source is undefined; the contents of the file
        will not be changed or managed. If source is left blank or None, please
        also set replaced to False to make your intention explicit.


        If the file is hosted on a HTTP or FTP server then the source_hash
        argument is also required.

        A list of sources can also be passed in to provide a default source and
        a set of fallbacks. The first source in the list that is found to exist
        will be used and subsequent entries in the list will be ignored. Source
        list functionality only supports local files and remote files hosted on
        the salt master server or retrievable via HTTP, HTTPS, or FTP.

        .. code-block:: yaml

            file_override_example:
              file.managed:
                - source:
                  - salt://file_that_does_not_exist
                  - salt://file_that_exists

    source_hash
        This can be one of the following:
            1. a source hash string
            2. the URI of a file that contains source hash strings

        The function accepts the first encountered long unbroken alphanumeric
        string of correct length as a valid hash, in order from most secure to
        least secure:

        .. code-block:: text

            Type    Length
            ======  ======
            sha512     128
            sha384      96
            sha256      64
            sha224      56
            sha1        40
            md5         32

        **Using a Source Hash File**
            The file can contain several checksums for several files. Each line
            must contain both the file name and the hash.  If no file name is
            matched, the first hash encountered will be used, otherwise the most
            secure hash with the correct source file name will be used.

            When using a source hash file the source_hash argument needs to be a
            url, the standard download urls are supported, ftp, http, salt etc:

            Example:

            .. code-block:: yaml

                tomdroid-src-0.7.3.tar.gz:
                  file.managed:
                    - name: /tmp/tomdroid-src-0.7.3.tar.gz
                    - source: https://launchpad.net/tomdroid/beta/0.7.3/+download/tomdroid-src-0.7.3.tar.gz
                    - source_hash: https://launchpad.net/tomdroid/beta/0.7.3/+download/tomdroid-src-0.7.3.hash

            The following lines are all supported formats:

            .. code-block:: text

                /etc/rc.conf ef6e82e4006dee563d98ada2a2a80a27
                sha254c8525aee419eb649f0233be91c151178b30f0dff8ebbdcc8de71b1d5c8bcc06a  /etc/resolv.conf
                ead48423703509d37c4a90e6a0d53e143b6fc268

            Debian file type ``*.dsc`` files are also supported.

        **Inserting the Source Hash in the SLS Data**

        The source_hash can be specified as a simple checksum, like so:

        .. code-block:: yaml

            tomdroid-src-0.7.3.tar.gz:
              file.managed:
                - name: /tmp/tomdroid-src-0.7.3.tar.gz
                - source: https://launchpad.net/tomdroid/beta/0.7.3/+download/tomdroid-src-0.7.3.tar.gz
                - source_hash: 79eef25f9b0b2c642c62b7f737d4f53f

        .. note::
            Releases prior to 2016.11.0 must also include the hash type, like
            in the below example:

            .. code-block:: yaml

                tomdroid-src-0.7.3.tar.gz:
                  file.managed:
                    - name: /tmp/tomdroid-src-0.7.3.tar.gz
                    - source: https://launchpad.net/tomdroid/beta/0.7.3/+download/tomdroid-src-0.7.3.tar.gz
                    - source_hash: md5=79eef25f9b0b2c642c62b7f737d4f53f

            source_hash is ignored if the file hosted is not on a HTTP, HTTPS or FTP server.

        Known issues:
            If the remote server URL has the hash file as an apparent
            sub-directory of the source file, the module will discover that it
            has already cached a directory where a file should be cached. For
            example:

            .. code-block:: yaml

                tomdroid-src-0.7.3.tar.gz:
                  file.managed:
                    - name: /tmp/tomdroid-src-0.7.3.tar.gz
                    - source: https://launchpad.net/tomdroid/beta/0.7.3/+download/tomdroid-src-0.7.3.tar.gz
                    - source_hash: https://launchpad.net/tomdroid/beta/0.7.3/+download/tomdroid-src-0.7.3.tar.gz/+md5

    source_hash_name
        When ``source_hash`` refers to a hash file, Salt will try to find the
        correct hash by matching the filename/URI associated with that hash. By
        default, Salt will look for the filename being managed. When managing a
        file at path ``/tmp/foo.txt``, then the following line in a hash file
        would match:

        .. code-block:: text

            acbd18db4cc2f85cedef654fccc4a4d8    foo.txt

        However, sometimes a hash file will include multiple similar paths:

        .. code-block:: text

            37b51d194a7513e45b56f6524f2d51f2    ./dir1/foo.txt
            acbd18db4cc2f85cedef654fccc4a4d8    ./dir2/foo.txt
            73feffa4b7f6bb68e44cf984c85f6e88    ./dir3/foo.txt

        In cases like this, Salt may match the incorrect hash. This argument
        can be used to tell Salt which filename to match, to ensure that the
        correct hash is identified. For example:

        .. code-block:: yaml

            /tmp/foo.txt:
              file.managed:
                - source: https://mydomain.tld/dir2/foo.txt
                - source_hash: https://mydomain.tld/hashes
                - source_hash_name: ./dir2/foo.txt

        .. note::
            This argument must contain the full filename entry from the
            checksum file, as this argument is meant to disambiguate matches
            for multiple files that have the same basename. So, in the
            example above, simply using ``foo.txt`` would not match.

        .. versionadded:: 2016.3.5

    keep_source
        Set to ``False`` to discard the cached copy of the source file once the
        state completes. This can be useful for larger files to keep them from
        taking up space in minion cache. However, keep in mind that discarding
        the source file might result in the state needing to re-download the
        source file if the state is run again. If the source is not a local or
        ``salt://`` one, the source hash is known, ``skip_verify`` is not true
        and the managed file exists with the correct hash and is not templated,
        this is not the case (i.e. remote downloads are avoided if the local hash
        matches the expected one).

        .. versionadded:: 2017.7.3

    user
        The user to own the file, this defaults to the user salt is running as
        on the minion

    group
        The group ownership set for the file, this defaults to the group salt
        is running as on the minion. On Windows, this is ignored

    mode
        The permissions to set on this file, e.g. ``644``, ``0775``, or
        ``4664``.

        The default mode for new files and directories corresponds to the
        umask of the salt process. The mode of existing files and directories
        will only be changed if ``mode`` is specified.

        .. note::
            This option is **not** supported on Windows.

        .. versionchanged:: 2016.11.0
            This option can be set to ``keep``, and Salt will keep the mode
            from the Salt fileserver. This is only supported when the
            ``source`` URL begins with ``salt://``, or for files local to the
            minion. Because the ``source`` option cannot be used with any of
            the ``contents`` options, setting the ``mode`` to ``keep`` is also
            incompatible with the ``contents`` options.

        .. note:: keep does not work with salt-ssh.

            As a consequence of how the files are transferred to the minion, and
            the inability to connect back to the master with salt-ssh, salt is
            unable to stat the file as it exists on the fileserver and thus
            cannot mirror the mode on the salt-ssh minion

    attrs
        The attributes to have on this file, e.g. ``a``, ``i``. The attributes
        can be any or a combination of the following characters:
        ``aAcCdDeijPsStTu``.

        .. note::
            This option is **not** supported on Windows.

        .. versionadded:: 2018.3.0

    template
        If this setting is applied, the named templating engine will be used to
        render the downloaded file. The following templates are supported:

        - :mod:`cheetah<salt.renderers.cheetah>`
        - :mod:`genshi<salt.renderers.genshi>`
        - :mod:`jinja<salt.renderers.jinja>`
        - :mod:`mako<salt.renderers.mako>`
        - :mod:`py<salt.renderers.py>`
        - :mod:`wempy<salt.renderers.wempy>`

    makedirs
        If set to ``True``, then the parent directories will be created to
        facilitate the creation of the named file. If ``False``, and the parent
        directory of the destination file doesn't exist, the state will fail.

    dir_mode
        If directories are to be created, passing this option specifies the
        permissions for those directories. If this is not set, directories
        will be assigned permissions by adding the execute bit to the mode of
        the files.

        The default mode for new files and directories corresponds umask of salt
        process. For existing files and directories it's not enforced.

    replace
        If set to ``False`` and the file already exists, the file will not be
        modified even if changes would otherwise be made. Permissions and
        ownership will still be enforced, however.

    context
        Overrides default context variables passed to the template.

    defaults
        Default context passed to the template.

    backup
        Overrides the default backup mode for this specific file. See
        :ref:`backup_mode documentation <file-state-backups>` for more details.

    show_changes
        Output a unified diff of the old file and the new file. If ``False``
        return a boolean if any changes were made.

    create
        If set to ``False``, then the file will only be managed if the file
        already exists on the system.

    contents
        Specify the contents of the file. Cannot be used in combination with
        ``source``. Ignores hashes and does not use a templating engine.

        This value can be either a single string, a multiline YAML string or a
        list of strings.  If a list of strings, then the strings will be joined
        together with newlines in the resulting file. For example, the below
        two example states would result in identical file contents:

        .. code-block:: yaml

            /path/to/file1:
              file.managed:
                - contents:
                  - This is line 1
                  - This is line 2

            /path/to/file2:
              file.managed:
                - contents: |
                    This is line 1
                    This is line 2


    contents_pillar
        .. versionadded:: 0.17.0
        .. versionchanged:: 2016.11.0
            contents_pillar can also be a list, and the pillars will be
            concatenated together to form one file.


        Operates like ``contents``, but draws from a value stored in pillar,
        using the pillar path syntax used in :mod:`pillar.get
        <salt.modules.pillar.get>`. This is useful when the pillar value
        contains newlines, as referencing a pillar variable using a jinja/mako
        template can result in YAML formatting issues due to the newlines
        causing indentation mismatches.

        For example, the following could be used to deploy an SSH private key:

        .. code-block:: yaml

            /home/deployer/.ssh/id_rsa:
              file.managed:
                - user: deployer
                - group: deployer
                - mode: 600
                - attrs: a
                - contents_pillar: userdata:deployer:id_rsa

        This would populate ``/home/deployer/.ssh/id_rsa`` with the contents of
        ``pillar['userdata']['deployer']['id_rsa']``. An example of this pillar
        setup would be like so:

        .. code-block:: yaml

            userdata:
              deployer:
                id_rsa: |
                    -----BEGIN RSA PRIVATE KEY-----
                    MIIEowIBAAKCAQEAoQiwO3JhBquPAalQF9qP1lLZNXVjYMIswrMe2HcWUVBgh+vY
                    U7sCwx/dH6+VvNwmCoqmNnP+8gTPKGl1vgAObJAnMT623dMXjVKwnEagZPRJIxDy
                    B/HaAre9euNiY3LvIzBTWRSeMfT+rWvIKVBpvwlgGrfgz70m0pqxu+UyFbAGLin+
                    GpxzZAMaFpZw4sSbIlRuissXZj/sHpQb8p9M5IeO4Z3rjkCP1cxI
                    -----END RSA PRIVATE KEY-----

        .. note::
            The private key above is shortened to keep the example brief, but
            shows how to do multiline string in YAML. The key is followed by a
            pipe character, and the multiline string is indented two more
            spaces.

            To avoid the hassle of creating an indented multiline YAML string,
            the :mod:`file_tree external pillar <salt.pillar.file_tree>` can
            be used instead. However, this will not work for binary files in
            Salt releases before 2015.8.4.

    contents_grains
        .. versionadded:: 2014.7.0

        Operates like ``contents``, but draws from a value stored in grains,
        using the grains path syntax used in :mod:`grains.get
        <salt.modules.grains.get>`. This functionality works similarly to
        ``contents_pillar``, but with grains.

        For example, the following could be used to deploy a "message of the day"
        file:

        .. code-block:: yaml

            write_motd:
              file.managed:
                - name: /etc/motd
                - contents_grains: motd

        This would populate ``/etc/motd`` file with the contents of the ``motd``
        grain. The ``motd`` grain is not a default grain, and would need to be
        set prior to running the state:

        .. code-block:: bash

            salt '*' grains.set motd 'Welcome! This system is managed by Salt.'

    contents_newline
        .. versionadded:: 2014.7.0
        .. versionchanged:: 2015.8.4
            This option is now ignored if the contents being deployed contain
            binary data.

        If ``True``, files managed using ``contents``, ``contents_pillar``, or
        ``contents_grains`` will have a newline added to the end of the file if
        one is not present. Setting this option to ``False`` will ensure the
        final line, or entry, does not contain a new line. If the last line, or
        entry in the file does contain a new line already, this option will not
        remove it.

    contents_delimiter
        .. versionadded:: 2015.8.4

        Can be used to specify an alternate delimiter for ``contents_pillar``
        or ``contents_grains``. This delimiter will be passed through to
        :py:func:`pillar.get <salt.modules.pillar.get>` or :py:func:`grains.get
        <salt.modules.grains.get>` when retrieving the contents.

    encoding
        If specified, then the specified encoding will be used. Otherwise, the
        file will be encoded using the system locale (usually UTF-8). See
        https://docs.python.org/3/library/codecs.html#standard-encodings for
        the list of available encodings.

        .. versionadded:: 2017.7.0

    encoding_errors
        Error encoding scheme. Default is ```'strict'```.
        See https://docs.python.org/2/library/codecs.html#codec-base-classes
        for the list of available schemes.

        .. versionadded:: 2017.7.0

    allow_empty
        .. versionadded:: 2015.8.4

        If set to ``False``, then the state will fail if the contents specified
        by ``contents_pillar`` or ``contents_grains`` are empty.

    follow_symlinks
        .. versionadded:: 2014.7.0

        If the desired path is a symlink follow it and make changes to the
        file to which the symlink points.

    check_cmd
        .. versionadded:: 2014.7.0

        The specified command will be run with an appended argument of a
        *temporary* file containing the new managed contents.  If the command
        exits with a zero status the new managed contents will be written to
        the managed destination. If the command exits with a nonzero exit
        code, the state will fail and no changes will be made to the file.

        For example, the following could be used to verify sudoers before making
        changes:

        .. code-block:: yaml

            /etc/sudoers:
              file.managed:
                - user: root
                - group: root
                - mode: 0440
                - attrs: i
                - source: salt://sudoers/files/sudoers.jinja
                - template: jinja
                - check_cmd: /usr/sbin/visudo -c -f

        **NOTE**: This ``check_cmd`` functions differently than the requisite
        ``check_cmd``.

    tmp_dir
        Directory for temp file created by ``check_cmd``. Useful for checkers
        dependent on config file location (e.g. daemons restricted to their
        own config directories by an apparmor profile).

        .. code-block:: yaml

            /etc/dhcp/dhcpd.conf:
              file.managed:
                - user: root
                - group: root
                - mode: 0755
                - tmp_dir: '/etc/dhcp'
                - contents: "# Managed by Salt"
                - check_cmd: dhcpd -t -cf

    tmp_ext
        Suffix for temp file created by ``check_cmd``. Useful for checkers
        dependent on config file extension (e.g. the init-checkconf upstart
        config checker).

        .. code-block:: yaml

            /etc/init/test.conf:
              file.managed:
                - user: root
                - group: root
                - mode: 0440
                - tmp_ext: '.conf'
                - contents:
                  - 'description "Salt Minion"'
                  - 'start on started mountall'
                  - 'stop on shutdown'
                  - 'respawn'
                  - 'exec salt-minion'
                - check_cmd: init-checkconf -f

    skip_verify
        If ``True``, hash verification of remote file sources (``http://``,
        ``https://``, ``ftp://``) will be skipped, and the ``source_hash``
        argument will be ignored.

        .. versionadded:: 2016.3.0

    selinux
        Allows setting the selinux user, role, type, and range of a managed file

        .. code-block:: yaml

            /tmp/selinux.test
              file.managed:
                - user: root
                - selinux:
                    seuser: system_u
                    serole: object_r
                    setype: system_conf_t
                    serange: s0

        .. versionadded:: 3000

    win_owner
        The owner of the directory. If this is not passed, user will be used. If
        user is not passed, the account under which Salt is running will be
        used.

        .. versionadded:: 2017.7.0

    win_perms
        A dictionary containing permissions to grant and their propagation. For
        example: ``{'Administrators': {'perms': 'full_control'}}`` Can be a
        single basic perm or a list of advanced perms. ``perms`` must be
        specified. ``applies_to`` does not apply to file objects.

        .. versionadded:: 2017.7.0

    win_deny_perms
        A dictionary containing permissions to deny and their propagation. For
        example: ``{'Administrators': {'perms': 'full_control'}}`` Can be a
        single basic perm or a list of advanced perms. ``perms`` must be
        specified. ``applies_to`` does not apply to file objects.

        .. versionadded:: 2017.7.0

    win_inheritance
        True to inherit permissions from the parent directory, False not to
        inherit permission.

        .. versionadded:: 2017.7.0

    win_perms_reset
        If ``True`` the existing DACL will be cleared and replaced with the
        settings defined in this function. If ``False``, new entries will be
        appended to the existing DACL. Default is ``False``.

        .. versionadded:: 2018.3.0

    Here's an example using the above ``win_*`` parameters:

    .. code-block:: yaml

        create_config_file:
          file.managed:
            - name: C:\config\settings.cfg
            - source: salt://settings.cfg
            - win_owner: Administrators
            - win_perms:
                # Basic Permissions
                dev_ops:
                  perms: full_control
                # List of advanced permissions
                appuser:
                  perms:
                    - read_attributes
                    - read_ea
                    - create_folders
                    - read_permissions
                joe_snuffy:
                  perms: read
            - win_deny_perms:
                fred_snuffy:
                  perms: full_control
            - win_inheritance: False

    verify_ssl
        If ``False``, remote https file sources (``https://``) and source_hash
        will not attempt to validate the servers certificate. Default is True.

        .. versionadded:: 3002

    use_etag
        If ``True``, remote http/https file sources will attempt to use the
        ETag header to determine if the remote file needs to be downloaded.
        This provides a lightweight mechanism for promptly refreshing files
        changed on a web server without requiring a full hash comparison via
        the ``source_hash`` parameter.

        .. versionadded:: 3005
    �envr5T�r�r�rRr�z!Destination file name is requiredNz-The 'mode' option is not supported on Windowsz.The 'attrs' option is not supported on Windowsz/The 'selinux' option is only supported on Linux�seuser�serole�setype�serangereFcSsg|]}|dur|�qSrPr�r�xrrrr�r�zmanaged.<locals>.<listcomp>rz_'source' cannot be used in combination with 'contents', 'contents_pillar', or 'contents_grains'zhMode preservation cannot be used in combination with 'contents', 'contents_pillar', or 'contents_grains'r�zMOnly one of 'contents', 'contents_pillar', and 'contents_grains' is permitted�6source_hash is only used with 'http', 'https' or 'ftp'a	State for file: %s - Neither 'source' nor 'contents' nor 'contents_pillar' nor 'contents_grains' was defined, yet 'replace' was set to 'True'. As there is no source to replace the file with, 'replace' has been set to 'False' to avoid reading the file unnecessarily.r��warningsz]The 'file_mode' argument will be ignored.  Please use 'mode' instead to set file permissions.�
pillar.get)�	delimiterzPillar rPz
grains.getzGrain zcontents_pillar zcontents_grains z
'contents'zg{} value would result in empty contents. Set allow_empty to True to allow the managed file to be empty.r(z�Contents specified by contents/contents_pillar/contents_grains is not a string or list of strings, and is not binary data. SLS is likely malformed.r��
z�Contents specified by contents/contents_pillar/contents_grains appears to be binary data, and as will not be able to be treated as a Jinja template.zfile.apply_template_on_contents)r r!r&rkr�r�z)Error while applying template on contentsrKrer��+ is not present and is not set for creationr�r�rQz is a directoryz Context must be formed as a dictz!Defaults must be formed as a dict�file.source_list)r&r��file.get_source_sum)�
verify_ssl�
file.get_hash�	hash_typezFFailed checking existing file's hash against specified source_hash: %s)Zexc_info_on_loglevel�hsum�file.check_perms�rr.r�r�r�r�r�)r�r�r�r�r:ZlmodezHFile {} will be updated with permissions {} from its current state of {}� not updatedr�z1 exists with proper permissions. No changes made.�accumulator�file.check_managed_changes)r�r�r�r�r�r��Path not foundZnewfile�	The file z is set to be changedzL
Note: No changes made, actual changes may
be different due to other states.�diff�<show_changes=False>r�zUnable to manage file: zfile.get_managed)r��use_etag)�suffix�dir�file.file_existsz	file.copyzUnable to copy file rr��file.manage_file)r�r�r�r�r�r)�encoding_errorsr�r�r�r�r�r&�cp.is_cachedzUnable to check_cmd file: rD)@ryrrrZr�r&r'r�r�r�r�r�AttributeErrorr(r�rrr<r=�
setdefaultrxrrXr;�__NOT_FOUND�lineseprrr-r$�rstrip�UnicodeDecodeErrorrprrr3rCr�r��dictr��urllibro�urlparse�schemer	r,�error�logging�DEBUGr r2�tuple�strerrorrrqrY�	traceback�
format_exc�mkstemprZZ
__grains__�mod_run_check_cmdr\)ErRrr�source_hash_name�keep_sourcer>r?r��attrsr r[r�r!rMr&�backup�show_changesru�contentsZtmp_dirZtmp_ext�contents_pillarZcontents_grainsZcontents_newlineZcontents_delimiterr)r�Zallow_emptyr�Z	check_cmd�skip_verifyZselinuxr�r�r�r�r�r�r�r}r.r�r�r�r��	keep_modeZcontents_countZ
list_contentsZnextpZnextcZuse_contentsZnextgZcontents_idZvalidated_contents�part�line�u_check�
source_sumr�r@Z	ret_perms�
accum_datar�r��sfnZcomment_Ztmp_filename�check_cmd_opts�cretrrr�managed�s*




������
�	�
�
�
�
�
�
�

�������

�

�





���
������
�������

�

��
���
�



��
�
�	�
���
��������������� 
�"����



�� �'���	����	�����	r�)r>r?r�r�r��silentcCs�|st�St|t�std��zt|�}Wntyd}Ynw|dus*tt�|ks9td�d�dd�tD�����d|vrEd|vrEtd	��|S)
z�
    Converse *recurse* definition to a set of strings.

    Raises TypeError or ValueError when *recurse* has wrong structure.
    z-"recurse" must be formed as a list of stringsNz"Types for "recurse" limited to {}.�, css�|]	}d|�d�VqdS)�"Nr)rZrtyperrr�	<genexpr>�
s�z#_get_recurse_set.<locals>.<genexpr>r�r�zUMust not specify "recurse" options "ignore_files" and "ignore_dirs" at the same time.)rmrrXr��_RECURSE_TYPESr�rr)r�r�rrrr��
s(
����r�ccsn�tjj�|�D],\}}}|dur(|�tjj�|�tjj�}||kr(|dd�=t|�t|�t|�fVqdS)z
    Walk the directory tree under root up till reaching max_depth.
    With max_depth=None (default), do not limit depth.
    N)	r&r'rr�rHrrErrX)r�r�r�r�r(Z	rel_depthrrrr��
s�
�r�c1
Ks�	tj�|�}|iddd�}|st|d�S|ddkr$|dkr$|dd�}|dur/|r/t|d�St||d	�}tjj��rU|durI|rC|ntjj	�
�}|durSt�d
|�|}d|vra|sa|�
dg�}|se|}tjj�|�}tjj�|�}tjj��r�z	tjj�|�Wn#ty�}zt||�WYd}~Sd}~wwt||�}|r�t||�Stj�|�s�t|d|�d
��Stj�|�s�|s�tj�|�s�|�r�tj�|��r�|
du�r$tj�|
�r�|s�t|d|
�d��Stdr�d|
�d�|dd<ntd|
�td�r|�d|
��|dd<ddi|d|<|�d�|d<d|d<|St�||
�n{|�rtj�|��rFtd�r:d|dd<net�|�d|dd<nYtd|��rftd�rYd |dd<nFtd|�d!|dd<n9td�rrd"|dd<n-td|�d#|dd<n tj�|��r�t|d$|�d%��Stj�|��r�t|d$|�d&��Stjj���r�t||||||d'�\}}}nt||||�p�g||||	|
|||�\}}}|�r�|d�|�td�r�||d<||d<|Stjj���s�|d�s�||d<||d<|Stj�|��sytj�tj� |���sB|�r9zt!||||||||d(�Wn&t�y8}zt|d)|j"�d*��WYd}~Sd}~wwt|d+|�d,��Stjj���rVtd-||||||d.�n
td-||||d/�tj�|��sot|d0|���Sddi|d|<|S|�s�tjj���r�td1|||||||d2�}ntd1|||||d|�\}}g}|�s�|�r�t#t$||��}i}|D]} | d3| d4f|| d5<�q�d}!|�r�zt%|�}!Wnt&t'f�y�}zd6|d<|�|d<WYd}~nd}~ww|!�r"d7|!v�r|�s�t(|t)��rtd8|�}"t(|"t*��rd6|d<d9�+|�|d<nd6|d<d:|d<nd}d;|!v�rO|�s.t(|t)��rFtd<|�}#t(|#t*��rEd6|d<d=|��|d<nd6|d<d>|d<nd}d|!v�rZd}d}d?|!v�red@dAi|d<dB|!v}$dC|!v}%|D]�\}&}'}(|$�r�|(D]P})tj�,|&|)�}*z&tjj���r�td1|*||||||d2�}ntd1|*||||d|�\}}+W�qyt�y�}z|j-�.dD��s�|�/|j-�WYd}~�qyd}~ww|%�r |'D]P},tj�,|&|,�}*z&tjj���r�td1|*||||||d2�}ntd1|*||||d|�\}}+W�q�t�y}z|j-�.dD��s|�/|j-�WYd}~�q�d}~ww�qo|�rIt0||	|�}-t�1dE|-�t2|t#|-�|
�}.|.�rI|.|ddF<dG|��|d<|d�sg|�rZdH|�dI�|d<n
|d�rgdH|�dJ�|d<td�rudH|�dK�|d<n+|d�s�|d�r�d}/|d�r�|d}/dH|�dL�|d<|/�r�dM�,|d|/g�|d<|�r�d6|d<|ddN7<|D]}0|ddO|0��7<�q�|S)PaV-
    Ensure that a named directory is present and has the right perms

    name
        The location to create or manage a directory, as an absolute path

    user
        The user to own the directory; this defaults to the user salt is
        running as on the minion

    group
        The group ownership set for the directory; this defaults to the group
        salt is running as on the minion. On Windows, this is ignored

    recurse
        Enforce user/group ownership and mode of directory recursively. Accepts
        a list of strings representing what you would like to recurse.  If
        ``mode`` is defined, will recurse on both ``file_mode`` and ``dir_mode`` if
        they are defined.  If ``ignore_files`` or ``ignore_dirs`` is included, files or
        directories will be left unchanged respectively.
        directories will be left unchanged respectively. If ``silent`` is defined,
        individual file/directory change notifications will be suppressed.

        Example:

        .. code-block:: yaml

            /var/log/httpd:
              file.directory:
                - user: root
                - group: root
                - dir_mode: 755
                - file_mode: 644
                - recurse:
                  - user
                  - group
                  - mode

        Leave files or directories unchanged:

        .. code-block:: yaml

            /var/log/httpd:
              file.directory:
                - user: root
                - group: root
                - dir_mode: 755
                - file_mode: 644
                - recurse:
                  - user
                  - group
                  - mode
                  - ignore_dirs

        .. versionadded:: 2015.5.0

    max_depth
        Limit the recursion depth. The default is no limit=None.
        'max_depth' and 'clean' are mutually exclusive.

        .. versionadded:: 2016.11.0

    dir_mode / mode
        The permissions mode to set any directories created. Not supported on
        Windows.

        The default mode for new files and directories corresponds umask of salt
        process. For existing files and directories it's not enforced.

    file_mode
        The permissions mode to set any files created if 'mode' is run in
        'recurse'. This defaults to dir_mode. Not supported on Windows.

        The default mode for new files and directories corresponds umask of salt
        process. For existing files and directories it's not enforced.

    makedirs
        If the directory is located in a path without a parent directory, then
        the state will fail. If makedirs is set to True, then the parent
        directories will be created to facilitate the creation of the named
        file.

    clean
        Remove any files that are not referenced by a required ``file`` state.
        See examples below for more info. If this option is set then everything
        in this directory will be deleted unless it is required. 'clean' and
        'max_depth' are mutually exclusive.

    require
        Require other resources such as packages or files.

    exclude_pat
        When 'clean' is set to True, exclude this pattern from removal list
        and preserve in the destination.

    follow_symlinks
        If the desired path is a symlink (or ``recurse`` is defined and a
        symlink is encountered while recursing), follow it and check the
        permissions of the directory/file to which the symlink points.

        .. versionadded:: 2014.1.4

        .. versionchanged:: 3001.1
            If set to False symlinks permissions are ignored on Linux systems
            because it does not support permissions modification. Symlinks
            permissions are always 0o777 on Linux.

    force
        If the name of the directory exists and is not a directory and
        force is set to False, the state will fail. If force is set to
        True, the file in the way of the directory will be deleted to
        make room for the directory, unless backupname is set,
        then it will be renamed.

        .. versionadded:: 2014.7.0

    backupname
        If the name of the directory exists and is not a directory, it will be
        renamed to the backupname. If the backupname already
        exists and force is False, the state will fail. Otherwise, the
        backupname will be removed first.

        .. versionadded:: 2014.7.0

    allow_symlink
        If allow_symlink is True and the specified path is a symlink, it will be
        allowed to remain if it points to a directory. If allow_symlink is False
        then the state will fail, unless force is also set to True, in which case
        it will be removed or renamed, depending on the value of the backupname
        argument.

        .. versionadded:: 2014.7.0

    children_only
        If children_only is True the base of a path is excluded when performing
        a recursive operation. In case of /path/to/base, base will be ignored
        while all of /path/to/base/* are still operated on.

    win_owner
        The owner of the directory. If this is not passed, user will be used. If
        user is not passed, the account under which Salt is running will be
        used.

        .. versionadded:: 2017.7.0

    win_perms
        A dictionary containing permissions to grant and their propagation. For
        example: ``{'Administrators': {'perms': 'full_control', 'applies_to':
        'this_folder_only'}}`` Can be a single basic perm or a list of advanced
        perms. ``perms`` must be specified. ``applies_to`` is optional and
        defaults to ``this_folder_subfolder_files``.

        .. versionadded:: 2017.7.0

    win_deny_perms
        A dictionary containing permissions to deny and their propagation. For
        example: ``{'Administrators': {'perms': 'full_control', 'applies_to':
        'this_folder_only'}}`` Can be a single basic perm or a list of advanced
        perms.

        .. versionadded:: 2017.7.0

    win_inheritance
        True to inherit permissions from the parent directory, False not to
        inherit permission.

        .. versionadded:: 2017.7.0

    win_perms_reset
        If ``True`` the existing DACL will be cleared and replaced with the
        settings defined in this function. If ``False``, new entries will be
        appended to the existing DACL. Default is ``False``.

        .. versionadded:: 2018.3.0

    Here's an example using the above ``win_*`` parameters:

    .. code-block:: yaml

        create_config_dir:
          file.directory:
            - name: 'C:\config\'
            - win_owner: Administrators
            - win_perms:
                # Basic Permissions
                dev_ops:
                  perms: full_control
                # List of advanced permissions
                appuser:
                  perms:
                    - read_attributes
                    - read_ea
                    - create_folders
                    - read_permissions
                  applies_to: this_folder_only
                joe_snuffy:
                  perms: read
                  applies_to: this_folder_files
            - win_deny_perms:
                fred_snuffy:
                  perms: full_control
            - win_inheritance: False


    For ``clean: True`` there is no mechanism that allows all states and
    modules to enumerate the files that they manage, so for file.directory to
    know what files are managed by Salt, a ``file`` state targeting managed
    files is required. To use a contrived example, the following states will
    always have changes, despite the file named ``okay`` being created by a
    Salt state:

    .. code-block:: yaml

        silly_way_of_creating_a_file:
          cmd.run:
             - name: mkdir -p /tmp/dont/do/this && echo "seriously" > /tmp/dont/do/this/okay
             - unless: grep seriously /tmp/dont/do/this/okay

        will_always_clean:
          file.directory:
            - name: /tmp/dont/do/this
            - clean: True

    Because ``cmd.run`` has no way of communicating that it's creating a file,
    ``will_always_clean`` will remove the newly created file. Of course, every
    time the states run the same thing will happen - the
    ``silly_way_of_creating_a_file`` will crete the file and
    ``will_always_clean`` will always remove it. Over and over again, no matter
    how many times you run it.

    To make this example work correctly, we need to add a ``file`` state that
    targets the file, and a ``require`` between the file states.

    .. code-block:: yaml

        silly_way_of_creating_a_file:
          cmd.run:
             - name: mkdir -p /tmp/dont/do/this && echo "seriously" > /tmp/dont/do/this/okay
             - unless: grep seriously /tmp/dont/do/this/okay
          file.managed:
             - name: /tmp/dont/do/this/okay
             - create: False
             - replace: False
             - require_in:
               - file: will_always_clean

    Now there is a ``file`` state that ``clean`` can check, so running those
    states will work as expected. The file will be created with the specific
    contents, and ``clean`` will ignore the file because it is being managed by
    a salt ``file`` state. Note that if ``require_in`` was placed under
    ``cmd.run``, it would **not** work, because the requisite is for the cmd,
    not the file.

    .. code-block:: yaml

        silly_way_of_creating_a_file:
          cmd.run:
             - name: mkdir -p /tmp/dont/do/this && echo "seriously" > /tmp/dont/do/this/okay
             - unless: grep seriously /tmp/dont/do/this/okay
             # This part should be under file.managed
             - require_in:
               - file: will_always_clean
          file.managed:
             - name: /tmp/dont/do/this/okay
             - create: False
             - replace: False


    Any other state that creates a file as a result, for example ``pkgrepo``,
    must have the resulting files referenced in a file state in order for
    ``clean: True`` to ignore them.  Also note that the requisite
    (``require_in`` vs ``require``) works in both directions:

    .. code-block:: yaml

        clean_dir:
          file.directory:
            - name: /tmp/a/better/way
            - require:
              - file: a_better_way

        a_better_way:
          file.managed:
            - name: /tmp/a/better/way/truely
            - makedirs: True
            - contents: a much better way

    Works the same as this:

    .. code-block:: yaml

        clean_dir:
          file.directory:
            - name: /tmp/a/better/way
            - clean: True

        a_better_way:
          file.managed:
            - name: /tmp/a/better/way/truely
            - makedirs: True
            - contents: a much better way
            - require_in:
              - file: clean_dir

    A common mistake here is to forget the state name and id are both required for requisites:

    .. code-block:: yaml

        # Correct:
        /path/to/some/file:
          file.managed:
            - contents: Cool
            - require_in:
              - file: clean_dir

        # Incorrect
        /path/to/some/file:
          file.managed:
            - contents: Cool
            - require_in:
              # should be `- file: clean_dir`
              - clean_dir

        # Also incorrect
        /path/to/some/file:
          file.managed:
            - contents: Cool
            - require_in:
              # should be `- file: clean_dir`
              - file

    Tr5rLz#Must provide name to file.directory���rtNz'Cannot specify both max_depth and cleanrKrer�r�r�z$File exists where the backup target z
 should gor:zExisting file at backup path z would be removedr�rWr�z would be renamed to r�r�r�z5 would be backed up and replaced with a new directoryr�r�zFile would be forcibly replacedzFile was forcibly replacedrz"Symlink would be forcibly replacedzSymlink was forcibly replacedz$Directory would be forcibly replacedzDirectory was forcibly replacedzSpecified location z exists and is a filez exists and is a symlink)rRr�r�r�r�r�rfrgrhzNo directory to create z inz
file.mkdir)rr�r�r�r�r�rVzFailed to create directory r�r�r�r�rFr>r6z=Failed to enforce ownership for user {} (user does not exist)zQuser not specified, but configured as a target for recursive ownership managementr?r8z,Failed to enforce group ownership for group zRgroup not specified, but configured as a target for recursive ownership managementr��	recursionzChanges silencedr�r�r�z9List of kept files when use file.directory with clean: %sr�zFiles cleaned from directory rwz
/* updatedz updatedr�r�r�z)

The following errors were encountered:
z
- )3rrrZr�rr&r'r�r�rmZget_current_userr<r=r�r(r�r�Zget_sidr	rCr�r3r�rpr r;�renamerZr�r�r\r�rtrJrnrXr�r�r�r�r�intrrrr�rrxr�rYr�)1rRr>r?r�r�r�r�r[r�r�rcr�r
rrZ
allow_symlinkr�r�r�r�r�r�r}r.r�r�r]r^r_�perms�errorsr�r�r�r�rArBr�r�r�r�r(r�fullr��dir_rer�Zorig_commentr�rrrr��
sFf

���


��
��
�

�
��
�



�	
�

�
 ���	�
���
���
���

�
�����
�����



r�c1
sfd�
vr	�
�d�tj�tjj�����t�
�d��tjj	�
�r,�dur*t�d�����idid��
d�
vrAd�
d	<d
�
d<�
St
dd
���|	fD��rXtjj	�
�rXt�
d�Stjj����z���dk��rjd�Wntyvd�Ynwtjj����t���}|r�t�
|�Stj���s�t�
d��d��St|�}t|�D]\}}|�d�||<q�|D]}|�d�s�t�
d|�d��Sq�ztd|dt�\}}Wnty�} zd�
d	<d| ���
d<�
WYd} ~ Sd} ~ wwtjj�|�\}!}"|"dur�t}"td|"|!dd�}#|!|#v�rd�
d	<d�|!|"��
d<�
Stj����sZtj� ���r1t�
d��d��St!d�sZtjj	�
��rP|�rB|n�}td�||||d �n
td����d!��
fd"d#����
fd$d%����������	�
����f
d&d'�}$������fd(d)�}%t"�||||||�\}&}'}(})|(D]$\}*}+t#tj�$�|*�|+d|��|	d*�},|,�s��q��tj�$�|*�|,��q�|'D]}-|%|-��q�|&D]\}.}/|$|.|/|��qň�r|)�%t&�|��t'�t(|)�|�}0|0�rt!d�r��
d	�r�d�
d	<�d+|0�n|0�
d,d+<d-�$d.d/��
d�)�D���*��
d<�
d�sd0����
d<�
d,�s1�
d	�r1d1��d2��
d<�
S)3a\
    Recurse through a subdirectory on the master and copy said subdirectory
    over to the specified path.

    name
        The directory to set the recursion in

    source
        The source directory, this directory is located on the salt master file
        server and is specified with the salt:// protocol. If the directory is
        located on the master in the directory named spam, and is called eggs,
        the source string is salt://spam/eggs

    keep_source
        Set to ``False`` to discard the cached copy of the source file once the
        state completes. This can be useful for larger files to keep them from
        taking up space in minion cache. However, keep in mind that discarding
        the source file will result in the state needing to re-download the
        source file if the state is run again.

        .. versionadded:: 2017.7.3

    clean
        Make sure that only files that are set up by salt and required by this
        function are kept. If this option is set then everything in this
        directory will be deleted unless it is required.

    require
        Require other resources such as packages or files

    user
        The user to own the directory. This defaults to the user salt is
        running as on the minion

    group
        The group ownership set for the directory. This defaults to the group
        salt is running as on the minion. On Windows, this is ignored

    dir_mode
        The permissions mode to set on any directories created.

        The default mode for new files and directories corresponds umask of salt
        process. For existing files and directories it's not enforced.

        .. note::
            This option is **not** supported on Windows.

    file_mode
        The permissions mode to set on any files created.

        The default mode for new files and directories corresponds umask of salt
        process. For existing files and directories it's not enforced.

        .. note::
            This option is **not** supported on Windows.

        .. versionchanged:: 2016.11.0
            This option can be set to ``keep``, and Salt will keep the mode
            from the Salt fileserver. This is only supported when the
            ``source`` URL begins with ``salt://``, or for files local to the
            minion. Because the ``source`` option cannot be used with any of
            the ``contents`` options, setting the ``mode`` to ``keep`` is also
            incompatible with the ``contents`` options.

    sym_mode
        The permissions mode to set on any symlink created.

        The default mode for new files and directories corresponds umask of salt
        process. For existing files and directories it's not enforced.

        .. note::
            This option is **not** supported on Windows.

    template
        If this setting is applied, the named templating engine will be used to
        render the downloaded file. The following templates are supported:

        - :mod:`cheetah<salt.renderers.cheetah>`
        - :mod:`genshi<salt.renderers.genshi>`
        - :mod:`jinja<salt.renderers.jinja>`
        - :mod:`mako<salt.renderers.mako>`
        - :mod:`py<salt.renderers.py>`
        - :mod:`wempy<salt.renderers.wempy>`

        .. note::

            The template option is required when recursively applying templates.

    replace
        If set to ``False`` and the file already exists, the file will not be
        modified even if changes would otherwise be made. Permissions and
        ownership will still be enforced, however.

    context
        Overrides default context variables passed to the template.

    defaults
        Default context passed to the template.

    include_empty
        Set this to True if empty directories should also be created
        (default is False)

    backup
        Overrides the default backup mode for all replaced files. See
        :ref:`backup_mode documentation <file-state-backups>` for more details.

    include_pat
        When copying, include only this pattern, or list of patterns, from the
        source. Default is glob match; if prefixed with 'E@', then regexp match.
        Example:

        .. code-block:: text

          - include_pat: hello*       :: glob matches 'hello01', 'hello02'
                                         ... but not 'otherhello'
          - include_pat: E@hello      :: regexp matches 'otherhello',
                                         'hello01' ...

        .. versionchanged:: 3001

            List patterns are now supported

        .. code-block:: text

            - include_pat:
                - hello01
                - hello02

    exclude_pat
        Exclude this pattern, or list of patterns, from the source when copying.
        If both `include_pat` and `exclude_pat` are supplied, then it will apply
        conditions cumulatively. i.e. first select based on include_pat, and
        then within that result apply exclude_pat.

        Also, when 'clean=True', exclude this pattern from the removal
        list and preserve in the destination.
        Example:

        .. code-block:: text

          - exclude_pat: APPDATA*               :: glob matches APPDATA.01,
                                                   APPDATA.02,.. for exclusion
          - exclude_pat: E@(APPDATA)|(TEMPDATA) :: regexp matches APPDATA
                                                   or TEMPDATA for exclusion

        .. versionchanged:: 3001

            List patterns are now supported

        .. code-block:: text

            - exclude_pat:
                - APPDATA.01
                - APPDATA.02

    maxdepth
        When copying, only copy paths which are of depth `maxdepth` from the
        source path.
        Example:

        .. code-block:: text

          - maxdepth: 0      :: Only include files located in the source
                                directory
          - maxdepth: 1      :: Only include files located in the source
                                or immediate subdirectories

    keep_symlinks

        Determines how symbolic links (symlinks) are handled during the copying
        process. When set to ``True``, the copy operation will copy the symlink
        itself, rather than the file or directory it points to. When set to
        ``False``, the operation will follow the symlink and copy the target
        file or directory. If you want behavior similar to rsync, set this
        option to ``True``.

        However, if the ``fileserver_followsymlinks`` option is set to ``False``,
        the ``keep_symlinks`` setting will be ignored, and symlinks will not be
        copied at all.

    force_symlinks

        Controls the creation of symlinks when using ``keep_symlinks``. When set
        to ``True``, it forces the creation of symlinks by removing any existing
        files or directories that might be obstructing their creation. This
        removal is done recursively if a directory is blocking the symlink. This
        option is only used when ``keep_symlinks`` is passed and is ignored if
        ``fileserver_followsymlinks`` is set to ``False``.

    win_owner
        The owner of the symlink and directories if ``makedirs`` is True. If
        this is not passed, ``user`` will be used. If ``user`` is not passed,
        the account under which Salt is running will be used.

        .. versionadded:: 2017.7.7

    win_perms
        A dictionary containing permissions to grant

        .. versionadded:: 2017.7.7

    win_deny_perms
        A dictionary containing permissions to deny

        .. versionadded:: 2017.7.7

    win_inheritance
        True to inherit permissions from parent, otherwise False

        .. versionadded:: 2017.7.7

    r�rKNrMTrLr�Fr�zO'mode' is not allowed in 'file.recurse'. Please use 'file_mode' and 'dir_mode'.r�cSsg|]}|du�qSrPrr�rrrr��zrecurse.<locals>.<listcomp>z+mode management is not supported on Windowsrer�r�rt�salt://zInvalid source 'z' (must be a salt:// URI)r�r5zRecurse failed: rl)rk�prefixzHThe directory '{}' does not exist on the salt fileserver in saltenv '{}'z	The path z exists and is not a directoryr:zfile.makedirs_permsrI)rRr>r?r�cs6�d�|g�}t|t�r|�|�dS|�|�dS)Nr�)r�rrrx�extend)rr�r�)r.rr�add_comments
zrecurse.<locals>.add_commentcsf|ddus�ddur|d�d<|ddur#|dr#�||d�|dr1|d�d|<dSdS)Nr�FTr�r�r�rr�)rr.rr�	merge_ret	s�zrecurse.<locals>.merge_retcs��r?tj�|�r?tj�|�r?|r?�
iddd�}tdr.d|�d�|d<d|d<�	||�dStd	|�d
di|d<�	||�i}d
dg}�D]}||vrS�|||<qGt|f|����r_dn�d�d|���d�|��}�	||�dS)NTr5rLr:zReplacing directory z with a filer�r�r�r�z"Replaced directory with a new filer�r�r[re)rr�r>r?r�r�r r[rMr!r&r�)rrr�r�r r;r�)rrrMr�Zpass_kwargsZfaultsr�)
r�r�r!r&r�r?r�r�r}rrRr r>rr�manage_filesF 

��
��zrecurse.<locals>.manage_filecs�tj�|�dkr
dS�rGtj�|�rGtj�|�sG�iddd�}tdr6d|�d�|d<d|d	<�||�dStd
|�ddi|d
<�||�t|��g�ddddd�	}�||�dS)N�..Tr5rLr:z
Replacing z with a directoryr�r�r�r�zReplaced file with a directoryr�F)r>r?r�r�r�r[r�r�)rrror�r�r r;r�r)r�r�r?rrRr>rr�manage_directory;s0

�z!recurse.<locals>.manage_directory)r[r
r>r?r�r�r�r�css4�|]\}}d�|t|t�r|nd�|��VqdS)z
#### {} ####
{}r�N)rrrr)r�k�vrrrr�{s
��
�zrecurse.<locals>.<genexpr>zRecursively updated r�r�)+ryrrrZr&r'rrrsrr�r�r<r=rr�r(r�rr�rCr�r-�	enumerater�rr;rpr	rnrorr�r�r r�rsrr\r�r�rXrUrG)1rRrr�r�r�r>r?r�r�Zsym_moder r!rMr&r|r�rdrcrJr{Zforce_symlinksr�r�r�r�r}r�r%�idxr�Zprecheckrr�rgr~Zmaster_dirsrrZ	mng_filesZmng_dirsZmng_symlinksrerar`r�rtr�r�r�r)rr�r�r!r&r�r�r?r�r�r}rrRr.r r>rr��sr
����
�
��



����
�
��$&��	



��
r�csXtj�����iddd�}�st|d�Stj���st|d�Std��}tddd����fd	d
�}��fdd�}�r=|n|}�fd
d����}	��}
t�}t�}|D]8}
||
�\}}|r�|
|	|j|j	|j
|j|<|��d}|
|
|j||�
�|<|�|
�qS|�|
�qSdddddd�}�fdd����fdd��t�}|��D]*\}}d|kr�tjnt|�}d|kr�d}|�|
||d�O}q�|�|	|||�O}q�t||�}|jdd�tt|�dd�|tt|�dd�d�}|r�||d<td�rd�t|���|d<|�r
d |d!<|S|D]}
td"tj��|
���qd#�t|���|d<|S)$a;	
    Apply retention scheduling to backup storage directory.

    .. versionadded:: 2016.11.0
    .. versionchanged:: 3006.0

    :param name:
        The filesystem path to the directory containing backups to be managed.

    :param retain:
        Delete the backups, except for the ones we want to keep.
        The N below should be an integer but may also be the special value of ``all``,
        which keeps all files matching the criteria.
        All of the retain options default to None,
        which means to not keep files based on this criteria.

        :most_recent N:
            Keep the most recent N files.

        :first_of_hour N:
            For the last N hours from now, keep the first file after the hour.

        :first_of_day N:
            For the last N days from now, keep the first file after midnight.
            See also ``timezone``.

        :first_of_week N:
            For the last N weeks from now, keep the first file after Sunday midnight.

        :first_of_month N:
            For the last N months from now, keep the first file after the start of the month.

        :first_of_year N:
            For the last N years from now, keep the first file after the start of the year.

    :param strptime_format:
        A python strptime format string used to first match the filenames of backups
        and then parse the filename to determine the datetime of the file.
        https://docs.python.org/2/library/datetime.html#datetime.datetime.strptime
        Defaults to None, which considers all files in the directory to be backups eligible for deletion
        and uses ``os.path.getmtime()`` to determine the datetime.

    :param timezone:
        The timezone to use when determining midnight.
        This is only used when datetime is pulled from ``os.path.getmtime()``.
        Defaults to ``None`` which uses the timezone from the locale.

    Usage example:

    .. code-block:: yaml

        /var/backups/example_directory:
          file.retention_schedule:
            - retain:
                most_recent: 5
                first_of_hour: 4
                first_of_day: 7
                first_of_week: 6    # NotImplemented yet.
                first_of_month: 6
                first_of_year: all
            - strptime_format: example_name_%Y%m%dT%H%M%S.tar.bz2
            - timezone: None

    Tr5rLz,Must provide name to file.retention_schedulez3Name provided to file.retention must be a directoryzfile.readdiri�r�cs>zt�|��}tjj�|��}||fWStyYdSw)N�NN)r�strptimer&r'Z	dateutils�
total_secondsr�)r/�ts�ts_epoch)�beginning_of_unix_time�strptime_formatrr�get_file_time_from_strptime�s
�z7retention_schedule.<locals>.get_file_time_from_strptimecsJ|dks|dkr
dStdtj��|��}|r#|d}t�|��|fSdS)NrSrr
z
file.lstatr�)r;rrrrr�)r/r�r�)rR�timezonerr�get_file_time_from_mtime�sz4retention_schedule.<locals>.get_file_time_from_mtimecst��SrPrr)�
dict_makerrrr�sz&retention_schedule.<locals>.dict_makerr�r���)Z
first_of_yearZfirst_of_monthZfirst_of_dayZ
first_of_hourZmost_recentcs,t|t�rt|���d}�||�S|hS)Nr)rr��sorted�keys)�fwtZ
first_sub_key)�	get_firstrrrs
z%retention_schedule.<locals>.get_firstcsb|dkr�|�St�}t|��dd�D]}|t|�}|dkr"|S|�|||d|�O}q|S)NrT��reverser�)rmrrr)r�depth�nZ
result_setrZneeded)r�get_first_n_at_depthrrrs�z0retention_schedule.<locals>.get_first_n_at_depthr?Z
first_of_weekr)Zretained�deletedZignoredr�r:z,{} backups would have been removed from {}.
r�Nr�r�z!{} backups were removed from {}.
)rrrZr�r�r;rrm�year�month�day�hour�isocalendar�weekdayr[rU�sys�maxsizer�rX�sortrr rrr)rRZretainrrr.Z	all_filesrrZ
get_file_timeZfiles_by_ymdZfiles_by_y_week_dowZrelevant_filesZ
ignored_filesr/r
rZweek_of_yearZRETAIN_TO_DEPTHZretained_filesZretention_ruleZ
keep_countZfirst_of_week_depthZdeletable_filesr�r)rrrrrRrrr�retention_schedule�s�A�

	
�	 �

�
��
���r*c
Cstj�|�}|iddd�}|st|d�St||||
|dd�t|�\}}|s+t||�S|r1|��p2|}|dur<t|d�Sd	g}||vrO|durOt|d
|���S~td||||||||||	|
d�}|r�||d
d<tdrwd|d<d|d<|Sd|d<d|d<|Sd|d<d|d<|S)a! 
    Line-focused editing of a file.

    .. versionadded:: 2015.8.0

    .. note::

        ``file.line`` exists for historic reasons, and is not
        generally recommended. It has a lot of quirks.  You may find
        ``file.replace`` to be more suitable.

    ``file.line`` is most useful if you have single lines in a file,
    potentially a config file, that you would like to manage. It can
    remove, add, and replace lines.

    name
        Filesystem path to the file to be edited.

    content
        Content of the line. Allowed to be empty if mode=delete.

    match
        Match the target line for an action by
        a fragment of a string or regular expression.

        If neither ``before`` nor ``after`` are provided, and ``match``
        is also ``None``, match falls back to the ``content`` value.

    mode
        Defines how to edit a line. One of the following options is
        required:

        - ensure
            If line does not exist, it will be added. If ``before``
            and ``after`` are specified either zero lines, or lines
            that contain the ``content`` line are allowed to be in between
            ``before`` and ``after``. If there are lines, and none of
            them match then it will produce an error.
        - replace
            If line already exists, it will be replaced.
        - delete
            Delete the line, if found.
        - insert
            Nearly identical to ``ensure``. If a line does not exist,
            it will be added.

            The differences are that multiple (and non-matching) lines are
            alloweed between ``before`` and ``after``, if they are
            specified. The line will always be inserted right before
            ``before``. ``insert`` also allows the use of ``location`` to
            specify that the line should be added at the beginning or end of
            the file.

        .. note::

            If ``mode=insert`` is used, at least one of the following
            options must also be defined: ``location``, ``before``, or
            ``after``. If ``location`` is used, it takes precedence
            over the other two options.

    location
        In ``mode=insert`` only, whether to place the ``content`` at the
        beginning or end of a the file. If ``location`` is provided,
        ``before`` and ``after`` are ignored. Valid locations:

        - start
            Place the content at the beginning of the file.
        - end
            Place the content at the end of the file.

    before
        Regular expression or an exact case-sensitive fragment of the string.
        Will be tried as **both** a regex **and** a part of the line.  Must
        match **exactly** one line in the file.  This value is only used in
        ``ensure`` and ``insert`` modes. The ``content`` will be inserted just
        before this line, matching its ``indent`` unless ``indent=False``.

    after
        Regular expression or an exact case-sensitive fragment of the string.
        Will be tried as **both** a regex **and** a part of the line.  Must
        match **exactly** one line in the file.  This value is only used in
        ``ensure`` and ``insert`` modes. The ``content`` will be inserted
        directly after this line, unless ``before`` is also provided. If
        ``before`` is not matched, indentation will match this line, unless
        ``indent=False``.

    show_changes
        Output a unified diff of the old file and the new file.
        If ``False`` return a boolean if any changes were made.
        Default is ``True``

        .. note::
            Using this option will store two copies of the file in-memory
            (the original version and the edited version) in order to generate the diff.

    backup
        Create a backup of the original file with the extension:
        "Year-Month-Day-Hour-Minutes-Seconds".

    quiet
        Do not raise any exceptions. E.g. ignore the fact that the file that is
        tried to be edited does not exist and nothing really happened.

    indent
        Keep indentation with the previous line. This option is not considered when
        the ``delete`` mode is specified. Default is ``True``.

    create
        Create an empty file if doesn't exist.

        .. versionadded:: 2016.11.0

    user
        The user to own the file, this defaults to the user salt is running as
        on the minion.

        .. versionadded:: 2016.11.0

    group
        The group ownership set for the file, this defaults to the group salt
        is running as on the minion On Windows, this is ignored.

        .. versionadded:: 2016.11.0

    file_mode
        The permissions to set on this file, aka 644, 0775, 4664. Not supported
        on Windows.

        .. versionadded:: 2016.11.0

    If an equal sign (``=``) appears in an argument to a Salt command, it is
    interpreted as a keyword argument in the format of ``key=val``. That
    processing can be bypassed in order to pass an equal sign through to the
    remote shell command by manually specifying the kwarg:

    .. code-block:: yaml

       update_config:
         file.line:
           - name: /etc/myconfig.conf
           - mode: ensure
           - content: my key = my value
           - before: somekey.*?


    **Examples:**

    Here's a simple config file.

    .. code-block:: ini

        [some_config]
        # Some config file
        # this line will go away

        here=False
        away=True
        goodybe=away

    And an sls file:

    .. code-block:: yaml

        remove_lines:
          file.line:
            - name: /some/file.conf
            - mode: delete
            - match: away

    This will produce:

    .. code-block:: ini

        [some_config]
        # Some config file

        here=False
        away=True
        goodbye=away

    If that state is executed 2 more times, this will be the result:

    .. code-block:: ini

        [some_config]
        # Some config file

        here=False

    Given that original file with this state:

    .. code-block:: yaml

        replace_things:
          file.line:
            - name: /some/file.conf
            - mode: replace
            - match: away
            - content: here

    Three passes will this state will result in this file:

    .. code-block:: ini

        [some_config]
        # Some config file
        here

        here=False
        here
        here

    Each pass replacing the first line found.

    Given this file:

    .. code-block:: text

        insert after me
        something
        insert before me

    The following state:

    .. code-block:: yaml

        insert_a_line:
          file.line:
            - name: /some/file.txt
            - mode: insert
            - after: insert after me
            - before: insert before me
            - content: thrice

    If this state is executed 3 times, the result will be:

    .. code-block:: text

        insert after me
        something
        thrice
        thrice
        thrice
        insert before me

    If the mode is ensure instead, it will fail each time. To succeed, we need
    to remove the incorrect line between before and after:

    .. code-block:: text

        insert after me
        insert before me

    With an ensure mode, this will insert ``thrice`` the first time and
    make no changes for subsequent calls. For something simple this is
    fine, but if you have instead blocks like this:

    .. code-block:: text

        Begin SomeBlock
            foo = bar
        End

        Begin AnotherBlock
            another = value
        End

    And given this state:

    .. code-block:: yaml

        ensure_someblock:
          file.line:
            - name: /some/file.conf
            - mode: ensure
            - after: Begin SomeBlock
            - content: this = should be my content
            - before: End

    This will fail because there are multiple ``End`` lines. Without that
    problem, it still would fail because there is a non-matching line,
    ``foo = bar``. Ensure **only** allows either zero, or the matching
    line present to be present in between ``before`` and ``after``.
    Tr5rLzMust provide name to file.lineF)rur>r?r�rMNz.Mode was not defined. How to process the file?�deletez%Content can only be empty if mode is z	file.line)	r�r��location�before�afterr�r��quiet�indentr�r�r:r��Changes would be mader��Changes were made�No changes needed to be made)	rrrZr�r�r�rr;r )rR�contentr�r�r,r-r.r�r�r/r0rur>r?r�r.�	check_res�	check_msgZmodeswithemptycontentr�rrrr�SsZ/


��
��r��r��.bakc
Cs�tj�|�}|iddd�}
|st|
d�St|�\}}|s-|r(d|vr(d|
d<|
St|
|�Std||||||||||	td	|
||d
�}|rc||
dd<td	rYd
|
d<d|
d<|
Sd|
d<d|
d<|
Sd|
d<d|
d<|
S)a�
    Maintain an edit in a file.

    .. versionadded:: 0.17.0

    name
        Filesystem path to the file to be edited. If a symlink is specified, it
        will be resolved to its target.

    pattern
        A regular expression, to be matched using Python's
        :py:func:`re.search`.

        .. note::

            If you need to match a literal string that contains regex special
            characters, you may want to use salt's custom Jinja filter,
            ``regex_escape``.

            .. code-block:: jinja

                {{ 'http://example.com?foo=bar%20baz' | regex_escape }}

    repl
        The replacement text

    count
        Maximum number of pattern occurrences to be replaced.  Defaults to 0.
        If count is a positive integer n, no more than n occurrences will be
        replaced, otherwise all occurrences will be replaced.

    flags
        A list of flags defined in the ``re`` module documentation from the
        Python standard library. Each list item should be a string that will
        correlate to the human-friendly flag name. E.g., ``['IGNORECASE',
        'MULTILINE']``.  Optionally, ``flags`` may be an int, with a value
        corresponding to the XOR (``|``) of all the desired flags. Defaults to
        ``8`` (which equates to ``['MULTILINE']``).

        .. note::

            ``file.replace`` reads the entire file as a string to support
            multiline regex patterns. Therefore, when using anchors such as
            ``^`` or ``$`` in the pattern, those anchors may be relative to
            the line OR relative to the file. The default for ``file.replace``
            is to treat anchors as relative to the line, which is implemented
            by setting the default value of ``flags`` to ``['MULTILINE']``.
            When overriding the default value for ``flags``, if
            ``'MULTILINE'`` is not present then anchors will be relative to
            the file. If the desired behavior is for anchors to be relative to
            the line, then simply add ``'MULTILINE'`` to the list of flags.

    bufsize
        How much of the file to buffer into memory at once. The default value
        ``1`` processes one line at a time. The special value ``file`` may be
        specified which will read the entire file into memory before
        processing.

    append_if_not_found
        If set to ``True``, and pattern is not found, then the content will be
        appended to the file.

        .. versionadded:: 2014.7.0

    prepend_if_not_found
        If set to ``True`` and pattern is not found, then the content will be
        prepended to the file.

        .. versionadded:: 2014.7.0

    not_found_content
        Content to use for append/prepend if not found. If ``None`` (default),
        uses ``repl``. Useful when ``repl`` uses references to group in
        pattern.

        .. versionadded:: 2014.7.0

    backup
        The file extension to use for a backup of the file before editing. Set
        to ``False`` to skip making a backup.

    show_changes
        Output a unified diff of the old file and the new file. If ``False``
        return a boolean if any changes were made. Returns a boolean or a
        string.

        .. note:
            Using this option will store two copies of the file in memory (the
            original version and the edited version) in order to generate the
            diff. This may not normally be a concern, but could impact
            performance if used with large files.

    ignore_if_missing
        .. versionadded:: 2016.3.4

        Controls what to do if the file is missing. If set to ``False``, the
        state will display an error raised by the execution module. If set to
        ``True``, the state will simply report no changes.

    backslash_literal
        .. versionadded:: 2016.11.7

        Interpret backslashes as literal backslashes for the repl and not
        escape characters.  This will help when using append/prepend so that
        the backslashes are not interpreted for the repl on the second run of
        the state.

    For complex regex patterns, it can be useful to avoid the need for complex
    quoting and escape sequences by making use of YAML's multiline string
    syntax.

    .. code-block:: yaml

        complex_search_and_replace:
          file.replace:
            # <...snip...>
            - pattern: |
                CentOS \(2.6.32[^\\n]+\\n\s+root[^\\n]+\\n\)+

    .. note::

       When using YAML multiline string syntax in ``pattern:``, make sure to
       also use that syntax in the ``repl:`` part, or you might loose line
       feeds.

    When regex capture groups are used in ``pattern:``, their captured value is
    available for reuse in the ``repl:`` part as a backreference (ex. ``\1``).

    .. code-block:: yaml

        add_login_group_to_winbind_ssh_access_list:
          file.replace:
            - name: '/etc/security/pam_winbind.conf'
            - pattern: '^(require_membership_of = )(.*)$'
            - repl: '\1\2,append-new-group-to-line'

    .. note::

       The ``file.replace`` state uses Python's ``re`` module.
       For more advanced options, see https://docs.python.org/2/library/re.html
    Tr5rLz!Must provide name to file.replacezfile not foundr3r�zfile.replacer:)rH�flags�bufsize�append_if_not_found�prepend_if_not_found�not_found_contentr��dry_runr��ignore_if_missing�backslash_literalr�r�Nr�zChanges would have been mader2)rrrZr�r�r;r )rR�pattern�replrHr9r:r;r<r=r�r�r?r@r.r5r6r�rrrrM�sN

���rM�=c%	s`tj�|�}|iddd�}|st|d�S|dur.|dur.t|�tur't|d�St|�|i}nt|t�r5|sBd}|s;d}t|d|�Sg}ztj	j
�|d	��}|��}Wd�n1s\wYWnt
y|d
|��|d<|	rtdnd
|d<|YSwg}g}dtj}d}�fdd�|��D�}|D]�}|�|�}t|�t|�kr�dnd
}|r�|��}|��D]�\}}|r�|��n|}|�|��r�|r�|�|�n|}|�|�\}}}||kr�q�d
}|r�|��|kr�d}n||kr�d}|�r�|��}t|���} |
�r|��}| ��} || k�rdnd
}!d
}"|�r#||dk�rd}"n|!�s"d}!n	||dk�r,d}"|!�r2|"�rt|�d|���|dd�}||dk�rpt|j|||d��}t|t��sh|�d�|��t|�jtj��n|�d|���|d7}||dk�r�||d8<q�q�|�|�q�|��|�r�g}#|��D]%\}}||dk�r�|j|||d�}|#�d|���|�|�|d7}�q�|#�r�|#�ddtj�|#�dtj�|�|#�n?|�rd
}$|��D]5\}}||dk�r|j|||d�}|$�s|�ddtj�d}$|�dd|���|�d|�|d7}�q�|dk�rctd�rNdj||d�|d<|�rMd�|�|dd<|dd 7<|dd!�|�7<d|d<nd"|�d#�|d<|�rbd�|�|dd<nd|d<|Std�s�z$tj	j
�|d$��}|� |�|��Wd�n	1�s�wYWnt
�y�|�d%�|d<d
|d<|YSwd|d<|S)&a0
    Key/Value based editing of a file.

    .. versionadded:: 3001

    This function differs from ``file.replace`` in that it is able to search for
    keys, followed by a customizable separator, and replace the value with the
    given value. Should the value be the same as the one already in the file, no
    changes will be made.

    Either supply both ``key`` and ``value`` parameters, or supply a dictionary
    with key / value pairs. It is an error to supply both.

    name
        Name of the file to search/replace in.

    key
        Key to search for when ensuring a value. Use in combination with a
        ``value`` parameter.

    value
        Value to set for a given key. Use in combination with a ``key``
        parameter.

    key_values
        Dictionary of key / value pairs to search for and ensure values for.
        Used to specify multiple key / values at once.

    separator
        Separator which separates key from value.

    append_if_not_found
        Append the key/value to the end of the file if not found. Note that this
        takes precedence over ``prepend_if_not_found``.

    prepend_if_not_found
        Prepend the key/value to the beginning of the file if not found. Note
        that ``append_if_not_found`` takes precedence.

    show_changes
        Show a diff of the resulting removals and inserts.

    ignore_if_missing
        Return with success even if the file is not found (or not readable).

    count
        Number of occurrences to allow (and correct), default is 1. Set to -1 to
        replace all, or set to 0 to remove all lines with this key regardsless
        of its value.

    .. note::
        Any additional occurrences after ``count`` are removed.
        A count of -1 will only replace all occurrences that are currently
        uncommented already. Lines commented out will be left alone.

    uncomment
        Disregard and remove supplied leading characters when finding keys. When
        set to None, lines that are commented out are left for what they are.

    .. note::
        The argument to ``uncomment`` is not a prefix string. Rather; it is a
        set of characters, each of which are stripped.

    key_ignore_case
        Keys are matched case insensitively. When a value is changed the matched
        key is kept as-is.

    value_ignore_case
        Values are checked case insensitively, trying to set e.g. 'Yes' while
        the current value is 'yes', will not result in changes when
        ``value_ignore_case`` is set to True.

    An example of using ``file.keyvalue`` to ensure sshd does not allow
    for root to login with a password and at the same time setting the
    login-gracetime to 1 minute and disabling all forwarding:

    .. code-block:: yaml

        sshd_config_harden:
            file.keyvalue:
              - name: /etc/ssh/sshd_config
              - key_values:
                  permitrootlogin: 'without-password'
                  LoginGraceTime: '1m'
                  DisableForwarding: 'yes'
              - separator: ' '
              - uncomment: '# '
              - key_ignore_case: True
              - append_if_not_found: True

    The same example, except for only ensuring PermitRootLogin is set correctly.
    Thus being able to use the shorthand ``key`` and ``value`` parameters
    instead of ``key_values``.

    .. code-block:: yaml

        sshd_config_harden:
            file.keyvalue:
              - name: /etc/ssh/sshd_config
              - key: PermitRootLogin
              - value: without-password
              - separator: ' '
              - uncomment: '# '
              - key_ignore_case: True
              - append_if_not_found: True

    .. note::
        Notice how the key is not matched case-sensitively, this way it will
        correctly identify both 'PermitRootLogin' as well as 'permitrootlogin'.

    Nr5rLz"Must provide name to file.keyvaluez;file.keyvalue can not combine key_values with key and valuezis not a dictionaryzis emptyz8file.keyvalue key and value not supplied and key_values �rzunable to open r�TFr�z{key}{sep}{value}rcsi|]}|��qSrr)rr�rHrr�
<dictcomp>.szkeyvalue.<locals>.<dictcomp>z- )r�rE�valuez+ {} (from {} type){}z+ r�z- <EOF>z+ <EOF>z  <SOF>r:z)File {n} is set to be changed ({c} lines))r�cr�r�z
Predicted diff:

		z
		zChanged � lines�wz
 not writable)!rrrZr�r�r�rrr&r'r(r)�	readlinesr,r�rr�rrrUr�	partitionrGrxrr��__name__�close�insertr�r r�
writelines)%rRr�rGZ
key_values�	separatorr;r<Zsearch_onlyr�r?rH�	uncommentZkey_ignore_caseZvalue_ignore_caser.r�Z
file_contents�fdr�r4Ztmplr�Z
diff_countr�Z	test_lineZ
did_uncommentZtest_keyZworking_lineZline_keyZline_sepZ
line_valueZ
keys_matchZ
test_valueZvalues_matchZneeds_changingZtmpdiffZdid_diffrrEr�keyvalues.�
��
���

������
�
��


���


���rT�#-- start managed zone --�#-- end managed zone --c$s,tj�|�}|iddd�}|st|d�S|dur#t|�s#|r#t�d�|dur)g}|dur/g}t||||d�\}}}|sAt||�St|�\}}|sNt||�St	�\}}||vr�||�|�
|g����fdd	��D�}|std
d	��D�}|D]}�|}|D]}|
dkr�|}
q~|
d|7}
q~qv|r�t||||	d�}|d
s�|S|d}t|�D]
\} }!|
t
|!�7}
q�ztd||||
|||||
td||d�}"Wn!ty�}#zt�d�d|#�d�|d<|WYd}#~#Sd}#~#ww|"�r|"|dd<td�rd|d
<d|d<|Sd|d
<d|d<|Sd|d
<d|d<|S)a
    Maintain an edit in a file in a zone delimited by two line markers

    .. versionadded:: 2014.1.0
    .. versionchanged:: 2017.7.5,2018.3.1
        ``append_newline`` argument added. Additionally, to improve
        idempotence, if the string represented by ``marker_end`` is found in
        the middle of the line, the content preceding the marker will be
        removed when the block is replaced. This allows one to remove
        ``append_newline: False`` from the SLS and have the block properly
        replaced if the end of the content block is immediately followed by the
        ``marker_end`` (i.e. no newline before the marker).

    A block of content delimited by comments can help you manage several lines
    entries without worrying about old entries removal. This can help you
    maintaining an un-managed file containing manual edits.

    .. note::
        This function will store two copies of the file in-memory (the original
        version and the edited version) in order to detect changes and only
        edit the targeted file if necessary.

        Additionally, you can use :py:func:`file.accumulated
        <salt.states.file.accumulated>` and target this state. All accumulated
        data dictionaries' content will be added in the content block.

    name
        Filesystem path to the file to be edited

    marker_start
        The line content identifying a line as the start of the content block.
        Note that the whole line containing this marker will be considered, so
        whitespace or extra content before or after the marker is included in
        final output

    marker_end
        The line content identifying the end of the content block. As of
        versions 2017.7.5 and 2018.3.1, everything up to the text matching the
        marker will be replaced, so it's important to ensure that your marker
        includes the beginning of the text you wish to replace.

    content
        The content to be used between the two lines identified by
        ``marker_start`` and ``marker_end``

    source
        The source file to download to the minion, this source file can be
        hosted on either the salt master server, or on an HTTP or FTP server.
        Both HTTPS and HTTP are supported as well as downloading directly
        from Amazon S3 compatible URLs with both pre-configured and automatic
        IAM credentials. (see s3.get state documentation)
        File retrieval from Openstack Swift object storage is supported via
        swift://container/object_path URLs, see swift.get documentation.
        For files hosted on the salt file server, if the file is located on
        the master in the directory named spam, and is called eggs, the source
        string is salt://spam/eggs. If source is left blank or None
        (use ~ in YAML), the file will be created as an empty file and
        the content will not be managed. This is also the case when a file
        already exists and the source is undefined; the contents of the file
        will not be changed or managed.

        If the file is hosted on a HTTP or FTP server then the source_hash
        argument is also required.

        A list of sources can also be passed in to provide a default source and
        a set of fallbacks. The first source in the list that is found to exist
        will be used and subsequent entries in the list will be ignored.

        .. code-block:: yaml

            file_override_example:
              file.blockreplace:
                - name: /etc/example.conf
                - source:
                  - salt://file_that_does_not_exist
                  - salt://file_that_exists

    source_hash
        This can be one of the following:
            1. a source hash string
            2. the URI of a file that contains source hash strings

        The function accepts the first encountered long unbroken alphanumeric
        string of correct length as a valid hash, in order from most secure to
        least secure:

        .. code-block:: text

            Type    Length
            ======  ======
            sha512     128
            sha384      96
            sha256      64
            sha224      56
            sha1        40
            md5         32

        See the ``source_hash`` parameter description for :mod:`file.managed
        <salt.states.file.managed>` function for more details and examples.

    template
        Templating engine to be used to render the downloaded file. The
        following engines are supported:

        - :mod:`cheetah <salt.renderers.cheetah>`
        - :mod:`genshi <salt.renderers.genshi>`
        - :mod:`jinja <salt.renderers.jinja>`
        - :mod:`mako <salt.renderers.mako>`
        - :mod:`py <salt.renderers.py>`
        - :mod:`wempy <salt.renderers.wempy>`

    context
        Overrides default context variables passed to the template

    defaults
        Default context passed to the template

    append_if_not_found
        If markers are not found and this option is set to ``True``, the
        content block will be appended to the file.

    prepend_if_not_found
        If markers are not found and this option is set to ``True``, the
        content block will be prepended to the file.

    insert_before_match
        If markers are not found, this parameter can be set to a regex which will
        insert the block before the first found occurrence in the file.

        .. versionadded:: 3001

    insert_after_match
        If markers are not found, this parameter can be set to a regex which will
        insert the block after the first found occurrence in the file.

        .. versionadded:: 3001

    backup
        The file extension to use for a backup of the file if any edit is made.
        Set this to ``False`` to skip making a backup.

    show_changes
        Controls how changes are presented. If ``True``, the ``Changes``
        section of the state return will contain a unified diff of the changes
        made. If False, then it will contain a boolean (``True`` if any changes
        were made, otherwise ``False``).

    append_newline
        Controls whether or not a newline is appended to the content block. If
        the value of this argument is ``True`` then a newline will be added to
        the content block. If it is ``False``, then a newline will *not* be
        added to the content block. If it is unspecified, then a newline will
        only be added to the content block if it does not already end in a
        newline.

        .. versionadded:: 2017.7.5,2018.3.1

    Example of usage with an accumulator and with a variable:

    .. code-block:: jinja

        {% set myvar = 42 %}
        hosts-config-block-{{ myvar }}:
          file.blockreplace:
            - name: /etc/hosts
            - marker_start: "# START managed zone {{ myvar }} -DO-NOT-EDIT-"
            - marker_end: "# END managed zone {{ myvar }} --"
            - content: 'First line of content'
            - append_if_not_found: True
            - backup: '.bak'
            - show_changes: True

        hosts-config-block-{{ myvar }}-accumulated1:
          file.accumulated:
            - filename: /etc/hosts
            - name: my-accumulator-{{ myvar }}
            - text: "text 2"
            - require_in:
              - file: hosts-config-block-{{ myvar }}

        hosts-config-block-{{ myvar }}-accumulated2:
          file.accumulated:
            - filename: /etc/hosts
            - name: my-accumulator-{{ myvar }}
            - text: |
                 text 3
                 text 4
            - require_in:
              - file: hosts-config-block-{{ myvar }}

    will generate and maintain a block of content in ``/etc/hosts``:

    .. code-block:: text

        # START managed zone 42 -DO-NOT-EDIT-
        First line of content
        text 2
        text 3
        text 4
        # END managed zone 42 --
    Fr5rLz&Must provide name to file.blockreplaceNr�rcs(g|]}td�|vr|�vr|�qS)r�)�__low__�r�a�r��depsrrr�s"z blockreplace.<locals>.<listcomp>cS�g|]}|�qSrrrXrrrr��r��r%r r&r!r�rrzfile.blockreplacer:)	r4r;r<�insert_before_match�insert_after_matchr�r>r��append_newlinez Encountered error managing blockz"Encountered error managing block: z. See the log for details.r�r�r�r1Tr2r3)rrrZr�rr<r=rr�r2r�rrrr;r rq�	exception)$rRZmarker_startZ
marker_endrrr rrr&r!r4r;r<r�r�rar_r`r.�ok_r@�sl_r5r6r��
accum_depsZfiltered�accZacc_contentr��tmpret�textrzr,r�r�rrZr�blockreplace�s�^

�


���
�

���
��ri�#cCstj�|�}|iddd�}|st|d�St|�\}}|s!t||�St�dd|�}d|�d�|}	||}
td	||	d
d�sdtd	||
d
d�rPd|d
<d
|d<|S|r\d|d
<d
|d<|St||�d��Stdr|d|d|<d|�d�|d
<d|d<|St	j
j�|d��}|�
�}|�t�}|�d
�}Wd�n1s�wYtd|||d
|�t	j
j�|d��}|�
�}
|
�t�}
|
�d
�}
Wd�n1s�wYtd	||
d
d�|d<||
kr�td|�s�d|dd<n
d�t�||
��|dd<|d�rd|d
<|Sd|d
<|S)aq
    .. versionadded:: 0.9.5
    .. versionchanged:: 3005

    Comment out specified lines in a file.

    name
        The full path to the file to be edited
    regex
        A regular expression used to find the lines that are to be commented;
        this pattern will be wrapped in parenthesis and will move any
        preceding/trailing ``^`` or ``$`` characters outside the parenthesis
        (e.g., the pattern ``^foo$`` will be rewritten as ``^(foo)$``)
        Note that you _need_ the leading ^, otherwise each time you run
        highstate, another comment char will be inserted.
    char
        The character to be inserted at the beginning of a line in order to
        comment it out
    backup
        The file will be backed up before edit with this file extension

        .. warning::

            This backup will be overwritten each time ``sed`` / ``comment`` /
            ``uncomment`` is called. Meaning the backup will only be useful
            after the first invocation.

        Set to False/None to not keep a backup.
    ignore_missing
        Ignore a failure to find the regex in the file. This is useful for
        scenarios where a line must only be commented if it is found in the
        file.

        .. versionadded:: 3005

    Usage:

    .. code-block:: yaml

        /etc/fstab:
          file.comment:
            - regex: ^bind 127.0.0.1

    Fr5rLz!Must provide name to file.commentz^(\(\?[iLmsux]\))?\^?(.*?)\$?$z\2z^(?!\s*z)\s*�file.searchT�Z	multilinezPattern already commentedr�r�z0Pattern not found and ignore_missing set to True�: Pattern not foundr:�updatedr�r�� is set to be updatedNr%�file.comment_line�
files.is_text�Replace binary filer�zCommented lines successfullyz"Expected commented lines not found)rrrZr�r�r��subr;r r&r'r(r)r"rs�__salt_system_encoding__r$�	__utils__r�difflib�unified_diff)rRr��charr�Zignore_missingr.r5r6Zunanchor_regexZuncomment_regexZ
comment_regexr'�slines�nlinesrrrr�*s\-


�
�
�r�c
Cs�tj�|�}|iddd�}|st|d�St|�\}}|s!t||�Std|d�||�d��dd	�r2n!td|d
�|�d��dd	�rKd|d<d|d
<|St||�d��Stdrkd|d|<d|�d�|d<d|d
<|St	j
j�|d��}t	j
j
�|���}Wd�n1s�wYtd|||d|�t	j
j�|d��}t	j
j
�|���}	Wd�n1s�wYtd|d
�|�d��dd	�|d
<||	kr�td|�s�d|dd<n
d�t�||	��|dd<|d
r�d|d<|Sd|d<|S)aJ
    Uncomment specified commented lines in a file

    name
        The full path to the file to be edited
    regex
        A regular expression used to find the lines that are to be uncommented.
        This regex should not include the comment character. A leading ``^``
        character will be stripped for convenience (for easily switching
        between comment() and uncomment()).  The regex will be searched for
        from the beginning of the line, ignoring leading spaces (we prepend
        '^[ \t]*')
    char
        The character to remove in order to uncomment a line
    backup
        The file will be backed up before edit with this file extension;

        .. warning::

            This backup will be overwritten each time ``sed`` / ``comment`` /
            ``uncomment`` is called. Meaning the backup will only be useful
            after the first invocation.

        Set to False/None to not keep a backup.

    Usage:

    .. code-block:: yaml

        /etc/adduser.conf:
          file.uncomment:
            - regex: EXTRA_GROUPS

    .. versionadded:: 0.9.5
    Fr5rLz#Must provide name to file.uncommentrkz	{}[ 	]*{}�^Trlz^[ 	]*{}zPattern already uncommentedr�r�rmr:rnr�r�roNr%rprqrrr�zUncommented lines successfullyz$Expected uncommented lines not found)rrrZr�r�r;rr�r r&r'r(r)rrrsrKrurrvrw)
rRr�rxr�r.r5r6r'ryrzrrrrR�sT$

����
��rRc
Cs�|iddd�}|st|d�S|durt|�s|rt�d�tj�|�}|dur)g}|dur/g}t||||d�\}}
}|sAt||
�S|dur�tj�|�}t	d	r\d
|�d�|d<d|d
<nFt
d|�s�zt|d�Wnty�}zt|d|j
�d��WYd}~Sd}~wwtjj��r�t|�nt|�\}}}|s�||d<t||�St|�\}}|s�t||d�}t	d	r�|St|�\}}|s�t||�S|r�t||||	d�}|d
s�|S|d}t|�}tjj�|d��}|��}|�t�}|��}Wd�n1s�wYg}z7|D]2}|
�rt
d|tjj�|�dd��r�qnt
d||dd��r(�q|��D]	}|� |���q,�qWnt!�yHt|d�YSwt	d	�r�d|�d�|d<d|d
<t"|�}|�#|�||k�r�t$d|��swd|dd<|Sd�%t&�'||��|dd<|Sd|�d�|d<d|d
<|S|�r�t
d ||d!�d"t(|��d#�|d<nd|�d�|d<tjj�|d��}|��}|�t�}|��}Wd�n	1�s�wY||k�r�t$d|��s�d|dd<n
d�%t&�'||��|dd<d|d
<|S)$a�
    Ensure that some text appears at the end of a file.

    The text will not be appended if it already exists in the file.
    A single string of text or a list of strings may be appended.

    name
        The location of the file to append to.

    text
        The text to be appended, which can be a single string or a list
        of strings.

    makedirs
        If the file is located in a path without a parent directory,
        then the state will fail. If makedirs is set to True, then
        the parent directories will be created to facilitate the
        creation of the named file. Defaults to False.

    source
        A single source file to append. This source file can be hosted on either
        the salt master server, or on an HTTP or FTP server. Both HTTPS and
        HTTP are supported as well as downloading directly from Amazon S3
        compatible URLs with both pre-configured and automatic IAM credentials
        (see s3.get state documentation). File retrieval from Openstack Swift
        object storage is supported via swift://container/object_path URLs
        (see swift.get documentation).

        For files hosted on the salt file server, if the file is located on
        the master in the directory named spam, and is called eggs, the source
        string is salt://spam/eggs.

        If the file is hosted on an HTTP or FTP server, the source_hash argument
        is also required.

    source_hash
        This can be one of the following:
            1. a source hash string
            2. the URI of a file that contains source hash strings

        The function accepts the first encountered long unbroken alphanumeric
        string of correct length as a valid hash, in order from most secure to
        least secure:

        .. code-block:: text

            Type    Length
            ======  ======
            sha512     128
            sha384      96
            sha256      64
            sha224      56
            sha1        40
            md5         32

        See the ``source_hash`` parameter description for :mod:`file.managed
        <salt.states.file.managed>` function for more details and examples.

    template
        The named templating engine will be used to render the appended-to file.
        Defaults to ``jinja``. The following templates are supported:

        - :mod:`cheetah<salt.renderers.cheetah>`
        - :mod:`genshi<salt.renderers.genshi>`
        - :mod:`jinja<salt.renderers.jinja>`
        - :mod:`mako<salt.renderers.mako>`
        - :mod:`py<salt.renderers.py>`
        - :mod:`wempy<salt.renderers.wempy>`

    sources
        A list of source files to append. If the files are hosted on an HTTP or
        FTP server, the source_hashes argument is also required.

    source_hashes
        A list of source_hashes corresponding to the sources list specified in
        the sources argument.

    defaults
        Default context passed to the template.

    context
        Overrides default context variables passed to the template.

    ignore_whitespace
        .. versionadded:: 2015.8.4

        Spaces and Tabs in text are ignored by default, when searching for the
        appending content, one space or multiple tabs are the same for salt.
        Set this option to ``False`` if you want to change this behavior.

    Multi-line example:

    .. code-block:: yaml

        /etc/motd:
          file.append:
            - text: |
                Thou hadst better eat salt with the Philosophers of Greece,
                than sugar with the Courtiers of Italy.
                - Benjamin Franklin

    Multiple lines of text:

    .. code-block:: yaml

        /etc/motd:
          file.append:
            - text:
              - Trust no one unless you have eaten much salt with him.
              - "Salt is born of the purest of parents: the sun and the sea."

    Gather text from multiple template files:

    .. code-block:: yaml

        /etc/motd:
          file:
            - append
            - template: jinja
            - sources:
              - salt://motd/devops-messages.tmpl
              - salt://motd/hr-messages.tmpl
              - salt://motd/general-messages.tmpl

    .. versionadded:: 0.9.5
    Fr5rLz Must provide name to file.appendNr�rTr:rwror�r��file.directory_existsrQrgrhr��r[r^rrr%rkrlz)No text found to append. Nothing appendedr�rqrrr�r�� is in correct statezfile.append)�argsz	Appended rI))r�rr<r=rrrZrrtr r;rJr	rnr&r'r�r�r�r�r��touchrr-r(r)r"rsrtr$rV�build_whitespace_split_regexrxr�rXr�rurrvrwr)rRrhr[rrr rrr&r!Zignore_whitespacer.rcr@rdrtr�r5r6�
check_changes�	touch_ret�	retry_res�	retry_msgrgr'ryZappend_lines�chunkZ	line_itemrzrrrrx�s�

�

 ��

��

�

������


��

�
rxc$
Cs�tj�|�}|iddd�}|st|d�S|dur#t|�s#|r#t�d�|dur)g}|dur/g}t||||d�\}}
}|sAt||
�S|dur�tj�|�}t	d	r\d
|�d�|d<d|d
<nFt
d|�s�zt|d�Wnty�}zt|d|j
�d��WYd}~Sd}~wwtjj��r�t|�nt|�\}}}|s�||d<t||�St|�\}}|s�t||d�}t	d	r�|St|�\}}|s�t||�S|r�t||||	d�}|d
s�|S|d}t|�}tjj�|d��}|��}|�t�}|�d�}Wd�n1s�wYd}g}g}|D]D}|
�st
d|tjj�|�dd��r�q	|��}|D]&}t	d	�rAd|�d�|d<d|d
<|� |�d��n|� |�|d7}�q%�q	t	d	�r�||} || k�r}t!d|��sjd|dd<n
d�"t#�$|| ��|dd<d|d
<|Sd|�d �|d<d|d
<|S|
�r�tjj�|d��>}|��}!|!�t�}!|!�d�}!|!dt%|��}"g}#|"D]	}|#|��7}#�q�|#|k�r�t
d!|g|�R�nd}Wd�n	1�s�wYn
t
d!|g|�R�tjj�|d��}|��} | �t�} | �d�} Wd�n	1�s
wY|| k�r/t!d|��s"d|dd<n
d�"t#�$|| ��|dd<|�r;d"|�d#�|d<nd|�d �|d<d|d
<|S)$a�
    Ensure that some text appears at the beginning of a file

    The text will not be prepended again if it already exists in the file. You
    may specify a single line of text or a list of lines to append.

    name
        The location of the file to append to.

    text
        The text to be appended, which can be a single string or a list
        of strings.

    makedirs
        If the file is located in a path without a parent directory,
        then the state will fail. If makedirs is set to True, then
        the parent directories will be created to facilitate the
        creation of the named file. Defaults to False.

    source
        A single source file to append. This source file can be hosted on either
        the salt master server, or on an HTTP or FTP server. Both HTTPS and
        HTTP are supported as well as downloading directly from Amazon S3
        compatible URLs with both pre-configured and automatic IAM credentials
        (see s3.get state documentation). File retrieval from Openstack Swift
        object storage is supported via swift://container/object_path URLs
        (see swift.get documentation).

        For files hosted on the salt file server, if the file is located on
        the master in the directory named spam, and is called eggs, the source
        string is salt://spam/eggs.

        If the file is hosted on an HTTP or FTP server, the source_hash argument
        is also required.

    source_hash
        This can be one of the following:
            1. a source hash string
            2. the URI of a file that contains source hash strings

        The function accepts the first encountered long unbroken alphanumeric
        string of correct length as a valid hash, in order from most secure to
        least secure:

        .. code-block:: text

            Type    Length
            ======  ======
            sha512     128
            sha384      96
            sha256      64
            sha224      56
            sha1        40
            md5         32

        See the ``source_hash`` parameter description for :mod:`file.managed
        <salt.states.file.managed>` function for more details and examples.

    template
        The named templating engine will be used to render the appended-to file.
        Defaults to ``jinja``. The following templates are supported:

        - :mod:`cheetah<salt.renderers.cheetah>`
        - :mod:`genshi<salt.renderers.genshi>`
        - :mod:`jinja<salt.renderers.jinja>`
        - :mod:`mako<salt.renderers.mako>`
        - :mod:`py<salt.renderers.py>`
        - :mod:`wempy<salt.renderers.wempy>`

    sources
        A list of source files to append. If the files are hosted on an HTTP or
        FTP server, the source_hashes argument is also required.

    source_hashes
        A list of source_hashes corresponding to the sources list specified in
        the sources argument.

    defaults
        Default context passed to the template.

    context
        Overrides default context variables passed to the template.

    ignore_whitespace
        .. versionadded:: 2015.8.4

        Spaces and Tabs in text are ignored by default, when searching for the
        appending content, one space or multiple tabs are the same for salt.
        Set this option to ``False`` if you want to change this behavior.

    Multi-line example:

    .. code-block:: yaml

        /etc/motd:
          file.prepend:
            - text: |
                Thou hadst better eat salt with the Philosophers of Greece,
                than sugar with the Courtiers of Italy.
                - Benjamin Franklin

    Multiple lines of text:

    .. code-block:: yaml

        /etc/motd:
          file.prepend:
            - text:
              - Trust no one unless you have eaten much salt with him.
              - "Salt is born of the purest of parents: the sun and the sea."

    Optionally, require the text to appear exactly as specified
    (order and position). Combine with multi-line or multiple lines of input.

    .. code-block:: yaml

        /etc/motd:
          file.prepend:
            - header: True
            - text:
              - This will be the very first line in the file.
              - The 2nd line, regardless of duplicates elsewhere in the file.
              - These will be written anew if they do not appear verbatim.

    Gather text from multiple template files:

    .. code-block:: yaml

        /etc/motd:
          file:
            - prepend
            - template: jinja
            - sources:
              - salt://motd/devops-messages.tmpl
              - salt://motd/hr-messages.tmpl
              - salt://motd/general-messages.tmpl

    .. versionadded:: 2014.7.0
    Fr5rLz!Must provide name to file.prependNr�rTr:rwror�r�r|rQrgrhr�r}r^rrr%rrkrlr�r�r�rqrrr�r~zfile.prependz
Prepended rI)&rrrZr�rr<r=rrtr r;rJr	rnr&r'r�r�r�r�r�r�rr-r(r)r"rsrtr$rVr�rxrurrvrwr)$rRrhr[rrr rrr&r!�headerr.rcr@rdrtr�r5r6r�r�r�r�rgr'ryrHZ
test_linesZprefacer��linesr�rzr�Ztarget_headZtarget_linesrrr�prepend
s�

�

 ��

��

�
��

�
	
�


���
�
r�c)s�	�iddd�}
tjj�d�sd|
d<|
Sd}�sd|
d<|
Sztj����Wnty9d��d	�|
d<|
YSwtj���sI��d
�|
d<|
Stj���sX��d�|
d<|
Stj�	��}dD]}||vrr|
�
d
g��d�|��q`|	dur�ztj�
|	�}Wnty�d|	�d�|
d<|
YSwtj�|�s�d	|	�d�|
d<|
Stj�	|�s�d�|	�|
d<|
Sg�tjj�|�}d}t|�d}g}||k�rf||}t|t�s�t|�}dD]}|�|�r�|}nq�d}|dur�|�|�|�d��rz
t|dd��}
Wn]t�yd|
d<|
YSw|�d��rYd|v�r9zt|�dd�d�}
Wn5t�y8d|
d<|
YSwz
t||d�}
Wnt�ySd|
d<|
YSw|d7}n��|�|d7}||ks�|�rud�d�|��|
d<|
S�}ztd||t�d}Wnt�y�}zd|
d <|j|
d<|
WYd}~Sd}~ww|du�r�tjj�|�\}}|�d!��r�|du�r�||k�r�|
�
d
g��d"�n|d#|��7}g}�z�tjj��}|�|�z�z!td$}dtd$<dt j!td%j"jd$<t#||||||||d&�}Wnjt�yl}z]d'�tjj�$|�|�}t%�&|�||
d<|
WYd}~W|td$<|t j!td%j"jd$<W|D]-} zt�'| �W�q9t(�yf}z|j)tj)j*k�r[t%�+d(| |�WYd}~�q9d}~wwSd}~wwt%�,d)|�W|td$<|t j!td%j"jd$<n|td$<|t j!td%j"jd$<w|�s�|d �s�t%�,d*tjj�$|��|W|D]-} zt�'| �W�q�t(�y�}z|j)tj)j*k�r�t%�+d(| |�WYd}~�q�d}~wwSdB��fd+d,�	}!|	du�r�|	}"ntjj��}"|�|"�tjj��}#|�|#�d-d.|"d/|#g}$|�r|
du�r|$�d|
���|!||$�}%|%d0dk�r|!|"d1d2gd3d4�}&|&d0dk}'|%d0dk�r{|%d5�r{|%d5|
d<d|
d <|
W|D]-} zt�'| �W�qLt(�yy}z|j)tj)j*k�rnt%�+d(| |�WYd}~�qLd}~wwS|'�r�d6|
d<d3|
d <|
W|D]-} zt�'| �W�q�t(�y�}z|j)tj)j*k�r�t%�+d(| |�WYd}~�q�d}~wwSd7|
d<|	du�r�|
dd87<t-�-t�}(d|(d9<|
dd:tj.j/|%d;|(d<d=�7<|
W|D]-} zt�'| �W�q�t(�y}z|j)tj)j*k�rt%�+d(| |�WYd}~�q�d}~wwStd$�r\d|
d <d>|
d<|%|
d?<|
W|D]-} zt�'| �W�q-t(�yZ}z|j)tj)j*k�rOt%�+d(| |�WYd}~�q-d}~wwSg}$|�rn|
du�rn|$�d|
���|!||$�|
d?<|
d?d0dk�r�d@|
d<d3|
d <ndA|
d<|
W|D]-} zt�'| �W�q�t(�y�}z|j)tj)j*k�r�t%�+d(| |�WYd}~�q�d}~wwS|D]-} zt�'| �W�q�t(�y�}z|j)tj)j*k�r�t%�+d(| |�WYd}~�q�d}~www)Ca
    Ensure that a patch has been applied to the specified file or directory

    .. versionchanged:: 2019.2.0
        The ``hash`` and ``dry_run_first`` options are now ignored, as the
        logic which determines whether or not the patch has already been
        applied no longer requires them. Additionally, this state now supports
        patch files that modify more than one file. To use these sort of
        patches, specify a directory (and, if necessary, the ``strip`` option)
        instead of a file.

    .. note::
        A suitable ``patch`` executable must be available on the minion. Also,
        keep in mind that the pre-check this state does to determine whether or
        not changes need to be made will create a temp file and send all patch
        output to that file. This means that, in the event that the patch would
        not have applied cleanly, the comment included in the state results will
        reference a temp file that will no longer exist once the state finishes
        running.

    name
        The file or directory to which the patch should be applied

    source
        The patch file to apply

        .. versionchanged:: 2019.2.0
            The source can now be from any file source supported by Salt
            (``salt://``, ``http://``, ``https://``, ``ftp://``, etc.).
            Templating is also now supported.

    source_hash
        Works the same way as in :py:func:`file.managed
        <salt.states.file.managed>`.

        .. versionadded:: 2019.2.0

    source_hash_name
        Works the same way as in :py:func:`file.managed
        <salt.states.file.managed>`

        .. versionadded:: 2019.2.0

    skip_verify
        Works the same way as in :py:func:`file.managed
        <salt.states.file.managed>`

        .. versionadded:: 2019.2.0

    template
        Works the same way as in :py:func:`file.managed
        <salt.states.file.managed>`

        .. versionadded:: 2019.2.0

    context
        Works the same way as in :py:func:`file.managed
        <salt.states.file.managed>`

        .. versionadded:: 2019.2.0

    defaults
        Works the same way as in :py:func:`file.managed
        <salt.states.file.managed>`

        .. versionadded:: 2019.2.0

    options
        Extra options to pass to patch. This should not be necessary in most
        cases.

        .. note::
            For best results, short opts should be separate from one another.
            The ``-N`` and ``-r``, and ``-o`` options are used internally by
            this state and cannot be used here. Additionally, instead of using
            ``-pN`` or ``--strip=N``, use the ``strip`` option documented
            below.

    reject_file
        If specified, any rejected hunks will be written to this file. If not
        specified, then they will be written to a temp file which will be
        deleted when the state finishes running.

        .. important::
            The parent directory must exist. Also, this will overwrite the file
            if it is already present.

        .. versionadded:: 2019.2.0

    strip
        Number of directories to strip from paths in the patch file. For
        example, using the below SLS would instruct Salt to use ``-p1`` when
        applying the patch:

        .. code-block:: yaml

            /etc/myfile.conf:
              file.patch:
                - source: salt://myfile.patch
                - strip: 1

        .. versionadded:: 2019.2.0
            In previous versions, ``-p1`` would need to be passed as part of
            the ``options`` value.

    saltenv
        Specify the environment from which to retrieve the patch file indicated
        by the ``source`` parameter. If not provided, this defaults to the
        environment from which the state is being executed.

        .. note::
            Ignored when the patch file is from a non-``salt://`` source.

    **Usage:**

    .. code-block:: yaml

        # Equivalent to ``patch --forward /opt/myfile.txt myfile.patch``
        /opt/myfile.txt:
          file.patch:
            - source: salt://myfile.patch
    Fr5rL�patchz$patch executable not found on minionr�z*A file/directory to be patched is requiredzInvalid path '�'r�rP)�hashZ
dry_run_firstr�z9The '{}' argument is no longer used and has been ignored.Nz' for reject_filez' is not an absolute pathzRParent directory for reject_file '{}' either does not exist, or is not a directoryrr�)�-Nz	--forward�-rz
--reject-file�-oz--outputz-pr�zUInvalid format for '-p' CLI option. Consider using the 'strip' option for this state.z--striprCr�zYInvalid format for '-strip' CLI option. Consider using the 'strip' option for this state.z-The following CLI options are not allowed: {}r�r�r�r�zIIgnoring 'saltenv' option in favor of saltenv included in the source URL.z	?saltenv=r:�
file.patch)rrr�r�r r!r&z!Failed to cache patch file {}: {}z-file.patch: Failed to remove temp file %s: %szfile.managed: %szfailed to download %scs0t���}|dur|�|�td�|||d�S)Nr�)�optionsr>)rr�r;)�
patch_filer�r>�
patch_opts�rRZsanitized_optionsrr�_patch�s

�zpatch.<locals>._patchr�r�r��retcodez-Rz-fT)r>�stderrzPatch was already appliedzMPatch would not apply cleanly, no changes made. Results of dry-run are below.zS Run state again using the reject_file option to save rejects to a persistent file.Zcolorz

�nested�)Z
nested_indentzThe patch would be appliedr�zPatch successfully appliedzFailed to apply patch)NF)0r&r'r�whichrrZrqr�r�r�r�rxrrtrZshlex_splitrrrrr��rsplitrr;rpr	r�rnror(r�r r'�modules�
__module__r��redact_http_basic_authr<rbrZr,�errno�ENOENTr�rYr�outputZ
out_format))rRrrr�r�r r!r&r�Zreject_filerGrkr}r.�is_dirZdeprecated_argZreject_file_parentrzZ	max_indexZblacklisted_options�optionr,ZblacklistedZsource_matchr�Zsource_match_urlZsource_match_saltenvZcleanupr�Z	orig_testr�r�rr�Z
patch_rejectsZpatch_outputr�Z	pre_checkZreverse_passZalready_applied�optsrr�rr�?s>
������


�
��
����
	
�7�
��

�

�
�
h��������[�����



,�����(������
�
�����
������
���
�����r�c
CsVtj�|�}|id�}|st|d�Stj�|�s!t|d|�d��Stdr0|�t|||��|S|rVzt|d�Wnt	yU}zt|d|j
�d��WYd	}~Sd	}~wwtj�tj�|��sht|d
|���Stj�
|�}td|||�|d<|s�|dr�d
|��|d<||dd<|S|r�|dr�d�tj�|�r�dnd|�|d<||dd<|S)a1
    Replicate the 'nix "touch" command to create a new empty
    file or update the atime and mtime of an existing file.

    Note that if you just want to create a file and don't care about atime or
    mtime, you should use ``file.managed`` instead, as it is more
    feature-complete.  (Just leave out the ``source``/``template``/``contents``
    arguments, and it will just create the file and/or check its permissions,
    without messing with contents)

    name
        name of the file

    atime
        atime of the file

    mtime
        mtime of the file

    makedirs
        whether we should create the parent directory/directories in order to
        touch the file

    Usage:

    .. code-block:: yaml

        /var/log/httpd/logrotate.empty:
          file.touch

    .. versionadded:: 0.9.5
    )rRr�zMust provide name to file.touchr�r�r:rQrgrhNz$Directory not present to touch file z
file.touchr�zCreated empty file r�r�r�zUpdated times on {} {}r�r�r�)rrrZr�r�r r\r�rJr	rnr�rtr�r;r)rRr�r�r[r.r�Zextantrrrr��s@!�
 ����r�c

Ks�tj�|�}tj�|�}|id|�d|�d�dd�}|s!t|d�Sd}tj�|�s2t|d|�d��Stj�|�sAt|d	|�d
��S|rVtd|�}td|�}td
|�}nOt|
|d�}|durdtd}t	j
j��rv|durtt
�d|�|}|dur�dtvr�tdtd|��dd��}n|}t||�}
|
r�t||
�S|dur�td
|�}tj�|�r�|	r�tj�|tj�|��}tj�|��rtj�|��r|r�tj�|�r�t	j
j�|�}t	j
j�|�}||kr�d}d�|ddg�|d<|s�d}n&td�s|�rz
td|dd�Wnt�yt|d|�d��YSwtd�r<|�r.d�||�|d<d|d <|Sd!|�d"�|d<d|d <|S|�sMd!|�d"�|d<d|d <|Stj�|�}tj�|��s�|�r�|du�r�|du�r�d#d$�t|�D�d%d�}t|�D]\}}|d&k�r�tt|�d'B�||<�qxd(�|�}z
t||||d)�Wn&t�y�}zt|d*|j �d+��WYd}~Sd}~wwt|d,|�d-��Sz�tj�|��rt!j"||dd.�t	j
j�#|�D],\}}}|D]}td/tj�||�||��q�|D]}td/tj�||�||��q�q�nt!�$||�||i|d0<|�sHt	j
j���r(td1|||d2�}ntd1|||||�\}}|d �sK|d |d <|d|d<W|SW|SW|St�yct|d3|�d|�d��YSw)4a�
    If the file defined by the ``source`` option exists on the minion, copy it
    to the named path. The file will not be overwritten if it already exists,
    unless the ``force`` option is set to ``True``.

    .. note::
        This state only copies files from one location on a minion to another
        location on the same minion. For copying files from the master, use a
        :py:func:`file.managed <salt.states.file.managed>` state.

    name
        The location of the file to copy to

    source
        The location of the file to copy to the location specified with name

    force
        If the target location is present then the file will not be moved,
        specify "force: True" to overwrite the target file

    makedirs
        If the target subdirectories don't exist create them

    preserve
        .. versionadded:: 2015.5.0

        Set ``preserve: True`` to preserve user/group ownership and mode
        after copying. Default is ``False``. If ``preserve`` is set to ``True``,
        then user/group/mode attributes will be ignored.

    user
        .. versionadded:: 2015.5.0

        The user to own the copied file, this defaults to the user salt is
        running as on the minion. If ``preserve`` is set to ``True``, then
        this will be ignored

    group
        .. versionadded:: 2015.5.0

        The group to own the copied file, this defaults to the group salt is
        running as on the minion. If ``preserve`` is set to ``True`` or on
        Windows this will be ignored

    mode
        .. versionadded:: 2015.5.0

        The permissions to set on the copied file, aka 644, '0775', '4664'.
        If ``preserve`` is set to ``True``, then this will be ignored.
        Not supported on Windows.

        The default mode for new files and directories corresponds umask of salt
        process. For existing files and directories it's not enforced.

    dir_mode
        .. versionadded:: 3006.0

        If directories are to be created, passing this option specifies the
        permissions for those directories. If this is not set, directories
        will be assigned permissions by adding the execute bit to the mode of
        the files.

        The default mode for new files and directories corresponds to the umask
        of the salt process. Not enforced for existing files and directories.

    subdir
        .. versionadded:: 2015.5.0

        If the name is a directory then place the file inside the named
        directory

    .. note::
        The copy function accepts paths that are local to the Salt minion.
        This function does not support salt://, http://, or the other
        additional file paths that are supported by :mod:`states.file.managed
        <salt.states.file.managed>` and :mod:`states.file.recurse
        <salt.states.file.recurse>`.

    Usage:

    .. code-block:: yaml

        # Use 'copy', not 'copy_'
        /etc/example.conf:
          file.copy:
            - source: /tmp/example.conf
    zCopied "r1r�T�rRr�r�r�zMust provide name to file.copyr�r�z
Source file "z" is not presentr�r�z
file.get_moderKNr>rMrNrOrBr� r�z+- files are identical but force flag is setFr:r�rv�Failed to delete "� " in preparation for forced movez%File "{}" is set to be copied to "{}"r��The target file "�$" exists and will not be overwrittencSr\rrr�rrrrr]zcopy_.<locals>.<listcomp>����0r�r5)rRr>r?r�rgrh�The target directory rz)r^rr�r�)rr.r�zFailed to copy ")%rrrZr�r�r�r;rr r&r'r�r�r<r=r�rCr�rrorpr3Z	hashutilsZget_hashr,rrtrrr�rJr	rn�shutil�copytreer�r)rRrr
r[Zpreserver>r?r�r�Zsubdirr}r.�changedr�Zhash1Zhash2�dnameZ	mode_listr	r�r�r�r�r(r�Zfile_Z	check_retr�rrrr
3s�d�
��


�
��
�
��
�
 ����
�
�
���c	
Ks,tj�|�}tj�|�}tj�|�}tj�|�}|iddd�}|s&t|d�Stj�|�s5t|d|�d��Stj�|�sDd�|�|d<|Stj�|�r{tj�|�r{|s\d	|�d
�|d<|Stds{zt	d|�Wnt
yzt|d
|�d��YSwtdr�d|�d|�d�|d<d|d<|Stj�|�}tj�|�s�|r�zt
|d�Wn%ty�}zt|d|j�d��WYd}~Sd}~wwt|d|�d��Sz!tj�|�r�tjj�|�}t�||�t�|�nt�||�Wnt
�yt|d|�d|�d��YSwd|�d|�d�|d<||i|d<|S)a)
    If the source file exists on the system, rename it to the named file. The
    named file will not be overwritten if it already exists unless the force
    option is set to True.

    name
        The location of the file to rename to

    source
        The location of the file to move to the location specified with name

    force
        If the target location is present then the file will not be moved,
        specify "force: True" to overwrite the target file

    makedirs
        If the target subdirectories don't exist create them

    r5Tr�z Must provide name to file.renamer�r�z4Source file "{}" has already been moved out of placer�r�r�r:r�r�r�zFile "z" is set to be moved to "r�Nr�rQrgrhr�rzzFailed to move "r1zMoved "r�)rrrZ�
expandvarsr�r�rprr r;r,rtr�rJr	rnr�r&r'�readlinkrs�unlinkr��move)	rRrr
r[r}r.r�r��linktorrrr�+sn
�
�
�� ����r�cKs�|iddd�}|st|d�S|durd|d<d|d	<|St�d
g�}t�dg�}||}dd
�|D�sFd|d<d�|tdtd�|d	<|St|t�rO|f}nt|t�rW|f}t�\}}	||vrdi||<||	vrli|	|<||	|vrxg|	||<|D]}
t|
ttf�r�|	||�	|
�
��qz|	||�	|
�qz|||vr�g|||<|D]}||||vr�|||�|�d�||�|d	<q�t||	�|S)a
    Prepare accumulator which can be used in template in file.managed state.
    Accumulator dictionary becomes available in template. It can also be used
    in file.blockreplace.

    name
        Accumulator name

    filename
        Filename which would receive this accumulator (see file.managed state
        documentation about ``name``)

    text
        String or list for adding in accumulator

    require_in / watch_in
        One of them required for sure we fill up accumulator before we manage
        the file. Probably the same as filename

    Example:

    Given the following:

    .. code-block:: yaml

        animals_doing_things:
          file.accumulated:
            - filename: /tmp/animal_file.txt
            - text: ' jumps over the lazy dog.'
            - require_in:
              - file: animal_file

        animal_file:
          file.managed:
            - name: /tmp/animal_file.txt
            - source: salt://animal_file.txt
            - template: jinja

    One might write a template for ``animal_file.txt`` like the following:

    .. code-block:: jinja

        The quick brown fox{% for animal in accumulator['animals_doing_things'] %}{{ animal }}{% endfor %}

    Collectively, the above states and template file will produce:

    .. code-block:: text

        The quick brown fox jumps over the lazy dog.

    Multiple accumulators can be "chained" together.

    .. note::
        The 'accumulator' data structure is a Python dictionary.
        Do not expect any loop over the keys in a deterministic order!
    Tr5rLz%Must provide name to file.accumulatedNFr�z No text supplied for accumulatorr��
require_in�watch_incSr�r�rr�rrrr�r�zaccumulated.<locals>.<listcomp>z Orphaned accumulator {} in {}:{}Z__sls__r�z.Accumulator {} for file {} was charged by text)
r�rWr�rrrr�r2rr��valuesrxr4)rRrbrhr}r.r�r�r[r�rer�r�rrr�accumulated�sT9
�


��
r�cKs�d|vr	|�d�tj�|�}ddidddd�d�}iid	�}|r3|d
�ddi�|d�d
di�id|dd�}|sAt|d�S|	sStj�|�sSd|�d�|d<|S|�dd�}|
rb|rbt|d�St|
ph|phd���}
t	dd�||fD��dkr~t|d�S|r�t
d|�}|dur�t|d�Stjj
��r�|dur�t�d|�|}|
�d�}|
�d �}|tvr�id!�|
�|dd�S|r�|�|i��tjj�|��|r�|�|i��tjj�|��d}|
�rstj�|��rr|tvr�id"�|
�|dd�Sd#}|
d$kr�|d%7}tjj�||��B}zt||fi|�|i���}Wn)ttf�y?}zd|d&<d'�|�|d<|WYd}~Wd�Sd}~wwWd�n	1�sKwY|du�rrtjj�||�}||k�rpd|d&<d(|�d)�|d<|S|}n|�r|�d*g��d+�t||fi|�|i���}z|d,7}Wn
t�y�Ynwtjj�|�}td-�r�t
d.dF|did|||ddddt |dd/�|��|d0<|d0�r�d|d&<d1�|�|d<|�s�d2|d0d3<nGd|d&<d(|�d)�|d<n:t
d4dFid5|�d6d�d7|�d8d�d9i�d:|�d;|�d<|�d=d�d>t �d?|�d@|�dAd�dB|�dC|�dD|�dE|��}t!|t"��r>t!|t"��r>tjj#�$||�j%|d0d3<|S)Ga�
    Serializes dataset and store it into managed file. Useful for sharing
    simple configuration files.

    name
        The location of the file to create

    dataset
        The dataset that will be serialized

    dataset_pillar
        Operates like ``dataset``, but draws from a value stored in pillar,
        using the pillar path syntax used in :mod:`pillar.get
        <salt.modules.pillar.get>`. This is useful when the pillar value
        contains newlines, as referencing a pillar variable using a jinja/mako
        template can result in YAML formatting issues due to the newlines
        causing indentation mismatches.

        .. versionadded:: 2015.8.0

    serializer (or formatter)
        Write the data as this format. See the list of
        :ref:`all-salt.serializers` for supported output formats.

        .. versionchanged:: 3002
            ``serializer`` argument added as an alternative to ``formatter``.
            Both are accepted, but using both will result in an error.

    encoding
        If specified, then the specified encoding will be used. Otherwise, the
        file will be encoded using the system locale (usually UTF-8). See
        https://docs.python.org/3/library/codecs.html#standard-encodings for
        the list of available encodings.

        .. versionadded:: 2017.7.0

    encoding_errors
        Error encoding scheme. Default is ```'strict'```.
        See https://docs.python.org/2/library/codecs.html#codec-base-classes
        for the list of available schemes.

        .. versionadded:: 2017.7.0

    user
        The user to own the directory, this defaults to the user salt is
        running as on the minion

    group
        The group ownership set for the directory, this defaults to the group
        salt is running as on the minion

    mode
        The permissions to set on this file, e.g. ``644``, ``0775``, or
        ``4664``.

        The default mode for new files and directories corresponds umask of salt
        process. For existing files and directories it's not enforced.

        .. note::
            This option is **not** supported on Windows.

    backup
        Overrides the default backup mode for this specific file.

    makedirs
        Create parent directories for destination file.

        .. versionadded:: 2014.1.3

    show_changes
        Output a unified diff of the old file and the new file. If ``False``
        return a boolean if any changes were made.

    create
        Default is True, if create is set to False then the file will only be
        managed if the file already exists on the system.

    merge_if_exists
        Default is False, if merge_if_exists is True then the existing file will
        be parsed and the dataset passed in will be merged with the existing
        content

        .. versionadded:: 2014.7.0

    serializer_opts
        Pass through options to serializer. For example:

        .. code-block:: yaml

           /etc/dummy/package.yaml
             file.serialize:
               - serializer: yaml
               - serializer_opts:
                 - explicit_start: True
                 - default_flow_style: True
                 - indent: 4

        The valid opts are the additional opts (i.e. not the data being
        serialized) for the function used to serialize the data. Documentation
        for the these functions can be found in the list below:

        - For **yaml**: `yaml.dump()`_
        - For **json**: `json.dumps()`_
        - For **python**: `pprint.pformat()`_
        - For **msgpack**: Run ``python -c 'import msgpack; help(msgpack.Packer)'``
          to see the available options (``encoding``, ``unicode_errors``, etc.)

        .. _`yaml.dump()`: https://pyyaml.org/wiki/PyYAMLDocumentation
        .. _`json.dumps()`: https://docs.python.org/2/library/json.html#json.dumps
        .. _`pprint.pformat()`: https://docs.python.org/2/library/pprint.html#pprint.pformat

    deserializer_opts
        Like ``serializer_opts`` above, but only used when merging with an
        existing file (i.e. when ``merge_if_exists`` is set to ``True``).

        The options specified here will be passed to the deserializer to load
        the existing data, before merging with the specified data and
        re-serializing.

        .. code-block:: yaml

           /etc/dummy/package.yaml
             file.serialize:
               - serializer: yaml
               - serializer_opts:
                 - explicit_start: True
                 - default_flow_style: True
                 - indent: 4
               - deserializer_opts:
                 - encoding: latin-1
               - merge_if_exists: True

        The valid opts are the additional opts (i.e. not the data being
        deserialized) for the function used to deserialize the data.
        Documentation for the these functions can be found in the list below:

        - For **yaml**: `yaml.load()`_
        - For **json**: `json.loads()`_

        .. _`yaml.load()`: https://pyyaml.org/wiki/PyYAMLDocumentation
        .. _`json.loads()`: https://docs.python.org/2/library/json.html#json.loads

        However, note that not all arguments are supported. For example, when
        deserializing JSON, arguments like ``parse_float`` and ``parse_int``
        which accept a callable object cannot be handled in an SLS file.

        .. versionadded:: 2019.2.0

    For example, this state:

    .. code-block:: yaml

        /etc/dummy/package.json:
          file.serialize:
            - dataset:
                name: naive
                description: A package using naive versioning
                author: A confused individual <iam@confused.com>
                dependencies:
                  express: '>= 1.2.0'
                  optimist: '>= 0.1.0'
                engine: node 0.4.1
            - serializer: json

    will manage the file ``/etc/dummy/package.json``:

    .. code-block:: json

        {
          "author": "A confused individual <iam@confused.com>",
          "dependencies": {
            "express": ">= 1.2.0",
            "optimist": ">= 0.1.0"
          },
          "description": "A package using naive versioning",
          "engine": "node 0.4.1",
          "name": "naive"
        }
    r�Zdefault_flow_styleFr�)�,r�T)r0�
separators�	sort_keys)�yaml.serialize�json.serialize)zyaml.deserializezjson.deserializer�Z
allow_unicoder��ensure_asciir5r�z#Must provide name to file.serializer�r�r��	formatterNz0Only one of serializer and formatter are allowedZyamlcSsg|]}|r|�qSrrr�rrrr�r�zserialize.<locals>.<listcomp>r�z7Only one of 'dataset' and 'dataset_pillar' is permittedr�z2Neither 'dataset' nor 'dataset_pillar' was definedrMz
.serializez.deserializezfThe {} serializer could not be found. It either does not exist or its prerequisites are not installed.z6merge_if_exists is not supported for the {} serializerrD�plist�br�z'Failed to deserialize existing data: {}r�r�r�zPThe 'deserializer_opts' option is ignored unless merge_if_exists is set to True.r�r:r�)rRrrr�r>r?r�r�r r!r&rkr�r�r�z-Dataset will be serialized and stored into {}r�r�r�rRr�r.rr�r>r?r�r�rkr�r[r r�r)r�r�r)&ryrrrZr\r�r3rrrr;r&r'r�r�r<r=Z__serializers__rr�rrZrepack_dictlistr(r)r�r�r
Z
dictupdateZ
merge_recurserxr�r rprr�Z
dictdifferZrecursive_diffZdiffs)rRZdatasetZdataset_pillarr>r?r�r�r[r�ruZmerge_if_existsr)r�Z
serializerZserializer_optsZdeserializer_optsr}Zserializer_optionsZdeserializer_optionsr.r�Zserializer_nameZdeserializer_nameZ
existing_dataZ	open_argsZfhrr�Zmerged_datar�rrr�	serialize�slG

��



�

�
����
�

������
�
���
��
�
�
����������	�
���
������
�r��0600c
Cs�tj�|�}|iddd�}|st|d�S|dkr�td|�r'd�|�|d<|Std	|�sMtd
r?d|�d�|d<d
|d<|Std|||||||�}|Std|�\}}	||f||	fkrhd�|||	�|d<|Std|d
|||�d}|ds�d|�d�|d<|S|dkr�td|�r�d�|�|d<|Std|�s�td
r�d|�d�|d<d
|d<|Std|||||||�}|Std|�\}}	||f||	fkr�d�|||	�|d<|Std|d
|||�d}|ds�d�|�|d<|S|dk�rGtd|��rd�|�|d<|Std|��s,td
�rd|�d�|d<d
|d<|Std|||||||�}|Std|d
|||�d}|d�sEd|�d�|d<|Sd �|�|d<|S)!au
    Create a special file similar to the 'nix mknod command. The supported
    device types are ``p`` (fifo pipe), ``c`` (character device), and ``b``
    (block device). Provide the major and minor numbers when specifying a
    character device or block device. A fifo pipe does not require this
    information. The command will create the necessary dirs if needed. If a
    file of the same name not of the same type/major/minor exists, it will not
    be overwritten or unlinked (deleted). This is logically in place as a
    safety measure because you can really shoot yourself in the foot here and
    it is the behavior of 'nix ``mknod``. It is also important to note that not
    just anyone can create special devices. Usually this is only done as root.
    If the state is executed as none other than root on a minion, you may
    receive a permission error.

    name
        name of the file

    ntype
        node type 'p' (fifo pipe), 'c' (character device), or 'b'
        (block device)

    major
        major number of the device
        does not apply to a fifo pipe

    minor
        minor number of the device
        does not apply to a fifo pipe

    user
        owning user of the device/pipe

    group
        owning group of the device/pipe

    mode
        permissions on the device/pipe

    Usage:

    .. code-block:: yaml

        /dev/chr:
          file.mknod:
            - ntype: c
            - major: 180
            - minor: 31
            - user: root
            - group: root
            - mode: 660

        /dev/blk:
          file.mknod:
            - ntype: b
            - major: 8
            - minor: 999
            - user: root
            - group: root
            - mode: 660

        /dev/fifo:
          file.mknod:
            - ntype: p
            - user: root
            - group: root
            - mode: 660

    .. versionadded:: 0.17.0
    r5Fr�zMust provide name to file.mknodrHr�zBFile {} exists and is not a character device. Refusing to continuer�zfile.is_chrdevr:zCharacter device r�Nr�z
file.mknodzfile.get_devmmzVCharacter device {} exists and has a different major/minor {}/{}. Refusing to continuer�rr�r�r�z>File {} exists and is not a block device. Refusing to continuezfile.is_blkdevz
Block device zRBlock device {} exists and has a different major/minor {}/{}. Refusing to continuez'Block device {} is in the correct stater�z;File {} exists and is not a fifo pipe. Refusing to continuezfile.is_fifoz
Fifo pipe zbNode type unavailable: '{}'. Available node types are character ('c'), block ('b'), and pipe ('p'))rrrZr�r;rr )
rRZntype�major�minorr>r?r�r.ZdevmajZdevminrrr�mknodo s�F
�a�X��V���N�D���?�5��3�
�+��#
����
���
��r�cKs�t�d�|�d|��}td|fi|��}|ddkrFdddd	�}|�d
�r3|dd|d
7<|�d
�rD|dd|d
7<|SdS)z�
    Execute the check_cmd logic.

    Return a result dict if ``check_cmd`` succeeds (check_cmd == 0)
    otherwise return True
    zrunning our check_cmdr�zcmd.run_allr�rzcheck_cmd execution failedTF)r�Z
skip_watchr��stdoutr�r�r�)r<rYr;r�)�cmdrbr�Z_cmdr�r.rrrr�"!s
�

r��base64�md5cCs.|iddd�}|s|std��|r|rtd��|r|}n|r.td|d�}|dur-td��ntd��td	|�}|rhtd
|�}td||�}	~td||�}
|	|
krZ|
|	d
�|d<|dshd|d<d|d<|Stddurxd|d<d|d<|Std||�|d<d|d<|ds�dtd||�d
�|d<|S)ag
    Decode an encoded file and write it to disk

    .. versionadded:: 2016.3.0

    name
        Path of the file to be written.
    encoded_data
        The encoded file. Either this option or ``contents_pillar`` must be
        specified.
    contents_pillar
        A Pillar path to the encoded file. Uses the same path syntax as
        :py:func:`pillar.get <salt.modules.pillar.get>`. The
        :py:func:`hashutil.base64_encodefile
        <salt.modules.hashutil.base64_encodefile>` function can load encoded
        content into Pillar. Either this option or ``encoded_data`` must be
        specified.
    encoding_type
        The type of encoding.
    checksum
        The hashing algorithm to use to generate checksums. Wraps the
        :py:func:`hashutil.digest <salt.modules.hashutil.digest>` execution
        function.

    Usage:

    .. code-block:: yaml

        write_base64_encoded_string_to_a_file:
          file.decode:
            - name: /tmp/new_file
            - encoding_type: base64
            - contents_pillar: mypillar:thefile

        # or

        write_base64_encoded_string_to_a_file:
          file.decode:
            - name: /tmp/new_file
            - encoding_type: base64
            - encoded_data: |
                Z2V0IHNhbHRlZAo=

    Be careful with multi-line strings that the YAML indentation is correct.
    E.g.,

    .. code-block:: jinja

        write_base64_encoded_string_to_a_file:
          file.decode:
            - name: /tmp/new_file
            - encoding_type: base64
            - encoded_data: |
                {{ salt.pillar.get('path:to:data') | indent(8) }}
    Fr5rLz@Specify either the 'encoded_data' or 'contents_pillar' argument.z>Specify only one 'encoded_data' or 'contents_pillar' argument.r�zPillar data not found.zNo contents given.r�zhashutil.base64_decodestringzhashutil.digestzhashutil.digest_file��oldr�r�zFile is in the correct state.r�Tr�r:zFile is set to be updated.Nzhashutil.base64_decodefilezFile was updated.)r	r;r )rRZencoded_datar�Z
encoding_typeZchecksumr.r4Zdest_existsZinstrZinsumZoutsumrrrrs?!sT>���
�
�rsc
Kstjjjddd�t|
|	d�}	|iddd�}tjj��s!t|d�S|s(t|d	�S|�d
�s7|�d�s7t|d�St	j
�t	j
�|��}|�d
�rPt	j
�t	j
�|��}|r\t	j
�t	j
�|��}|rht	j
�t	j
�|��}|	d
urpt
d}	td|	�std�}	|	sd}	g}td|	�}
|
dkr�|�d|	�d��t	j
�|�s�|�d|�d��|r�d�|�}t|�dkr�|d7}t||�St||||||||	�\}}}t
dr�||d<||d<||d<|St	j
�t	j
�|���s|�r
zt||	d�Wn)t�y}zt|d|j�d ��WYd
}~Sd
}~wwt|d!�t	j
�|���St	j
�|��s't	j
�|��r�|d
u�r�t	j
�|��rI|�s>t|d"�|��Std#|�t�d�t	j
�t	j
�|���s�|�r|zt|d$�Wn)t�y{}zt|d|j�d ��WYd
}~Sd
}~wwt|d%�t	j
�|���St	�||�t�d�n|�r�td#|�d&|dd'<t�d�nt|d(�|��Stjj� ���Jt!j"�#d)�}|�$|�}|j%�&�|�&�kg}|d
u�r�|�|j'|k�|d
u�r�|�|j(�&�|�&�k�|d
u�r�|�|j)|k�|d
u�r
|�|j*�&�|�&�k�td*|��rZt+|��st	�,|�n>t-||	��r+d+�||	�|d<n&t.||	��rAd,�||	�|d<|	�|dd-<nd.|d<|dd/�||	�7<|Wd
�St	j
�/|��s�z)||_%|d
u�rm||_'|d
u�ru||_(|d
u�r}||_)|d
u�r�||_*|�0�Wn,t1t2j3f�y�}zd.|d<d0�|||�|d<|WYd
}~Wd
�Sd
}~wwd1|�d2|��|d<||dd3<t-||	��s�t.||	��s�d.|d<|dd4�|	�7<Wd
�|SWd
�|SWd
�|SWd
�|S1�swY|S)5a�
    Create a Windows shortcut

    If the file already exists and is a shortcut pointing to any location other
    than the specified target, the shortcut will be replaced. If it is
    a regular file or directory then the state will return False. If the
    regular file or directory is desired to be replaced with a shortcut pass
    force: True, if it is to be renamed, pass a backupname.

    name
        The location of the shortcut to create. Must end with either
        ".lnk" or ".url"

    target
        The location that the shortcut points to

    arguments
        Any arguments to pass in the shortcut

    working_dir
        Working directory in which to execute target

    description
        Description to set on shortcut

    icon_location
        Location of shortcut's icon

    force
        If the name of the shortcut exists and is not a file and
        force is set to False, the state will fail. If force is set to
        True, the link or directory in the way of the shortcut file
        will be deleted to make room for the shortcut, unless
        backupname is set, when it will be renamed

    backupname
        If the name of the shortcut exists and is not a file, it will be
        renamed to the backupname. If the backupname already
        exists and force is False, the state will fail. Otherwise, the
        backupname will be removed first.

    makedirs
        If the location of the shortcut does not already have a parent
        directory then the state will fail, setting makedirs to True will
        allow Salt to create the parent directory. Setting this to True will
        also create the parent for backupname if necessary.

    user
        The user to own the file, this defaults to the user salt is running as
        on the minion

        The default mode for new files and directories corresponds umask of salt
        process. For existing files and directories it's not enforced.
    ZArgonz@This function is being deprecated in favor of 'shortcut.present')�versionrnrKTr5rLz'Shortcuts are only supported on Windowsz"Must provide name to file.shortcutz.lnkz.urlz*Name must end with either ".lnk" or ".url"Nr>rNrcrdr6r7rPr�r�rRr�rSr:r�r�r�)rRr>rgrhz*Directory "{}" for shortcut is not presentz0File exists where the backup target {} should gor�rQz+Directory does not exist for backup at "{}"zShortcut was forcibly replacedrWz=Directory or symlink exists where the shortcut "{}" should ber2r�z&Shortcut {} is present and owned by {}z"Set ownership of shortcut {} to {}r
Fz,Failed to set ownership of shortcut {} to {}z*Unable to create new shortcut {} -> {}: {}zCreated new shortcut rYr�z', but was unable to set ownership to {})4r&r'ZversionsZ
warn_untilrr�r�r�rqrr�realpathrZr r;rxr�rrrGr�rtrJr	rnrr�rp�time�sleepr�r4r5r6r7r8r9r:rr;r<r=r>r?rZr/r0r�ZSaver��
pywintypesZ	com_error)rRrr@rArBrCr
rrr[r>r}r.r\rAr�r]r^r_r�rDrErFrrr�shortcut�!sXC�







� ��
��
��
 ��
����	






������*



���7
�
�G�
�G�
�G�
�G�Gr��basec
Cs�id|dd�}ztj�|�}Wntyd|d<|YSw|s;|s;|s;|jtjjjvr;d�	tjj
�|��|d<|S|roztd||||d�}Wnt
yb}	z
|	j|d<|WYd	}	~	Sd	}	~	ww|snd
�	|�|d<|Sni}tdr�td||d
�}
|
r�|r�td|
td�}||dkr�d|��|d<nd|��|d<nd|��|d<nd|��|d<i|d<d	|d<|S|jtjjjv�r tj�tj�|j��}tj�|��r|�s|�rtd||�dtd��}
|
|dkr�d|d<d�	||
�|d<|Sd�	||
|d�|d<|Sd|d<d|�d�|d<|Sd|�d�|d<|Std||d
�}
|
�rRtd|
|�dtd��}|�sQ|�rQ||dk�rQd|d<d�	|
|�|d<nd	}ztd|||�d�|d�}
Wn t�y�}	ztjj
�t|	��|d<|WYd	}	~	Sd	}	~	ww|
�s�d �	tjj
�|��|d<|Std|
|�dtd��}||k�r�||d!�|dd"<|�s�|�r�||dk�r�d|d<d�	|
|�|d<|Sd#�	|
||d�|d<|Sd|d<d$|
��|d<|S)%a�
    .. versionadded:: 2017.7.3
    .. versionchanged:: 3005

    Ensures that a file is saved to the minion's cache. This state is primarily
    invoked by other states to ensure that we do not re-download a source file
    if we do not need to.

    name
        The URL of the file to be cached. To cache a file from an environment
        other than ``base``, either use the ``saltenv`` argument or include the
        saltenv in the URL (e.g. ``salt://path/to/file.conf?saltenv=dev``).

        .. note::
            A list of URLs is not supported, this must be a single URL. If a
            local file is passed here, then the state will obviously not try to
            download anything, but it will compare a hash if one is specified.

    source_hash
        See the documentation for this same argument in the
        :py:func:`file.managed <salt.states.file.managed>` state.

        .. note::
            For remote files not originating from the ``salt://`` fileserver,
            such as http(s) or ftp servers, this state will not re-download the
            file if the locally-cached copy matches this hash. This is done to
            prevent unnecessary downloading on repeated runs of this state. To
            update the cached copy of a file, it is necessary to update this
            hash.

    source_hash_name
        See the documentation for this same argument in the
        :py:func:`file.managed <salt.states.file.managed>` state.

    skip_verify
        See the documentation for this same argument in the
        :py:func:`file.managed <salt.states.file.managed>` state.

        .. note::
            Setting this to ``True`` will result in a copy of the file being
            downloaded from a remote (http(s), ftp, etc.) source each time the
            state is run.

    saltenv
        Used to specify the environment from which to download a file from the
        Salt fileserver (i.e. those with ``salt://`` URL).

    use_etag
        If ``True``, remote http/https file sources will attempt to use the
        ETag header to determine if the remote file needs to be downloaded.
        This provides a lightweight mechanism for promptly refreshing files
        changed on a web server without requiring a full hash comparison via
        the ``source_hash`` parameter.

        .. versionadded:: 3005


    This state will in most cases not be useful in SLS files, but it is useful
    when writing a state or remote-execution module that needs to make sure
    that a file at a given URL has been downloaded to the cachedir. One example
    of this is in the :py:func:`archive.extracted <salt.states.file.extracted>`
    state:

    .. code-block:: python

        result = __states__['file.cached'](source_match,
                                           source_hash=source_hash,
                                           source_hash_name=source_hash_name,
                                           skip_verify=skip_verify,
                                           saltenv=__env__)

    This will return a dictionary containing the state's return data, including
    a ``result`` key which will state whether or not the state was successful.
    Note that this will not catch exceptions, so it is best used within a
    try/except.

    Once this state has been run from within another state or remote-execution
    module, the actual location of the cached file can be obtained using
    :py:func:`cp.is_cached <salt.modules.cp.is_cached>`:

    .. code-block:: python

        cached = __salt__['cp.is_cached'](source_match, saltenv=__env__)

    This function will return the cached path of the file, or an empty string
    if the file is not present in the minion cache.
    r5Fr��-Only URLs or local file paths are valid inputr�zoUnable to verify upstream hash of source file {}, please set source_hash or set skip_verify or use_etag to Truer�)rrr�rkNz�Failed to get source hash from {}. This may be a bug. If this error persists, please report it and set skip_verify to True to work around it.r:r�rjr�r�r�zFile already cached: z)Hashes don't match.
File will be cached: z$No hash found. File will be cached: zFile will be cached: r�r�Tz0File {} is present on the minion and has hash {}zZFile {} is present on the minion, but the hash ({}) does not match the specified hash ({})r�z is present on the minionz is not present on the minionz)File is already cached to {} with hash {}z
cp.cache_file)rkrr�z9Failed to cache {}, check minion log for more informationr�r�zNFile is cached to {}, but the hash ({}) does not match the specified hash ({})zFile is cached to )r�ror�rqr�r&r'r(Z
REMOTE_PROTOSrrnr�r;r	r�r �LOCAL_PROTOSrrr�rZr�r�r)rRrr�r�rkr�r.�parsedr�r��
local_copyr�rSZ
local_hashZpre_hashZ	post_hashrrr�cached�"s�_������
�
����
����
�����
������
�
�
��r�c
Csid|dd�}ztj�|�}Wntyd|d<|YSw|jtjjjvr?t	j
�t	j
�|j
��}d|d<d�
|�|d<|Std	||d
�}|r�zt	�|�Wntym}zd|�d|��|d<WYd
}~|Sd
}~wwd|d<d|dd<|�d�|d<|Sd|d<|�d�|d<|S)a(
    .. versionadded:: 2017.7.3

    Ensures that a file is not present in the minion's cache, deleting it
    if found. This state is primarily invoked by other states to ensure
    that a fresh copy is fetched.

    name
        The URL of the file to be removed from cache. To remove a file from
        cache in an environment other than ``base``, either use the ``saltenv``
        argument or include the saltenv in the URL (e.g.
        ``salt://path/to/file.conf?saltenv=dev``).

        .. note::
            A list of URLs is not supported, this must be a single URL. If a
            local file is passed here, the state will take no action.

    saltenv
        Used to specify the environment from which to download a file from the
        Salt fileserver (i.e. those with ``salt://`` URL).
    r5Fr�r�r�Tr�z(File {} is a local path, no action takenr�rjzFailed to delete r�Nr�r z was deletedz is not cached)r�ror�rqr�r&r'r(r�rrr�rZrr;rZ)rRrkr.r�rSr�r�rrr�
not_cached�#s<�����r�cKs�|�dd�}ddg}||vrq|�d�rjd}i}|�di�}gd�}|�d	|�|d	<|dkrE|�d
d�|d
<|�dd�|d<|�d
g�|d
<d|�d|��}|||i|�dd�|�dd�|d�}	tddi|	��}
|
S|iddd�S|id�|�dd�S)z�
    Create a beacon to monitor a file based on a beacon state argument.

    .. note::
        This state exists to support special handling of the ``beacon``
        state argument for supported state functions. It should not be called directly.

    �sfunNr�r�ZbeaconZinotifyZbeacon_data)rur+�modify�maskZauto_addTr�r�Zbeacon_r��interval�<�coalesceF)rRr(r�r��
beacon_modulezbeacon.presentzNot adding beacon.r�z4file.{} does not work with the beacon state functionr)ryr�Z
__states__r)rRr}r�Zsupported_funcsr�rrZ_beacon_dataZdefault_maskZbeacon_nameZ
beacon_kwargsr.rrr�
mod_beacon$sD	


����r�cCs<tj�|�}|iddd�}|rd}tj�|�r�tdr/d|d<||dd<d	|�d
�|d<|Std||d|d
�}|�d�}|rj|rY|drYd|��|d<t|d�|dd<|S|shd|��|d<||dd<|S|r�|dr�d�|�|d<t|d�|dd<|S||d<||d<d|��|d<|Sd	|�d�|d<|S)a�
    .. versionadded:: 3006.0

    Ensure that the named directory is absent. If it exists and is empty, it
    will be deleted. An entire directory tree can be pruned of empty
    directories as well, by using the ``recurse`` option.

    name
        The directory which should be deleted if empty.

    recurse
        If set to ``True``, this option will recursive deletion of empty
        directories. This is useful if nested paths are all empty, and would
        be the only items preventing removal of the named root directory.

    ignore_errors
        If set to ``True``, any errors encountered while attempting to delete a
        directory are ignored. This **AUTOMATICALLY ENABLES** the ``recurse``
        option since it's not terribly useful to ignore errors on the removal of
        a single directory. Useful for pruning only the empty directories in a
        tree which contains non-empty directories as well.

    older_than
        When ``older_than`` is set to a number, it is used to determine the
        **number of days** which must have passed since the last modification
        timestamp before a directory will be allowed to be removed. Setting
        the value to 0 is equivalent to leaving it at the default of ``None``.
    r5Tr�r:Nr�r�r rwrur�z
file.rmdir)r��verbose�
older_thanz,Recursively removed empty directories under rxz.Recursively removed empty directories under {}ryrz)	rrrZr�r r;ryrr)rRr��
ignore_errorsr�r.rr�rrr�pruned=$sF�
��r�rP)FNNNF)NNFNNFFNNFF)NNNNN)F)NNNN)NrNN)NNNNNNN)FFNNN)
FNFNNNNNNNFFF)rNFrNFFr�r|NT)&Nr5NTNNNNNFNNTNr5TTNr5r5NNTriNr�TTNFNNNNTFTF)NNNNNNFFNNFFNTFNNNTF)TFNNNNNNNNTNFr5NNNFFNNNTr
)NNNNNNTFFTFNNN)
rr7r�FFNr8TFF)
NNNrCFFFTFr�NFF)rUrVNNrNNNNr5FFr8TNNN)rjr8F)rjr8)
NFNNrNNNNT)
NFNNrNNNNN)NNNFNNNr5NNN)NNF)FFFNNNNF)FF)NNNNNr5FTTFNr�NNN)rrNNr�)NNr�r�)NNNNFNFN)r5NFr�F)r�)FFN)��__doc__rrvr�r�rrvrDr�r�r�r'r�r��urllib.parser��collectionsr�collections.abcrrrrrZsalt.loaderr&Zsalt.payloadZsalt.utils.dataZsalt.utils.dateutilsZsalt.utils.dictdifferZsalt.utils.dictupdateZsalt.utils.filesZsalt.utils.hashutilsZsalt.utils.pathZsalt.utils.platformZsalt.utils.stringutilsZsalt.utils.templatesZsalt.utils.urlZsalt.utils.versionsZsalt.exceptionsr	Zsalt.serializersr
Z
salt.staterrZsalt.utils.odictrr'r�r�Zsalt.utils.win_daclZsalt.utils.win_functionsZsalt.utils.winapir�Zwin32com.clientr6�	getLoggerrMr<Z
COMMENT_REGEX�objectr�Z__func_alias__rr!r2r4rCrKrOr�r�r�r�r�r�r�r�r�r�r�rrrrrrrrr-r.r/r0rGrJrbrsr{r�r�r�r�r�r�r�r�r�r*r�rMrTrir�rRrxr�r�r�r
r�r�r�r�r�rsr�r�r�r�r�rrrr�<module>s\
�	

�
N
�|
�
 #1
-
�
�
8G
�9
�D
�7
�x
�D

�W
�
jM
�h
�M
�t
�
=
ob
�
�9
�
0J
�
yUg
�

4
�x
�
�
78