FFmpeg
libsmbclient.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Lukasz Marek <lukasz.m.luki@gmail.com>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <libsmbclient.h>
22 #include <string.h>
23 #include "libavutil/avstring.h"
24 #include "libavutil/error.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/opt.h"
27 #include "url.h"
28 
29 typedef struct {
30  const AVClass *class;
31  SMBCCTX *ctx;
32  int dh;
33  int fd;
35  int trunc;
36  int timeout;
37  char *workgroup;
39 
40 static void libsmbc_get_auth_data(SMBCCTX *c, const char *server, const char *share,
41  char *workgroup, int workgroup_len,
42  char *username, int username_len,
43  char *password, int password_len)
44 {
45  /* Do nothing yet. Credentials are passed via url.
46  * Callback must exists, there might be a segmentation fault otherwise. */
47 }
48 
50 {
51  LIBSMBContext *libsmbc = h->priv_data;
52 
53  libsmbc->ctx = smbc_new_context();
54  if (!libsmbc->ctx) {
55  int ret = AVERROR(errno);
56  av_log(h, AV_LOG_ERROR, "Cannot create context: %s.\n", strerror(errno));
57  return ret;
58  }
59  if (!smbc_init_context(libsmbc->ctx)) {
60  int ret = AVERROR(errno);
61  av_log(h, AV_LOG_ERROR, "Cannot initialize context: %s.\n", strerror(errno));
62  return ret;
63  }
64  smbc_set_context(libsmbc->ctx);
65 
66  smbc_setOptionUserData(libsmbc->ctx, h);
67  smbc_setFunctionAuthDataWithContext(libsmbc->ctx, libsmbc_get_auth_data);
68 
69  if (libsmbc->timeout != -1)
70  smbc_setTimeout(libsmbc->ctx, libsmbc->timeout);
71  if (libsmbc->workgroup)
72  smbc_setWorkgroup(libsmbc->ctx, libsmbc->workgroup);
73 
74  if (smbc_init(NULL, 0) < 0) {
75  int ret = AVERROR(errno);
76  av_log(h, AV_LOG_ERROR, "Initialization failed: %s\n", strerror(errno));
77  return ret;
78  }
79  return 0;
80 }
81 
83 {
84  LIBSMBContext *libsmbc = h->priv_data;
85  if (libsmbc->fd >= 0) {
86  smbc_close(libsmbc->fd);
87  libsmbc->fd = -1;
88  }
89  if (libsmbc->ctx) {
90  smbc_free_context(libsmbc->ctx, 1);
91  libsmbc->ctx = NULL;
92  }
93  return 0;
94 }
95 
96 static av_cold int libsmbc_open(URLContext *h, const char *url, int flags)
97 {
98  LIBSMBContext *libsmbc = h->priv_data;
99  int access, ret;
100  struct stat st;
101 
102  libsmbc->fd = -1;
103  libsmbc->filesize = -1;
104 
105  if ((ret = libsmbc_connect(h)) < 0)
106  goto fail;
107 
108  if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
109  access = O_CREAT | O_RDWR;
110  if (libsmbc->trunc)
111  access |= O_TRUNC;
112  } else if (flags & AVIO_FLAG_WRITE) {
113  access = O_CREAT | O_WRONLY;
114  if (libsmbc->trunc)
115  access |= O_TRUNC;
116  } else
117  access = O_RDONLY;
118 
119  /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
120  if ((libsmbc->fd = smbc_open(url, access, 0666)) < 0) {
121  ret = AVERROR(errno);
122  av_log(h, AV_LOG_ERROR, "File open failed: %s\n", strerror(errno));
123  goto fail;
124  }
125 
126  if (smbc_fstat(libsmbc->fd, &st) < 0)
127  av_log(h, AV_LOG_WARNING, "Cannot stat file: %s\n", strerror(errno));
128  else
129  libsmbc->filesize = st.st_size;
130 
131  return 0;
132  fail:
133  libsmbc_close(h);
134  return ret;
135 }
136 
138 {
139  LIBSMBContext *libsmbc = h->priv_data;
140  int64_t newpos;
141 
142  if (whence == AVSEEK_SIZE) {
143  if (libsmbc->filesize == -1) {
144  av_log(h, AV_LOG_ERROR, "Error during seeking: filesize is unknown.\n");
145  return AVERROR(EIO);
146  } else
147  return libsmbc->filesize;
148  }
149 
150  if ((newpos = smbc_lseek(libsmbc->fd, pos, whence)) < 0) {
151  int err = errno;
152  av_log(h, AV_LOG_ERROR, "Error during seeking: %s\n", strerror(err));
153  return AVERROR(err);
154  }
155 
156  return newpos;
157 }
158 
159 static int libsmbc_read(URLContext *h, unsigned char *buf, int size)
160 {
161  LIBSMBContext *libsmbc = h->priv_data;
162  int bytes_read;
163 
164  if ((bytes_read = smbc_read(libsmbc->fd, buf, size)) < 0) {
165  int ret = AVERROR(errno);
166  av_log(h, AV_LOG_ERROR, "Read error: %s\n", strerror(errno));
167  return ret;
168  }
169 
170  return bytes_read ? bytes_read : AVERROR_EOF;
171 }
172 
173 static int libsmbc_write(URLContext *h, const unsigned char *buf, int size)
174 {
175  LIBSMBContext *libsmbc = h->priv_data;
176  int bytes_written;
177 
178  if ((bytes_written = smbc_write(libsmbc->fd, buf, size)) < 0) {
179  int ret = AVERROR(errno);
180  av_log(h, AV_LOG_ERROR, "Write error: %s\n", strerror(errno));
181  return ret;
182  }
183 
184  return bytes_written;
185 }
186 
188 {
189  LIBSMBContext *libsmbc = h->priv_data;
190  int ret;
191 
192  if ((ret = libsmbc_connect(h)) < 0)
193  goto fail;
194 
195  if ((libsmbc->dh = smbc_opendir(h->filename)) < 0) {
196  ret = AVERROR(errno);
197  av_log(h, AV_LOG_ERROR, "Error opening dir: %s\n", strerror(errno));
198  goto fail;
199  }
200 
201  return 0;
202 
203  fail:
204  libsmbc_close(h);
205  return ret;
206 }
207 
209 {
210  LIBSMBContext *libsmbc = h->priv_data;
212  struct smbc_dirent *dirent = NULL;
213  char *url = NULL;
214  int skip_entry;
215 
216  *next = entry = ff_alloc_dir_entry();
217  if (!entry)
218  return AVERROR(ENOMEM);
219 
220  do {
221  skip_entry = 0;
222  dirent = smbc_readdir(libsmbc->dh);
223  if (!dirent) {
224  av_freep(next);
225  return 0;
226  }
227  switch (dirent->smbc_type) {
228  case SMBC_DIR:
229  entry->type = AVIO_ENTRY_DIRECTORY;
230  break;
231  case SMBC_FILE:
232  entry->type = AVIO_ENTRY_FILE;
233  break;
234  case SMBC_FILE_SHARE:
235  entry->type = AVIO_ENTRY_SHARE;
236  break;
237  case SMBC_SERVER:
238  entry->type = AVIO_ENTRY_SERVER;
239  break;
240  case SMBC_WORKGROUP:
241  entry->type = AVIO_ENTRY_WORKGROUP;
242  break;
243  case SMBC_COMMS_SHARE:
244  case SMBC_IPC_SHARE:
245  case SMBC_PRINTER_SHARE:
246  skip_entry = 1;
247  break;
248  case SMBC_LINK:
249  default:
250  entry->type = AVIO_ENTRY_UNKNOWN;
251  break;
252  }
253  } while (skip_entry || !strcmp(dirent->name, ".") ||
254  !strcmp(dirent->name, ".."));
255 
256  entry->name = av_strdup(dirent->name);
257  if (!entry->name) {
258  av_freep(next);
259  return AVERROR(ENOMEM);
260  }
261 
262  url = av_append_path_component(h->filename, dirent->name);
263  if (url) {
264  struct stat st;
265  if (!smbc_stat(url, &st)) {
266  entry->group_id = st.st_gid;
267  entry->user_id = st.st_uid;
268  entry->size = st.st_size;
269  entry->filemode = st.st_mode & 0777;
270  entry->modification_timestamp = INT64_C(1000000) * st.st_mtime;
271  entry->access_timestamp = INT64_C(1000000) * st.st_atime;
272  entry->status_change_timestamp = INT64_C(1000000) * st.st_ctime;
273  }
274  av_free(url);
275  }
276 
277  return 0;
278 }
279 
281 {
282  LIBSMBContext *libsmbc = h->priv_data;
283  if (libsmbc->dh >= 0) {
284  smbc_closedir(libsmbc->dh);
285  libsmbc->dh = -1;
286  }
287  libsmbc_close(h);
288  return 0;
289 }
290 
292 {
293  LIBSMBContext *libsmbc = h->priv_data;
294  int ret;
295  struct stat st;
296 
297  if ((ret = libsmbc_connect(h)) < 0)
298  goto cleanup;
299 
300  if ((libsmbc->fd = smbc_open(h->filename, O_WRONLY, 0666)) < 0) {
301  ret = AVERROR(errno);
302  goto cleanup;
303  }
304 
305  if (smbc_fstat(libsmbc->fd, &st) < 0) {
306  ret = AVERROR(errno);
307  goto cleanup;
308  }
309 
310  smbc_close(libsmbc->fd);
311  libsmbc->fd = -1;
312 
313  if (S_ISDIR(st.st_mode)) {
314  if (smbc_rmdir(h->filename) < 0) {
315  ret = AVERROR(errno);
316  goto cleanup;
317  }
318  } else {
319  if (smbc_unlink(h->filename) < 0) {
320  ret = AVERROR(errno);
321  goto cleanup;
322  }
323  }
324 
325  ret = 0;
326 
327 cleanup:
328  libsmbc_close(h);
329  return ret;
330 }
331 
332 static int libsmbc_move(URLContext *h_src, URLContext *h_dst)
333 {
334  LIBSMBContext *libsmbc = h_src->priv_data;
335  int ret;
336 
337  if ((ret = libsmbc_connect(h_src)) < 0)
338  goto cleanup;
339 
340  if ((libsmbc->dh = smbc_rename(h_src->filename, h_dst->filename)) < 0) {
341  ret = AVERROR(errno);
342  goto cleanup;
343  }
344 
345  ret = 0;
346 
347 cleanup:
348  libsmbc_close(h_src);
349  return ret;
350 }
351 
352 #define OFFSET(x) offsetof(LIBSMBContext, x)
353 #define D AV_OPT_FLAG_DECODING_PARAM
354 #define E AV_OPT_FLAG_ENCODING_PARAM
355 static const AVOption options[] = {
356  {"timeout", "set timeout in ms of socket I/O operations", OFFSET(timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
357  {"truncate", "truncate existing files on write", OFFSET(trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
358  {"workgroup", "set the workgroup used for making connections", OFFSET(workgroup), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
359  {NULL}
360 };
361 
363  .class_name = "libsmbc",
364  .item_name = av_default_item_name,
365  .option = options,
366  .version = LIBAVUTIL_VERSION_INT,
367 };
368 
370  .name = "smb",
371  .url_open = libsmbc_open,
372  .url_read = libsmbc_read,
373  .url_write = libsmbc_write,
374  .url_seek = libsmbc_seek,
375  .url_close = libsmbc_close,
376  .url_delete = libsmbc_delete,
377  .url_move = libsmbc_move,
378  .url_open_dir = libsmbc_open_dir,
379  .url_read_dir = libsmbc_read_dir,
380  .url_close_dir = libsmbc_close_dir,
381  .priv_data_size = sizeof(LIBSMBContext),
382  .priv_data_class = &libsmbclient_context_class,
384 };
flags
const SwsFlags flags[]
Definition: swscale.c:85
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
entry
#define entry
Definition: aom_film_grain_template.c:66
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
libsmbc_seek
static int64_t libsmbc_seek(URLContext *h, int64_t pos, int whence)
Definition: libsmbclient.c:137
URLContext::filename
char * filename
specified URL
Definition: url.h:39
URL_PROTOCOL_FLAG_NETWORK
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:33
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
options
static const AVOption options[]
Definition: libsmbclient.c:355
av_cold
#define av_cold
Definition: attributes.h:119
int64_t
long long int64_t
Definition: coverity.c:34
cleanup
static av_cold void cleanup(FlashSV2Context *s)
Definition: flashsv2enc.c:130
AVOption
AVOption.
Definition: opt.h:428
AVSEEK_SIZE
#define AVSEEK_SIZE
Passing this as the "whence" parameter to a seek function causes it to return the filesize without se...
Definition: avio.h:468
server
FFmpeg hosted at Telepoint in bulgaria ns2 avcodec org Replica Name server(provided by an ffmpeg developer, hosted at Hetzner in germany) ns3.avcodec.org Replica Name server(provided by an ffmpeg developer
LIBSMBContext::trunc
int trunc
Definition: libsmbclient.c:35
LIBSMBContext::dh
int dh
Definition: libsmbclient.c:32
URLProtocol
Definition: url.h:51
AVIO_ENTRY_UNKNOWN
@ AVIO_ENTRY_UNKNOWN
Definition: avio.h:68
av_append_path_component
char * av_append_path_component(const char *path, const char *component)
Append path component to the existing path.
Definition: avstring.c:297
AVIO_ENTRY_DIRECTORY
@ AVIO_ENTRY_DIRECTORY
Definition: avio.h:71
LIBSMBContext::ctx
SMBCCTX * ctx
Definition: libsmbclient.c:31
libsmbc_delete
static int libsmbc_delete(URLContext *h)
Definition: libsmbclient.c:291
LIBSMBContext::workgroup
char * workgroup
Definition: libsmbclient.c:37
trunc
static __device__ float trunc(float a)
Definition: cuda_runtime.h:179
LIBSMBContext::timeout
int timeout
Definition: libsmbclient.c:36
URLContext::priv_data
void * priv_data
Definition: url.h:38
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
libsmbc_close
static av_cold int libsmbc_close(URLContext *h)
Definition: libsmbclient.c:82
LIBSMBContext::fd
int fd
Definition: libsmbclient.c:33
AVIO_FLAG_WRITE
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:618
D
#define D
Definition: libsmbclient.c:353
fail
#define fail
Definition: test.h:478
libsmbc_open
static av_cold int libsmbc_open(URLContext *h, const char *url, int flags)
Definition: libsmbclient.c:96
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
NULL
#define NULL
Definition: coverity.c:32
ff_libsmbclient_protocol
const URLProtocol ff_libsmbclient_protocol
Definition: libsmbclient.c:369
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:242
options
Definition: swscale.c:50
AVIO_ENTRY_FILE
@ AVIO_ENTRY_FILE
Definition: avio.h:75
libsmbc_get_auth_data
static void libsmbc_get_auth_data(SMBCCTX *c, const char *server, const char *share, char *workgroup, int workgroup_len, char *username, int username_len, char *password, int password_len)
Definition: libsmbclient.c:40
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
error.h
size
int size
Definition: twinvq_data.h:10344
AVIODirEntry
Describes single entry of the directory.
Definition: avio.h:87
libsmbc_open_dir
static int libsmbc_open_dir(URLContext *h)
Definition: libsmbclient.c:187
URLProtocol::name
const char * name
Definition: url.h:52
libsmbc_close_dir
static int libsmbc_close_dir(URLContext *h)
Definition: libsmbclient.c:280
libsmbclient_context_class
static const AVClass libsmbclient_context_class
Definition: libsmbclient.c:362
OFFSET
#define OFFSET(x)
Definition: libsmbclient.c:352
ff_alloc_dir_entry
AVIODirEntry * ff_alloc_dir_entry(void)
Allocate directory entry with default values.
Definition: url.c:327
URLContext
Definition: url.h:35
libsmbc_move
static int libsmbc_move(URLContext *h_src, URLContext *h_dst)
Definition: libsmbclient.c:332
libsmbc_read_dir
static int libsmbc_read_dir(URLContext *h, AVIODirEntry **next)
Definition: libsmbclient.c:208
url.h
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:81
pos
unsigned int pos
Definition: spdifenc.c:414
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:258
LIBSMBContext::filesize
int64_t filesize
Definition: libsmbclient.c:34
E
#define E
Definition: libsmbclient.c:354
AVIO_ENTRY_WORKGROUP
@ AVIO_ENTRY_WORKGROUP
Definition: avio.h:78
AVIO_FLAG_READ
#define AVIO_FLAG_READ
read-only
Definition: avio.h:617
mem.h
av_strdup
#define av_strdup(s)
Definition: ops_asmgen.c:47
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
libsmbc_connect
static av_cold int libsmbc_connect(URLContext *h)
Definition: libsmbclient.c:49
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVIO_ENTRY_SERVER
@ AVIO_ENTRY_SERVER
Definition: avio.h:76
libsmbc_read
static int libsmbc_read(URLContext *h, unsigned char *buf, int size)
Definition: libsmbclient.c:159
LIBSMBContext
Definition: libsmbclient.c:29
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
h
h
Definition: vp9dsp_template.c:2070
avstring.h
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition: opt.h:275
libsmbc_write
static int libsmbc_write(URLContext *h, const unsigned char *buf, int size)
Definition: libsmbclient.c:173
AVIO_ENTRY_SHARE
@ AVIO_ENTRY_SHARE
Definition: avio.h:77