
    VhO                    N   d dl mZ dZdZdZd dlZd dlZd dlZd dlZd dl	Z	d dl
mZmZ d dlmZmZ d dlmZ d d	lmZmZ da G d
 de      Z G d de      Z G d de      Zd Zd Zd Zd Zd Zd Z d#dZ!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)d Z*d  Z+d! Z,e-d"k(  r e,        yy)$    )annotationsa  
---
module: file
version_added: historical
short_description: Manage files and file properties
extends_documentation_fragment: [files, action_common_attributes]
description:
- Set attributes of files, directories, or symlinks and their targets.
- Alternatively, remove files, symlinks or directories.
- Many other modules support the same options as the M(ansible.builtin.file) module - including M(ansible.builtin.copy),
  M(ansible.builtin.template), and M(ansible.builtin.assemble).
- For Windows targets, use the M(ansible.windows.win_file) module instead.
options:
  path:
    description:
    - Path to the file being managed.
    type: path
    required: yes
    aliases: [ dest, name ]
  state:
    description:
    - If V(absent), directories will be recursively deleted, and files or symlinks will
      be unlinked. In the case of a directory, if C(diff) is declared, you will see the files and folders deleted listed
      under C(path_contents). Note that V(absent) will not cause M(ansible.builtin.file) to fail if the O(path) does
      not exist as the state did not change.
    - If V(directory), all intermediate subdirectories will be created if they
      do not exist. Since Ansible 1.7 they will be created with the supplied permissions.
    - If V(file), with no other options, returns the current state of C(path).
    - If V(file), even with other options (such as O(mode)), the file will be modified if it exists but will NOT be created if it does not exist.
      Set to V(touch) or use the M(ansible.builtin.copy) or M(ansible.builtin.template) module if you want to create the file if it does not exist.
    - If V(hard), the hard link will be created or changed.
    - If V(link), the symbolic link will be created or changed.
    - If V(touch) (new in 1.4), an empty file will be created if the file does not
      exist, while an existing file or directory will receive updated file access and
      modification times (similar to the way V(touch) works from the command line).
    - Default is the current state of the file if it exists, V(directory) if O(recurse=yes), or V(file) otherwise.
    type: str
    choices: [ absent, directory, file, hard, link, touch ]
  src:
    description:
    - Path of the file to link to.
    - This applies only to O(state=link) and O(state=hard).
    - For O(state=link), this will also accept a non-existing path.
    - Relative paths are relative to the file being created (O(path)) which is how
      the Unix command C(ln -s SRC DEST) treats relative paths.
    type: path
  recurse:
    description:
    - Recursively set the specified file attributes on directory contents.
    - This applies only when O(state) is set to V(directory).
    type: bool
    default: no
    version_added: '1.1'
  force:
    description:
    - >
      Force the creation of the links in two cases: if the link type is symbolic and the source file does
      not exist (but will appear later); the destination exists and is a file (so, we need to unlink the
      O(path) file and create a link to the O(src) file in place of it).
    type: bool
    default: no
  follow:
    description:
    - This flag indicates that filesystem links, if they exist, should be followed.
    - O(follow=yes) and O(state=link) can modify O(src) when combined with parameters such as O(mode).
    - Previous to Ansible 2.5, this was V(false) by default.
    - While creating a symlink with a non-existent destination, set O(follow=false) to avoid a warning message related to permission issues.
      The warning message is added to notify the user that we can not set permissions to the non-existent destination.
    type: bool
    default: yes
    version_added: '1.8'
  modification_time:
    description:
    - This parameter indicates the time the file's modification time should be set to.
    - Should be V(preserve) when no modification is required, C(YYYYMMDDHHMM.SS) when using default time format, or V(now).
    - Default is None meaning that V(preserve) is the default for O(state=[file,directory,link,hard]) and V(now) is default for O(state=touch).
    type: str
    version_added: "2.7"
  modification_time_format:
    description:
    - When used with O(modification_time), indicates the time format that must be used.
    - Based on default Python format (see time.strftime doc).
    type: str
    default: "%Y%m%d%H%M.%S"
    version_added: '2.7'
  access_time:
    description:
    - This parameter indicates the time the file's access time should be set to.
    - Should be V(preserve) when no modification is required, C(YYYYMMDDHHMM.SS) when using default time format, or V(now).
    - Default is V(None) meaning that V(preserve) is the default for O(state=[file,directory,link,hard]) and V(now) is default for O(state=touch).
    type: str
    version_added: '2.7'
  access_time_format:
    description:
    - When used with O(access_time), indicates the time format that must be used.
    - Based on default Python format (see time.strftime doc).
    type: str
    default: "%Y%m%d%H%M.%S"
    version_added: '2.7'
seealso:
- module: ansible.builtin.assemble
- module: ansible.builtin.copy
- module: ansible.builtin.stat
- module: ansible.builtin.template
- module: ansible.windows.win_file
attributes:
    check_mode:
        support: full
    diff_mode:
        details: permissions and ownership will be shown but file contents on absent/touch will not.
        support: partial
    platform:
        platforms: posix
author:
- Ansible Core Team
- Michael DeHaan
a$  
- name: Change file ownership, group and permissions
  ansible.builtin.file:
    path: /etc/foo.conf
    owner: foo
    group: foo
    mode: '0644'

- name: Give insecure permissions to an existing file
  ansible.builtin.file:
    path: /work
    owner: root
    group: root
    mode: '1777'

- name: Create a symbolic link
  ansible.builtin.file:
    src: /file/to/link/to
    dest: /path/to/symlink
    owner: foo
    group: foo
    state: link

- name: Create two hard links
  ansible.builtin.file:
    src: '/tmp/{{ item.src }}'
    dest: '{{ item.dest }}'
    state: hard
  loop:
    - { src: x, dest: y }
    - { src: z, dest: k }

- name: Touch a file, using symbolic modes to set the permissions (equivalent to 0644)
  ansible.builtin.file:
    path: /etc/foo.conf
    state: touch
    mode: u=rw,g=r,o=r

- name: Touch the same file, but add/remove some permissions
  ansible.builtin.file:
    path: /etc/foo.conf
    state: touch
    mode: u+rw,g-wx,o-rwx

- name: Touch again the same file, but do not change times this makes the task idempotent
  ansible.builtin.file:
    path: /etc/foo.conf
    state: touch
    mode: u+rw,g-wx,o-rwx
    modification_time: preserve
    access_time: preserve

- name: Create a directory if it does not exist
  ansible.builtin.file:
    path: /etc/some_directory
    state: directory
    mode: '0755'

- name: Update modification and access time of given file
  ansible.builtin.file:
    path: /etc/some_file
    state: file
    modification_time: now
    access_time: now

- name: Set access time based on seconds from epoch value
  ansible.builtin.file:
    path: /etc/another_file
    state: file
    access_time: '{{ "%Y%m%d%H%M.%S" | strftime(stat_var.stat.atime) }}'

- name: Recursively change ownership of a directory
  ansible.builtin.file:
    path: /etc/foo
    state: directory
    recurse: yes
    owner: foo
    group: foo

- name: Remove file (delete file)
  ansible.builtin.file:
    path: /etc/foo.txt
    state: absent

- name: Recursively remove directory
  ansible.builtin.file:
    path: /etc/foo
    state: absent

a}  
dest:
    description: Destination file/path, equal to the value passed to O(path).
    returned: O(state=touch), O(state=hard), O(state=link)
    type: str
    sample: /path/to/file.txt
path:
    description: Destination file/path, equal to the value passed to O(path).
    returned: O(state=absent), O(state=directory), O(state=file)
    type: str
    sample: /path/to/file.txt
N)getpwnamgetpwuid)getgrnamgetgrgid)AnsibleModule)to_bytes	to_nativec                      e Zd Zd Zd Zy)AnsibleModuleErrorc                    || _         y Nresults)selfr   s     D/home/dcms/DCMS/lib/python3.12/site-packages/ansible/modules/file.py__init__zAnsibleModuleError.__init__   s	        c                8    dj                  | j                        S )NzAnsibleModuleError(results={0}))formatr   )r   s    r   __repr__zAnsibleModuleError.__repr__   s    077EEr   N)__name__
__module____qualname__r   r    r   r   r   r      s    Fr   r   c                      e Zd Zy)ParameterErrorN)r   r   r   r   r   r   r   r      s    r   r   c                      e Zd Zd Zy)Sentinelc                    | S r   r   )clsargskwargss      r   __new__zSentinel.__new__  s    
r   N)r   r   r   r$   r   r   r   r   r     s    r   r   c                    t        | t              r t        j                  di |j                   y t        j                  | ||       y )Nr   )
issubclassr   module	fail_jsonr   sys__excepthook__)exc_type	exc_valuetbs      r   _ansible_excepthookr.   	  s6    (./-9,,-8Y3r   c                   | d   dvrt         j                  j                  t        | d   d            r\d}| d   r| d   }n'| d   r"t         j                  j	                  | d         }|r&t         j                  j                  | d   |      | d<   t        t        | d   d            }| d   |d	k7  r|| d<   n| d
   rd| d<   nd| d<   | d
   r| d   dk7  rt        d| d   d      | d   r| d   dvrt        d| d   d      yy)z0Additional parameter validation and reformattingstate)linkabsentpathsurrogate_or_stricterrorsN_original_basenamesrcr2   recurse	directoryfilez/recurse option requires state to be 'directory'msgr3   r   )r1   hardz0src option requires state to be 'link' or 'hard')osr3   isdirr	   basenamejoin	get_stater   )paramsrA   
prev_states      r   additional_parameter_handlingrF     s;    	w11bggmmHVTZ^dyDz6{&'23HE]ww''u6HWW\\&.(CF6N 8F6N;PQRJg!(F7OI)F7O$F7O iVG_;-^.4Vn&> ? 	? e}0@@-_.4Vn&> ? 	? A}r   c                   t        | d      }	 t        j                  j                  |      rdt        j                  j	                  |      ryt        j                  j                  |      ryt        j                  |      j                  dkD  ryyy# t        $ r(}|j                  t        j                  k(  rY d	}~y d	}~ww xY w)
z Find out current state r4   r5   r1   r:      r>   r;   r2   N)r	   r?   r3   lexistsislinkr@   statst_nlinkOSErrorerrnoENOENT)r3   b_pathes      r   rC   rC   ?  s     d#89F77??6"ww~~f%v&"))A-  77ell"	s)   >B B ."B 	CB?>B??Cc           
     :   d}	 t        j                  |       D ]  \  }}}||z   D ]  }	t         j                  j                  ||	      }
t         j                  j	                  |
      sO|j                         }t        |
d      |d<   |t        j                  ||d      z  }|t        |d   ||      z  }|j                         }t        |
d      |d<   |t        j                  ||d      z  }|t        |d   ||      z  }|st         j                  j                  |t        j                  |
            }
t         j                  j                  |
      s7t         j                  j                  |
      r|t        |
||||      z  }|j                         }t        |
d      |d<   |t        j                  ||d      z  }|t        |d   ||      z  }  	 |S # t        $ r,}t        ddt        |       dt        |      d	i
      d }~ww xY w)NFr4   r5   r3   expandr=   z(Could not recursively set attributes on z. Original error was: ''r   )r?   walkr3   rB   rJ   copyr
   r'   set_fs_attributes_if_differentupdate_timestamp_for_filereadlinkexistsr@   recursive_set_attributesRuntimeErrorr   )rP   follow	file_argsmtimeatimechangedb_rootb_dirsb_filesb_fsobjb_fsnametmp_file_argsrQ   s                r   r\   r\   X  s    G#
')wwv 	f#FFG!G+ f77<<8ww~~h/$-NN$4M,5hG\,]M&)vDD]T[diDjjG8v9NPUW\]]G %.NN$4M,5hG\,]M&)vDD]T[diDjjG8v9NPUW\]]G#%77<<H8M#N77>>(3!ww}}X6 '+CHfV_afhm+n n -6NN,<M4=hOd4eM&1#v'L'L]\clq'L'rrG#'@vAVX]_d'eeG7f	fH N  
 !fopvfw  zC  DE  zF  G  H
 	

s&   DG% 	AG% BG% %	H.'HHc                   d| id| id}||k7  r||d   d<   ||d   d<   |dk(  r|dk(  rg g d}t        | d	
      }t        j                  |      D ]|  \  }}}|D ]6  }	t        j                  j	                  ||	      }
|d   j                  |
       8 |D ]6  }t        j                  j	                  ||      }|d   j                  |       8 ~ ||d   d<   |S )Nr3   )beforeafterrj   r0   rk   r2   r:   )directoriesfilesr4   r5   rl   rm   path_content)r	   r?   rV   r3   rB   append)r3   r0   rE   diffwalklistrP   	base_pathsub_foldersrm   folder
folderpathfilenamefilepaths                r   initial_diffrx     s	   tnd^D U",Xw!&WgH{!:!H d+@AF13 7-	;) ?F!#i!@J]+22:>? !& 7H!ww||Ix@HW%,,X677 .6DN>*Kr   c                    | dk(  ry | dk(  rt         S 	 t        j                  | |      }t        j                  |      }|S # t        t
        f$ r'}t        dd| d|dt        |d      i	      d }~ww xY w)
Npreservenowr=   z)Error while obtaining timestamp for time z using format z: 
simplerepr	nonstringr   )r   timestrptimemktime
ValueErrorOverflowErrorr   r
   )formatted_timetime_formatstructstruct_timerQ   s        r   get_timestamp_for_timer     s    #	5	 	v]]>;?F++f-K
 	 M* 	v$e1?iXYeqNr6t .u v v	vs   +A   A6"A11A6c                :   t        | d      }	 |t        u r_|t        u rWt        j                         x}}t        j                  |      j
                  }t        j                  |      j                  }d }n||yt        j                  |      j
                  }t        j                  |      j                  }||}n|t        u rt        j                         }||}n|t        u rt        j                         }||k(  r||k(  ry||f}t        j                  st        j                  ||       |<d|vri |d<   d|vri |d<   ||k7  r||d   d<   ||d   d<   ||k7  r||d   d<   ||d   d<   y# t        $ r"}t        dt        |d	
      z  | d      d }~ww xY w)Nr4   r5   Frj   rk   r`   ra   z4Error while updating modification or access time: %sr|   r}   r<   r   T)r	   r   r   r?   rK   st_mtimest_atimer'   
check_modeutimerM   r   r
   )	r3   r`   ra   rp   rP   previous_mtimeprevious_atimeset_timerQ   s	            r   rY   rY     s   d#89F4a H(!2 !IIK'EEWWV_55NWWV_55NH }WWV_55NWWV_55N}&("		}&("		 &5N+Bu~H  HHVX&t#!#Xd" "W&*8Xw').Wg&&*8Xw').Wg&   a 1g,5a<,P2QZ^*` a 	aas&   A+E/ ;B
E/ A(E/ /	F8FFc                $    |dv r| y|dk(  r| y| S )N)r;   r>   r:   r1   rz   touchr{   r   )	parameterr0   s     r   )keep_backward_compatibility_on_timestampsr     s)    55):K	'	i/r   c                    t        | d      }d}	 t        |d      5 }|j                  d      }ddd       dv rd}|S # 1 sw Y   xY w# t        $ r Y |S w xY w)	z2Take a guess as to whether a file is a binary filer4   r5   Frbi    N    T)r	   openread	Exception)r3   rP   appears_binaryfheads        r   execute_diff_peekr     st    d#89FN"&$ 	 166$<D	  d?!N	  	  
 s%   A ?A AA 	AAc                2   t        | d      }t        |      }i }|dk7  rit        | d|      }t        j                  s4|dk(  r	 t        j                  |d       n	 t        j                  |       |j                  | d|dd       |S |j                  | ddd       |S # t        $ r}t        ddt        |      z  i	      d }~ww xY w# t        $ rB}|j                  t        j                  k7  rt        d
t        |      z  | d	      Y d }~d }~ww xY w)Nr4   r5   r2   r:   F)ignore_errorsr=   zrmtree failed: %sr   zunlinking failed: %s r<   T)r3   rb   rp   r0   )r3   rb   r0   )r	   rC   rx   r'   r   shutilrmtreer   r   r
   r?   unlinkrM   rN   rO   update)r3   rP   rE   resultrp   rQ   s         r   ensure_absentr     s#   d#89F6"JFXD(J7  [(bMM&>IIIf% 	tdXVW M 	tIJM ! b,e=PS\]^S_=_5`aab
  Iww%,,.0AX[def[gAgBF:H I I /Is0   B  C  	C)CC	D8DDc                   t        | d      }t        |      }d}d| i}t        |d   |d         }t        |d   |d         }|d	k(  r4t        j                  rd
|d<   |S 	 t        |d      j                          d
}t        | d|      }
t        j                  t        j                        }	 t        j                  |||
d      }|t        |d   |||
      z  }||d<   |
|d<   |S # t        t        f$ r"}	t        dt        |	d      z  | d      d }	~	ww xY w# t         $ r,}	|	j"                  r|d	k(  rt%        j&                  |        d }	~	ww xY w)Nr4   r5   Fdestmodification_timemodification_time_formataccess_timeaccess_time_formatr2   Trb   wbz!Error, could not touch target: %sr|   r}   r<   r   r   rS   r3   rp   )r	   rC   r   r'   r   r   closerM   IOErrorr   r
   rx   load_file_common_argumentsrD   rX   rY   
SystemExitcoder?   remove)r3   r^   
timestampsrP   rE   rb   r   r`   ra   rQ   rp   r_   s               r   execute_touchr   %  s   d#89F6"JGd^F":.A#BJOiDjkE":m#<jI]>^_E X  $F9M	=$$&G gz2D11&--@I
77	7DY^7_,Yv->udSS  F9F6NM+ ! 	=$5X09!|0T6U6:.< = =	=  66 X%		&!s0    C& --D &D5DD	E#'E

Ec                   t        | d      }t        |      }t        j                  t        j                        }t        |d   |d         }t        |d   |d         }|dk7  rC|rA|dk(  r<t        j                  j                  |      }t        |d	      } t        |      }| |d
<   |dvrt        d| d|d| |d      t        | d|      }t        j                  |d|d      }	|	t        |d
   |||      z  }	| |	|dS )Nr4   r5   r   r   r   r   r;   r1   strictr3   r;   r>   zfile (z) is z, cannot continue)r=   r3   r0   r   FrS   r3   rb   rp   )r	   rC   r'   r   rD   r   r?   r3   realpathr
   r   rx   rX   rY   )
r3   r^   r   rP   rE   r_   r`   ra   rp   rb   s
             r   ensure_file_attributesr   P  s    d#89F6"J11&--@I":.A#BJOiDjkE":m#<jI]>^_EVjF*WW%%f-FVH5D"6*J $If)) W[]g1h26*M N 	N fj1D33IudSX3YG(6):E5$OOGWd;;r   c           	     R   t        | d      }t        |      }t        j                  t        j                        }t        |d   |d         }t        |d   |d         }|rA|dk(  r<t        j                  j                  |      }t        |d      } | |d	<   t        |      }d
}	t        | d|      }
|dk(  rt        j                  r| d|
dS d}	 | j                  d      j                  d      D ]  }dj                  ||g      }t        j                  j                  |       s|j!                  d      }t        |d      }t        j                  j#                  |      rs	 t        j$                  |       d}	|j/                         }||d	<   t        j1                  ||	|
d
      }	|	t3        |d	   |||
      z  }	 	 | |	|
dS |dk7  rt7        | d|| d      t        j1                  ||	|
d
      }	|	t3        |d	   |||
      z  }	|r|	t9        |||||      z  }	| |	|
dS # t&        $ rG}|j(                  t(        j*                  k(  rt        j                  j-                  |      s Y d }~d }~ww xY w# t4        $ r#}t7        d|dt        |      | d      d }~ww xY w)Nr4   r5   r   r   r   r   r1   r   r3   Fr:   r2   Tr    /rS   zThere was an issue creating z as requested: r<   r   z already exists as a )r	   rC   r'   r   rD   r   r?   r3   r   r
   rx   r   stripsplitrB   isabslstripr[   mkdirrM   rN   EEXISTr@   rW   rX   rY   r   r   r\   )r3   r^   r9   r   rP   rE   r_   r`   ra   rb   rp   curpathdirname	b_curpathexrh   rQ   s                    r   ensure_directoryr   j  s   d#89F6"J11&--@I":.A#BJOiDjkE":m#<jI]>^_E *&!!&)1 	&v&
Gk:6DX T4@@	=  ::c?005 `((GW#56ww}}T*%nnS1G$W5JK	ww~~i0"+"& %.NN$4M,3M&)$CCMSZ\`inCoG869JESXZ^__G%`. $??	{	" PTV`1a26*8 9 	9 33IwUZ3[G(6):E5$OOG+FFIueTTWd;;; # " !#ELL 8RWW]]9=U! >V"  	=$>EyQR|6U6:.< = =	=sD   BI: %H'<AI: '	I70=I2-I: 2I77I: :	J&J!!J&c           
     f	   t        | d      }t        |d      }t        |      }t        |d   |d         }t        |d   |d         }	|N|rLt        j                  j                  |      r-t        t        j                  |      d      }t        |d      }t        j                  j                  |      s"t        j                  j                  |      r| }
n,t        j                  j                  |      }t        |d      }
|d }n t        j                  j                  |
|      }t        |d      }|s4|2t        j                  j                  |      st        d|z  | |d	
      |dk(  r>|st        d|d| | d
      t        j                  |      r-t        d| z  | d
      |dv r|st        d|d| | d
      t        | d|      }d}|dv r|t        ddi
      d}nO|dk(  r:|Ht        j                  |      }||k7  r.t        |d      |d   d<   ||d   d<   d}nt        d| |d
      |rt        j                   s|dk7  rt        t        j                  j"                        j                  t        j                  j                  |      t        dt        j$                         dt'        j&                         d      g      }	 |dk(  rt        j(                  |       t        j*                  ||       t        j,                  ||       n	 t        j*                  ||       t        j                   r&t        j                  j                  |      s| |||d#S t        j3                  t        j4                        }|rWt        j                  j                  |      r8t        j                  j                  |d$         st        j7                  d%       n-t        j9                  |||d&      }|t;        |d$   ||	|      z  }| |||d#S # t.        $ rV}t        j                  j                  |      rt        j0                  |       t        dt        |d !      z  | d
      d }~ww xY w# t.        $ r"}t        d"t        |d !      z  | d
      d }~ww xY w)'Nr4   r5   r   r   r   r   r   zRsrc file does not exist, use "force=yes" if you really want to create the link: %s)r=   r3   r8   r   r:   zrefusing to convert from z to symlink for r<   z5the directory %s is not empty, refusing to convert itr   r1   F)r>   r;   r:   r2   r=   z)src is required for creating new symlinksTrj   r8   rk   unexpected position reachedr=   r   r8   r2   ..tmpError while replacing: %sr|   r}   Error while linking: %sr   r8   rb   rp   r3   zgCannot set fs attributes on a non-existent symlink target. follow should be set to False to avoid this.rS   )r	   rC   r   r?   r3   r[   r
   rZ   rJ   r@   r   rB   r   listdirrx   r'   r   sepgetpidr   rmdirsymlinkrenamerM   r   r   rD   warnrX   rY   )r3   r8   r^   forcer   rP   b_srcrE   r`   ra   relpath	b_relpathabsrcb_absrcrp   rb   	b_old_src	b_tmppathrQ   r_   s                       r   ensure_symlinkr     sf   d#89FS!67E6"J":.A#BJOiDjkE":m#<jI]>^_E {bggnnV,BKK/ACS)>?E77>>&!bggmmF&;GGOOF+	Ih7 {Wc*u%:;GS_RWW^^G-D  2WY^2_26s*D E 	E [ $8BD6J6:.< = = ZZ$ 6CEI6J6:.< = = 
'	' 4>2F26*8 9 	9 fj1DG<<;$e5`-abb	v	?F+IE!(1)H(MXu%'*We$ 1NX\eh)ijjv((! -22((299;PTPYPYP[3\*]^I
A,HHV$

5),		)V,A

5&) !7SWdKK 11&--@I
 "''..(	&@Q1R 3 	4 77	7DY^7_,Yv->udSS$GGC  A77>>),IIi((9T;DQR^;_:`:>2@ A AA  A(9R;DQR^;_:`:>2@ A AAs2   AP# 'R #	R,AQ==R	R0R++R0c           
     	   t        | d      }t        |d      }t        |      }t        j                  t        j                        }t        |d   |d         }	t        |d   |d         }
|dk7  r|t        dd	i
      |/t        j                  j                  |      st        d| |d
      t        | d|      }d}|dk(  rd}nA|dk(  r:t        j                  |      }||k7  r!t        |d      |d   d<   ||d   d<   d}n|dk(  rU|t        j                  |      j                  t        j                  |      j                  k(  sd}|st        d| |d
      |dk(  rd}|st        d|z  | |d
      |dk(  rwd}t        j                  j                  |      rft        j                  |      j                  t        j                  |      j                  k(  r| ddS |s t        d| |d
      t        d| |d
      |rPt        j                  s?|dk7  rt        t        j                  j                         j#                  t        j                  j%                  |      t        dt        j&                         dt)        j(                         d      g      }	 |dk(  r5t        j                  j                  |      r	 t        j*                  |       t        j2                  ||       t        j4                  ||       nM	 |r4t        j                  j7                  |      rt        j                  |      }t        j2                  ||       t        j                  r&t        j                  j                  |      s| |||d#S t        j9                  |||d$      }|t;        |d%   |	|
|      z  }| |||d#S # t,        $ r)}|j.                  t.        j0                  k7  r Y d }~d }~ww xY w# t,        $ rV}t        j                  j                  |      rt        j*                  |       t        dt        |d       z  | d!
      d }~ww xY w# t,        $ r"}t        d"t        |d       z  | d!
      d }~ww xY w)&Nr4   r5   r   r   r   r   r>   r=   z*src is required for creating new hardlinksr   zsrc does not existr   Fr2   Tr1   r   rj   r8   rk   z6Cannot link, different hard link exists at destinationr;   z%Cannot link, %s exists at destinationr:   )r3   rb   z6Cannot link: different hard link exists at destinationr   r   r   r   r|   r}   r<   r   r   rS   r3   )r	   rC   r'   r   rD   r   r   r?   r3   r[   rx   rZ   r
   rK   st_inor   r   rB   r   r   r   r   rM   rN   rO   r1   r   rJ   rX   rY   )r3   r8   r^   r   r   rP   r   rE   r_   r`   ra   rp   rb   r   r   rQ   s                   r   ensure_hardlinkr      s4   d#89FS!67E6"J11&--@I":.A#BJOiDjkE":m#<jI]>^_E V %1])^__ rww~~e4 1Et\_)`aafj1DGX	v	KK'	$-i$IDN5!#&DM% G	v	?2776?#9#9RWWU^=R=R#RG(9q:>s2L M M	v	$5\_i5i6:3.H I I	{	"77>>&!wwv%%)>)>> $77(9q:>s2L M M !1NX\eh)ijjv((! -22((299;PTPYPYP[3\*]^IA,ww~~f-&IIf- y)		)V,AbggnnU3KK.Ev& !7SWdKK33IwUZ3[G(6):E5$OOG$GG7  ' & ww%,,6 %  7&
  A77>>),IIi((9T;DQR^;_:`:>2@ A AA  A(9R;DQR^;_:`:>2@ A AAs\   +$O> O	 %,O> AQ  		O;O60O> 6O;;O> >	QAQQ 	R)RRc                    	 t        |      }	 t        |      j                   y # t        $ r | j	                  d|z         Y y w xY w# t
        $ r= 	 t        |      j                   Y y # t        $ r | j	                  d|z         Y Y y w xY ww xY w)NzMfailed to look up user with uid %s. Create user up to this point in real playzDfailed to look up user %s. Create user up to this point in real play)intr   pw_nameKeyErrorr   r   r   pw_uid)r'   owneruids      r   check_owner_existsr   |  s    
h%j	oSM!! 	oKKgjmmn	o h	hUO"" 	hKK^affg	hhD   A $ AA AA 	BA((B	BB		Bc                    	 t        |      }	 t        |      j                   y # t        $ r | j	                  d|z         Y y w xY w# t
        $ r= 	 t        |      j                   Y y # t        $ r | j	                  d|z         Y Y y w xY ww xY w)NzOfailed to look up group with gid %s. Create group up to this point in real playzFfailed to look up group %s. Create group up to this point in real play)r   r   gr_namer   r   r   r   gr_gid)r'   groupgids      r   check_group_existsr     s    
j%j	qSM!! 	qKKiloop	q j	jUO"" 	jKK`chhi	jjr   c                    t        t        t        dg d      t        ddddg      t        d	      t        d
d      t        d
d      t        d
d      t        d
	      t        d	      t        d	      t        dd      t        d	      t        dd            dd      at        t        _        t        t        j                         t        j                  } | d   }| d   }| d   }| d   }| d   }| d   }t        j                  rX|dk7  rSt        j                  t        j                        }|d   rt        t        |d          |d   rt        t        |d          i }t        | d   |      |d<   | d   |d<   t        | d   |      |d<   | d   |d<   | d   .t        t        |d            }	t        j                  |d|	       |d k(  rt!        |||      }
na|d!k(  rt#        ||||      }
nM|d"k(  rt%        |||||      }
n8|d#k(  rt'        |||||      }
n#|d$k(  rt)        |||      }
n|dk(  rt+        |      }
t        j                  d%i 
 y )&Nstr)r2   r:   r;   r>   r1   r   )typechoicesr3   Tr   name)r   requiredaliases)r   boolF)r   defaultz%Y%m%d%H%M.%S)r0   r3   r7   r9   r   r^   
_diff_peekr8   r   r   r   r   )argument_specadd_file_common_argssupports_check_moder0   r9   r   r^   r8   r2   r   r   r   r   r   r   r   r4   r5   )r3   rb   r   r;   r:   r1   r>   r   r   )r   dictr'   r.   r)   
excepthookrF   rD   r   r   r   r   r   r   r	   	exit_jsonr   r   r   r   r   r   )rD   r0   r9   r   r^   r3   r8   r_   r   r   r   s              r   mainr    sa    E+cd6D66:JK#/fe4FE2VT2(&!".%)uo%N%(#H
 " !F( )CN!&--0]]F7OEYG7OEHF&>D
-CUh.55fmmD	Wvy'9:Wvy'9:J&OPVWjPkmr&sJ"#-34N-OJ)* I&Q^J_af gJ}'-.B'CJ#$ l'*8DAV+WXdE.Q'fjA	+	!$D	&c65*E	& sFE:F	'	tVZ8	(	t$
vr   __main__r   ).
__future__r   DOCUMENTATIONEXAMPLESRETURNrN   r?   r   r)   r   pwdr   r   grpr   r   ansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr	   r
   r'   r   r   r   objectr   r.   rF   rC   r\   rx   r   rY   r   r   r   r   r   r   r   r   r   r   r  r   r   r   r   <module>r     s    #tlYt
  	  
  " " 4 K 
F F	' 	v 
4+?\2(V@ 8v":(V<4D<NlH^YHxhjBJ zF r   