Moved common send_file() code to a separate function.
[uftps.git] / send_file-generic.c
blob7281133a69edaa73a5d25030931cf01ee197aba8
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 <unistd.h>
22 #include <fcntl.h>
25 * Generic RETR command implementation.
27 void send_file (void)
29 int f, b, e;
30 off_t size, progress = 0;
32 f = open_file(&size);
33 if (f == -1)
34 return;
36 /* Apply a possible previous REST command */
37 if (SS.file_offset > 0)
39 progress = lseek(f, SS.file_offset, SEEK_SET);
40 if (progress == -1)
42 error("Seeking file %s", SS.arg);
43 reply_c("450 Could not restart transfer.\r\n");
44 close(f);
45 return;
49 e = open_data_channel();
50 if (e == -1)
52 close(f);
53 return;
56 reply_c("150 Sending file content.\r\n");
59 * Main transfer loop. We use the auxiliary buffer to temporarily store
60 * chunks of file.
62 while (progress < size)
64 b = read(f, SS.aux, LINE_SIZE);
65 if (b == -1)
66 break; /* Cannot show a useful error message here */
68 e = data_reply(SS.aux, b);
69 if (e == -1)
70 break;
72 progress += b;
75 if (b != -1 && e != -1)
76 reply_c("226 File content sent.\r\n");
77 else
78 reply_c("426 Something happened, transfer aborted.\r\n");
80 close(f);
81 close(SS.data_sk);
82 SS.data_sk = -1;
83 SS.file_offset = 0;