Enqueue the contents of a file, or file-like object, FSQWorkItem,
       file-descriptor or the contents of a files queues at an address
       (e.g. '/my/file') queue with arbitrary arguments from one queue to
       other queues, reenqueue is to vreenqueue what printf is to vprintf
       Uses include:
            vreenqueue(FSQWorkItem, [trg_queue, ...], link=, kwargs)
            vreenqueue(fileish, file_name, [trg_queue, ...], kwargs)
            vreenqueue(fd, file_name, [trg_queue, ...], kwargs)

def vreenqueue(item_f, *args, **kwargs):
    '''Enqueue the contents of a file, or file-like object, FSQWorkItem,
       file-descriptor or the contents of a files queues at an address
       (e.g. '/my/file') queue with arbitrary arguments from one queue to
       other queues, reenqueue is to vreenqueue what printf is to vprintf
       Uses include:
            vreenqueue(FSQWorkItem, [trg_queue, ...], link=, kwargs)
            vreenqueue(fileish, file_name, [trg_queue, ...], kwargs)
            vreenqueue(fd, file_name, [trg_queue, ...], kwargs)
    '''
    item_id = None
    src_queue = kwargs.pop('src_queue', None)
    link = kwargs.pop('link', False)
    hosts = kwargs.pop('hosts', None)
    all_hosts = kwargs.pop('all_hosts', False)
    item_f, src_queue, item_id, args, link = _unpack_args(item_f, src_queue,
                                                          link, args)
    if 1 < len(args):
        raise ValueError('Too many arguements')
    try:
        args = args[0]
    except IndexError:
        raise ValueError('Insufficient arguments')
    try:
        if item_f is None:
            item_f = fsq_path.item(src_queue, item_id)
        if link:
            src_file = item_f
        else:
            src_file = rationalize_file(item_f, _c.FSQ_CHARSET)
    except (OSError, IOError, ), e:
        raise FSQReenqueueError(e.errno, wrap_io_os_err(e))
    tmp_names = []
    try:
        paths = _formhostpath(args, hosts, all_hosts)
        if link:
            tmp_name = os.path.join(fsq_path.tmp(src_queue), item_id)
            # hard link directly to tmp
            try:
                try:
                    os.link(fsq_path.item(src_queue, item_id), tmp_name)
                except (OSError, IOError, ), e:
                    if not e.errno == errno.EEXIST:
                        raise FSQReenqueueError(e.errno, wrap_io_os_err(e))
                for queue, host in paths:
                    try:
                        os.link(tmp_name, os.path.join(fsq_path.item(queue,
                                                       item_id, host=host)))
                    except (OSError, IOError, ), e:
                        if not e.errno == errno.EEXIST:
                            raise FSQReenqueueError(e.errno, wrap_io_os_err(e))
            finally:
                os.unlink(tmp_name)
        else:
            tmp_fos = []
            try:
                for queue, host in paths:
                    try:
                        tmp_name = os.path.join(fsq_path.tmp(queue, host=host),
                                                             item_id)
                        tmp_names.append(tmp_name)
                        # copy to n trg_queues
                        tmp_fo = os.open(tmp_name, os.O_RDWR|os.O_CREAT|\
                                               os.O_TRUNC, _c.FSQ_ITEM_MODE)
                        tmp_fos.append(os.fdopen(tmp_fo, 'wb', 1))
                    except Exception, e:
                        raise FSQReenqueueError(wrap_io_os_err(e))
                real_file = True if hasattr(src_file, 'fileno') else False
                # read src_file once
                while True:
                    if real_file:
                        reads, dis, card = select.select([src_file], [], [])
                        try:
                            chunk = os.read(reads[0].fileno(), 2048)
                        except (OSError, IOError, ), e:
                            if e.errno in (errno.EWOULDBLOCK, errno.EAGAIN,):
                                continue
                            raise
                    else:
                        chunk = src_file.readline()
                    if 0 == len(chunk):
                        break
                    for tmp_fo in tmp_fos:
                        tmp_fo.write(chunk)
                        # flush buffers, and force write to disk pre mv.
                        tmp_fo.flush()
                        os.fsync(tmp_fo.fileno())
                for queue, host in paths:
                    tmp_name = os.path.join(fsq_path.tmp(queue, host=host),
                                                         item_id)
                    # hard-link into queue, unlink tmp, failure case here
                    # leaves cruft in tmp, but no race condition into queue
                    try:
                        os.link(tmp_name, os.path.join(fsq_path.item(queue,
                                                       item_id, host=host)))
                    except (OSError, IOError, ), e:
                        if link and not e.errno == errno.EEXIST:
                            raise FSQReenqueueError(e.errno, wrap_io_os_err(e))
                    finally:
                        os.unlink(tmp_name)
            finally:
                for tmp_fo in tmp_fos:
                    tmp_fo.close()
        return item_id
    except Exception, e:
        try:
            if link:
                tmp_name = os.path.join(fsq_path.tmp(src_queue, item_id))
                try:
                    os.unlink(tmp_name)
                except (OSError, IOError, ), err:
                    if err.errno == errno.ENOENT:
                        pass
                raise FSQReenqueueError(err.errno, wrap_io_os_err(err))
            else:
                for tmp_name in tmp_names:
                    try:
                        os.unlink(tmp_name)
                    except (OSError, IOError, ), err:
                        if err.errno == errno.ENOENT:
                            pass
                        raise FSQReenqueueError(err.errno, wrap_io_os_err(err))
        except (OSError, IOError, ), err:
            if err.errno != errno.ENOENT:
               raise FSQReenqueueError(err.errno, wrap_io_os_err(err))
        except OSError, err:
            if err.errno != errno.ENOENT:
               raise FSQReenqueueError(err.errno, wrap_io_os_err(err))
        if (isinstance(e, OSError) or isinstance(e, IOError)) and\
                not isinstance(e, FSQError):
            raise FSQReenqueueError(e.errno, wrap_io_os_err(e))
        raise e
    finally:
        if not link:
            src_file.close()