+
     Giq  c                   sd    ^ RI t ^ RIt^ RIHtHt R tR
R ltRR ltRR lt	R t
RR ltRR	 ltR# )i    N)versiontuplekernel_versionc                s   \         P                  P                  \         P                  P                  V 4      4      p \        RR4      ;_uu_ 4       pV FU  pVP	                  4       p\        V4      ^8  d   V^,          V 8X  d    RRR4       R# V^ ,          V 8X  g   KL   RRR4       R# 	  RRR4       R#   + '       g   i     R# ; i)zk
Ismount() implementation, that works for mount --bind.
Workaround for: https://bugs.python.org/issue29707
/proc/mountsrNTF)ospathrealpathopensplitlen)folderhandlelinewordss   &   (/usr/lib/waydroid/tools/helpers/mount.pyismountr      s    
 WWbgg..v67F	nc	"	"fDJJLE5zQ58v#5	 
#	"
 Qx6! 
#	" 
#  
#	" s   5B7B7 B7+B77C	c                s   \        V4      '       d   V'       d   \        W4       MR# W3 Fq  p\        P                  P	                  V4      '       d   K*  V'       d0   \
        P                  P                  P                  V RRV.4       Ka  \        RV,           4      h	  \
        P                  P                  P                  V RRRW.4       \        V4      '       g!   \        RV,           R	,           V,           4      hR# )
z
Mount --bind a folder and create necessary directory structure.
:param umount: when destination is already a mount point, umount it first.
Nmkdir-p%Mount failed, folder does not exist: mount-obindMount failed:  -> )
r   
umount_allr   r   existstoolshelpersrunuserRuntimeError)argssourcedestinationcreate_foldersumountr   s   &&&&& r   r   r      s     {t) %77>>$MM""4'4)>?F# $ % % & 
MM4'4!MN ;+f4v=KLL      c                s   \        V4      '       d   R# \        P                  P                  V4      '       g   V'       dr   \        P                  P	                  V4      p\        P                  P                  V4      '       g.   \        P                  P                  P                  V RRV.4       \        P                  P                  P                  V RV.4       \        P                  P                  P                  V RRRVV.4       R# )zU
Mount a file with the --bind option, and create the destination file,
if necessary.
Nr   r   Ztouchr   r   r   )
r   r   r   r   ZdirnameZisdirr   r   r   r   )r   r    r!   r"   Zdirs   &&&& r   	bind_filer%   6   s     { 77>>+&&''//+.C77==%%!!&&tgtS-ABtg{%;< 
MM4'4 +"- .r$   c                s   . p\         P                  P                  V 4      p \        VR4      ;_uu_ 4       pV F  pVP	                  4       p\        V4      ^8  d!   \        RV,           R,           V,           4      hV^,          pVP                  V 4      '       g   Ke  RpVP                  V4      '       d   VR\        V4      )  pVP                  V4       K  	  RRR4       VP                  RR7       V#   + '       g   i     L$; i)z
Parses `/proc/mounts` for all folders beginning with a prefix.
:source: can be changed for testcases
:returns: a list of folders, that need to be umounted
r   zFailed to parse line in z: z\040(deleted)NT)Zreverse)r   r   r   r   r   r	   r   Z
startswithZendswithappendZsort)Zprefixr    Zretr   r   r   
mountpointZdeleted_strs   &&      r   umount_all_listr(   M   s     CWWf%F	fc		fDJJLE5zA~"#=#F#M#'$( ) )qJ$$V,,.&&{33!+,>c+.>->!?J

:&  
 HHTHJ 
	s   A#C7C77$C77D	c                s    \        V4      pV F/  p\        P                  P                  P	                  V RV.4       K1  	  V F&  p\        V4      '       g   K  \        RV,           4      h	  R# )z=
Umount all folders, that are mounted inside a given folder.
r#   zFailed to umount: N)r(   r   r   r   r   r   r   )r   r
   Zall_listr'   s   &&  r   r   r   f   s]     v&H
th
%;< 
:3j@AA r$   c	                s   \        V4      '       d   V'       d   \        W4       M
V'       g   R# \        P                  P	                  V4      '       gI   V'       d/   \
        P                  P                  P                  V RRV.4       M\        RV,           4      h. p	R.p
V'       d   V	P                  RV.4       V'       d   V
P                  R4       V'       d   V
P                  V4       V
'       d#   V	P                  RR	P                  V
4      .4       \
        P                  P                  P                  V R
.V	OVNVN4       \        V4      '       g!   \        RV,           R,           V,           4      hR# )z
Mount and create necessary directory structure.
:param umount: when destination is already a mount point, umount it first.
:param force: attempt mounting even if the mount point already exists.
Nr   r   r   z0context="system_u:object_r:waydroid_rootfs_t:s0"z-tZror   Z,r   r   r   )r   r   r   r   r   r   r   r   r   r   Zextendr&   join)r   r    r!   r"   r#   readonly
mount_typeoptionsforceZ
extra_argsZopt_argss   &&&&&&&&&  r   r   r   q   s.    {t) 77>>+&&MM""4'4)EFF' ( ) ) JDEH4,- 4(!345 
MM4'!LJ!L!L!LM ;+f4v=KLL  r$   c                sX   . VOpRRP                  V4      ,           .pV'       dS   VP                  V4       VP                  V4       VP                  RV,           4       VP                  RV,           4       \        4       \        R4      8  d   VP                  R4       V Fq  p	\        P
                  P                  V	4      '       d   K*  V'       d0   \        P                  P                  P                  V RRV	.4       Ka  \        R	V	,           4      h	  \        V R
VR
VWeRR7       R# )z
Mount an overlay.
z	lowerdir=Z:z	upperdir=zworkdir=z4.17zxino=offr   r   r   ZoverlayT)r+   r,   r*   r"   r-   N)r)   r&   r   r    r   r   r   r   r   r   r   r   r   )
r   Z
lower_dirsr!   Z	upper_dirZwork_dirr"   r*   Zdirsr,   Zdir_paths
   &&&&&&&   r   mount_overlayr.      s    
 Z=Dchhz234GIH{Y./zH,-<//z"ww~~h''!!&&tgtX-FG"#J#+$, - -  
$	;9g$Hr$   )TF)F)r   )TFTNNT)NNTT)r   Ztools.helpers.runr   Ztools.helpers.versionr    r   r   r   r%   r(   r   r   r.   ) r$   r   <module>r/      s8    
  > M<..2	B'MRHr$   