
    J                        S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKrSSK	r	SSK
r
SSKrSSKrSSKrSSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  \(       d  SSKrSSKr\R6                  (       a  \rSrSrSr Sr!Sr"Sr#Sr$Sr%\RL                  " S5      r'Sr(Sr)Sr*Sr+Sr,Sr-Sr.Sr/Sr0Sq1\2" 5       q3 " S S\45      r5 " S  S!\45      r6S" r7S# r8 S1S$ jr9S% r:S& r;S' r<\$\$\%4S( jr=  S2S) jr>S* r?S+ r@S, rAS- rBS. rCS/ rDS0 rEg)3zHelper file for POSIX methods.    )absolute_import)print_function)division)unicode_literals)timegmN)CommandException)UTC)CreateCustomMetadata) GetValueFromObjectCustomMetadata)
IS_WINDOWS)SECONDS_PER_DAYzgoog-reserved-file-atimezgoog-reserved-posix-gidzgoog-reserved-posix-modezgoog-reserved-file-mtimezgoog-reserved-posix-uidz
^[0-7]{3}$      @                      c                   ,    \ rS rSrSr\\\\S4S jrSrg)POSIXAttributesS   z1Class to hold POSIX attributes for a file/object.Nc                 n    Xl         X l        X0l        X@l        [	        U(       a  UO[
        5      U l        g)a  Constructor for POSIXAttributes class which holds relevant data.

Args:
  atime: The access time of the file/object.
  mtime: The modification time of the file/object.
  uid: The user ID that owns the file.
  gid: The group ID that the user is in.
  mode: An instance of POSIXMode.
N)atimemtimeuidgid	POSIXModeNA_MODEmode)selfr   r   r   r   r"   s         )platform/gsutil/gslib/utils/posix_util.py__init__POSIXAttributes.__init__V   s(     JJHH$$G4DI    )r   r   r"   r   r   )	__name__
__module____qualname____firstlineno____doc__NA_TIMENA_IDr%   __static_attributes__ r'   r$   r   r   S   s    9 5r'   r   c                       \ rS rSrS rSrg)r    l   c                     Xl         g )Npermissions)r#   r5   s     r$   r%   POSIXMode.__init__n   s    "r'   r4   N)r(   r)   r*   r+   r%   r/   r0   r'   r$   r    r    l   s    #r'   r    c                 0    [        [        U 5      SS 5      $ )z7Converts a base-10 mode integer from os.stat to base-8.N)intoctr"   s    r$   ConvertModeToBase8r<   r   s     
SYrs^	r'   c                    [        5       n[        U [        [        5      u  p4 [	        U5      nU(       a  U[        ::  a  [        SU5        [        nO;U[	        [        R                  " 5       5      [        -   :  a  [        SU5        [        nXBl        [        U [        X5        [        U [        X5        [        U [        [         5      u  p5U(       a6  ["        R%                  U5      (       a   ['        [)        U5      5      Ul        U$ U$ ! [         a    [        SU5        [        n Nf = f! [         a    [        SU5         U$ f = f)a3  Parses the POSIX attributes from the supplied metadata.

Args:
  obj_metadata: The metadata for an object.
  url_str: File/object path that provides context if a warning is thrown.

Returns:
  A POSIXAttribute object with the retrieved values or a default value for
  any attribute that could not be found.
r   r"   )r   r   
ATIME_ATTRr-   longWarnNegativeAttributetimer   WarnFutureTimestamp
ValueErrorWarnInvalidValuer   DeserializeIDAttributeGID_ATTRUID_ATTR	MODE_ATTRr!   
MODE_REGEXmatchr    r9   r"   )obj_metadataurl_strposix_attrsfoundr   r"   s         r$   +DeserializeFileAttributesFromObjectMetadatarO   y   s     !+1,
29;,%
KE'!GW-e	diik"_4	4'7+e xFxF0y18:+%
z%%("3t9-k 
! 
 Wg&E  (vw'	(s)   .D ;D .D- D*)D*-EEc                 
   U R                   [        :w  a  [        [        U R                   0US9  U(       a  U R                  [        :w  a  [        [
        U R                  0US9  U R                  [        :w  a  [        [        U R                  0US9  U R                  [        :w  a  [        [        U R                  0US9  U R                  R                  [        :w  a%  [        [        U R                  R                  0US9  ggg)a  Takes a POSIXAttributes object and serializes it into custom metadata.

Args:
  posix_attrs: A POSIXAttributes object.
  custom_metadata: A custom metadata object to serialize values into.
  preserve_posix: Whether or not to preserve POSIX attributes other than
                  mtime.
)entriescustom_metadataN)r   r-   r
   
MTIME_ATTRr   r>   r   r.   rG   r   rF   r"   r5   r!   rH   )rM   rR   preserve_posixs      r$   'SerializeFileAttributesToObjectMetadatarU      s     '!*k.?.?!@)8: G#J0A0A#B+:<%Hkoo#>+:<%Hkoo#>+:<##w.I{/?/?/K/K#L+:< / r'   c                    UR                  S5      S   n[        X[        5      u  pV [        U5      nU(       a  U[        ::  a  [	        XB5        [        n[        X4U5        g! [
         a    [        XB5        [        n N*f = f)a  Parses the POSIX attributes from the supplied metadata into posix_attrs.

Args:
  obj_metadata: The metadata for an object.
  attr: Either GID_ATTR or UID_ATTR.
  url_str: File/object path that provides context if a warning is thrown.
  posix_attrs: POSIXAttribute object.
-r   N)splitr   r.   r9   r@   rC   rD   setattr)rK   attrrL   rM   	attr_namerN   vals          r$   rE   rE      sr     jjob!)/EJ*%
c(CI/c 
+#& 
 Y(
Cs   -A" "B ?B c
                    [        5       n
U [        :  nU[        :  nU[        :  nU[        :  nU[        :  nU[        :  nU[        :  nU[        :  nU[        :  nU	[        :  nU(       a  U(       d  X
l        U(       a  U(       d  X*l        U(       a  U(       d  XJl        U(       a  U(       d  Xjl        U(       a  U(       d  XR                  l	        X=(       a    U(       + =(       dU    U=(       a    U(       + =(       d>    U=(       a    U(       + =(       d'    U=(       a    U(       + =(       d    U=(       a    U(       + 4$ )a  Checks whether an update for any POSIX attribute is needed.

Args:
  src_atime: The source access time.
  dst_atime: The destination access time.
  src_mtime: The source modification time.
  dst_mtime: The destination modification time.
  src_uid: The source user ID.
  dst_uid: The destination user ID.
  src_gid: The source group ID.
  dst_gid: The destination group ID.
  src_mode: The source mode.
  dst_mode: The destination mode.

Returns:
  A tuple containing a POSIXAttribute object and a boolean for whether an
  update was needed.
)
r   r-   r.   r!   r   r   r   r   r"   r5   )	src_atime	dst_atime	src_mtime	dst_mtimesrc_uiddst_uidsrc_giddst_gidsrc_modedst_moderM   has_src_atimehas_dst_atimehas_src_mtimehas_dst_mtimehas_src_uidhas_dst_uidhas_src_gidhas_dst_gidhas_src_modehas_dst_modes                        r$   NeedsPOSIXAttributeUpdaterr      s    *  !+g%-g%-g%-g%-%+%+%+%+G#,G#,=!=!OO,#+ 	;m*; ;%;m*;;#7K; $7K; %9\)9	
< <r'   c                     [         R                  [        U 5      SS 5      =(       a)    U [        -  =(       d    U [        -  =(       d	    U [
        -  $ )zValidates whether the mode is valid.

In order for the mode to be valid either the user, group, or other byte must
be >= 4.

Args:
  mode: The mode as a 3-digit, base-8 integer.

Returns:
  True/False
r8   N)rI   rJ   r:   U_RG_RO_Rr;   s    r$   ValidatePOSIXModerw   	  sC     
		#d)BC.	) 
;tcz 0:TCZ 0:/3cz;r'   c                 `   [         (       a  gU[        :  n[        U5      [        :  nU[        :  nU(       d  U(       d  U(       d  g[        R
                  " 5       S:X  a  g[        [        [        U5      S5      5      nU(       a  U(       d  SSU -  4$ O[        [        5      nU(       a   [        R                  " U5        U(       a   [        R                  " U5        U[        R                  " 5       :H  n[        [        U5      S5      nU(       d:  U(       a3  U(       a,  [!        U["        -  5      (       d  SSX[%        U5      S	S
 4-  4$ U(       a0  [!        U["        -  5      n	X(       a  S4$ SX[%        U5      S	S
 4-  4$ [        U5      [&        ;   a0  [!        U[(        -  5      n	X(       a  S4$ SX[%        U5      S	S
 4-  4$ U[*        -  (       a  gU(       d  U(       d  U(       a  gSSU -  4$ ! [        [        4 a    SSX4-  4s $ f = f! [        [        4 a    SSX4-  4s $ f = f)a  Validates that the user has file access if uid, gid, and mode are applied.

Args:
  url_str: The path to the object for which this is validating.
  uid: A POSIX user ID.
  gid: A POSIX group ID.
  mode: A 3-digit, number representing POSIX permissions, must be in base-8.

Returns:
  A (bool, str) tuple, True if and only if it's safe to copy the file, and a
  string containing details for the error.
)T r   r   Fz$Mode for %s won't allow read access.z3UID for %s doesn't exist on current system. uid: %dz3GID for %s doesn't exist on current system. gid: %dz?Insufficient access with uid/gid/mode for %s, gid: %d, mode: %sr8   Nry   z?Insufficient access with uid/gid/mode for %s, uid: %d, mode: %sz"There was a problem validating %s.)r   r.   r9   r!   osgeteuidrw   strSYSTEM_POSIX_MODEpwdgetpwuidKeyErrorOverflowErrorgrpgetgrgidgetuidboolrt   r:   USER_GROUPSru   rv   )
rL   r   r   r"   uid_presentgid_presentmode_present
mode_validuid_is_current_uservalids
             r$   ValidateFilePermissionAccessr     s8    Ze+C5 +,
 ZZ\Q SY!23*;gEEE 
  !D	ll3 	ll3 ryy{* 
SY	$ 
d4#:>N>N !D	"#78 9 9E2 9 9!D	"#789 9 3x;E2 9 9!D	"#789 9 cz{z	4w>	>>S m$ Kn   m$ Kn  s$   ,G4 
H 4HHH-,H-c                 "   Uc  [        SU -  5      e [        U[        [        S9u  pE[        U[        [        S9u  pg[        U[
        [        S9u  p[        U[        [        S9u  p[        U[        [        S9u  pU(       aL  [        U5      nU(       d9  [        R                  " U 5      R                  n[        R                  " XU45        gO4U(       a-  [        UR                   5      n[        R                  " XU45        U(       d  gU(       a  [        U5      nU[        :  a#  U[        :  a  [        R                  " XU45        OU[        :  aC  U[        ::  a9  [        R                  " U 5      R"                  n[        R                  " XU45        OLU[        ::  aB  U[        :  a8  [        R                  " U 5      R                  n[        R                  " XU45        [$        (       a  gU(       a%  [        R&                  " 5       S:X  a  [)        U	5      n	O[        n	U
(       a  [)        U5      nU	[        :  a"  U[        :  a  [        R*                  " X	U5        OXU	[        :  a"  U[        ::  a  [        R*                  " X	S5        O,U	[        ::  a"  U[        :  a  [        R*                  " U SU5        U(       a,  [)        [-        U5      S5      n[        R.                  " X5        gg! [0         a    [        SUR2                  -  5      ef = f)a  Parses POSIX attributes from obj_metadata and sets them.

Attributes will only be set if they exist in custom metadata. This function
should only be called after ValidateFilePermissionAccess has been called for
the specific file/object so as not to orphan files.

Args:
  path: The local filesystem path for the file. Valid metadata attributes will
        be set for the file located at path, some attributes will only be set
        if preserve_posix is set to True.
  obj_metadata: The metadata for the file/object.
  is_rsync: Whether or not the caller is the rsync command. Used to determine
            if timeCreated should be used.
  preserve_posix: Whether or not all POSIX attributes should be set.
Nz"obj_metadata cannot be None for %s)default_valuer   r   r   z#Check POSIX attribute values for %s)r   r   r>   r-   rS   rG   r.   rF   rH   r!   r?   rz   statst_atimeutimeConvertDatetimeToPOSIXtimeCreatedst_mtimer   r{   r9   chownr|   chmodrC   name)pathrK   is_rsyncrT   found_atr   found_mtr   	found_uidr   	found_gidr   
found_moder"   	atime_tmp	mtime_tmps                   r$   ParseAndSetPOSIXAttributesr   o  sd   &  ?$F
GGE.6|7AELNOH 7|7AELNOH 6l6>DIKNI 6l6>DIKNI 88AFMOJ 5keGGDM**	
5)*  
$\%=%=>ehhtU^$5kew57?hhtU^$	Ug-''$-((ihhtY'(	'	ego''$-((ihht'(z RZZ\Q&HccHc
U{sU{hht#	uhht"	#+hhtRTAdhht  
 .
@',,- . ..s    B:K+ <K+ C#K+ /C:K+ +#Lc                 P    [         R                  " 5       R                  SUU 5        g)zLogs if an attribute has a negative value.

Args:
  attr_name: The name of the attribute to log.
  url_str: The path of the file for context.
z$%s has a negative %s in its metadataNlogging	getLoggerwarnr[   rL   s     r$   r@   r@     #     
A7$&r'   c                 P    [         R                  " 5       R                  SUU 5        g)Logs if an attribute has an invalid value.

Args:
  attr_name: The name of the attribute to log.
  url_str: The path of the file for context.
z$%s has an invalid %s in its metadataNr   r   s     r$   rD   rD     r   r'   c                 N    [         R                  " 5       R                  SX5        g)r   z5%s has an %s more than 1 day from current system timeNr   r   s     r$   rB   rB     s#     
#r'   c                 p    [        [        U R                  [        5       S9R	                  5       5      5      $ )zConverts a datetime object to UTC and formats as POSIX.

Sanitize the timestamp returned in dt, and put it in UTC format. For more
information see the UTC class.

Args:
  dt: A Python datetime object.

Returns:
  A POSIX timestamp according to UTC.
)tzinfo)r?   r   replacer	   	timetuple)dts    r$   r   r     s*     
fRZZsuZ-779:	;;r'   c                      [         (       a  SqgSn [        R                  " S5      n[        R                  " U5        X-
  n[	        US-  5      SS qg)z.Records the default POSIX mode using os.umask.666Ni     i  r8   )r   r}   rz   umaskr:   )max_permissionscurrent_umaskr"   s      r$   InitializeDefaultModer     sR     Z
 /((5/-((=		($ $,',r'   c                  h   [         (       a  g[        R                  " 5       n [        R                  " U 5      R
                  n[        [        R                  " U 5      R                  /[        R                  " 5        Vs/ s H   o!UR                  ;   d  M  UR                  PM"     sn-   5      qgs  snf )z~Initializes the set of groups that the user is in.

Should only be called if the flag for preserving POSIX attributes is set.
N)r   rz   r   r~   r   pw_namesetpw_gidr   getgrallgr_memgr_gidr   )user_id	user_namegs      r$   InitializeUserGroupsr     s~     Z
IIK'll7#++)
||G##$AA+@xqxxABC+ Bs   >B/
B/
c                  ,    [        5         [        5         g)z<Initializes POSIX data. Run once at the beginning of a copy.N)r   r   r0   r'   r$   InitializePreservePosixDatar   "  s    r'   )F)FF)Fr,   
__future__r   r   r   r   calendarr   getpassr   rz   rerA   sixgslib.exceptionr   gslib.tz_utcr	   gslib.utils.metadata_utilr
   r   gslib.utils.system_utilr   gslib.utils.unit_utilr   r   r~   PY3r9   r?   r>   rF   rH   rS   rG   r-   r.   r!   compilerI   rt   U_WU_Xru   G_WG_Xrv   O_WO_Xr}   r   r   objectr   r    r<   rO   rU   rE   rr   rw   r   r   r@   rD   rB   r   r   r   r   r0   r'   r$   <module>r      sO   % & %  '    	 	  
 ,  : F . 1 77	$ (
$&	'
$


ZZ%
     e5f 52# #'X <A<>',.<b;  /4W S?p )..3\.~&&	#<-$C"r'   