ó õùPc@sˆdZddlmZddlZddlZddlZddlTddlTddlm Z ddl m Z de fd„ƒYZ dS( s L{SFTPFile} iÿÿÿÿ(thexlifyN(t*(t BufferedFile(tSFTPAttributestSFTPFilecBseZdZdZddd„Zd„Zd„Zed„Zd„Z d „Z d „Z d „Z d „Z d „Zd„Zd„Zdd„Zd„Zd„Zd„Zd„Zd„Zdddd„Zed„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Z RS( sL Proxy object for a file on the remote server, in client mode SFTP. i€triÿÿÿÿcCsltj|ƒ||_||_tj|||ƒt|_t|_t|_i|_ g|_ d|_ dS(N( Rt__init__tsftpthandlet _set_modetFalset pipelinedt _prefetchingt_prefetch_donet_prefetch_datat_prefetch_readstNonet_saved_exception(tselfRRtmodetbufsize((s-..\python\site-packages\paramiko\sftp_file.pyR+s        cCs|jdtƒdS(Ntasync(t_closetTrue(R((s-..\python\site-packages\paramiko\sftp_file.pyt__del__7scCs|jdtƒdS(NR(RR (R((s-..\python\site-packages\paramiko\sftp_file.pytclose:scCsÉ|jr dS|jjtdt|jƒƒ|jrI|jj|ƒntj |ƒyB|r|jj t dƒt |jƒn|jjt |jƒWn*tk r«nttjfk rÄnXdS(Ns close(%s)(t_closedRt_logtDEBUGRRR t_finish_responsesRRt_async_requestttypeRt CMD_CLOSEt_requesttEOFErrortIOErrortsocketterror(RR((s-..\python\site-packages\paramiko\sftp_file.pyR=s    " cCs®g|jD]}|d|kr |^q }t|ƒdkrBtS|jd„ƒ|d\}}|||krvtS||||krŽtS|j||||||ƒS(NicSst|d|dƒS(Ni(tcmp(txty((s-..\python\site-packages\paramiko\sftp_file.pyt\siÿÿÿÿ(RtlenR tsortRt_data_in_prefetch_requests(Rtoffsettsizetitkt buf_offsettbuf_size((s-..\python\site-packages\paramiko\sftp_file.pyR,Xs,cCs{g|jjƒD]}||kr|^q}t|ƒdkrDdSt|ƒ}||}|t|j|ƒkrwdS|S(s if a block of data is present in the prefetch buffers, at the given offset, return the offset of the relevant prefetch buffer. otherwise, return None. this guarantees nothing about the number of bytes collected in the prefetch buffer so far. iN(RtkeysR*Rtmax(RR-R/R0tindexR1((s-..\python\site-packages\paramiko\sftp_file.pyt_data_in_prefetch_buffersgs.  cCsþxYtr[|j|jƒ}|dk r+Pn|js=|jrAPn|jjƒ|jƒqW|dkrut |_ dS|j |}|j |=|j|}|dkrÃ|| |j |<||}n|t |ƒkrú|||j |j|<|| }n|S(s£ read data out of the prefetch buffer, if possible. if the data isn't in the buffer, return None. otherwise, behaves like a normal read. iN( RR6t_realposRR RRt_read_responset_check_exceptionR R RR*(RR.R-tprefetchR1((s-..\python\site-packages\paramiko\sftp_file.pyt_read_prefetchxs*           cCs•t||jƒ}|jr=|j|ƒ}|dk r=|Sn|jjt|jt |j ƒt |ƒƒ\}}|t kr‹t dƒ‚n|jƒS(Ns Expected data(tmintMAX_REQUEST_SIZER R;RRR!tCMD_READRtlongR7tinttCMD_DATAt SFTPErrort get_string(RR.tdatatttmsg((s-..\python\site-packages\paramiko\sftp_file.pyt_read•s  3 cCs¨tt|ƒ|jƒ}|jjtdƒt|jt |j ƒt || ƒƒ}|j sn|jj jƒr¤|jj|ƒ\}}|tkr¤tdƒ‚q¤n|S(NsExpected status(R<R*R=RRRRt CMD_WRITERR?R7tstrR tsockt recv_readyR8t CMD_STATUSRB(RRDtchunktreqRERF((s-..\python\site-packages\paramiko\sftp_file.pyt_write s: cCs|jjj|ƒdS(sA Set a timeout on read/write operations on the underlying socket or ssh L{Channel}. @see: L{Channel.settimeout} @param timeout: seconds to wait for a pending read/write operation before raising C{socket.timeout}, or C{None} for no timeout @type timeout: float N(RRJt settimeout(Rttimeout((s-..\python\site-packages\paramiko\sftp_file.pyRP«s cCs|jjjƒS(sÁ Returns the timeout in seconds (as a float) associated with the socket or ssh L{Channel} used for this file. @see: L{Channel.gettimeout} @rtype: float (RRJt gettimeout(R((s-..\python\site-packages\paramiko\sftp_file.pyRR·scCs|jjj|ƒdS(s Set blocking or non-blocking mode on the underiying socket or ssh L{Channel}. @see: L{Channel.setblocking} @param blocking: 0 to set non-blocking mode; non-0 to set blocking mode. @type blocking: int N(RRJt setblocking(Rtblocking((s-..\python\site-packages\paramiko\sftp_file.pyRSÁs icCs€|jƒ||jkr,||_|_nG||jkrY|j|7_|j|_n|jƒ||_|_d|_dS(Nt(tflushtSEEK_SETR7t_postSEEK_CURt _get_sizet_rbuffer(RR-twhence((s-..\python\site-packages\paramiko\sftp_file.pytseekÍs cCsF|jjt|jƒ\}}|tkr9tdƒ‚ntj|ƒS(s Retrieve information about this file from the remote system. This is exactly like L{SFTP.stat}, except that it operates on an already-open file. @return: an object containing attributes about this file. @rtype: SFTPAttributes sExpected attributes(RR!t CMD_FSTATRt CMD_ATTRSRBRt _from_msg(RRERF((s-..\python\site-packages\paramiko\sftp_file.pytstatØs  cCsU|jjtdt|jƒ|fƒtƒ}||_|jjt|j|ƒdS(sé Change the mode (permissions) of this file. The permissions are unix-style and identical to those used by python's C{os.chmod} function. @param mode: new permissions @type mode: int s chmod(%s, %r)N( RRRRRRtst_modeR!t CMD_FSETSTAT(RRtattr((s-..\python\site-packages\paramiko\sftp_file.pytchmodæs &  cCsb|jjtdt|jƒ||fƒtƒ}|||_|_|jjt |j|ƒdS(s| Change the owner (C{uid}) and group (C{gid}) of this file. As with python's C{os.chown} function, you must pass both arguments, so if you only want to change one, use L{stat} first to retrieve the current owner and group. @param uid: new owner's uid @type uid: int @param gid: new group id @type gid: int schown(%s, %r, %r)N( RRRRRRtst_uidtst_gidR!Rc(RtuidtgidRd((s-..\python\site-packages\paramiko\sftp_file.pytchownôs ) cCs…|dkr'tjƒtjƒf}n|jjtdt|jƒ|fƒtƒ}|\|_|_ |jj t |j|ƒdS(sm Set the access and modified times of this file. If C{times} is C{None}, then the file's access and modified times are set to the current time. Otherwise, C{times} must be a 2-tuple of numbers, of the form C{(atime, mtime)}, which is used to set the access and modified times, respectively. This bizarre API is mimicked from python for the sake of consistency -- I apologize. @param times: C{None} or a tuple of (access time, modified time) in standard internet epoch time (seconds since 01 January 1970 GMT) @type times: tuple(int) s utime(%s, %r)N( RttimeRRRRRRtst_atimetst_mtimeR!Rc(RttimesRd((s-..\python\site-packages\paramiko\sftp_file.pytutimes & cCsU|jjtdt|jƒ|fƒtƒ}||_|jjt|j|ƒdS(s Change the size of this file. This usually extends or shrinks the size of the file, just like the C{truncate()} method on python file objects. @param size: the new size of the file @type size: int or long struncate(%s, %r)N( RRRRRRtst_sizeR!Rc(RR.Rd((s-..\python\site-packages\paramiko\sftp_file.pyttruncates &  c Csa|jjtd|j|t|ƒt|ƒ|ƒ\}}|jƒ}|jƒ}|jƒ} | S(s€ Ask the server for a hash of a section of this file. This can be used to verify a successful upload or download, or for various rsync-like operations. The file is hashed from C{offset}, for C{length} bytes. If C{length} is 0, the remainder of the file is hashed. Thus, if both C{offset} and C{length} are zero, the entire file is hashed. Normally, C{block_size} will be 0 (the default), and this method will return a byte string representing the requested hash (for example, a string of length 16 for MD5, or 20 for SHA-1). If a non-zero C{block_size} is given, each chunk of the file (from C{offset} to C{offset + length}) of C{block_size} bytes is computed as a separate hash. The hash results are all concatenated and returned as a single string. For example, C{check('sha1', 0, 1024, 512)} will return a string of length 40. The first 20 bytes will be the SHA-1 of the first 512 bytes of the file, and the last 20 bytes will be the SHA-1 of the next 512 bytes. @param hash_algorithm: the name of the hash algorithm to use (normally C{"sha1"} or C{"md5"}) @type hash_algorithm: str @param offset: offset into the file to begin hashing (0 means to start from the beginning) @type offset: int or long @param length: number of bytes to hash (0 means continue to the end of the file) @type length: int or long @param block_size: number of bytes to hash per result (must not be less than 256; 0 means to compute only one hash of the entire segment) @type block_size: int @return: string of bytes representing the hash of each block, concatenated together @rtype: str @note: Many (most?) servers don't support this extension yet. @raise IOError: if the server doesn't support the "check-file" extension, or possibly doesn't support the hash algorithm requested @since: 1.4 s check-file(RR!t CMD_EXTENDEDRR?RCt get_remainder( Rthash_algorithmR-tlengtht block_sizeRERFtexttalgRD((s-..\python\site-packages\paramiko\sftp_file.pytcheck's /$   cCs ||_dS(sì Turn on/off the pipelining of write operations to this file. When pipelining is on, paramiko won't wait for the server response after each write operation. Instead, they're collected as they come in. At the first non-write operation (including L{close}), all remaining server responses are collected. This means that if there was an error with one of your later writes, an exception might be thrown from within L{close} instead of L{write}. By default, files are I{not} pipelined. @param pipelined: C{True} if pipelining should be turned on for this file; C{False} otherwise @type pipelined: bool @since: 1.5 N(R (RR ((s-..\python\site-packages\paramiko\sftp_file.pyt set_pipelined]scCsŠ|jƒj}g}|j}xC||krct|j||ƒ}|j||fƒ||7}q!Wt|ƒdkr†|j|ƒndS(s„ Pre-fetch the remaining contents of this file in anticipation of future L{read} calls. If reading the entire file, pre-fetching can dramatically improve the download speed by avoiding roundtrip latency. The file's contents are incrementally buffered in a background thread. The prefetched data is stored in a buffer until read via the L{read} method. Once data has been read, it's removed from the buffer. The data may be read in a random order (using L{seek}); chunks of the buffer that haven't been read will continue to be buffered. @since: 1.5.1 iN(RaRpR7R<R=tappendR*t_start_prefetch(RR.tchunkstnRM((s-..\python\site-packages\paramiko\sftp_file.pyR:qs ccsû|jjtdt|jƒ|fƒg}x‡|D]\}}|j|ƒs3|j||ƒrfq3nxI|dkr±t||jƒ}|j ||fƒ||7}||8}qiWq3W|j |ƒx1|D])}|j |dƒ|j |dƒVqÊWdS(s* Read a set of blocks from the file by (offset, length). This is more efficient than doing a series of L{seek} and L{read} calls, since the prefetch machinery is used to retrieve all the requested blocks at once. @param chunks: a list of (offset, length) tuples indicating which sections of the file to read @type chunks: list(tuple(long, int)) @return: a list of blocks read, in the same order as in C{chunks} @rtype: list(str) @since: 1.5.4 s readv(%s, %r)iiN( RRRRRR6R,R<R=R{R|R]tread(RR}t read_chunksR-R.t chunk_sizeR'((s-..\python\site-packages\paramiko\sftp_file.pytreadvŠs&!   cCs y|jƒjSWndSXdS(Ni(RaRp(R((s-..\python\site-packages\paramiko\sftp_file.pyRZ²scCs[t|_t|_|jj|ƒtjd|jd|fƒ}|j tƒ|j ƒdS(Nttargettargs( RR R R Rtextendt threadingtThreadt_prefetch_threadt setDaemontstart(RR}RE((s-..\python\site-packages\paramiko\sftp_file.pyR|¸s    cCsFx?|D]7\}}|jj|t|jt|ƒt|ƒƒqWdS(N(RRR>RR?R@(RR}R-Ru((s-..\python\site-packages\paramiko\sftp_file.pyRˆÁscCs´|tkrCy|jj|ƒWntk r>}||_nXdS|tkr^tdƒ‚n|jƒ}|jj dƒ\}}||j |s