
    VhJ                        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 d dlmZ d Zd	 Zed
k(  r e        yy)    )annotationsa%  
---
module: stat
version_added: "1.3"
short_description: Retrieve file or file system status
description:
     - Retrieves facts for a file similar to the Linux/Unix C(stat) command.
     - For Windows targets, use the M(ansible.windows.win_stat) module instead.
options:
  path:
    description:
      - The full path of the file/object to get the facts of.
    type: path
    required: true
    aliases: [ dest, name ]
  follow:
    description:
      - Whether to follow symlinks.
    type: bool
    default: no
  get_checksum:
    description:
      - Whether to return a checksum of the file.
    type: bool
    default: yes
    version_added: "1.8"
  checksum_algorithm:
    description:
      - Algorithm to determine checksum of file.
      - Will throw an error if the host is unable to use specified algorithm.
      - The remote host has to support the hashing method specified, V(md5)
        can be unavailable if the host is FIPS-140 compliant.
    type: str
    choices: [ md5, sha1, sha224, sha256, sha384, sha512 ]
    default: sha1
    aliases: [ checksum, checksum_algo ]
    version_added: "2.0"
  get_mime:
    description:
      - Use file magic and return data about the nature of the file. This uses
        the C(file) utility found on most Linux/Unix systems.
      - This will add both RV(stat.mimetype) and RV(stat.charset) fields to the return, if possible.
      - In Ansible 2.3 this option changed from O(mime) to O(get_mime) and the default changed to V(true).
    type: bool
    default: yes
    aliases: [ mime, mime_type, mime-type ]
    version_added: "2.1"
  get_attributes:
    description:
      - Get file attributes using lsattr tool if present.
    type: bool
    default: yes
    aliases: [ attr, attributes ]
    version_added: "2.3"
extends_documentation_fragment:
  -  action_common_attributes
attributes:
    check_mode:
        support: full
    diff_mode:
        support: none
    platform:
        platforms: posix
seealso:
- module: ansible.builtin.file
- module: ansible.windows.win_stat
author: Bruce Pennypacker (@bpennypacker)
a  
# Obtain the stats of /etc/foo.conf, and check that the file still belongs
# to 'root'. Fail otherwise.
- name: Get stats of a file
  ansible.builtin.stat:
    path: /etc/foo.conf
  register: st
- name: Fail if the file does not belong to 'root'
  ansible.builtin.fail:
    msg: "Whoops! file ownership has changed"
  when: st.stat.pw_name != 'root'

# Determine if a path exists and is a symlink. Note that if the path does
# not exist, and we test sym.stat.islnk, it will fail with an error. So
# therefore, we must test whether it is defined.
# Run this to understand the structure, the skipped ones do not pass the
# check performed by 'when'
- name: Get stats of the FS object
  ansible.builtin.stat:
    path: /path/to/something
  register: sym

- name: Print a debug message
  ansible.builtin.debug:
    msg: "islnk isn't defined (path doesn't exist)"
  when: sym.stat.islnk is not defined

- name: Print a debug message
  ansible.builtin.debug:
    msg: "islnk is defined (path must exist)"
  when: sym.stat.islnk is defined

- name: Print a debug message
  ansible.builtin.debug:
    msg: "Path exists and is a symlink"
  when: sym.stat.islnk is defined and sym.stat.islnk

- name: Print a debug message
  ansible.builtin.debug:
    msg: "Path exists and isn't a symlink"
  when: sym.stat.islnk is defined and sym.stat.islnk == False


# Determine if a path exists and is a directory.  Note that we need to test
# both that p.stat.isdir actually exists, and also that it's set to true.
- name: Get stats of the FS object
  ansible.builtin.stat:
    path: /path/to/something
  register: p
- name: Print a debug message
  ansible.builtin.debug:
    msg: "Path exists and is a directory"
  when: p.stat.isdir is defined and p.stat.isdir

- name: Do not calculate the checksum
  ansible.builtin.stat:
    path: /path/to/myhugefile
    get_checksum: no

- name: Use sha256 to calculate the checksum
  ansible.builtin.stat:
    path: /path/to/something
    checksum_algorithm: sha256
a-%  
stat:
    description: Dictionary containing all the stat data, some platforms might add additional fields.
    returned: success
    type: dict
    contains:
        exists:
            description: If the destination path actually exists or not
            returned: success
            type: bool
            sample: True
        path:
            description: The full path of the file/object to get the facts of
            returned: success and if path exists
            type: str
            sample: '/path/to/file'
        mode:
            description: Unix permissions of the file in octal representation as a string
            returned: success, path exists and user can read stats
            type: str
            sample: 1755
        isdir:
            description: Tells you if the path is a directory
            returned: success, path exists and user can read stats
            type: bool
            sample: False
        ischr:
            description: Tells you if the path is a character device
            returned: success, path exists and user can read stats
            type: bool
            sample: False
        isblk:
            description: Tells you if the path is a block device
            returned: success, path exists and user can read stats
            type: bool
            sample: False
        isreg:
            description: Tells you if the path is a regular file
            returned: success, path exists and user can read stats
            type: bool
            sample: True
        isfifo:
            description: Tells you if the path is a named pipe
            returned: success, path exists and user can read stats
            type: bool
            sample: False
        islnk:
            description: Tells you if the path is a symbolic link
            returned: success, path exists and user can read stats
            type: bool
            sample: False
        issock:
            description: Tells you if the path is a unix domain socket
            returned: success, path exists and user can read stats
            type: bool
            sample: False
        uid:
            description: Numeric id representing the file owner
            returned: success, path exists and user can read stats
            type: int
            sample: 1003
        gid:
            description: Numeric id representing the group of the owner
            returned: success, path exists and user can read stats
            type: int
            sample: 1003
        size:
            description: Size in bytes for a plain file, amount of data for some special files
            returned: success, path exists and user can read stats
            type: int
            sample: 203
        inode:
            description: Inode number of the path
            returned: success, path exists and user can read stats
            type: int
            sample: 12758
        dev:
            description: Device the inode resides on
            returned: success, path exists and user can read stats
            type: int
            sample: 33
        nlink:
            description: Number of links to the inode (hard links)
            returned: success, path exists and user can read stats
            type: int
            sample: 1
        atime:
            description: Time of last access
            returned: success, path exists and user can read stats
            type: float
            sample: 1424348972.575
        mtime:
            description: Time of last modification
            returned: success, path exists and user can read stats
            type: float
            sample: 1424348972.575
        ctime:
            description: Time of last metadata update or creation (depends on OS)
            returned: success, path exists and user can read stats
            type: float
            sample: 1424348972.575
        wusr:
            description: Tells you if the owner has write permission
            returned: success, path exists and user can read stats
            type: bool
            sample: True
        rusr:
            description: Tells you if the owner has read permission
            returned: success, path exists and user can read stats
            type: bool
            sample: True
        xusr:
            description: Tells you if the owner has execute permission
            returned: success, path exists and user can read stats
            type: bool
            sample: True
        wgrp:
            description: Tells you if the owner's group has write permission
            returned: success, path exists and user can read stats
            type: bool
            sample: False
        rgrp:
            description: Tells you if the owner's group has read permission
            returned: success, path exists and user can read stats
            type: bool
            sample: True
        xgrp:
            description: Tells you if the owner's group has execute permission
            returned: success, path exists and user can read stats
            type: bool
            sample: True
        woth:
            description: Tells you if others have write permission
            returned: success, path exists and user can read stats
            type: bool
            sample: False
        roth:
            description: Tells you if others have read permission
            returned: success, path exists and user can read stats
            type: bool
            sample: True
        xoth:
            description: Tells you if others have execute permission
            returned: success, path exists and user can read stats
            type: bool
            sample: True
        isuid:
            description: Tells you if the invoking user's id matches the owner's id
            returned: success, path exists and user can read stats
            type: bool
            sample: False
        isgid:
            description: Tells you if the invoking user's group id matches the owner's group id
            returned: success, path exists and user can read stats
            type: bool
            sample: False
        lnk_source:
            description: Target of the symlink normalized for the remote filesystem
            returned: success, path exists and user can read stats and the path is a symbolic link
            type: str
            sample: /home/foobar/21102015-1445431274-908472971
        lnk_target:
            description: Target of the symlink.  Note that relative paths remain relative
            returned: success, path exists and user can read stats and the path is a symbolic link
            type: str
            sample: ../foobar/21102015-1445431274-908472971
            version_added: 2.4
        checksum:
            description: hash of the file
            returned: success, path exists, user can read stats, path supports
                hashing and supplied checksum algorithm is available
            type: str
            sample: 50ba294cdf28c0d5bcde25708df53346825a429f
        pw_name:
            description: User name of owner
            returned: success, path exists, user can read stats, owner name can be looked up and installed python supports it
            type: str
            sample: httpd
        gr_name:
            description: Group name of owner
            returned: success, path exists, user can read stats, owner group can be looked up and installed python supports it
            type: str
            sample: www-data
        mimetype:
            description: file magic data or mime-type
            returned: success, path exists and user can read stats and
                installed python supports it and the O(get_mime) option was V(true), will
                return V(unknown) on error.
            type: str
            sample: application/pdf; charset=binary
        charset:
            description: file character set or encoding
            returned: success, path exists and user can read stats and
                installed python supports it and the O(get_mime) option was V(true), will
                return V(unknown) on error.
            type: str
            sample: us-ascii
        readable:
            description: Tells you if the invoking user has the right to read the path
            returned: success, path exists and user can read the path
            type: bool
            sample: False
            version_added: 2.2
        writeable:
            description: Tells you if the invoking user has the right to write the path
            returned: success, path exists and user can write the path
            type: bool
            sample: False
            version_added: 2.2
        executable:
            description: Tells you if the invoking user has execute permission on the path
            returned: success, path exists and user can execute the path
            type: bool
            sample: False
            version_added: 2.2
        attributes:
            description: list of file attributes
            returned: success, path exists and user can execute the path
            type: list
            sample: [ immutable, extent ]
            version_added: 2.3
        version:
            description: The version/generation attribute of a file according to the filesystem
            returned: success, path exists, user can execute the path, lsattr is available and filesystem supports
            type: str
            sample: "381700746"
            version_added: 2.3
N)AnsibleModule)to_bytesc           	     b   |j                   }t        d$i ddd|ddt        j                  |      z  dt        j                  |      dt        j
                  |      dt        j                  |      d	t        j                  |      d
t        j                  |      dt        j                  |      dt        j                  |      d|j                  d|j                  d|j                  d|j                  d|j                  d|j                   d|j"                  d|j$                  d|j&                  dt)        |t        j*                  z        dt)        |t        j,                  z        dt)        |t        j.                  z        dt)        |t        j0                  z        dt)        |t        j2                  z        dt)        |t        j4                  z        dt)        |t        j6                  z        dt)        |t        j8                  z        dt)        |t        j:                  z        dt)        |t        j<                  z        d t)        |t        j>                  z        }d!D ]'  }tA        ||d"         stC        ||d"         ||d#   <   ) |S )%NexistsTpathmodez%04oisdirischrisblkisregisfifoislnkissockuidgidsizeinodedevnlinkatimemtimectimewusrrusrxusrwgrprgrpxgrpwothrothxothisuidisgid))	st_blocksblocks)
st_blksize
block_size)st_rdevdevice_type)st_flagsflags)st_gen
generation)st_birthtime	birthtime)st_ftype	file_type)st_attrsattrs)	st_obtypeobject_type)st_rsize	real_size)
st_creatorcreator)st_typer2   r       )"st_modedictstatS_IMODES_ISDIRS_ISCHRS_ISBLKS_ISREGS_ISFIFOS_ISLNKS_ISSOCKst_uidst_gidst_sizest_inost_devst_nlinkst_atimest_mtimest_ctimeboolS_IWUSRS_IRUSRS_IXUSRS_IWGRPS_IRGRPS_IXGRPS_IWOTHS_IROTHS_IXOTHS_ISUIDS_ISGIDhasattrgetattr)moduler   str	   outputothers         D/home/dcms/DCMS/lib/python3.12/site-packages/ansible/modules/stat.pyformat_outputre   ~  s   ::D   dll4(( ll4 	
 ll4  ll4  ll4  }}T" ll4  }}T" II II ZZ ii II  kk!" kk#$ kk%& kk'( $%&)* $%&+, $%&-. $%&/0 $%&12 $%&34 $%&56 $%&78 $%&9: 4$,,&';< 4$,,&'=FD 5$ 2uQx &r584F58'5* M    c                 F   t        t        t        ddddg      t        dd      t        dd      t        ddg d	
      t        ddddg
      t        ddg dddg            d      } | j                  j                  d      }t	        |d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }	 |rt        j                  |      }nt        j                  |      }t        | |      }
dt
        j                  fd t
        j                   fd!t
        j"                  ffD ]!  }t        j$                  ||d"         |
|d#   <   # |
j                  d$      r:t
        j&                  j)                  |      |
d%<   t        j*                  |      |
d&<   	 t-        j.                  |j0                        }|j2                  |
d'<   	 t9        j:                  |j<                        }|j>                  |
d(<   |
j                  d)      r(|
j                  d      r|r| jE                  ||      |
d<   |rd*x|
d+<   |
d,<   | jG                  d-      }|r|d.d/|g}	 | jI                  |      \  }}}|d#k(  r_|jK                  d0d"      d"   jM                  d1      \  }}|jO                         |
d+<   |jM                  d2      d"   jO                         |
d,<   |r4d |
d3<   g |
d<   d4|
d5<   | jS                  |      }d6D ]  }||v s||   |
|<    | j                  d|
       y # t        $ r[}	|	j                  t        j                  k(  rddi}
| j                  d|
       | j                  |	j                         Y d }	~	d }	~	ww xY w# t4        t6        f$ r Y w xY w# t6        t@        tB        f$ r Y w xY w# tP        $ r Y w xY w)7Nr   Tdestname)typerequiredaliasesrR   F)rj   default)mime	mime_typez	mime-type)rj   rm   rl   attr
attributesstrsha1)md5rs   sha224sha256sha384sha512checksumchecksum_algo)rj   rm   choicesrl   )r   followget_checksumget_mimeget_attributeschecksum_algorithm)argument_specsupports_check_modesurrogate_or_strict)errorsr|   r~   r   r}   r   r   )changedr@   )msgreadable	writeable
executabler<   r   r   
lnk_source
lnk_targetpw_namegr_namer   unknownmimetypecharsetfilez--mime-typez--mime-encoding:;=version 
attr_flags)r   rq   r   )*r   r?   paramsgetr   osr@   lstatOSErrorerrnoENOENT	exit_json	fail_jsonstrerrorre   R_OKW_OKX_OKaccessr   realpathreadlinkpwdgetpwuidrI   r   	TypeErrorKeyErrorgrpgetgrgidrJ   r   
ValueErrorOverflowErrordigest_from_fileget_bin_pathrun_commandrsplitsplitstrip	Exceptionget_file_attributes)r`   r   b_pathr|   r~   get_attrr}   r   ra   erb   permpwgrp_infomimecmdrcouterrr   r   xs                        rd   mainr     s   6D66:JKVU3648vt=_`VTFLCYZ#,c-7,I K	
 !F ==V$Dd#89F]]x(F}}  ,H}}  !12H==$$^4L**+?@
)B&!B 64,F bgg&bgg(>rww@WX 5))FDG4tAw5 zz'!ww//7|!{{62|\\"))$JJy<<		*$,,y
 zz'vzz*5!'!8!8AS!TF: 1::zVI.%%f-/@&IG%11':C7(+

3(:1(=(C(CC(H%Hg)1)9F:&(/c(:1(=(C(C(EF9%
  y!|!|((08 	#ACxFq		# U0{  )77ell"&FU8QZZ((), x   j-0 (  sP   -M< =.O# ,.O9 9A9P <	O AOO #O65O69PP	P P __main__)
__future__r   DOCUMENTATIONEXAMPLESRETURNr   r   r   r   r@   ansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr   re   r   __name__r=   rf   rd   <module>r      s^   
 #CJ?Bc
J  
 	 
  5 @;|Z1z zF rf   