2 * Copyright (c) 2014 Lukasz Marek <lukasz.m.luki@gmail.com>
4 * This file is part of FFmpeg.
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.
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.
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
21 #include <libsmbclient.h>
22 #include "libavutil/avstring.h"
23 #include "libavutil/mem.h"
24 #include "libavutil/opt.h"
38 static void libsmbc_get_auth_data(SMBCCTX
*c
, const char *server
, const char *share
,
39 char *workgroup
, int workgroup_len
,
40 char *username
, int username_len
,
41 char *password
, int password_len
)
43 /* Do nothing yet. Credentials are passed via url.
44 * Callback must exists, there might be a segmentation fault otherwise. */
47 static av_cold
int libsmbc_connect(URLContext
*h
)
49 LIBSMBContext
*libsmbc
= h
->priv_data
;
51 libsmbc
->ctx
= smbc_new_context();
53 int ret
= AVERROR(errno
);
54 av_log(h
, AV_LOG_ERROR
, "Cannot create context: %s.\n", strerror(errno
));
57 if (!smbc_init_context(libsmbc
->ctx
)) {
58 int ret
= AVERROR(errno
);
59 av_log(h
, AV_LOG_ERROR
, "Cannot initialize context: %s.\n", strerror(errno
));
62 smbc_set_context(libsmbc
->ctx
);
64 smbc_setOptionUserData(libsmbc
->ctx
, h
);
65 smbc_setFunctionAuthDataWithContext(libsmbc
->ctx
, libsmbc_get_auth_data
);
67 if (libsmbc
->timeout
!= -1)
68 smbc_setTimeout(libsmbc
->ctx
, libsmbc
->timeout
);
69 if (libsmbc
->workgroup
)
70 smbc_setWorkgroup(libsmbc
->ctx
, libsmbc
->workgroup
);
72 if (smbc_init(NULL
, 0) < 0) {
73 int ret
= AVERROR(errno
);
74 av_log(h
, AV_LOG_ERROR
, "Initialization failed: %s\n", strerror(errno
));
80 static av_cold
int libsmbc_close(URLContext
*h
)
82 LIBSMBContext
*libsmbc
= h
->priv_data
;
83 if (libsmbc
->fd
>= 0) {
84 smbc_close(libsmbc
->fd
);
88 smbc_free_context(libsmbc
->ctx
, 1);
94 static av_cold
int libsmbc_open(URLContext
*h
, const char *url
, int flags
)
96 LIBSMBContext
*libsmbc
= h
->priv_data
;
101 libsmbc
->filesize
= -1;
103 if ((ret
= libsmbc_connect(h
)) < 0)
106 if ((flags
& AVIO_FLAG_WRITE
) && (flags
& AVIO_FLAG_READ
)) {
107 access
= O_CREAT
| O_RDWR
;
110 } else if (flags
& AVIO_FLAG_WRITE
) {
111 access
= O_CREAT
| O_WRONLY
;
117 /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
118 if ((libsmbc
->fd
= smbc_open(url
, access
, 0666)) < 0) {
119 ret
= AVERROR(errno
);
120 av_log(h
, AV_LOG_ERROR
, "File open failed: %s\n", strerror(errno
));
124 if (smbc_fstat(libsmbc
->fd
, &st
) < 0)
125 av_log(h
, AV_LOG_WARNING
, "Cannot stat file: %s\n", strerror(errno
));
127 libsmbc
->filesize
= st
.st_size
;
135 static int64_t libsmbc_seek(URLContext
*h
, int64_t pos
, int whence
)
137 LIBSMBContext
*libsmbc
= h
->priv_data
;
140 if (whence
== AVSEEK_SIZE
) {
141 if (libsmbc
->filesize
== -1) {
142 av_log(h
, AV_LOG_ERROR
, "Error during seeking: filesize is unknown.\n");
145 return libsmbc
->filesize
;
148 if ((newpos
= smbc_lseek(libsmbc
->fd
, pos
, whence
)) < 0) {
150 av_log(h
, AV_LOG_ERROR
, "Error during seeking: %s\n", strerror(err
));
157 static int libsmbc_read(URLContext
*h
, unsigned char *buf
, int size
)
159 LIBSMBContext
*libsmbc
= h
->priv_data
;
162 if ((bytes_read
= smbc_read(libsmbc
->fd
, buf
, size
)) < 0) {
163 int ret
= AVERROR(errno
);
164 av_log(h
, AV_LOG_ERROR
, "Read error: %s\n", strerror(errno
));
168 return bytes_read
? bytes_read
: AVERROR_EOF
;
171 static int libsmbc_write(URLContext
*h
, const unsigned char *buf
, int size
)
173 LIBSMBContext
*libsmbc
= h
->priv_data
;
176 if ((bytes_written
= smbc_write(libsmbc
->fd
, buf
, size
)) < 0) {
177 int ret
= AVERROR(errno
);
178 av_log(h
, AV_LOG_ERROR
, "Write error: %s\n", strerror(errno
));
182 return bytes_written
;
185 static int libsmbc_open_dir(URLContext
*h
)
187 LIBSMBContext
*libsmbc
= h
->priv_data
;
190 if ((ret
= libsmbc_connect(h
)) < 0)
193 if ((libsmbc
->dh
= smbc_opendir(h
->filename
)) < 0) {
194 ret
= AVERROR(errno
);
195 av_log(h
, AV_LOG_ERROR
, "Error opening dir: %s\n", strerror(errno
));
206 static int libsmbc_read_dir(URLContext
*h
, AVIODirEntry
**next
)
208 LIBSMBContext
*libsmbc
= h
->priv_data
;
210 struct smbc_dirent
*dirent
= NULL
;
214 *next
= entry
= ff_alloc_dir_entry();
216 return AVERROR(ENOMEM
);
220 dirent
= smbc_readdir(libsmbc
->dh
);
225 switch (dirent
->smbc_type
) {
227 entry
->type
= AVIO_ENTRY_DIRECTORY
;
230 entry
->type
= AVIO_ENTRY_FILE
;
232 case SMBC_FILE_SHARE
:
233 entry
->type
= AVIO_ENTRY_SHARE
;
236 entry
->type
= AVIO_ENTRY_SERVER
;
239 entry
->type
= AVIO_ENTRY_WORKGROUP
;
241 case SMBC_COMMS_SHARE
:
243 case SMBC_PRINTER_SHARE
:
248 entry
->type
= AVIO_ENTRY_UNKNOWN
;
251 } while (skip_entry
|| !strcmp(dirent
->name
, ".") ||
252 !strcmp(dirent
->name
, ".."));
254 entry
->name
= av_strdup(dirent
->name
);
257 return AVERROR(ENOMEM
);
260 url
= av_append_path_component(h
->filename
, dirent
->name
);
263 if (!smbc_stat(url
, &st
)) {
264 entry
->group_id
= st
.st_gid
;
265 entry
->user_id
= st
.st_uid
;
266 entry
->size
= st
.st_size
;
267 entry
->filemode
= st
.st_mode
& 0777;
268 entry
->modification_timestamp
= INT64_C(1000000) * st
.st_mtime
;
269 entry
->access_timestamp
= INT64_C(1000000) * st
.st_atime
;
270 entry
->status_change_timestamp
= INT64_C(1000000) * st
.st_ctime
;
278 static int libsmbc_close_dir(URLContext
*h
)
280 LIBSMBContext
*libsmbc
= h
->priv_data
;
281 if (libsmbc
->dh
>= 0) {
282 smbc_closedir(libsmbc
->dh
);
289 static int libsmbc_delete(URLContext
*h
)
291 LIBSMBContext
*libsmbc
= h
->priv_data
;
295 if ((ret
= libsmbc_connect(h
)) < 0)
298 if ((libsmbc
->fd
= smbc_open(h
->filename
, O_WRONLY
, 0666)) < 0) {
299 ret
= AVERROR(errno
);
303 if (smbc_fstat(libsmbc
->fd
, &st
) < 0) {
304 ret
= AVERROR(errno
);
308 smbc_close(libsmbc
->fd
);
311 if (S_ISDIR(st
.st_mode
)) {
312 if (smbc_rmdir(h
->filename
) < 0) {
313 ret
= AVERROR(errno
);
317 if (smbc_unlink(h
->filename
) < 0) {
318 ret
= AVERROR(errno
);
330 static int libsmbc_move(URLContext
*h_src
, URLContext
*h_dst
)
332 LIBSMBContext
*libsmbc
= h_src
->priv_data
;
335 if ((ret
= libsmbc_connect(h_src
)) < 0)
338 if ((libsmbc
->dh
= smbc_rename(h_src
->filename
, h_dst
->filename
)) < 0) {
339 ret
= AVERROR(errno
);
346 libsmbc_close(h_src
);
350 #define OFFSET(x) offsetof(LIBSMBContext, x)
351 #define D AV_OPT_FLAG_DECODING_PARAM
352 #define E AV_OPT_FLAG_ENCODING_PARAM
353 static const AVOption options
[] = {
354 {"timeout", "set timeout in ms of socket I/O operations", OFFSET(timeout
), AV_OPT_TYPE_INT
, {.i64
= -1}, -1, INT_MAX
, D
|E
},
355 {"truncate", "truncate existing files on write", OFFSET(trunc
), AV_OPT_TYPE_INT
, { .i64
= 1 }, 0, 1, E
},
356 {"workgroup", "set the workgroup used for making connections", OFFSET(workgroup
), AV_OPT_TYPE_STRING
, { 0 }, 0, 0, D
|E
},
360 static const AVClass libsmbclient_context_class
= {
361 .class_name
= "libsmbc",
362 .item_name
= av_default_item_name
,
364 .version
= LIBAVUTIL_VERSION_INT
,
367 const URLProtocol ff_libsmbclient_protocol
= {
369 .url_open
= libsmbc_open
,
370 .url_read
= libsmbc_read
,
371 .url_write
= libsmbc_write
,
372 .url_seek
= libsmbc_seek
,
373 .url_close
= libsmbc_close
,
374 .url_delete
= libsmbc_delete
,
375 .url_move
= libsmbc_move
,
376 .url_open_dir
= libsmbc_open_dir
,
377 .url_read_dir
= libsmbc_read_dir
,
378 .url_close_dir
= libsmbc_close_dir
,
379 .priv_data_size
= sizeof(LIBSMBContext
),
380 .priv_data_class
= &libsmbclient_context_class
,
381 .flags
= URL_PROTOCOL_FLAG_NETWORK
,