FFmpeg
Loading...
Searching...
No Matches
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
29typedef struct {
30 const AVClass *class;
31 SMBCCTX *ctx;
32 int dh;
33 int fd;
35 int trunc;
37 char *workgroup;
39
40static 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
96static 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:
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
159static 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
173static 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:
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:
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:
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:
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 }
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
327cleanup:
329 return ret;
330}
331
332static 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
347cleanup:
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
355static 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};
#define entry
#define E
Definition avdct.c:34
#define D
Definition avdct.c:35
#define AVIO_FLAG_READ
read-only
Definition avio.h:617
#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
#define AVIO_FLAG_WRITE
write-only
Definition avio.h:618
@ AVIO_ENTRY_UNKNOWN
Definition avio.h:68
@ AVIO_ENTRY_WORKGROUP
Definition avio.h:78
@ AVIO_ENTRY_SERVER
Definition avio.h:76
@ AVIO_ENTRY_SHARE
Definition avio.h:77
@ AVIO_ENTRY_DIRECTORY
Definition avio.h:71
@ AVIO_ENTRY_FILE
Definition avio.h:75
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static __device__ float trunc(float a)
error code definitions
static av_cold void cleanup(FlashSV2Context *s)
#define fail
Definition test.h:479
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ 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
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
char * av_append_path_component(const char *path, const char *component)
Append path component to the existing path.
Definition avstring.c:292
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define av_cold
Definition attributes.h:117
static int libsmbc_read(URLContext *h, unsigned char *buf, int size)
static int libsmbc_move(URLContext *h_src, URLContext *h_dst)
static av_cold int libsmbc_close(URLContext *h)
const URLProtocol ff_libsmbclient_protocol
static av_cold int libsmbc_connect(URLContext *h)
static av_cold int libsmbc_open(URLContext *h, const char *url, int flags)
static int libsmbc_delete(URLContext *h)
static int64_t libsmbc_seek(URLContext *h, int64_t pos, int whence)
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)
static int libsmbc_open_dir(URLContext *h)
static int libsmbc_write(URLContext *h, const unsigned char *buf, int size)
static const AVClass libsmbclient_context_class
static int libsmbc_close_dir(URLContext *h)
#define OFFSET(x)
static int libsmbc_read_dir(URLContext *h, AVIODirEntry **next)
Memory handling functions.
#define av_strdup(s)
Definition ops_static.c:55
AVOptions.
unsigned int pos
Definition spdifenc.c:431
Describe the class of an AVClass context structure.
Definition log.h:76
Describes single entry of the directory.
Definition avio.h:87
AVOption.
Definition opt.h:428
int64_t filesize
SMBCCTX * ctx
void * priv_data
Definition url.h:38
char * filename
specified URL
Definition url.h:39
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)
int size
AVIODirEntry * ff_alloc_dir_entry(void)
Allocate directory entry with default values.
Definition url.c:327
unbuffered private I/O API
#define URL_PROTOCOL_FLAG_NETWORK
Definition url.h:33
static double c[64]