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