
 X
                 @   s]   d  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  rE y t j j |  |  SWqE t k
 rA d SYqE Xn  t j j t j j |    t j j t j j |   k S)NsamefileF)hasattrospathr   OSErrornormcaseabspath)srcdst r   D/home/ubuntu/projects/ifolica/build/django/django/core/files/move.py	_samefile   s    r   i   @   Fc          &   C   s  t  |  |  r d SyG | rE t j | t j  rE t d |   n  t j |  |  d SWn t k
 rn 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 QXt |  |  y t j |   Wn[ t k
 r} z; t
 | d d  d k rt
 | d	 d  d
 k r  n  WYd d } ~ Xn Xd S)ag  
    Moves a file from one location to another in the safest way possible.

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

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