DOWNLOADING,
UPLOADING,
LISTING_DIR,
- DISCONNECTED
+ DISCONNECTED,
+ ENDOFFILE,
} FTPState;
typedef enum {
static void ftp_close_data_connection(FTPContext *s)
{
ffurl_closep(&s->conn_data);
- s->position = 0;
s->state = DISCONNECTED;
}
*end = '\0';
/* skip ip */
if (!av_strtok(start, ",", &end)) goto fail;
- if (!av_strtok(end, ",", &end)) goto fail;
- if (!av_strtok(end, ",", &end)) goto fail;
- if (!av_strtok(end, ",", &end)) goto fail;
+ if (!av_strtok(NULL, ",", &end)) goto fail;
+ if (!av_strtok(NULL, ",", &end)) goto fail;
+ if (!av_strtok(NULL, ",", &end)) goto fail;
/* parse port number */
- start = av_strtok(end, ",", &end);
+ start = av_strtok(NULL, ",", &end);
if (!start) goto fail;
s->server_data_port = atoi(start) * 256;
- start = av_strtok(end, ",", &end);
+ start = av_strtok(NULL, ",", &end);
if (!start) goto fail;
s->server_data_port += atoi(start);
ff_dlog(s, "Server data port: %d\n", s->server_data_port);
if (ftp_restart(s, 0) < 0) {
h->is_streamed = 1;
} else {
- if (ftp_file_size(s) < 0 && flags & AVIO_FLAG_READ)
- h->is_streamed = 1;
+ ftp_file_size(s);
if (s->write_seekable != 1 && flags & AVIO_FLAG_WRITE)
h->is_streamed = 1;
}
{
FTPContext *s = h->priv_data;
int err;
- int64_t new_pos, fake_pos;
+ int64_t new_pos;
ff_dlog(h, "ftp protocol seek %"PRId64" %d\n", pos, whence);
return AVERROR(EINVAL);
}
- fake_pos = s->filesize != -1 ? FFMIN(new_pos, s->filesize) : new_pos;
- if (fake_pos != s->position) {
+ if (new_pos != s->position) {
if ((err = ftp_abort(h)) < 0)
return err;
- s->position = fake_pos;
+ s->position = new_pos;
}
return new_pos;
}
ff_dlog(h, "ftp protocol read %d bytes\n", size);
retry:
+ if (s->state == ENDOFFILE)
+ return AVERROR_EOF;
if (s->state == DISCONNECTED) {
- /* optimization */
- if (s->position >= s->filesize)
- return AVERROR_EOF;
if ((err = ftp_connect_data_connection(h)) < 0)
return err;
}
if (s->state == READY) {
- if (s->position >= s->filesize)
- return AVERROR_EOF;
if ((err = ftp_retrieve(s)) < 0)
return err;
}
read = ffurl_read(s->conn_data, buf, size);
if (read >= 0) {
s->position += read;
- if (s->position >= s->filesize) {
- /* server will terminate, but keep current position to avoid madness */
- /* save position to restart from it */
- int64_t pos = s->position;
- if (ftp_abort(h) < 0) {
- s->position = pos;
- return AVERROR(EIO);
- }
- s->position = pos;
- }
+ s->filesize = FFMAX(s->filesize, s->position);
+ }
+ if (read == AVERROR_EOF) {
+ static const int retr_codes[] = {226, 250, 425, 426, 451, 0};
+ char *response = NULL;
+ err = ftp_status(s, &response, retr_codes);
+ if (err == 226) {
+ ftp_close_data_connection(s);
+ av_freep(&response);
+ s->state = ENDOFFILE;
+ return AVERROR_EOF;
+ }
+ /* 250 is not allowed, any other status means some kind of error */
+ av_log(h, AV_LOG_ERROR, "FTP transfer failed: %s\n", response ? response : (err < 0 ? av_err2str(err) : "?"));
+ av_freep(&response);
+ read = AVERROR(EIO);
}
- if (read <= 0 && s->position < s->filesize && !h->is_streamed) {
+ if (read <= 0 && !h->is_streamed) {
/* Server closed connection. Probably due to inactivity */
- int64_t pos = s->position;
av_log(h, AV_LOG_INFO, "Reconnect to FTP server.\n");
if ((err = ftp_abort(h)) < 0)
return err;
- if ((err = ftp_seek(h, pos, SEEK_SET)) < 0) {
- av_log(h, AV_LOG_ERROR, "Position cannot be restored.\n");
- return err;
- }
if (!retry_done) {
retry_done = 1;
goto retry;
static int ftp_parse_entry_mlsd(char *mlsd, AVIODirEntry *next)
{
char *fact, *value;
+ char *saveptr = NULL, *p = mlsd;
ff_dlog(NULL, "%s\n", mlsd);
- while(fact = av_strtok(mlsd, ";", &mlsd)) {
+ while(fact = av_strtok(p, ";", &saveptr)) {
+ p = NULL;
if (fact[0] == ' ') {
next->name = av_strdup(&fact[1]);
continue;