store filename in struct tip_client
parent
95a06d7111
commit
7d70e7abec
55
src/main.c
55
src/main.c
|
@ -37,7 +37,6 @@
|
|||
typeof( ((type *)0)->member ) *__mptr = (ptr); \
|
||||
(type *)( (char *)__mptr - offsetof(type,member) );})
|
||||
|
||||
static const char *filename;
|
||||
static const char *addr;
|
||||
struct ev_loop *tip_main_loop;
|
||||
|
||||
|
@ -52,6 +51,7 @@ enum {
|
|||
struct tip_client {
|
||||
ev_io io;
|
||||
struct sockaddr_in addr;
|
||||
const char *filename;
|
||||
char buf[10240000];
|
||||
uint32_t buf_len;
|
||||
uint64_t data_len;
|
||||
|
@ -121,7 +121,7 @@ static void tip_client_progress(struct tip_client *cli, bool now)
|
|||
printf("%3lu%% (%lu Mbytes/second) %s from %s:9999\n",
|
||||
cli->content_len > 0 ? 100 * cli->data_len / cli->content_len : 0,
|
||||
tv.tv_sec > 0 ? cli->data_len / 1024000 / tv.tv_sec : cli->data_len / 1024000,
|
||||
filename, inet_ntoa(cli->addr.sin_addr));
|
||||
cli->filename, inet_ntoa(cli->addr.sin_addr));
|
||||
cli->tv_last = tv_cur;
|
||||
}
|
||||
}
|
||||
|
@ -141,7 +141,7 @@ static int tip_client_get_hdr(struct tip_client *cli)
|
|||
return 0;
|
||||
|
||||
if (!strncmp(cli->buf, "HTTP/1.1 404 Not Found", strlen("HTTP/1.1 404 Not Found"))) {
|
||||
syslog(LOG_ERR, "server says file `%s' not found\n", filename);
|
||||
syslog(LOG_ERR, "server says file `%s' not found\n", cli->filename);
|
||||
return -1;
|
||||
}
|
||||
if (!strncmp(cli->buf, "HTTP/1.1 301 Moves Permanently", strlen("HTTP/1.1 301 Moves Permanently"))) {
|
||||
|
@ -161,7 +161,7 @@ static int tip_client_get_hdr(struct tip_client *cli)
|
|||
ptr[0] = '\0';
|
||||
|
||||
syslog(LOG_INFO, "Redirected to %s to fetch file %s\n",
|
||||
redirect_addr, filename);
|
||||
redirect_addr, cli->filename);
|
||||
|
||||
cli->redirected = true;
|
||||
tip_client_close(cli);
|
||||
|
@ -202,7 +202,7 @@ static int tip_client_get_hdr(struct tip_client *cli)
|
|||
ret = write(cli->fd, payload, payload_len);
|
||||
if (ret < 0) {
|
||||
syslog(LOG_ERR, "failed to write to file %s: %s",
|
||||
filename, strerror(errno));
|
||||
cli->filename, strerror(errno));
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
@ -219,7 +219,7 @@ static int tip_client_get_payload(struct tip_client *cli)
|
|||
ret = write(cli->fd, cli->buf, cli->buf_len);
|
||||
if (ret < 0) {
|
||||
syslog(LOG_ERR, "failed to write to file %s: %s",
|
||||
filename, strerror(errno));
|
||||
cli->filename, strerror(errno));
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -262,7 +262,7 @@ static int tip_client_head_hdr(struct tip_client *cli)
|
|||
return 0;
|
||||
|
||||
if (!strncmp(cli->buf, "HTTP/1.1 404 Not Found", strlen("HTTP/1.1 404 Not Found"))) {
|
||||
syslog(LOG_ERR, "server says file `%s' not found\n", filename);
|
||||
syslog(LOG_ERR, "server says file `%s' not found\n", cli->filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -275,21 +275,21 @@ static int tip_client_head_hdr(struct tip_client *cli)
|
|||
if (cli->content_len < 0)
|
||||
return -1;
|
||||
if (cli->content_len == 0) {
|
||||
syslog(LOG_ERR, "server reports zero size file %s", filename);
|
||||
syslog(LOG_ERR, "server reports zero size file %s", cli->filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
cli->fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
||||
cli->fd = open(cli->filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
||||
if (cli->fd < 0) {
|
||||
syslog(LOG_ERR, "failed to open file %s: %s",
|
||||
filename, strerror(errno));
|
||||
cli->filename, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (fallocate(cli->fd, 0, 0, cli->content_len) < 0) {
|
||||
syslog(LOG_ERR, "failed to allocate room for file %s: %s",
|
||||
filename, strerror(errno));
|
||||
delete_file(filename);
|
||||
cli->filename, strerror(errno));
|
||||
delete_file(cli->filename);
|
||||
cli->fatal = true;
|
||||
return -1;
|
||||
}
|
||||
|
@ -402,7 +402,7 @@ static void tip_client_connect_cb(struct ev_loop *loop, struct ev_io *io, int ev
|
|||
len = sizeof(cli->addr);
|
||||
ret = connect(cli->io.fd, (struct sockaddr *)&cli->addr, len);
|
||||
if (ret < 0) {
|
||||
syslog(LOG_ERR, "failed to connect to server to fetch %s", filename);
|
||||
syslog(LOG_ERR, "failed to connect to server to fetch %s", cli->filename);
|
||||
tip_client_error(cli);
|
||||
return;
|
||||
}
|
||||
|
@ -410,27 +410,27 @@ static void tip_client_connect_cb(struct ev_loop *loop, struct ev_io *io, int ev
|
|||
switch (cli->state) {
|
||||
case TIP_CLIENT_GET_HEADER:
|
||||
syslog(LOG_INFO, "connected to %s to fetch file %s\n",
|
||||
inet_ntoa(cli->addr.sin_addr), filename);
|
||||
inet_ntoa(cli->addr.sin_addr), cli->filename);
|
||||
if (cli->server_only)
|
||||
snprintf(buf, sizeof(buf), "GET /%s HTTP/1.1\r\nX-Accept-Redirect: off\r\n\r\n", filename);
|
||||
snprintf(buf, sizeof(buf), "GET /%s HTTP/1.1\r\nX-Accept-Redirect: off\r\n\r\n", cli->filename);
|
||||
else
|
||||
snprintf(buf, sizeof(buf), "GET /%s HTTP/1.1\r\n\r\n", filename);
|
||||
snprintf(buf, sizeof(buf), "GET /%s HTTP/1.1\r\n\r\n", cli->filename);
|
||||
break;
|
||||
case TIP_CLIENT_HEAD_HEADER:
|
||||
syslog(LOG_INFO, "connected to %s to get file size of %s\n",
|
||||
inet_ntoa(cli->addr.sin_addr), filename);
|
||||
snprintf(buf, sizeof(buf), "HEAD /%s HTTP/1.1\r\n\r\n", filename);
|
||||
inet_ntoa(cli->addr.sin_addr), cli->filename);
|
||||
snprintf(buf, sizeof(buf), "HEAD /%s HTTP/1.1\r\n\r\n", cli->filename);
|
||||
break;
|
||||
case TIP_CLIENT_POST_REDIRECT:
|
||||
syslog(LOG_INFO, "connected to %s to report redirection for %s\n",
|
||||
inet_ntoa(cli->addr.sin_addr), filename);
|
||||
snprintf(buf, sizeof(buf), "POST /%s HTTP/1.1\r\n\r\n", filename);
|
||||
inet_ntoa(cli->addr.sin_addr), cli->filename);
|
||||
snprintf(buf, sizeof(buf), "POST /%s HTTP/1.1\r\n\r\n", cli->filename);
|
||||
break;
|
||||
}
|
||||
|
||||
ret = send(cli->io.fd, buf, strlen(buf), 0);
|
||||
if (ret < 0) {
|
||||
syslog(LOG_ERR, "failed to send request for %s", filename);
|
||||
syslog(LOG_ERR, "failed to send request for %s", cli->filename);
|
||||
tip_client_error(cli);
|
||||
return;
|
||||
}
|
||||
|
@ -479,7 +479,8 @@ static int tip_client_connect(struct tip_client *cli, const char *addr)
|
|||
len = sizeof(cli->addr);
|
||||
ret = connect(remote_fd, (struct sockaddr *)&cli->addr, len);
|
||||
if (ret < 0 && errno != EINPROGRESS) {
|
||||
syslog(LOG_ERR, "failed to connect to server to fetch %s", filename);
|
||||
syslog(LOG_ERR, "failed to connect to server to fetch %s",
|
||||
cli->filename);
|
||||
tip_client_error(cli);
|
||||
return ret;
|
||||
}
|
||||
|
@ -496,6 +497,7 @@ static int tip_client_connect(struct tip_client *cli, const char *addr)
|
|||
static int tip_client_request_file(struct tip_client *cli,
|
||||
const char *server, const char *filename)
|
||||
{
|
||||
cli->filename = filename;
|
||||
tip_client_connect(cli, server);
|
||||
|
||||
while (cli->state != TIP_CLIENT_DONE && !cli->error)
|
||||
|
@ -590,13 +592,12 @@ static int tip_client_run(struct tip_client *cli, int fd, const char *addr,
|
|||
return 0;
|
||||
}
|
||||
|
||||
static char _filename[PATH_MAX + 1];
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct timeval tv_start, tv_stop, tv;
|
||||
uint64_t data_len = 0, file_size = 0;
|
||||
bool file_chunk[MAX_CHUNKS] = {};
|
||||
char filename[PATH_MAX + 1];
|
||||
uint64_t chunk_size;
|
||||
int i, k, fd, ret;
|
||||
|
||||
|
@ -605,7 +606,6 @@ int main(int argc, char *argv[])
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
addr = argv[1];
|
||||
filename = argv[2];
|
||||
|
||||
openlog("tiptorrent-client", LOG_PID, LOG_DAEMON);
|
||||
|
||||
|
@ -618,7 +618,7 @@ int main(int argc, char *argv[])
|
|||
do {
|
||||
tip_client_reset_state(&_cli, -1, 0);
|
||||
_cli.state = TIP_CLIENT_HEAD_HEADER;
|
||||
ret = tip_client_request_file(&_cli, addr, filename);
|
||||
ret = tip_client_request_file(&_cli, addr, argv[2]);
|
||||
} while (ret > 0);
|
||||
|
||||
if (ret < 0)
|
||||
|
@ -630,8 +630,7 @@ int main(int argc, char *argv[])
|
|||
|
||||
for (i = 0; i < MAX_CHUNKS; i++) {
|
||||
k = select_file_chunk(file_chunk);
|
||||
snprintf(_filename, sizeof(_filename), "%s.%u", argv[2], k);
|
||||
filename = _filename;
|
||||
snprintf(filename, sizeof(filename), "%s.%u", argv[2], k);
|
||||
|
||||
if (tip_client_run(&_cli, fd, addr, filename, k, chunk_size) < 0)
|
||||
goto err_bailout;
|
||||
|
|
Loading…
Reference in New Issue