# Path: airy/core/files/locks.py
#     LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
#     LOCK_SH = 0
#     LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
#     LOCK_EX = fcntl.LOCK_EX
#     LOCK_SH = fcntl.LOCK_SH
#     LOCK_NB = fcntl.LOCK_NB
#     LOCK_EX = LOCK_SH = LOCK_NB = None
# def fd(f):
#     def lock(file, flags):
#     def unlock(file):
#     def lock(file, flags):
#     def unlock(file):
#     def lock(file, flags):
#     def unlock(file):


import os
    import stat
from airy.core.files import locks
    from shutil import copystat
def file_move_safe(old_file_name, new_file_name, chunk_size = 1024*64, allow_overwrite=False):
    """
    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``.
    """

    # There's no reason to move if we don't have to.
    if _samefile(old_file_name, new_file_name):
        return

    try:
        os.rename(old_file_name, new_file_name)
        return
    except OSError:
        # This will happen with os.rename if moving to another filesystem
        # or when moving opened files on certain operating systems
        pass

    # first open the old file, so that it won't go away
    old_file = open(old_file_name, 'rb')
    try:
        # now open the new file, not forgetting allow_overwrite
        fd = os.open(new_file_name, os.O_WRONLY | os.O_CREAT | getattr(os, 'O_BINARY', 0) |
                                    (not allow_overwrite and os.O_EXCL or 0))
        try:

# Path: airy/core/files/locks.py
#     LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
#     LOCK_SH = 0
#     LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
#     LOCK_EX = fcntl.LOCK_EX
#     LOCK_SH = fcntl.LOCK_SH
#     LOCK_NB = fcntl.LOCK_NB
#     LOCK_EX = LOCK_SH = LOCK_NB = None
# def fd(f):
#     def lock(file, flags):
#     def unlock(file):
#     def lock(file, flags):
#     def unlock(file):
#     def lock(file, flags):
#     def unlock(file):


# Path: airy/core/files/move.py
import os
    import stat
from airy.core.files import locks
    from shutil import copystat

def file_move_safe(old_file_name, new_file_name, chunk_size = 1024*64, allow_overwrite=False):
    """
    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``.
    """

    # There's no reason to move if we don't have to.
    if _samefile(old_file_name, new_file_name):
        return

    try:
        os.rename(old_file_name, new_file_name)
        return
    except OSError:
        # This will happen with os.rename if moving to another filesystem
        # or when moving opened files on certain operating systems
        pass

    # first open the old file, so that it won't go away
    old_file = open(old_file_name, 'rb')
    try:
        # now open the new file, not forgetting allow_overwrite
        fd = os.open(new_file_name, os.O_WRONLY | os.O_CREAT | getattr(os, 'O_BINARY', 0) |
                                    (not allow_overwrite and os.O_EXCL or 0))
        try:

            locks.lock(fd, locks.LOCK_EX)