Imported Debian version 2.5.0~trusty1.1
[deb_ffmpeg.git] / ffmpeg / libavformat / libsmbclient.c
CommitLineData
2ba45a60
DM
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 "libavutil/avstring.h"
23#include "libavutil/opt.h"
24#include "avformat.h"
25#include "internal.h"
26#include "url.h"
27
28typedef struct {
29 const AVClass *class;
30 SMBCCTX *ctx;
31 int fd;
32 int64_t filesize;
33 int trunc;
34 int timeout;
35 char *workgroup;
36} LIBSMBContext;
37
38static 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)
42{
43 /* Do nothing yet. Credentials are passed via url.
44 * Callback must exists, there might be a segmentation fault otherwise. */
45}
46
47static av_cold int libsmbc_connect(URLContext *h)
48{
49 LIBSMBContext *libsmbc = h->priv_data;
50
51 libsmbc->ctx = smbc_new_context();
52 if (!libsmbc->ctx) {
f6fa7814 53 int ret = AVERROR(errno);
2ba45a60 54 av_log(h, AV_LOG_ERROR, "Cannot create context: %s.\n", strerror(errno));
f6fa7814 55 return ret;
2ba45a60
DM
56 }
57 if (!smbc_init_context(libsmbc->ctx)) {
f6fa7814 58 int ret = AVERROR(errno);
2ba45a60 59 av_log(h, AV_LOG_ERROR, "Cannot initialize context: %s.\n", strerror(errno));
f6fa7814 60 return ret;
2ba45a60
DM
61 }
62 smbc_set_context(libsmbc->ctx);
63
64 smbc_setOptionUserData(libsmbc->ctx, h);
65 smbc_setFunctionAuthDataWithContext(libsmbc->ctx, libsmbc_get_auth_data);
66
67 if (libsmbc->timeout != -1)
68 smbc_setTimeout(libsmbc->ctx, libsmbc->timeout);
69 if (libsmbc->workgroup)
70 smbc_setWorkgroup(libsmbc->ctx, libsmbc->workgroup);
71
72 if (smbc_init(NULL, 0) < 0) {
f6fa7814 73 int ret = AVERROR(errno);
2ba45a60 74 av_log(h, AV_LOG_ERROR, "Initialization failed: %s\n", strerror(errno));
f6fa7814 75 return ret;
2ba45a60
DM
76 }
77 return 0;
78}
79
80static av_cold int libsmbc_close(URLContext *h)
81{
82 LIBSMBContext *libsmbc = h->priv_data;
83 if (libsmbc->fd >= 0) {
84 smbc_close(libsmbc->fd);
85 libsmbc->fd = -1;
86 }
87 if (libsmbc->ctx) {
88 smbc_free_context(libsmbc->ctx, 1);
89 libsmbc->ctx = NULL;
90 }
91 return 0;
92}
93
94static av_cold int libsmbc_open(URLContext *h, const char *url, int flags)
95{
96 LIBSMBContext *libsmbc = h->priv_data;
97 int access, ret;
98 struct stat st;
99
100 libsmbc->fd = -1;
101 libsmbc->filesize = -1;
102
103 if ((ret = libsmbc_connect(h)) < 0)
104 goto fail;
105
106 if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
107 access = O_CREAT | O_RDWR;
108 if (libsmbc->trunc)
109 access |= O_TRUNC;
110 } else if (flags & AVIO_FLAG_WRITE) {
111 access = O_CREAT | O_WRONLY;
112 if (libsmbc->trunc)
113 access |= O_TRUNC;
114 } else
115 access = O_RDONLY;
116
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));
121 goto fail;
122 }
123
124 if (smbc_fstat(libsmbc->fd, &st) < 0)
125 av_log(h, AV_LOG_WARNING, "Cannot stat file: %s\n", strerror(errno));
126 else
127 libsmbc->filesize = st.st_size;
128
129 return 0;
130 fail:
131 libsmbc_close(h);
132 return ret;
133}
134
135static int64_t libsmbc_seek(URLContext *h, int64_t pos, int whence)
136{
137 LIBSMBContext *libsmbc = h->priv_data;
138 int64_t newpos;
139
140 if (whence == AVSEEK_SIZE) {
141 if (libsmbc->filesize == -1) {
142 av_log(h, AV_LOG_ERROR, "Error during seeking: filesize is unknown.\n");
143 return AVERROR(EIO);
144 } else
145 return libsmbc->filesize;
146 }
147
148 if ((newpos = smbc_lseek(libsmbc->fd, pos, whence)) < 0) {
149 int err = errno;
150 av_log(h, AV_LOG_ERROR, "Error during seeking: %s\n", strerror(err));
151 return AVERROR(err);
152 }
153
154 return newpos;
155}
156
157static int libsmbc_read(URLContext *h, unsigned char *buf, int size)
158{
159 LIBSMBContext *libsmbc = h->priv_data;
160 int bytes_read;
161
162 if ((bytes_read = smbc_read(libsmbc->fd, buf, size)) < 0) {
f6fa7814 163 int ret = AVERROR(errno);
2ba45a60 164 av_log(h, AV_LOG_ERROR, "Read error: %s\n", strerror(errno));
f6fa7814 165 return ret;
2ba45a60
DM
166 }
167
168 return bytes_read;
169}
170
171static int libsmbc_write(URLContext *h, const unsigned char *buf, int size)
172{
173 LIBSMBContext *libsmbc = h->priv_data;
174 int bytes_written;
175
176 if ((bytes_written = smbc_write(libsmbc->fd, buf, size)) < 0) {
f6fa7814 177 int ret = AVERROR(errno);
2ba45a60 178 av_log(h, AV_LOG_ERROR, "Write error: %s\n", strerror(errno));
f6fa7814 179 return ret;
2ba45a60
DM
180 }
181
182 return bytes_written;
183}
184
185#define OFFSET(x) offsetof(LIBSMBContext, x)
186#define D AV_OPT_FLAG_DECODING_PARAM
187#define E AV_OPT_FLAG_ENCODING_PARAM
188static const AVOption options[] = {
189 {"timeout", "set timeout in ms of socket I/O operations", OFFSET(timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
190 {"truncate", "truncate existing files on write", OFFSET(trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
191 {"workgroup", "set the workgroup used for making connections", OFFSET(workgroup), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
192 {NULL}
193};
194
195static const AVClass libsmbclient_context_class = {
196 .class_name = "libsmbc",
197 .item_name = av_default_item_name,
198 .option = options,
199 .version = LIBAVUTIL_VERSION_INT,
200};
201
202URLProtocol ff_libsmbclient_protocol = {
203 .name = "smb",
204 .url_open = libsmbc_open,
205 .url_read = libsmbc_read,
206 .url_write = libsmbc_write,
207 .url_seek = libsmbc_seek,
208 .url_close = libsmbc_close,
209 .priv_data_size = sizeof(LIBSMBContext),
210 .priv_data_class = &libsmbclient_context_class,
211 .flags = URL_PROTOCOL_FLAG_NETWORK,
212};