1/*  Copyright 1996,1997,1999,2001,2002,2009 Alain Knaff.
2 *  This file is part of mtools.
3 *
4 *  Mtools is free software: you can redistribute it and/or modify
5 *  it under the terms of the GNU General Public License as published by
6 *  the Free Software Foundation, either version 3 of the License, or
7 *  (at your option) any later version.
8 *
9 *  Mtools is distributed in the hope that it will be useful,
10 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 *  GNU General Public License for more details.
13 *
14 *  You should have received a copy of the GNU General Public License
15 *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Force I/O to be done to complete transfer length
18 *
19 * written by:
20 *
21 * Alain L. Knaff
22 * alain@knaff.lu
23 *
24 */
25
26#include "sysincludes.h"
27#include "msdos.h"
28#include "stream.h"
29
30static int force_io(Stream_t *Stream,
31		    char *buf, mt_off_t start, size_t len,
32		    int (*io)(Stream_t *, char *, mt_off_t, size_t))
33{
34	int ret;
35	int done=0;
36
37	while(len){
38		ret = io(Stream, buf, start, len);
39		if ( ret <= 0 ){
40			if (done)
41				return done;
42			else
43				return ret;
44		}
45		start += ret;
46		done += ret;
47		len -= ret;
48		buf += ret;
49	}
50	return done;
51}
52
53int force_write(Stream_t *Stream, char *buf, mt_off_t start, size_t len)
54{
55	return force_io(Stream, buf, start, len,
56					Stream->Class->write);
57}
58
59int force_read(Stream_t *Stream, char *buf, mt_off_t start, size_t len)
60{
61	return force_io(Stream, buf, start, len,
62					Stream->Class->read);
63}
64