// place ->fd at offset off int kftp_connect_file(knetFile *fp) { int ret; long long file_size; if (fp->fd != -1) { netclose(fp->fd); if (fp->no_reconnect) kftp_get_response(fp); } kftp_pasv_prep(fp); kftp_send_cmd(fp, fp->size_cmd, 1); #ifndef _WIN32 if ( sscanf(fp->response,"%*d %lld", &file_size) != 1 ) { if(!knetsilent) { fprintf(stderr,"[kftp_connect_file] %s\n", fp->response); } return -1; } #else const char *p = fp->response; while (*p != ' ') ++p; while (*p < '0' || *p > '9') ++p; file_size = strtoint64(p); #endif fp->file_size = file_size; if (fp->offset>=0) { char tmp[32]; #ifndef _WIN32 sprintf(tmp, "REST %lld\r\n", (long long)fp->offset); #else strcpy(tmp, "REST "); int64tostr(tmp + 5, fp->offset); strcat(tmp, "\r\n"); #endif kftp_send_cmd(fp, tmp, 1); } kftp_send_cmd(fp, fp->retr, 0); kftp_pasv_connect(fp); ret = kftp_get_response(fp); if (ret != 150) { if(!knetsilent) { fprintf(stderr, "[kftp_connect_file] %s\n", fp->response); } netclose(fp->fd); fp->fd = -1; return -1; } fp->is_ready = 1; return 0; }
static int kftp_send_cmd(knetFile *ftp, const char *cmd, int is_get) { if (socket_wait(ftp->ctrl_fd, 0) <= 0) return -1; // socket is not ready for writing int len = strlen(cmd); if ( netwrite(ftp->ctrl_fd, cmd, len) != len ) return -1; return is_get? kftp_get_response(ftp) : 0; }
// place ->fd at offset off int kftp_connect_file(knetFile *fp) { int ret; long long file_size; if (fp->fd != -1) { netclose(fp->fd); if (fp->no_reconnect) kftp_get_response(fp); } kftp_pasv_prep(fp); kftp_send_cmd(fp, fp->size_cmd, 1); #ifndef _WIN32 // If the file does not exist, the response will be "550 Could not get file // size". Be silent on failure, hts_idx_load can be trying the existence of .csi or .tbi. if ( sscanf(fp->response,"%*d %lld", &file_size) != 1 ) return -1; #else const char *p = fp->response; while (*p != ' ') ++p; while (*p < '0' || *p > '9') ++p; file_size = strtoint64(p); #endif fp->file_size = file_size; if (fp->offset>=0) { char tmp[32]; #ifndef _WIN32 sprintf(tmp, "REST %lld\r\n", (long long)fp->offset); #else strcpy(tmp, "REST "); int64tostr(tmp + 5, fp->offset); strcat(tmp, "\r\n"); #endif kftp_send_cmd(fp, tmp, 1); } kftp_send_cmd(fp, fp->retr, 0); kftp_pasv_connect(fp); ret = kftp_get_response(fp); if (ret != 150) { fprintf(stderr, "[kftp_connect_file] %s\n", fp->response); netclose(fp->fd); fp->fd = -1; return -1; } fp->is_ready = 1; return 0; }
int kftp_connect(knetFile *ftp) { ftp->ctrl_fd = socket_connect(ftp->host, ftp->port); if (ftp->ctrl_fd == -1) return -1; kftp_get_response(ftp); kftp_send_cmd(ftp, "USER anonymous\r\n", 1); kftp_send_cmd(ftp, "PASS kftp@\r\n", 1); kftp_send_cmd(ftp, "TYPE I\r\n", 1); return 0; }
static int kftp_send_cmd(knetFile *ftp, const char *cmd, int is_get) { ssize_t byteswritten; /* tabix/knetfile.c:252:2: warning: ISO C90 forbids mixed declarations and code [-Wpedantic] */ if (socket_wait(ftp->ctrl_fd, 0) <= 0) return -1; /* socket is not ready for writing */ byteswritten = netwrite(ftp->ctrl_fd, cmd, strlen(cmd)); if( 0 >= byteswritten ) /* @ubw fixed unused result warning */ { PRINT_ERROR( "[kftp_send_cmd] no (%d) bytes written!\n", byteswritten); return -1; } return is_get? kftp_get_response(ftp) : 0; }