Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 1 | /* Copyright 1996-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 | #include "sysincludes.h" |
| 18 | #include "msdos.h" |
| 19 | #include "mtools.h" |
| 20 | #include "file.h" |
| 21 | #include "llong.h" |
| 22 | |
| 23 | /* |
| 24 | * Copy the data from source to target |
| 25 | */ |
| 26 | |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 27 | mt_off_t copyfile(Stream_t *Source, Stream_t *Target) |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 28 | { |
| 29 | char buffer[8*16384]; |
| 30 | mt_off_t pos; |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 31 | ssize_t ret; |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 32 | ssize_t retw; |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 33 | |
| 34 | if (!Source){ |
| 35 | fprintf(stderr,"Couldn't open source file\n"); |
| 36 | return -1; |
| 37 | } |
| 38 | |
| 39 | if (!Target){ |
| 40 | fprintf(stderr,"Couldn't open target file\n"); |
| 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | pos = 0; |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 45 | while(1){ |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 46 | ret = READS(Source, buffer, 8*16384); |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 47 | if (ret < 0 ){ |
| 48 | perror("file read"); |
| 49 | return -1; |
| 50 | } |
| 51 | if(!ret) |
| 52 | break; |
| 53 | if(got_signal) |
| 54 | return -1; |
| 55 | if (ret == 0) |
| 56 | break; |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 57 | if ((retw = force_write(Target, buffer, (size_t) ret)) != ret){ |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 58 | if(retw < 0 ) |
| 59 | perror("write in copy"); |
| 60 | else |
| 61 | fprintf(stderr, |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 62 | "Short write %zd instead of %zd\n", |
| 63 | retw, ret); |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 64 | if(errno == ENOSPC) |
| 65 | got_signal = 1; |
| 66 | return ret; |
| 67 | } |
| 68 | pos += ret; |
| 69 | } |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 70 | return pos; |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 71 | } |