Renamed the sendfile RETR flavour to linux.
[uftps.git] / send_file-linux.c
blob867055d434b26ccae83f61c7de8426cd0d24ed71
1 /*
2 * User FTP Server, Share folders over FTP without being root.
3 * Copyright (C) 2008 Isaac Jurado
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option) any later
8 * version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "uftps.h"
21 #include <sys/sendfile.h>
22 #include <unistd.h>
23 #include <fcntl.h>
27 * RETR command implementation usin Linux sendfile() to minimize memory copies.
28 * Due to the nature of the system call, it is not even necessary to perform the
29 * initial seek in case a REST command was issued.
31 void send_file (void)
33 int f, e;
34 off_t size;
36 f = open_file(&size);
37 if (f == -1)
38 return;
40 e = open_data_channel();
41 if (e == -1)
43 close(f);
44 return;
47 debug("Initial offset is %lld", (long long) SS.file_offset);
48 reply_c("150 Sending file content.\r\n");
50 /* Main transfer loop */
51 while (SS.file_offset < size && e != -1)
52 e = sendfile(SS.data_sk, f, &SS.file_offset, INT_MAX);
54 if (e != -1)
55 reply_c("226 File content sent.\r\n");
56 else
58 error("Sending file %s", SS.aux);
59 reply_c("426 Connection closed, transfer aborted.\r\n");
62 close(f);
63 close(SS.data_sk);
64 SS.data_sk = -1;
65 SS.file_offset = 0;