
v^                 @   si   d  Z  d d l Z d d l Z d d l m Z d d l m Z d g Z d d   Z d d
 d d  Z	 d S)z
Move a file in the safest way possible::

    >>> from django.core.files.move import file_move_safe
    >>> file_move_safe("/tmp/old_file", "/tmp/new_file")
    N)copystat)locksfile_move_safec             C   s|   t  t j d  rB y t j j |  |  SWn t k
 rA d SYn Xt j j t j j |    t j j t j j |   k S)NsamefileF)hasattrospathr   OSErrornormcaseabspath)srcdst r   8/tmp/pip-build-8lau8j11/django/django/core/files/move.py	_samefile   s    	r   i   @   Fc          5   C   s  t  |  |  r d SyD | rB t j | t j  rB t d |   t j |  |  d SWn t k
 rk Yn Xt |  d   } t j | t j t j	 Bt
 t d d  B| s t j n d B } zO t j | t j  d } x/ | d k r
| j |  } t j | |  q WWd t j |  t j |  XWd QRXy t |  |  Wn: t k
 r~} z | j t j k rl  WYd d } ~ Xn Xy t j |   Wn@ t k
 r} z  t
 | d d  d k r  WYd d } ~ Xn Xd S)	aU  
    Move a file from one location to another in the safest way possible.

    First, try ``os.rename``, which is simple but will break across filesystems.
    If that fails, stream manually from one file to another in pure Python.

    If the destination file exists and ``allow_overwrite`` is ``False``, raise
    ``FileExistsError``.
    Nz8Destination file %s exists and allow_overwrite is False.rbO_BINARYr       winerror    )r   r   accessF_OKFileExistsErrorrenamer	   openO_WRONLYO_CREATgetattrO_EXCLr   lockLOCK_EXreadwriteZunlockcloser   PermissionErrorerrnoEPERMremove)Zold_file_nameZnew_file_name
chunk_sizeZallow_overwriteZold_filefdZcurrent_chunker   r   r   r      s<    &i   )
__doc__r&   r   shutilr   Zdjango.core.filesr   __all__r   r   r   r   r   r   <module>   s   	