1- From 4d97173c68e40e94f90af90341107fe0b8944133 Mon Sep 17 00:00:00 2001
1+ From dd3692c5f2b6eb587c0853de3b7f9b6f0b30dc6a Mon Sep 17 00:00:00 2001
22From: qianlongxu <
[email protected] >
3- Date: Thu, 31 Oct 2024 14:19:42 +0800
4- Subject: [PATCH] add built-in smb2 protocol via libsmb2
3+ Date: Mon, 4 Nov 2024 14:47:57 +0800
4+ Subject: [PATCH 20 ] add built-in smb2 protocol via libsmb2
55
66---
77 configure | 5 +
88 libavformat/Makefile | 1 +
9- libavformat/libsmb2.c | 410 ++++++++++++++++++++++++++++++++++++++++
9+ libavformat/libsmb2.c | 412 ++++++++++++++++++++++++++++++++++++++++
1010 libavformat/protocols.c | 1 +
11- 4 files changed, 417 insertions(+)
11+ 4 files changed, 419 insertions(+)
1212 create mode 100644 libavformat/libsmb2.c
1313
1414diff --git a/configure b/configure
@@ -62,10 +62,10 @@ index ae952eb..427c45a 100644
6262 OBJS-$(CONFIG_LIBZMQ_PROTOCOL) += libzmq.o
6363diff --git a/libavformat/libsmb2.c b/libavformat/libsmb2.c
6464new file mode 100644
65- index 0000000..bf31726
65+ index 0000000..741014c
6666--- /dev/null
6767+++ b/libavformat/libsmb2.c
68- @@ -0,0 +1,410 @@
68+ @@ -0,0 +1,412 @@
6969+ /*
7070+ * Copyright (c) 2014 Lukasz Marek <[email protected] >7171+ *
@@ -117,17 +117,16 @@ index 0000000..bf31726
117117+
118118+ static void destroy_smb2(LIBSMB2Context *libsmb2)
119119+ {
120- + if (libsmb2->fh && libsmb2->ctx)
121- + {
120+ + if (libsmb2->fh && libsmb2->ctx) {
122121+ smb2_close(libsmb2->ctx, libsmb2->fh);
123122+ }
124- + if (libsmb2->ctx)
125- + {
123+ +
124+ + if (libsmb2->ctx) {
126125+ smb2_disconnect_share(libsmb2->ctx);
127126+ smb2_destroy_context(libsmb2->ctx);
128127+ }
129- + if (libsmb2->url)
130- + {
128+ +
129+ + if (libsmb2->url) {
131130+ smb2_destroy_url(libsmb2->url);
132131+ }
133132+ libsmb2->fh = NULL;
@@ -153,7 +152,7 @@ index 0000000..bf31726
153152+ av_application_will_http_open(libsmb2->app_ctx, (void *)h, uri);
154153+
155154+ if (!libsmb2->ctx) {
156- + av_log(h, AV_LOG_ERROR, "Cannot create context: %s.\n", smb2_get_error(libsmb2->ctx));
155+ + av_log(h, AV_LOG_ERROR, "smb2 create context failed : %s.\n", smb2_get_error(libsmb2->ctx));
157156+ ret = AVERROR(ENOMEM);
158157+ goto failed;
159158+ }
@@ -162,8 +161,7 @@ index 0000000..bf31726
162161+ struct smb2_url *url = smb2_parse_url(libsmb2->ctx, smb_url);
163162+
164163+ if (url == NULL) {
165- + av_log(h, AV_LOG_ERROR, "Failed to parse url: %s\n",
166- + smb2_get_error(libsmb2->ctx));
164+ + av_log(h, AV_LOG_ERROR, "smb2 parse url failed: %s\n", smb2_get_error(libsmb2->ctx));
167165+ ret = AVERROR(ENOMEM);
168166+ goto failed;
169167+ } else {
@@ -178,7 +176,7 @@ index 0000000..bf31726
178176+ }
179177+ }
180178+ }
181- +
179+ +
182180+ if (url->domain) {
183181+ smb2_set_domain(libsmb2->ctx, url->domain);
184182+ }
@@ -206,7 +204,7 @@ index 0000000..bf31726
206204+ smb2_set_timeout(libsmb2->ctx, 60);
207205+
208206+ if (smb2_connect_share(libsmb2->ctx, url->server, url->share, url->user) != 0) {
209- + av_log(h, AV_LOG_ERROR, "smb2_connect_share failed. %s\n", smb2_get_error(libsmb2->ctx));
207+ + av_log(h, AV_LOG_ERROR, "smb2 connect share failed: %s\n", smb2_get_error(libsmb2->ctx));
210208+ ret = AVERROR(ECONNREFUSED);
211209+ goto failed;
212210+ }
@@ -222,13 +220,13 @@ index 0000000..bf31726
222220+
223221+ if (flags & AVIO_FLAG_DIRECT) {
224222+ if ((libsmb2->dir = smb2_opendir(libsmb2->ctx, url->path)) == NULL) {
225- + av_log(h, AV_LOG_ERROR, "smb2_open dir failed:%s,error:%s\n", url->path, smb2_get_error(libsmb2->ctx));
223+ + av_log(h, AV_LOG_ERROR, "smb2 open dir failed: %s, error: %s\n", url->path, smb2_get_error(libsmb2->ctx));
226224+ ret = AVERROR(ENOTDIR);
227225+ goto failed;
228226+ }
229227+ } else {
230228+ if ((libsmb2->fh = smb2_open(libsmb2->ctx, url->path, access)) == NULL) {
231- + av_log(h, AV_LOG_ERROR, "smb2_open file failed:%s,error:%s\n", url->path, smb2_get_error(libsmb2->ctx));
229+ + av_log(h, AV_LOG_ERROR, "smb2 open file failed: %s, error: %s\n", url->path, smb2_get_error(libsmb2->ctx));
232230+ ret = AVERROR(ENOENT);
233231+ goto failed;
234232+ }
@@ -267,7 +265,7 @@ index 0000000..bf31726
267265+
268266+ if (whence == AVSEEK_SIZE) {
269267+ if (libsmb2->filesize == -1) {
270- + av_log(h, AV_LOG_ERROR, "Error during seeking: filesize is unknown.\n");
268+ + av_log(h, AV_LOG_ERROR, "smb2 seek failed, filesize is unknown.\n");
271269+ return AVERROR(EIO);
272270+ } else {
273271+ return libsmb2->filesize;
@@ -277,7 +275,7 @@ index 0000000..bf31726
277275+ av_application_will_http_seek(libsmb2->app_ctx, (void *)h, h->filename, pos);
278276+
279277+ if ((newpos = smb2_lseek(libsmb2->ctx, libsmb2->fh, pos, whence, NULL)) < 0) {
280- + av_log(h, AV_LOG_ERROR, "Error during seeking : %s\n", smb2_get_error(libsmb2->ctx));
278+ + av_log(h, AV_LOG_ERROR, "smb2 seek failed : %s\n", smb2_get_error(libsmb2->ctx));
281279+ av_application_did_http_seek(libsmb2->app_ctx, (void *)h, h->filename, pos, AVERROR(errno), 500);
282280+ return AVERROR(errno);
283281+ }
@@ -291,12 +289,14 @@ index 0000000..bf31726
291289+
292290+ uint8_t *buf1 = buf;
293291+ int buf_size1 = size;
292+ + int has_error = 0;
294293+
295294+ while (buf_size1 > 0) {
296295+ int read = smb2_read(libsmb2->ctx, libsmb2->fh, buf1, buf_size1);
297296+ if (read < 0) {
298- + av_log(h, AV_LOG_ERROR, "Failed to read file. %s\n",
297+ + av_log(h, AV_LOG_ERROR, "smb2 read file failed: %s\n",
299298+ smb2_get_error(libsmb2->ctx));
299+ + has_error = 1;
300300+ break;
301301+ }
302302+ if (read == 0) {
@@ -311,7 +311,7 @@ index 0000000..bf31726
311311+ if (bytes_read > 0)
312312+ av_application_did_io_tcp_read(libsmb2->app_ctx, (void*)h, bytes_read);
313313+
314- + return bytes_read ? bytes_read : AVERROR_EOF;
314+ + return bytes_read ? bytes_read : (has_error ? AVERROR(NOTCONN) : AVERROR_EOF) ;
315315+ }
316316+
317317+ static int libsmb2_write(URLContext *h, const unsigned char *buf, int size)
@@ -321,7 +321,7 @@ index 0000000..bf31726
321321+
322322+ if ((bytes_written = smb2_write(libsmb2->ctx, libsmb2->fh, buf, size)) < 0) {
323323+ int ret = AVERROR(errno);
324- + av_log(h, AV_LOG_ERROR, "Write error : %s\n", strerror(errno));
324+ + av_log(h, AV_LOG_ERROR, "smb2 write failed : %s\n", strerror(errno));
325325+ return ret;
326326+ }
327327+
@@ -334,7 +334,7 @@ index 0000000..bf31726
334334+
335335+ struct smb2_url *url = smb2_parse_url(libsmb2->ctx, h->filename);
336336+ if (url == NULL) {
337- + av_log(h, AV_LOG_ERROR, "Failed to parse url: %s\n",
337+ + av_log(h, AV_LOG_ERROR, "smb2 parse url failed : %s\n",
338338+ smb2_get_error(libsmb2->ctx));
339339+ return -1;
340340+ } else {
@@ -355,8 +355,7 @@ index 0000000..bf31726
355355+ struct smb2_url *dst_url = smb2_parse_url(libsmb2->ctx, h_dst->filename);
356356+
357357+ if (src_url == NULL || dst_url == NULL) {
358- + av_log(h_src, AV_LOG_ERROR, "Failed to parse url: %s\n",
359- + smb2_get_error(libsmb2->ctx));
358+ + av_log(h_src, AV_LOG_ERROR, "smb2 parse url failed: %s\n", smb2_get_error(libsmb2->ctx));
360359+ return -2;
361360+ } else {
362361+ char *src_path = ff_urldecode(src_url->path, 0);
@@ -370,13 +369,16 @@ index 0000000..bf31726
370369+ LIBSMB2Context *libsmb2 = h->priv_data;
371370+ struct smb2_url *url = smb2_parse_url(libsmb2->ctx, h->filename);
372371+ if (url == NULL) {
373- + av_log(h, AV_LOG_ERROR, "Failed to parse url: %s\n",
374- + smb2_get_error(libsmb2->ctx));
372+ + av_log(h, AV_LOG_ERROR, "smb2 parse url failed: %s\n", smb2_get_error(libsmb2->ctx));
375373+ return -1;
376374+ } else {
377375+ char *path = ff_urldecode(url->path, 0);
378376+ libsmb2->dir = smb2_opendir(libsmb2->ctx, path);
379- + return libsmb2->dir ? 0 : smb2_get_error(libsmb2->ctx);
377+ + if (!libsmb2->dir){
378+ + av_log(h, AV_LOG_ERROR, "smb2 open dir failed: %s\n", smb2_get_error(libsmb2->ctx));
379+ + return 0;
380+ + }
381+ + return AVERROR(ENOTDIR);
380382+ }
381383+ }
382384+
@@ -475,7 +477,8 @@ index 0000000..bf31726
475477+ .priv_data_size = sizeof(LIBSMB2Context),
476478+ .priv_data_class = &libsmb2lient_context_class,
477479+ .flags = URL_PROTOCOL_FLAG_NETWORK,
478- + };
480+ + }
481+ \ No newline at end of file
479482diff --git a/libavformat/protocols.c b/libavformat/protocols.c
480483index 73df344..2bda874 100644
481484--- a/libavformat/protocols.c
0 commit comments