| 1 | /*
|
|---|
| 2 | * Unix SMB/CIFS implementation.
|
|---|
| 3 | * Utility functions to transfer files.
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright (C) Jeremy Allison 2001-2002
|
|---|
| 6 | * Copyright (C) Michael Adam 2008
|
|---|
| 7 | *
|
|---|
| 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.
|
|---|
| 12 | *
|
|---|
| 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.
|
|---|
| 17 | *
|
|---|
| 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/>.
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | #include <includes.h>
|
|---|
| 24 | #include "transfer_file.h"
|
|---|
| 25 |
|
|---|
| 26 | /****************************************************************************
|
|---|
| 27 | Transfer some data between two fd's.
|
|---|
| 28 | ****************************************************************************/
|
|---|
| 29 |
|
|---|
| 30 | #ifndef TRANSFER_BUF_SIZE
|
|---|
| 31 | #define TRANSFER_BUF_SIZE 65536
|
|---|
| 32 | #endif
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | ssize_t transfer_file_internal(void *in_file,
|
|---|
| 36 | void *out_file,
|
|---|
| 37 | size_t n,
|
|---|
| 38 | ssize_t (*read_fn)(void *, void *, size_t),
|
|---|
| 39 | ssize_t (*write_fn)(void *, const void *, size_t))
|
|---|
| 40 | {
|
|---|
| 41 | char *buf;
|
|---|
| 42 | size_t total = 0;
|
|---|
| 43 | ssize_t read_ret;
|
|---|
| 44 | ssize_t write_ret;
|
|---|
| 45 | size_t num_to_read_thistime;
|
|---|
| 46 | size_t num_written = 0;
|
|---|
| 47 |
|
|---|
| 48 | if ((buf = SMB_MALLOC_ARRAY(char, TRANSFER_BUF_SIZE)) == NULL) {
|
|---|
| 49 | return -1;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | while (total < n) {
|
|---|
| 53 | num_to_read_thistime = MIN((n - total), TRANSFER_BUF_SIZE);
|
|---|
| 54 |
|
|---|
| 55 | read_ret = (*read_fn)(in_file, buf, num_to_read_thistime);
|
|---|
| 56 | if (read_ret == -1) {
|
|---|
| 57 | DEBUG(0,("transfer_file_internal: read failure. "
|
|---|
| 58 | "Error = %s\n", strerror(errno) ));
|
|---|
| 59 | SAFE_FREE(buf);
|
|---|
| 60 | return -1;
|
|---|
| 61 | }
|
|---|
| 62 | if (read_ret == 0) {
|
|---|
| 63 | break;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | num_written = 0;
|
|---|
| 67 |
|
|---|
| 68 | while (num_written < read_ret) {
|
|---|
| 69 | write_ret = (*write_fn)(out_file, buf + num_written,
|
|---|
| 70 | read_ret - num_written);
|
|---|
| 71 |
|
|---|
| 72 | if (write_ret == -1) {
|
|---|
| 73 | DEBUG(0,("transfer_file_internal: "
|
|---|
| 74 | "write failure. Error = %s\n",
|
|---|
| 75 | strerror(errno) ));
|
|---|
| 76 | SAFE_FREE(buf);
|
|---|
| 77 | return -1;
|
|---|
| 78 | }
|
|---|
| 79 | if (write_ret == 0) {
|
|---|
| 80 | return (ssize_t)total;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | num_written += (size_t)write_ret;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | total += (size_t)read_ret;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | SAFE_FREE(buf);
|
|---|
| 90 | return (ssize_t)total;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | static ssize_t sys_read_fn(void *file, void *buf, size_t len)
|
|---|
| 94 | {
|
|---|
| 95 | int *fd = (int *)file;
|
|---|
| 96 |
|
|---|
| 97 | return sys_read(*fd, buf, len);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | static ssize_t sys_write_fn(void *file, const void *buf, size_t len)
|
|---|
| 101 | {
|
|---|
| 102 | int *fd = (int *)file;
|
|---|
| 103 |
|
|---|
| 104 | return sys_write(*fd, buf, len);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | SMB_OFF_T transfer_file(int infd, int outfd, SMB_OFF_T n)
|
|---|
| 108 | {
|
|---|
| 109 | return (SMB_OFF_T)transfer_file_internal(&infd, &outfd, (size_t)n,
|
|---|
| 110 | sys_read_fn, sys_write_fn);
|
|---|
| 111 | }
|
|---|