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/opt.h"
39 static void libsmbc_get_auth_data(SMBCCTX
*c
, const char *server
, const char *share
,
40 char *workgroup
, int workgroup_len
,
41 char *username
, int username_len
,
42 char *password
, int password_len
)
44 /* Do nothing yet. Credentials are passed via url.
45 * Callback must exists, there might be a segmentation fault otherwise. */
48 static av_cold
int libsmbc_connect(URLContext
*h
)
50 LIBSMBContext
*libsmbc
= h
->priv_data
;
52 libsmbc
->ctx
= smbc_new_context();
54 int ret
= AVERROR(errno
);
55 av_log(h
, AV_LOG_ERROR
, "Cannot create context: %s.\n", strerror(errno
));
58 if (!smbc_init_context(libsmbc
->ctx
)) {
59 int ret
= AVERROR(errno
);
60 av_log(h
, AV_LOG_ERROR
, "Cannot initialize context: %s.\n", strerror(errno
));
63 smbc_set_context(libsmbc
->ctx
);
65 smbc_setOptionUserData(libsmbc
->ctx
, h
);
66 smbc_setFunctionAuthDataWithContext(libsmbc
->ctx
, libsmbc_get_auth_data
);
68 if (libsmbc
->timeout
!= -1)
69 smbc_setTimeout(libsmbc
->ctx
, libsmbc
->timeout
);
70 if (libsmbc
->workgroup
)
71 smbc_setWorkgroup(libsmbc
->ctx
, libsmbc
->workgroup
);
73 if (smbc_init(NULL
, 0) < 0) {
74 int ret
= AVERROR(errno
);
75 av_log(h
, AV_LOG_ERROR
, "Initialization failed: %s\n", strerror(errno
));
81 static av_cold
int libsmbc_close(URLContext
*h
)
83 LIBSMBContext
*libsmbc
= h
->priv_data
;
84 if (libsmbc
->fd
>= 0) {
85 smbc_close(libsmbc
->fd
);
89 smbc_free_context(libsmbc
->ctx
, 1);
95 static av_cold
int libsmbc_open(URLContext
*h
, const char *url
, int flags
)
97 LIBSMBContext
*libsmbc
= h
->priv_data
;
102 libsmbc
->filesize
= -1;
104 if ((ret
= libsmbc_connect(h
)) < 0)
107 if ((flags
& AVIO_FLAG_WRITE
) && (flags
& AVIO_FLAG_READ
)) {
108 access
= O_CREAT
| O_RDWR
;
111 } else if (flags
& AVIO_FLAG_WRITE
) {
112 access
= O_CREAT
| O_WRONLY
;
118 /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
119 if ((libsmbc
->fd
= smbc_open(url
, access
, 0666)) < 0) {
120 ret
= AVERROR(errno
);
121 av_log(h
, AV_LOG_ERROR
, "File open failed: %s\n", strerror(errno
));
125 if (smbc_fstat(libsmbc
->fd
, &st
) < 0)
126 av_log(h
, AV_LOG_WARNING
, "Cannot stat file: %s\n", strerror(errno
));
128 libsmbc
->filesize
= st
.st_size
;
136 static int64_t libsmbc_seek(URLContext
*h
, int64_t pos
, int whence
)
138 LIBSMBContext
*libsmbc
= h
->priv_data
;
141 if (whence
== AVSEEK_SIZE
) {
142 if (libsmbc
->filesize
== -1) {
143 av_log(h
, AV_LOG_ERROR
, "Error during seeking: filesize is unknown.\n");
146 return libsmbc
->filesize
;
149 if ((newpos
= smbc_lseek(libsmbc
->fd
, pos
, whence
)) < 0) {
151 av_log(h
, AV_LOG_ERROR
, "Error during seeking: %s\n", strerror(err
));
158 static int libsmbc_read(URLContext
*h
, unsigned char *buf
, int size
)
160 LIBSMBContext
*libsmbc
= h
->priv_data
;
163 if ((bytes_read
= smbc_read(libsmbc
->fd
, buf
, size
)) < 0) {
164 int ret
= AVERROR(errno
);
165 av_log(h
, AV_LOG_ERROR
, "Read error: %s\n", strerror(errno
));
169 return bytes_read
? bytes_read
: AVERROR_EOF
;
172 static int libsmbc_write(URLContext
*h
, const unsigned char *buf
, int size
)
174 LIBSMBContext
*libsmbc
= h
->priv_data
;
177 if ((bytes_written
= smbc_write(libsmbc
->fd
, buf
, size
)) < 0) {
178 int ret
= AVERROR(errno
);
179 av_log(h
, AV_LOG_ERROR
, "Write error: %s\n", strerror(errno
));
183 return bytes_written
;
186 static int libsmbc_open_dir(URLContext
*h
)
188 LIBSMBContext
*libsmbc
= h
->priv_data
;
191 if ((ret
= libsmbc_connect(h
)) < 0)
194 if ((libsmbc
->dh
= smbc_opendir(h
->filename
)) < 0) {
195 ret
= AVERROR(errno
);
196 av_log(h
, AV_LOG_ERROR
, "Error opening dir: %s\n", strerror(errno
));
207 static int libsmbc_read_dir(URLContext
*h
, AVIODirEntry
**next
)
209 LIBSMBContext
*libsmbc
= h
->priv_data
;
211 struct smbc_dirent
*dirent
= NULL
;
215 *next
= entry
= ff_alloc_dir_entry();
217 return AVERROR(ENOMEM
);
221 dirent
= smbc_readdir(libsmbc
->dh
);
226 switch (dirent
->smbc_type
) {
228 entry
->type
= AVIO_ENTRY_DIRECTORY
;
231 entry
->type
= AVIO_ENTRY_FILE
;
233 case SMBC_FILE_SHARE
:
234 entry
->type
= AVIO_ENTRY_SHARE
;
237 entry
->type
= AVIO_ENTRY_SERVER
;
240 entry
->type
= AVIO_ENTRY_WORKGROUP
;
242 case SMBC_COMMS_SHARE
:
244 case SMBC_PRINTER_SHARE
:
249 entry
->type
= AVIO_ENTRY_UNKNOWN
;
252 } while (skip_entry
|| !strcmp(dirent
->name
, ".") ||
253 !strcmp(dirent
->name
, ".."));
255 entry
->name
= av_strdup(dirent
->name
);
258 return AVERROR(ENOMEM
);
261 url
= av_append_path_component(h
->filename
, dirent
->name
);
264 if (!smbc_stat(url
, &st
)) {
265 entry
->group_id
= st
.st_gid
;
266 entry
->user_id
= st
.st_uid
;
267 entry
->size
= st
.st_size
;
268 entry
->filemode
= st
.st_mode
& 0777;
269 entry
->modification_timestamp
= INT64_C(1000000) * st
.st_mtime
;
270 entry
->access_timestamp
= INT64_C(1000000) * st
.st_atime
;
271 entry
->status_change_timestamp
= INT64_C(1000000) * st
.st_ctime
;
279 static int libsmbc_close_dir(URLContext
*h
)
281 LIBSMBContext
*libsmbc
= h
->priv_data
;
282 if (libsmbc
->dh
>= 0) {
283 smbc_closedir(libsmbc
->dh
);
290 static int libsmbc_delete(URLContext
*h
)
292 LIBSMBContext
*libsmbc
= h
->priv_data
;
296 if ((ret
= libsmbc_connect(h
)) < 0)
299 if ((libsmbc
->fd
= smbc_open(h
->filename
, O_WRONLY
, 0666)) < 0) {
300 ret
= AVERROR(errno
);
304 if (smbc_fstat(libsmbc
->fd
, &st
) < 0) {
305 ret
= AVERROR(errno
);
309 smbc_close(libsmbc
->fd
);
312 if (S_ISDIR(st
.st_mode
)) {
313 if (smbc_rmdir(h
->filename
) < 0) {
314 ret
= AVERROR(errno
);
318 if (smbc_unlink(h
->filename
) < 0) {
319 ret
= AVERROR(errno
);
331 static int libsmbc_move(URLContext
*h_src
, URLContext
*h_dst
)
333 LIBSMBContext
*libsmbc
= h_src
->priv_data
;
336 if ((ret
= libsmbc_connect(h_src
)) < 0)
339 if ((libsmbc
->dh
= smbc_rename(h_src
->filename
, h_dst
->filename
)) < 0) {
340 ret
= AVERROR(errno
);
347 libsmbc_close(h_src
);
351 #define OFFSET(x) offsetof(LIBSMBContext, x)
352 #define D AV_OPT_FLAG_DECODING_PARAM
353 #define E AV_OPT_FLAG_ENCODING_PARAM
354 static const AVOption options
[] = {
355 {"timeout", "set timeout in ms of socket I/O operations", OFFSET(timeout
), AV_OPT_TYPE_INT
, {.i64
= -1}, -1, INT_MAX
, D
|E
},
356 {"truncate", "truncate existing files on write", OFFSET(trunc
), AV_OPT_TYPE_INT
, { .i64
= 1 }, 0, 1, E
},
357 {"workgroup", "set the workgroup used for making connections", OFFSET(workgroup
), AV_OPT_TYPE_STRING
, { 0 }, 0, 0, D
|E
},
361 static const AVClass libsmbclient_context_class
= {
362 .class_name
= "libsmbc",
363 .item_name
= av_default_item_name
,
365 .version
= LIBAVUTIL_VERSION_INT
,
368 const URLProtocol ff_libsmbclient_protocol
= {
370 .url_open
= libsmbc_open
,
371 .url_read
= libsmbc_read
,
372 .url_write
= libsmbc_write
,
373 .url_seek
= libsmbc_seek
,
374 .url_close
= libsmbc_close
,
375 .url_delete
= libsmbc_delete
,
376 .url_move
= libsmbc_move
,
377 .url_open_dir
= libsmbc_open_dir
,
378 .url_read_dir
= libsmbc_read_dir
,
379 .url_close_dir
= libsmbc_close_dir
,
380 .priv_data_size
= sizeof(LIBSMBContext
),
381 .priv_data_class
= &libsmbclient_context_class
,
382 .flags
= URL_PROTOCOL_FLAG_NETWORK
,