Current File : //usr/lib64/python3.6/site-packages/borg/__pycache__/fuse.cpython-36.pyc
3

y��bnz�@sBddlZddlZddlZddlZddlZddlZddlZddlZddlm	Z	ddl
mZddlZddl
mZe�Z
ddlmZddlmZddlmZdd	lmZdd
lmZmZmZmZmZddlmZddlmZdd
l m!Z!ddl"m#Z#e$ej%d�Z&e$ej%d�Z'e$ej%d�Z(dd�Z)dZ*Gdd�d�Z+Gdd�dej,�Z-dS)�N)�defaultdict)�SIGINT�)�
create_logger)�blake2b_128)�Archiver)�Archive)�FuseVersionsIndex)�	daemonize�hardlinkable�signal_handler�format_file_size�Error)�msgpack)�Item)�LRUCache)�RemoteRepository�st_mtime_ns�st_birthtime�st_birthtime_nscCstjdd�S)Nr)�workers)�llfuse�main�rr�/usr/lib64/python3.6/fuse.py�	fuse_main#sr�c@sFeZdZdZdZejd�Zejdks(t	�dd�Z
dd	�Zddd
�Zd
S)�	ItemCachez�
    This is the "meat" of the file system's metadata storage.

    This class generates inode numbers that efficiently index items in archives,
    and retrieves items from these inode numbers.
    �iz=cII�	cCsJ||_t�|_d|_d|_tjdd�|_tddd�d�|_	d|_
d|_dS)	Nri@Bzborg-tmp)�prefix�
cSsdS)Nr)�_rrr�<lambda>_sz$ItemCache.__init__.<locals>.<lambda>)�capacity�dispose)�decrypted_repository�	bytearray�meta�write_offset�offset�tempfileZ
TemporaryFile�fdr�chunks�indirect_items�direct_items)�selfr&rrr�__init__AszItemCache.__init__c
Cs(||j}|dkrtd��|j|td�kr�|jj|j|�\}}}||}t|j||d��}|jj|�}|s�t	|j
j|g��\}	}||j|<t|�|d�}
t
j�}|j|
�tt	|�d�S|j|td�k�rtj|j|d|d�d	�}|jj|tj�tt	t
j|jd
d��d�Std��dS)
Nrz3ItemCache.get() called with an invalid inode number�I� )�
internal_dict�Srr�littlei)Z	read_sizezInvalid entry type in self.meta)r*�
ValueErrorr(�ord�indirect_entry_struct�unpack_from�bytesr-�get�nextr&�get_many�
memoryviewr�Unpacker�feedr�int�
from_bytesr,�seek�io�SEEK_SET)
r0�inoder*r"Zchunk_id_relative_offset�chunk_offsetZchunk_id_offset�chunk_id�chunk�csize�data�unpackerZ	fd_offsetrrrr<gs(


z
ItemCache.getNFc#stj�}d�d}d}d�|j}|j}|jj}	��fdd�}
�x�t||jj|��D�]�\}\}}
|dt	|�kr�|t
|j�|_}||||d�<|}|d7}||7}t	|
�}|j|
��x8y|j
|
�}Wntjk
r�PYnXt|d�}|r�||��s|�rd|k�rd�q��}t	|�}�||k}d�|dt	|�k�rX|t
|j�|_}|�r�|jjdtj�}|jj|�d	|jd
d�|||d�<|jd7_n.�||}|	||d
|||�|jd7_||j}|d7}||fVq�WqPW||_dS)Nr�cs�|7��t|�7�dS)N)�len)Zappend_msgpacked_bytes)�msgpacked_bytes�
stream_offsetrr�write_bytes�sz1ItemCache.iter_archive_items.<locals>.write_bytesr3)r4�partrr5�r6rr2)rr@r)r(r9�	pack_into�zipr&r>rOr;�GROW_META_BYrA�unpackZ	OutOfDatarr,rDrE�SEEK_END�write�to_bytesr/r.r*)r0Zarchive_item_ids�filter�consider_part_filesrMZchunk_beginZlast_chunk_lengthr)r(Zpack_indirect_intorR�keyrKrLZcurrent_id_offset�itemZcurrent_itemZcurrent_item_lengthZcurrent_spans_chunks�posZitem_offsetrGr)rPrQr�iter_archive_itemssZ$

"
zItemCache.iter_archive_itemsii )NF)
�__name__�
__module__�__qualname__�__doc__rW�struct�Structr9�size�AssertionErrorr1r<rarrrrr.s
&rcs�eZdZdZdZdZdZdZdZ�fdd�Z	dd�Z
d	d
�Zd1dd�Zd2d
d�Z
gfdd�Zdd�Zdd�Zdd�Zd3dd�Zdd�Zgfdd�Zd4dd�Zd5dd �Zd6d!d"�Zd#d$�Zd7d%d&�Zd8d'd(�Zd9d)d*�Zd+d,�Zd-d.�Zd:d/d0�Z�ZS);�FuseOperationsz(Export archive as a FUSE filesystem
    FNrcs�t�j�||_||_||_||_||_i|_tt	dd�d�|_
d|_i|_t
t�|_tj�|_tj�|_d|_i|_t|�|_ttjjdtj�p�d��}tjd|�t|dd�d�|_tt	d	d�d�|_ dS)
NcSsdS)Nr)r"rrrr#�sz)FuseOperations.__init__.<locals>.<lambda>)r$r%rZBORG_MOUNT_DATA_CACHE_ENTRIESrz$mount data cache capacity: %d chunkscSsdS)Nr)r"rrrr#�scSsdS)Nr)r"rrrr#�s)!�superr1�repository_uncachedr&�args�manifestr^�itemsr�FILES�_inode_cache�_inode_count�parentr�dict�contents�os�getuid�default_uid�getgid�default_gid�default_dir�pending_archivesr�cacherB�environr<�	cpu_count�logger�debug�
data_cache�	_last_pos)r0r^Z
repositoryrnrmr&Zdata_cache_capacity)�	__class__rrr1�s(




zFuseOperations.__init__cCs�|jdd�|jjjr6|jr$td��|j|jjj�ntt�|_xj|j	j
j|j�D]V}|jrh|j|j�qP|jdt
|jj�d�d�}||jdtj|j�<|j|j|<qPWdS)Nr)rsz_for versions view, do not specify a single archive, but always give the repository as location.ge��A)rs�mtime)�_create_dirrm�location�archive�versionsr�process_archiver	�versions_indexrnZarchivesZlist_considering�namerBZtsZ	timestamprurv�fsencoder|)r0r�Z
archive_inoderrr�_create_filesystems
z!FuseOperations._create_filesystemc	Cs�tjd|jt|j�ttj|j�t|j�tj|j���tjdt|j��tjd|j	j
|j	j|j	j
|j	jttj|j	j��tt
j|j	jj��j��tjdt|jj��|jjttdd�|jj�D����|jj�dS)Nz$fuse: %d synth inodes, %d edges (%s)zfuse: %d pending archivesz]fuse: ItemCache %d entries (%d direct, %d indirect), meta-array size %s, direct items size %sz#fuse: data cache: %d/%d entries, %scss|]\}}t|�VqdS)N)rO)�.0r^rJrrr�	<genexpr> sz2FuseOperations.sig_info_handler.<locals>.<genexpr>)r�r�rrrOrsr
�sys�	getsizeofr|r}r/r.r(rv�statr,�fileno�st_sizer�roZ	_capacity�sumr&Zlog_instrumentation)r0Zsig_no�stackrrr�sig_info_handlers(zFuseOperations.sig_info_handlercCs�ddd�}dddg}|r(|j|jd��||dd	d
t�}|rL||dd	d
t�||dd	d
t�|_||dd	d
t�|_||d
ddt�|_||dddt�|_||dddtdd�|_|jdk	r�|jn|j	}|jdk	r�|jn|j
}d|j@}	t|	ttj�d�||d�|_
|j�tj|||�|�sBt�\}
}t|jt��sB|jj|
|�d
}zRtd|j��$td|j��t�}
WdQRXWdQRX|
dk�p�|
tk�o�|}Wdtj|�XdS)z6Mount filesystem on *mountpoint* with *mount_options*.rc
Sst|t�st��x�t|�D]�\}}||kr8|j|�|S|j|d�r|j|�|jdd�d}|tkr�|j�}	|	dkr|dS|	dkr�dSt	d
|��|t
kr�yt
||d�St	k
r�t	d
|�d�YnXy||�St	k
�r�t	d
|�d�YqXqW|SdS)N�=r�y�yes�true�1T�n�no�false�0Fzunsupported value in option: %s)�base)r�r�r�r�)r�r�r�r�)�
isinstance�listri�	enumerate�pop�
startswith�split�bool�lowerr7rB)
�optionsr^ZpresentZnot_presentZwanted_type�int_base�idxZoption�value�vrrr�
pop_option&s2

z(FuseOperations.mount.<locals>.pop_optionz
fsname=borgfsZroZdefault_permissions�,�ignore_permissionsTF�allow_damaged_filesr��uidN�gid�umaskrT)r�i�Age��A)�moder�r�r��SIGUSR1ZSIGINFO)r)�extendr�r�r�r�rB�
uid_forced�
gid_forcedr�rxrzr�timer{r�rZinitr
r�rlrZmigrate_lockrr�rr�close)r0Z
mountpointZ
mount_optionsZ
foregroundr�r�r�Zdir_uidZdir_gidZdir_modeZold_idZnew_idZumount�signalrrr�mount#s:
!

zFuseOperations.mountcCsN|j�}|dk	r4tf|jj��|j|<||j|_n|j|j|<||j|<|S)zCreate directory
        N)�allocate_inoderr{Zas_dictror�rs)r0rsr�Zinorrrr�ps
zFuseOperations._create_dircs�i|_tj�}t|j|j|j||jjd�}|jj	}t
j|jj|jj
�}|j�pR|��r\ind���fdd�}t
j|||�}x�|jj|jj||jjd�D]�\}	}
|r�tjj|
jjtj�|d��|
_tj|
j�}tj|
j�}|�ry|j||�}
Wntk
�rYnX|
|j|
<q�||jd�}d}x"|dd	�D]}|j||�}�q6W|j |d
|
||||	�|�q�Wtj�|}t!j"d||j#�dS)z9Build FUSE inode hierarchy from archive metadata
        )r]NcsD�r@|r@t|j�r@|jdd�r@d|kr@|jd�df�|jd�<dS)NZhardlink_masterT�sourcer-�path)rr�r<)r_Zmatched)�hardlink_masters�partial_extractrr�peek_and_store_hardlink_masters�szGFuseOperations.process_archive.<locals>.peek_and_store_hardlink_masters)r\r]�/rz8fuse: process_archive completed in %.1f s for archive %s���r�)$�
file_versionsr�Zperf_counterrrlr^rnrmr]�strip_componentsrZ
build_matcherZpatterns�paths�emptyZbuild_filterr}raZmetadatarorv�sep�joinr�r�r�r��S_ISDIRr��_find_inode�KeyError�
process_inner�process_leafr�r�r�)r0�archive_namer Zt0r�r�Zmatcherr�r\�
item_inoder_r��is_dirrG�segmentsrs�segmentZdurationr)r�r�rr�|s> 
zFuseOperations.process_archivec	s�|j}	|`|pi}�fdd�}
ddd�}d|ko8t|j��r,tjj|jjtj�|d��}|j|jd|f�\}
}|r�tj	|�}�j
r��j|}|||dd�}y�j||�}Wn"t
k
r�tjd	|	|�dSX�j|�}|jd
d�d|_|�j|<n2|
dk	�r0|
|_|}|�j|<|�r0d|	f||j<n|}�j
�r~|�r~�j||�}tj	|	�}|
||�}|dk	�r~|||�}|�j|<|�j|<|�r�|�j||<dS)
Ncsfd|krbt|�}�jj|d�\}}dd�|jD�}tdj|��}||kr^|d7}||f�j|<|SdS)Nr-rcSsg|]\}}}|�qSrr)r�rIr"rrr�
<listcomp>�szEFuseOperations.process_leaf.<locals>.file_version.<locals>.<listcomp>rNr)rN)rr�r<r-r�)r_r�Zfile_idZcurrent_versionZprevious_idZ	chunk_idsZcontents_id)r0rr�file_version�sz1FuseOperations.process_leaf.<locals>.file_versionFcSsJ|r |jdd�}|d|d7}tjj|�\}}tjd|�}|||S)Nr�rz.%05dr�)�rsplitrvr��splitextr�)r��version�add_dirZ
path_fnameZextZversion_encrrr�make_versioned_name�sz8FuseOperations.process_leaf.<locals>.make_versioned_namer�T)r�z#Skipping broken hard link: %s -> %s�nlinkr)F)r�rr�rvr�r�r�r�r<r�r�r�r�r�r��warning�get_itemr�ror-r�rsru)r0r�r_rsr r�r�r�Zstripped_componentsr�r�r�r�r-Zlink_targetr�rGZenc_pathr)r0rr��sL












zFuseOperations.process_leafcCs6|j|}||kr||}n|j|�}|r2|||<|S)N)rur�)r0r��parent_inode�dirrGrrrr��s


zFuseOperations.process_innercCs|jd7_|jS)Nr)rr)r0rrrr��szFuseOperations.allocate_inodecCsLtj�}d|_d|_d|_d|_d|_d|_d|_d|_	t
|d�rHd|_|S)Nir�	f_namemax�)rZStatvfsData�f_bsize�f_frsize�f_blocks�f_bfree�f_bavail�f_files�f_ffree�f_favail�hasattrr�)r0�ctxZstat_rrr�statfs�s
zFuseOperations.statfscCsR|jj|�}|dk	r|Sy
|j|Stk
rL|jj|�}||j|<|SXdS)N)rqr<ror�r})r0rGr_rrrr�s

zFuseOperations.get_itemcCs2||jd�}d}x|D]}|j||}qW|S)Nr�r)r�ru)r0r�r r�rGr�rrrr�s

zFuseOperations._find_inodecCs\|j|�}tj�}||_d|_d|_d|_|j|j@|_	|j
dd�|_|jdk	rX|jn|j
dkrh|j
n|j|_|jdk	r�|jn|jdkr�|jn|j|_|j
dd�|_|j�|_d|_|j|jd|j|_|j}t�r||_|j
d|�|_|j
d|�|_t�rX|j
d	|�|_nF|d
|_ |j
d|�d
|_!|j
d|�d
|_"t#�rX|j
d	|�d
|_$|S)Nri,r�rZrdeviZatimeZctimeZ	birthtimege��A)%r�r�EntryAttributes�st_inoZ
generationZ
entry_timeoutZattr_timeoutr�r��st_moder<�st_nlinkr�r�rx�st_uidr�r�rz�st_gid�st_rdevZget_sizer��
st_blksize�	st_blocksr��have_fuse_xtime_nsr�st_atime_ns�st_ctime_ns�have_fuse_birthtime_nsr�st_mtime�st_atime�st_ctime�have_fuse_birthtimer)r0rGr�r_�entryZmtime_nsrrr�getattrs6
((

zFuseOperations.getattrcCs|j|�}|jdi�j�S)N�xattrs)r�r<�keys)r0rGr�r_rrr�	listxattr>s
zFuseOperations.listxattrcCsF|j|�}y|jdi�|pdStk
r@tjtj�d�YnXdS)NrrN)r�r<r�r�	FUSEErrorZENOATTR)r0rGr�r�r_rrr�getxattrBs

zFuseOperations.getxattrcCs*|jj|d�}|r&|j|tj|�g�dS)N)r|r�r�rvr�)r0rGr�rrr�_load_pending_archiveIsz$FuseOperations._load_pending_archivecCsV|j|�|dkr|}n4|dkr,|j|}n |j|j|�}|sLtjtj��|j|�S)N�.s..)	r	rsrur<rr�errno�ENOENTr)r0r�r�r�rGrrr�lookupOs
zFuseOperations.lookupcCs2|js.|j|�}d|kr.tjd�tjtj��|S)NZchunks_healthyzzFile has damaged (all-zero) chunks. Try running borg check --repair. Mount with allow_damaged_files to read damaged files.)r�r�r�r�rrrZEIO)r0rG�flagsr�r_rrr�open[s

zFuseOperations.opencCs|j|�|S)N)r	)r0rGr�rrr�opendirgs
zFuseOperations.opendircCsTg}|j|�}|jj|d�\}}||kr0d\}}||8}|j}�xt|t|��D]�}	||	\}
}}||kr�||8}||7}|d7}qPt|||�}
|
|jkr�|j|
}||
t|�kr�|j|
=n0|jj	|
|j
j|
��}||
t|�kr�||j|
<|j||||
��d}||
8}|sP||jk�r6|jj|||f�n||f|j|<PqPWdj
|�S)NrrrN)rr)rr)r�r�r<r-�rangerO�minr�r^Zdecryptrl�appendZupdr�)r0�fhr*rh�partsr_Zchunk_norHr-r��id�srKr�rLrrr�readks>




zFuseOperations.readccshd|fd|j|fg}|j|j|j��x8t||d�|�D]"\}\}}||j|�|dfVq>WdS)Nr
s..r)rsr�ruror�r)r0rZoffZentries�ir�rGrrr�readdir�s zFuseOperations.readdircCs|j|�}tj|j�S)N)r�rvr�r�)r0rGr�r_rrr�readlink�s
zFuseOperations.readlink)F)N)N)N)N)N)N)N)N)N) rbrcrdrer�r�r�r�r�r1r�r�r�r�r�r�r�r�r�r�r�rrrr	r
rrrrr�
__classcell__rr)r�rrj�s8
M
,G








+rj).rrErvr�rfr�r+r��collectionsrr�rrr�rZcrypto.low_levelrZarchiverrr�rZ	hashindexr	Zhelpersr
rrr
rrr_rZlrucacherZremoterr�r�r�rr�rrprZ
Operationsrjrrrr�<module>s:+