2 * Unix SMB/CIFS implementation.
3 * Utility functions to transfer files.
5 * Copyright (C) Jeremy Allison 2001-2002
6 * Copyright (C) Michael Adam 2008
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "transfer_file.h"
25 #include "lib/util/sys_rw.h"
27 /****************************************************************************
28 Transfer some data between two fd's.
29 ****************************************************************************/
31 #ifndef TRANSFER_BUF_SIZE
32 #define TRANSFER_BUF_SIZE 65536
36 ssize_t
transfer_file_internal(void *in_file
,
39 ssize_t (*pread_fn
)(void *, void *, size_t, off_t
),
40 ssize_t (*pwrite_fn
)(void *, const void *, size_t, off_t
))
46 size_t num_to_read_thistime
;
47 size_t num_written
= 0;
54 if ((buf
= SMB_MALLOC_ARRAY(char, TRANSFER_BUF_SIZE
)) == NULL
) {
59 num_to_read_thistime
= MIN((n
- total
), TRANSFER_BUF_SIZE
);
61 read_ret
= (*pread_fn
)(in_file
, buf
, num_to_read_thistime
, offset
);
63 DEBUG(0,("transfer_file_internal: read failure. "
64 "Error = %s\n", strerror(errno
) ));
74 while (num_written
< read_ret
) {
75 write_ret
= (*pwrite_fn
)(out_file
, buf
+ num_written
,
76 read_ret
- num_written
,
77 offset
+ num_written
);
79 if (write_ret
== -1) {
80 DEBUG(0,("transfer_file_internal: "
81 "write failure. Error = %s\n",
87 return (ssize_t
)total
;
90 num_written
+= (size_t)write_ret
;
93 total
+= (size_t)read_ret
;
94 offset
+= (off_t
)read_ret
;
98 return (ssize_t
)total
;
101 static ssize_t
sys_pread_fn(void *file
, void *buf
, size_t len
, off_t offset
)
103 int *fd
= (int *)file
;
105 return sys_pread(*fd
, buf
, len
, offset
);
108 static ssize_t
sys_pwrite_fn(void *file
, const void *buf
, size_t len
, off_t offset
)
110 int *fd
= (int *)file
;
112 return sys_pwrite(*fd
, buf
, len
, offset
);
115 off_t
transfer_file(int infd
, int outfd
, off_t n
)
117 return (off_t
)transfer_file_internal(&infd
, &outfd
, (size_t)n
,
118 sys_pread_fn
, sys_pwrite_fn
);