blob: 7884c22fa75d6078e9bfb772be2da5dc63ca8f72 [file] [log] [blame]
Yi Kong39bbd962022-01-09 19:41:38 +08001/* Copyright 1996,1997,1999,2001,2002,2009,2021 Alain Knaff.
Alistair Delvabeaee832021-02-24 11:27:23 -08002 * 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 *
Yi Kong39bbd962022-01-09 19:41:38 +080021 * Alain L. Knaff
Alistair Delvabeaee832021-02-24 11:27:23 -080022 * alain@knaff.lu
23 *
24 */
25
26#include "sysincludes.h"
27#include "msdos.h"
28#include "stream.h"
29
Yi Kong39bbd962022-01-09 19:41:38 +080030static 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))
Alistair Delvabeaee832021-02-24 11:27:23 -080033{
Yi Kong39bbd962022-01-09 19:41:38 +080034 ssize_t ret;
Alistair Delvabeaee832021-02-24 11:27:23 -080035 int done=0;
Yi Kong39bbd962022-01-09 19:41:38 +080036
Alistair Delvabeaee832021-02-24 11:27:23 -080037 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 }
Yi Kong39bbd962022-01-09 19:41:38 +080045 assert((size_t)ret <= len);
46 start += (size_t) ret;
Alistair Delvabeaee832021-02-24 11:27:23 -080047 done += ret;
Yi Kong39bbd962022-01-09 19:41:38 +080048 len -= (size_t) ret;
Alistair Delvabeaee832021-02-24 11:27:23 -080049 buf += ret;
50 }
51 return done;
52}
53
Yi Kong39bbd962022-01-09 19:41:38 +080054static ssize_t write_wrapper(Stream_t *Stream, char *buf,
55 mt_off_t start UNUSEDP, size_t len)
Alistair Delvabeaee832021-02-24 11:27:23 -080056{
Yi Kong39bbd962022-01-09 19:41:38 +080057 return Stream->Class->write(Stream, buf, len);
Alistair Delvabeaee832021-02-24 11:27:23 -080058}
59
Yi Kong39bbd962022-01-09 19:41:38 +080060ssize_t force_write(Stream_t *Stream, char *buf, size_t len)
Alistair Delvabeaee832021-02-24 11:27:23 -080061{
Yi Kong39bbd962022-01-09 19:41:38 +080062 return force_pio(Stream, buf, 0, len, write_wrapper);
63}
64
65ssize_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
71ssize_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);
Alistair Delvabeaee832021-02-24 11:27:23 -080075}