mirror of
https://github.com/fergalmoran/picard.git
synced 2026-02-27 01:54:06 +00:00
Merge pull request #592 from samj1912/picard943
PICARD-943: Remove monkey patching of windows file write methods
This commit is contained in:
@@ -86,107 +86,6 @@ def open(filename):
|
||||
return None
|
||||
|
||||
|
||||
def _insert_bytes_no_mmap(fobj, size, offset, BUFFER_SIZE=2**16):
|
||||
"""Insert size bytes of empty space starting at offset.
|
||||
|
||||
fobj must be an open file object, open rb+ or
|
||||
equivalent. Mutagen tries to use mmap to resize the file, but
|
||||
falls back to a significantly slower method if mmap fails.
|
||||
"""
|
||||
assert 0 < size
|
||||
assert 0 <= offset
|
||||
locked = False
|
||||
fobj.seek(0, 2)
|
||||
filesize = fobj.tell()
|
||||
movesize = filesize - offset
|
||||
fobj.write('\x00' * size)
|
||||
fobj.flush()
|
||||
try:
|
||||
locked = _win32_locking(fobj, filesize, msvcrt.LK_LOCK)
|
||||
fobj.truncate(filesize)
|
||||
|
||||
fobj.seek(0, 2)
|
||||
padsize = size
|
||||
# Don't generate an enormous string if we need to pad
|
||||
# the file out several megs.
|
||||
while padsize:
|
||||
addsize = min(BUFFER_SIZE, padsize)
|
||||
fobj.write("\x00" * addsize)
|
||||
padsize -= addsize
|
||||
|
||||
fobj.seek(filesize, 0)
|
||||
while movesize:
|
||||
# At the start of this loop, fobj is pointing at the end
|
||||
# of the data we need to move, which is of movesize length.
|
||||
thismove = min(BUFFER_SIZE, movesize)
|
||||
# Seek back however much we're going to read this frame.
|
||||
fobj.seek(-thismove, 1)
|
||||
nextpos = fobj.tell()
|
||||
# Read it, so we're back at the end.
|
||||
data = fobj.read(thismove)
|
||||
# Seek back to where we need to write it.
|
||||
fobj.seek(-thismove + size, 1)
|
||||
# Write it.
|
||||
fobj.write(data)
|
||||
# And seek back to the end of the unmoved data.
|
||||
fobj.seek(nextpos)
|
||||
movesize -= thismove
|
||||
|
||||
fobj.flush()
|
||||
finally:
|
||||
if locked:
|
||||
_win32_locking(fobj, filesize, msvcrt.LK_UNLCK)
|
||||
|
||||
|
||||
def _delete_bytes_no_mmap(fobj, size, offset, BUFFER_SIZE=2**16):
|
||||
"""Delete size bytes of empty space starting at offset.
|
||||
|
||||
fobj must be an open file object, open rb+ or
|
||||
equivalent. Mutagen tries to use mmap to resize the file, but
|
||||
falls back to a significantly slower method if mmap fails.
|
||||
"""
|
||||
locked = False
|
||||
assert 0 < size
|
||||
assert 0 <= offset
|
||||
fobj.seek(0, 2)
|
||||
filesize = fobj.tell()
|
||||
movesize = filesize - offset - size
|
||||
assert 0 <= movesize
|
||||
try:
|
||||
if movesize > 0:
|
||||
fobj.flush()
|
||||
locked = _win32_locking(fobj, filesize, msvcrt.LK_LOCK)
|
||||
fobj.seek(offset + size)
|
||||
buf = fobj.read(BUFFER_SIZE)
|
||||
while buf:
|
||||
fobj.seek(offset)
|
||||
fobj.write(buf)
|
||||
offset += len(buf)
|
||||
fobj.seek(offset + size)
|
||||
buf = fobj.read(BUFFER_SIZE)
|
||||
fobj.truncate(filesize - size)
|
||||
fobj.flush()
|
||||
finally:
|
||||
if locked:
|
||||
_win32_locking(fobj, filesize, msvcrt.LK_UNLCK)
|
||||
|
||||
|
||||
def _win32_locking(fobj, nbytes, mode):
|
||||
try:
|
||||
fobj.seek(0)
|
||||
msvcrt.locking(fobj.fileno(), mode, nbytes)
|
||||
except IOError:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
|
||||
if sys.platform == 'win32':
|
||||
import msvcrt
|
||||
_util.insert_bytes = _insert_bytes_no_mmap
|
||||
_util.delete_bytes = _delete_bytes_no_mmap
|
||||
|
||||
|
||||
from picard.formats.id3 import (
|
||||
AiffFile,
|
||||
MP3File,
|
||||
|
||||
Reference in New Issue
Block a user