You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

384 lines
10KB

  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. #include <libsmbclient.h>
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/opt.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #include "url.h"
  26. typedef struct {
  27. const AVClass *class;
  28. SMBCCTX *ctx;
  29. int dh;
  30. int fd;
  31. int64_t filesize;
  32. int trunc;
  33. int timeout;
  34. char *workgroup;
  35. } LIBSMBContext;
  36. static void libsmbc_get_auth_data(SMBCCTX *c, const char *server, const char *share,
  37. char *workgroup, int workgroup_len,
  38. char *username, int username_len,
  39. char *password, int password_len)
  40. {
  41. /* Do nothing yet. Credentials are passed via url.
  42. * Callback must exists, there might be a segmentation fault otherwise. */
  43. }
  44. static av_cold int libsmbc_connect(URLContext *h)
  45. {
  46. LIBSMBContext *libsmbc = h->priv_data;
  47. libsmbc->ctx = smbc_new_context();
  48. if (!libsmbc->ctx) {
  49. int ret = AVERROR(errno);
  50. av_log(h, AV_LOG_ERROR, "Cannot create context: %s.\n", strerror(errno));
  51. return ret;
  52. }
  53. if (!smbc_init_context(libsmbc->ctx)) {
  54. int ret = AVERROR(errno);
  55. av_log(h, AV_LOG_ERROR, "Cannot initialize context: %s.\n", strerror(errno));
  56. return ret;
  57. }
  58. smbc_set_context(libsmbc->ctx);
  59. smbc_setOptionUserData(libsmbc->ctx, h);
  60. smbc_setFunctionAuthDataWithContext(libsmbc->ctx, libsmbc_get_auth_data);
  61. if (libsmbc->timeout != -1)
  62. smbc_setTimeout(libsmbc->ctx, libsmbc->timeout);
  63. if (libsmbc->workgroup)
  64. smbc_setWorkgroup(libsmbc->ctx, libsmbc->workgroup);
  65. if (smbc_init(NULL, 0) < 0) {
  66. int ret = AVERROR(errno);
  67. av_log(h, AV_LOG_ERROR, "Initialization failed: %s\n", strerror(errno));
  68. return ret;
  69. }
  70. return 0;
  71. }
  72. static av_cold int libsmbc_close(URLContext *h)
  73. {
  74. LIBSMBContext *libsmbc = h->priv_data;
  75. if (libsmbc->fd >= 0) {
  76. smbc_close(libsmbc->fd);
  77. libsmbc->fd = -1;
  78. }
  79. if (libsmbc->ctx) {
  80. smbc_free_context(libsmbc->ctx, 1);
  81. libsmbc->ctx = NULL;
  82. }
  83. return 0;
  84. }
  85. static av_cold int libsmbc_open(URLContext *h, const char *url, int flags)
  86. {
  87. LIBSMBContext *libsmbc = h->priv_data;
  88. int access, ret;
  89. struct stat st;
  90. libsmbc->fd = -1;
  91. libsmbc->filesize = -1;
  92. if ((ret = libsmbc_connect(h)) < 0)
  93. goto fail;
  94. if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
  95. access = O_CREAT | O_RDWR;
  96. if (libsmbc->trunc)
  97. access |= O_TRUNC;
  98. } else if (flags & AVIO_FLAG_WRITE) {
  99. access = O_CREAT | O_WRONLY;
  100. if (libsmbc->trunc)
  101. access |= O_TRUNC;
  102. } else
  103. access = O_RDONLY;
  104. /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
  105. if ((libsmbc->fd = smbc_open(url, access, 0666)) < 0) {
  106. ret = AVERROR(errno);
  107. av_log(h, AV_LOG_ERROR, "File open failed: %s\n", strerror(errno));
  108. goto fail;
  109. }
  110. if (smbc_fstat(libsmbc->fd, &st) < 0)
  111. av_log(h, AV_LOG_WARNING, "Cannot stat file: %s\n", strerror(errno));
  112. else
  113. libsmbc->filesize = st.st_size;
  114. return 0;
  115. fail:
  116. libsmbc_close(h);
  117. return ret;
  118. }
  119. static int64_t libsmbc_seek(URLContext *h, int64_t pos, int whence)
  120. {
  121. LIBSMBContext *libsmbc = h->priv_data;
  122. int64_t newpos;
  123. if (whence == AVSEEK_SIZE) {
  124. if (libsmbc->filesize == -1) {
  125. av_log(h, AV_LOG_ERROR, "Error during seeking: filesize is unknown.\n");
  126. return AVERROR(EIO);
  127. } else
  128. return libsmbc->filesize;
  129. }
  130. if ((newpos = smbc_lseek(libsmbc->fd, pos, whence)) < 0) {
  131. int err = errno;
  132. av_log(h, AV_LOG_ERROR, "Error during seeking: %s\n", strerror(err));
  133. return AVERROR(err);
  134. }
  135. return newpos;
  136. }
  137. static int libsmbc_read(URLContext *h, unsigned char *buf, int size)
  138. {
  139. LIBSMBContext *libsmbc = h->priv_data;
  140. int bytes_read;
  141. if ((bytes_read = smbc_read(libsmbc->fd, buf, size)) < 0) {
  142. int ret = AVERROR(errno);
  143. av_log(h, AV_LOG_ERROR, "Read error: %s\n", strerror(errno));
  144. return ret;
  145. }
  146. return bytes_read ? bytes_read : AVERROR_EOF;
  147. }
  148. static int libsmbc_write(URLContext *h, const unsigned char *buf, int size)
  149. {
  150. LIBSMBContext *libsmbc = h->priv_data;
  151. int bytes_written;
  152. if ((bytes_written = smbc_write(libsmbc->fd, buf, size)) < 0) {
  153. int ret = AVERROR(errno);
  154. av_log(h, AV_LOG_ERROR, "Write error: %s\n", strerror(errno));
  155. return ret;
  156. }
  157. return bytes_written;
  158. }
  159. static int libsmbc_open_dir(URLContext *h)
  160. {
  161. LIBSMBContext *libsmbc = h->priv_data;
  162. int ret;
  163. if ((ret = libsmbc_connect(h)) < 0)
  164. goto fail;
  165. if ((libsmbc->dh = smbc_opendir(h->filename)) < 0) {
  166. ret = AVERROR(errno);
  167. av_log(h, AV_LOG_ERROR, "Error opening dir: %s\n", strerror(errno));
  168. goto fail;
  169. }
  170. return 0;
  171. fail:
  172. libsmbc_close(h);
  173. return ret;
  174. }
  175. static int libsmbc_read_dir(URLContext *h, AVIODirEntry **next)
  176. {
  177. LIBSMBContext *libsmbc = h->priv_data;
  178. AVIODirEntry *entry;
  179. struct smbc_dirent *dirent = NULL;
  180. char *url = NULL;
  181. int skip_entry;
  182. *next = entry = ff_alloc_dir_entry();
  183. if (!entry)
  184. return AVERROR(ENOMEM);
  185. do {
  186. skip_entry = 0;
  187. dirent = smbc_readdir(libsmbc->dh);
  188. if (!dirent) {
  189. av_freep(next);
  190. return 0;
  191. }
  192. switch (dirent->smbc_type) {
  193. case SMBC_DIR:
  194. entry->type = AVIO_ENTRY_DIRECTORY;
  195. break;
  196. case SMBC_FILE:
  197. entry->type = AVIO_ENTRY_FILE;
  198. break;
  199. case SMBC_FILE_SHARE:
  200. entry->type = AVIO_ENTRY_SHARE;
  201. break;
  202. case SMBC_SERVER:
  203. entry->type = AVIO_ENTRY_SERVER;
  204. break;
  205. case SMBC_WORKGROUP:
  206. entry->type = AVIO_ENTRY_WORKGROUP;
  207. break;
  208. case SMBC_COMMS_SHARE:
  209. case SMBC_IPC_SHARE:
  210. case SMBC_PRINTER_SHARE:
  211. skip_entry = 1;
  212. break;
  213. case SMBC_LINK:
  214. default:
  215. entry->type = AVIO_ENTRY_UNKNOWN;
  216. break;
  217. }
  218. } while (skip_entry || !strcmp(dirent->name, ".") ||
  219. !strcmp(dirent->name, ".."));
  220. entry->name = av_strdup(dirent->name);
  221. if (!entry->name) {
  222. av_freep(next);
  223. return AVERROR(ENOMEM);
  224. }
  225. url = av_append_path_component(h->filename, dirent->name);
  226. if (url) {
  227. struct stat st;
  228. if (!smbc_stat(url, &st)) {
  229. entry->group_id = st.st_gid;
  230. entry->user_id = st.st_uid;
  231. entry->size = st.st_size;
  232. entry->filemode = st.st_mode & 0777;
  233. entry->modification_timestamp = INT64_C(1000000) * st.st_mtime;
  234. entry->access_timestamp = INT64_C(1000000) * st.st_atime;
  235. entry->status_change_timestamp = INT64_C(1000000) * st.st_ctime;
  236. }
  237. av_free(url);
  238. }
  239. return 0;
  240. }
  241. static int libsmbc_close_dir(URLContext *h)
  242. {
  243. LIBSMBContext *libsmbc = h->priv_data;
  244. if (libsmbc->dh >= 0) {
  245. smbc_closedir(libsmbc->dh);
  246. libsmbc->dh = -1;
  247. }
  248. libsmbc_close(h);
  249. return 0;
  250. }
  251. static int libsmbc_delete(URLContext *h)
  252. {
  253. LIBSMBContext *libsmbc = h->priv_data;
  254. int ret;
  255. struct stat st;
  256. if ((ret = libsmbc_connect(h)) < 0)
  257. goto cleanup;
  258. if ((libsmbc->fd = smbc_open(h->filename, O_WRONLY, 0666)) < 0) {
  259. ret = AVERROR(errno);
  260. goto cleanup;
  261. }
  262. if (smbc_fstat(libsmbc->fd, &st) < 0) {
  263. ret = AVERROR(errno);
  264. goto cleanup;
  265. }
  266. smbc_close(libsmbc->fd);
  267. libsmbc->fd = -1;
  268. if (S_ISDIR(st.st_mode)) {
  269. if (smbc_rmdir(h->filename) < 0) {
  270. ret = AVERROR(errno);
  271. goto cleanup;
  272. }
  273. } else {
  274. if (smbc_unlink(h->filename) < 0) {
  275. ret = AVERROR(errno);
  276. goto cleanup;
  277. }
  278. }
  279. ret = 0;
  280. cleanup:
  281. libsmbc_close(h);
  282. return ret;
  283. }
  284. static int libsmbc_move(URLContext *h_src, URLContext *h_dst)
  285. {
  286. LIBSMBContext *libsmbc = h_src->priv_data;
  287. int ret;
  288. if ((ret = libsmbc_connect(h_src)) < 0)
  289. goto cleanup;
  290. if ((libsmbc->dh = smbc_rename(h_src->filename, h_dst->filename)) < 0) {
  291. ret = AVERROR(errno);
  292. goto cleanup;
  293. }
  294. ret = 0;
  295. cleanup:
  296. libsmbc_close(h_src);
  297. return ret;
  298. }
  299. #define OFFSET(x) offsetof(LIBSMBContext, x)
  300. #define D AV_OPT_FLAG_DECODING_PARAM
  301. #define E AV_OPT_FLAG_ENCODING_PARAM
  302. static const AVOption options[] = {
  303. {"timeout", "set timeout in ms of socket I/O operations", OFFSET(timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
  304. {"truncate", "truncate existing files on write", OFFSET(trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
  305. {"workgroup", "set the workgroup used for making connections", OFFSET(workgroup), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
  306. {NULL}
  307. };
  308. static const AVClass libsmbclient_context_class = {
  309. .class_name = "libsmbc",
  310. .item_name = av_default_item_name,
  311. .option = options,
  312. .version = LIBAVUTIL_VERSION_INT,
  313. };
  314. const URLProtocol ff_libsmbclient_protocol = {
  315. .name = "smb",
  316. .url_open = libsmbc_open,
  317. .url_read = libsmbc_read,
  318. .url_write = libsmbc_write,
  319. .url_seek = libsmbc_seek,
  320. .url_close = libsmbc_close,
  321. .url_delete = libsmbc_delete,
  322. .url_move = libsmbc_move,
  323. .url_open_dir = libsmbc_open_dir,
  324. .url_read_dir = libsmbc_read_dir,
  325. .url_close_dir = libsmbc_close_dir,
  326. .priv_data_size = sizeof(LIBSMBContext),
  327. .priv_data_class = &libsmbclient_context_class,
  328. .flags = URL_PROTOCOL_FLAG_NETWORK,
  329. };