def _create_files(self, filename):
# XXX this is racy
assert filename
logger.debug('Opening files for ‘%s’', filename)
mkdir_p(os.path.dirname(filename))
self._part_filename = '%s.part' % (filename, )
self._part_fd = os.open(
self._part_filename, os.O_CREAT | os.O_WRONLY | os.O_NONBLOCK, 0o600)
try:
self._segments_file.lock()
except IOError as e:
# Clean up.
os.close(self._part_fd)
self._part_fd = -1
self._part_filename = None
if e.errno == errno.EAGAIN:
# Cannot acquire lock: some other process (or part of this
# process) is already downloading it. Clean up and watch that
# file for completion.
logger.debug('File ‘%s.sgt’ is locked: waiting on completion.',
filename)
self._watch_for_completion(filename)
return False
else:
raise
# XXX hack
return True
# Reserve space for the full file and truncate any existing content to
# the start of the final chunk (because it might be smaller than the
# chunk size).
size = self.chunk_size * (self._num_segments - 1)
try:
fallocate.fallocate(self._part_fd, 0, size)
except IOError as e: # if it fails, we might get surprises later, but it's ok.
logger.debug('Error calling fallocate(%u, 0, %u): %s' %
(self._part_fd, self._size, e.message))
try:
os.ftruncate(self._part_fd, size)
except IOError as e:
logger.debug('Error calling ftruncate(%u, %u): %s' %
(self._part_fd, size, e.message))
return True
评论列表
文章目录