[FFmpeg-devel] [PATCH 2/4] lavf/libssh: implement directory listing callbacks
Lukasz Marek
lukasz.m.luki2 at gmail.com
Sat Jul 12 03:12:13 CEST 2014
Signed-off-by: Lukasz Marek <lukasz.m.luki2 at gmail.com>
---
libavformat/libssh.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 111 insertions(+), 5 deletions(-)
diff --git a/libavformat/libssh.c b/libavformat/libssh.c
index 87a2a7f..85a6cc4 100644
--- a/libavformat/libssh.c
+++ b/libavformat/libssh.c
@@ -24,6 +24,7 @@
#include "libavutil/avstring.h"
#include "libavutil/opt.h"
#include "libavutil/attributes.h"
+#include "libavformat/avio.h"
#include "avformat.h"
#include "internal.h"
#include "url.h"
@@ -33,10 +34,12 @@ typedef struct {
ssh_session session;
sftp_session sftp;
sftp_file file;
+ sftp_dir dir;
int64_t filesize;
int rw_timeout;
int trunc;
char *priv_key;
+ AVIODirEntry *dir_entry;
} LIBSSHContext;
static av_cold int libssh_create_ssh_session(LIBSSHContext *libssh, const char* hostname, unsigned int port)
@@ -178,10 +181,10 @@ static av_cold int libssh_close(URLContext *h)
return 0;
}
-static av_cold int libssh_open(URLContext *h, const char *url, int flags)
+static av_cold int libssh_connect(URLContext *h, const char *url, char *path, size_t path_size)
{
LIBSSHContext *libssh = h->priv_data;
- char proto[10], path[MAX_URL_SIZE], hostname[1024], credencials[1024];
+ char proto[10], hostname[1024], credencials[1024];
int port = 22, ret;
const char *user = NULL, *pass = NULL;
char *end = NULL;
@@ -190,22 +193,34 @@ static av_cold int libssh_open(URLContext *h, const char *url, int flags)
credencials, sizeof(credencials),
hostname, sizeof(hostname),
&port,
- path, sizeof(path),
+ path, path_size,
url);
if (port <= 0 || port > 65535)
port = 22;
if ((ret = libssh_create_ssh_session(libssh, hostname, port)) < 0)
- goto fail;
+ return ret;
user = av_strtok(credencials, ":", &end);
pass = av_strtok(end, ":", &end);
if ((ret = libssh_authentication(libssh, user, pass)) < 0)
- goto fail;
+ return ret;
if ((ret = libssh_create_sftp_session(libssh)) < 0)
+ return ret;
+
+ return 0;
+}
+
+static av_cold int libssh_open(URLContext *h, const char *url, int flags)
+{
+ int ret;
+ LIBSSHContext *libssh = h->priv_data;
+ char path[MAX_URL_SIZE];
+
+ if ((ret = libssh_connect(h, url, path, sizeof(path))) < 0)
goto fail;
if ((ret = libssh_open_file(libssh, flags, path)) < 0)
@@ -283,6 +298,94 @@ static int libssh_write(URLContext *h, const unsigned char *buf, int size)
return bytes_written;
}
+static int libssh_open_dir(URLContext *h)
+{
+ LIBSSHContext *libssh = h->priv_data;
+ int ret;
+ char path[MAX_URL_SIZE];
+
+ libssh->dir_entry = av_mallocz(sizeof(*libssh->dir_entry));
+ if (!libssh->dir_entry)
+ return AVERROR(ENOMEM);
+
+ if ((ret = libssh_connect(h, h->filename, path, sizeof(path))) < 0)
+ goto fail;
+
+ if (!(libssh->dir = sftp_opendir(libssh->sftp, path))) {
+ av_log(libssh, AV_LOG_ERROR, "Error opening sftp dir: %s\n", ssh_get_error(libssh->session));
+ ret = AVERROR(EIO);
+ goto fail;
+ }
+
+ return 0;
+
+ fail:
+ libssh_close(h);
+ return ret;
+}
+
+static int libssh_read_dir(URLContext *h, AVIODirEntry const **next)
+{
+ LIBSSHContext *libssh = h->priv_data;
+ sftp_attributes attr = NULL;
+
+ do {
+ if (attr)
+ sftp_attributes_free(attr);
+ attr = sftp_readdir(libssh->sftp, libssh->dir);
+ if (!attr) {
+ *next = NULL;
+ if (sftp_dir_eof(libssh->dir))
+ return 0;
+ return AVERROR(EIO);
+ }
+ } while (!strcmp(attr->name, ".") || !strcmp(attr->name, ".."));
+
+ av_free(libssh->dir_entry->name);
+ memset(libssh->dir_entry, 0, sizeof(*libssh->dir_entry));
+ libssh->dir_entry->name = av_strdup(attr->name);
+ libssh->dir_entry->group_id = attr->gid;
+ libssh->dir_entry->user_id = attr->uid;
+ libssh->dir_entry->size = attr->size;
+ libssh->dir_entry->access_timestamp = INT64_C(1000000) * attr->atime;
+ libssh->dir_entry->modification_timestamp = INT64_C(1000000) * attr->mtime;
+ libssh->dir_entry->filemode = attr->permissions & 0777;
+ switch(attr->type) {
+ case SSH_FILEXFER_TYPE_REGULAR:
+ libssh->dir_entry->type = AVIO_ENTRY_FILE;
+ break;
+ case SSH_FILEXFER_TYPE_DIRECTORY:
+ libssh->dir_entry->type = AVIO_ENTRY_DIRECTORY;
+ break;
+ case SSH_FILEXFER_TYPE_SYMLINK:
+ libssh->dir_entry->type = AVIO_ENTRY_SYMBOLIC_LINK;
+ break;
+ case SSH_FILEXFER_TYPE_SPECIAL:
+ /* Special type includes: sockets, char devices, block devices and pipes.
+ It is probably better to return unknown type, to not confuse anybody. */
+ case SSH_FILEXFER_TYPE_UNKNOWN:
+ default:
+ libssh->dir_entry->type = AVIO_ENTRY_UNKNOWN;
+ }
+ sftp_attributes_free(attr);
+ *next = libssh->dir_entry;
+ return 0;
+}
+
+static void libssh_close_dir(URLContext *h)
+{
+ LIBSSHContext *libssh = h->priv_data;
+ if (libssh->dir_entry) {
+ av_free(libssh->dir_entry->name);
+ av_freep(&libssh->dir_entry);
+ }
+ if (libssh->dir) {
+ sftp_closedir(libssh->dir);
+ libssh->dir = NULL;
+ }
+ libssh_close(h);
+}
+
#define OFFSET(x) offsetof(LIBSSHContext, x)
#define D AV_OPT_FLAG_DECODING_PARAM
#define E AV_OPT_FLAG_ENCODING_PARAM
@@ -307,6 +410,9 @@ URLProtocol ff_libssh_protocol = {
.url_write = libssh_write,
.url_seek = libssh_seek,
.url_close = libssh_close,
+ .url_open_dir = libssh_open_dir,
+ .url_read_dir = libssh_read_dir,
+ .url_close_dir = libssh_close_dir,
.priv_data_size = sizeof(LIBSSHContext),
.priv_data_class = &libssh_context_class,
.flags = URL_PROTOCOL_FLAG_NETWORK,
--
1.9.1
More information about the ffmpeg-devel
mailing list