1 /* Copyright 1996,1997,1999,2001,2002,2009,2021 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
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 static ssize_t force_pio(Stream_t *Stream,
31 char *buf, mt_off_t start, size_t len,
32 ssize_t (*io)(Stream_t *, char *, mt_off_t, size_t))
33 {
34 ssize_t 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 assert((size_t)ret <= len);
46 start += (size_t) ret;
47 done += ret;
48 len -= (size_t) ret;
49 buf += ret;
50 }
51 return done;
52 }
53
write_wrapper(Stream_t * Stream,char * buf,mt_off_t start UNUSEDP,size_t len)54 static ssize_t write_wrapper(Stream_t *Stream, char *buf,
55 mt_off_t start UNUSEDP, size_t len)
56 {
57 return Stream->Class->write(Stream, buf, len);
58 }
59
force_write(Stream_t * Stream,char * buf,size_t len)60 ssize_t force_write(Stream_t *Stream, char *buf, size_t len)
61 {
62 return force_pio(Stream, buf, 0, len, write_wrapper);
63 }
64
force_pwrite(Stream_t * Stream,char * buf,mt_off_t start,size_t len)65 ssize_t force_pwrite(Stream_t *Stream, char *buf, mt_off_t start, size_t len)
66 {
67 return force_pio(Stream, buf, start, len,
68 Stream->Class->pwrite);
69 }
70
force_pread(Stream_t * Stream,char * buf,mt_off_t start,size_t len)71 ssize_t force_pread(Stream_t *Stream, char *buf, mt_off_t start, size_t len)
72 {
73 return force_pio(Stream, buf, start, len,
74 Stream->Class->pread);
75 }
76