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.

415 lines
11KB

  1. /*
  2. * buffered file I/O
  3. * Copyright (c) 2001 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/internal.h"
  23. #include "libavutil/opt.h"
  24. #include "avformat.h"
  25. #if HAVE_DIRENT_H
  26. #include <dirent.h>
  27. #endif
  28. #include <fcntl.h>
  29. #if HAVE_IO_H
  30. #include <io.h>
  31. #endif
  32. #if HAVE_UNISTD_H
  33. #include <unistd.h>
  34. #endif
  35. #include <sys/stat.h>
  36. #include <stdlib.h>
  37. #include "os_support.h"
  38. #include "url.h"
  39. /* Some systems may not have S_ISFIFO */
  40. #ifndef S_ISFIFO
  41. # ifdef S_IFIFO
  42. # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
  43. # else
  44. # define S_ISFIFO(m) 0
  45. # endif
  46. #endif
  47. /* Not available in POSIX.1-1996 */
  48. #ifndef S_ISLNK
  49. # ifdef S_IFLNK
  50. # define S_ISLNK(m) (((m) & S_IFLNK) == S_IFLNK)
  51. # else
  52. # define S_ISLNK(m) 0
  53. # endif
  54. #endif
  55. /* Not available in POSIX.1-1996 */
  56. #ifndef S_ISSOCK
  57. # ifdef S_IFSOCK
  58. # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
  59. # else
  60. # define S_ISSOCK(m) 0
  61. # endif
  62. #endif
  63. /* standard file protocol */
  64. typedef struct FileContext {
  65. const AVClass *class;
  66. int fd;
  67. int trunc;
  68. int blocksize;
  69. int follow;
  70. int seekable;
  71. #if HAVE_DIRENT_H
  72. DIR *dir;
  73. #endif
  74. } FileContext;
  75. static const AVOption file_options[] = {
  76. { "truncate", "truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
  77. { "blocksize", "set I/O operation maximum block size", offsetof(FileContext, blocksize), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  78. { "follow", "Follow a file as it is being written", offsetof(FileContext, follow), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
  79. { "seekable", "Sets if the file is seekable", offsetof(FileContext, seekable), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 0, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
  80. { NULL }
  81. };
  82. static const AVOption pipe_options[] = {
  83. { "blocksize", "set I/O operation maximum block size", offsetof(FileContext, blocksize), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  84. { NULL }
  85. };
  86. static const AVClass file_class = {
  87. .class_name = "file",
  88. .item_name = av_default_item_name,
  89. .option = file_options,
  90. .version = LIBAVUTIL_VERSION_INT,
  91. };
  92. static const AVClass pipe_class = {
  93. .class_name = "pipe",
  94. .item_name = av_default_item_name,
  95. .option = pipe_options,
  96. .version = LIBAVUTIL_VERSION_INT,
  97. };
  98. static int file_read(URLContext *h, unsigned char *buf, int size)
  99. {
  100. FileContext *c = h->priv_data;
  101. int ret;
  102. size = FFMIN(size, c->blocksize);
  103. ret = read(c->fd, buf, size);
  104. if (ret == 0 && c->follow)
  105. return AVERROR(EAGAIN);
  106. if (ret == 0)
  107. return AVERROR_EOF;
  108. return (ret == -1) ? AVERROR(errno) : ret;
  109. }
  110. static int file_write(URLContext *h, const unsigned char *buf, int size)
  111. {
  112. FileContext *c = h->priv_data;
  113. int ret;
  114. size = FFMIN(size, c->blocksize);
  115. ret = write(c->fd, buf, size);
  116. return (ret == -1) ? AVERROR(errno) : ret;
  117. }
  118. static int file_get_handle(URLContext *h)
  119. {
  120. FileContext *c = h->priv_data;
  121. return c->fd;
  122. }
  123. static int file_check(URLContext *h, int mask)
  124. {
  125. int ret = 0;
  126. const char *filename = h->filename;
  127. av_strstart(filename, "file:", &filename);
  128. {
  129. #if HAVE_ACCESS && defined(R_OK)
  130. if (access(filename, F_OK) < 0)
  131. return AVERROR(errno);
  132. if (mask&AVIO_FLAG_READ)
  133. if (access(filename, R_OK) >= 0)
  134. ret |= AVIO_FLAG_READ;
  135. if (mask&AVIO_FLAG_WRITE)
  136. if (access(filename, W_OK) >= 0)
  137. ret |= AVIO_FLAG_WRITE;
  138. #else
  139. struct stat st;
  140. # ifndef _WIN32
  141. ret = stat(filename, &st);
  142. # else
  143. ret = win32_stat(filename, &st);
  144. # endif
  145. if (ret < 0)
  146. return AVERROR(errno);
  147. ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
  148. ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
  149. #endif
  150. }
  151. return ret;
  152. }
  153. static int file_delete(URLContext *h)
  154. {
  155. #if HAVE_UNISTD_H
  156. int ret;
  157. const char *filename = h->filename;
  158. av_strstart(filename, "file:", &filename);
  159. ret = rmdir(filename);
  160. if (ret < 0 && (errno == ENOTDIR
  161. # ifdef _WIN32
  162. || errno == EINVAL
  163. # endif
  164. ))
  165. ret = unlink(filename);
  166. if (ret < 0)
  167. return AVERROR(errno);
  168. return ret;
  169. #else
  170. return AVERROR(ENOSYS);
  171. #endif /* HAVE_UNISTD_H */
  172. }
  173. static int file_move(URLContext *h_src, URLContext *h_dst)
  174. {
  175. const char *filename_src = h_src->filename;
  176. const char *filename_dst = h_dst->filename;
  177. av_strstart(filename_src, "file:", &filename_src);
  178. av_strstart(filename_dst, "file:", &filename_dst);
  179. if (rename(filename_src, filename_dst) < 0)
  180. return AVERROR(errno);
  181. return 0;
  182. }
  183. #if CONFIG_FILE_PROTOCOL
  184. static int file_open(URLContext *h, const char *filename, int flags)
  185. {
  186. FileContext *c = h->priv_data;
  187. int access;
  188. int fd;
  189. struct stat st;
  190. av_strstart(filename, "file:", &filename);
  191. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  192. access = O_CREAT | O_RDWR;
  193. if (c->trunc)
  194. access |= O_TRUNC;
  195. } else if (flags & AVIO_FLAG_WRITE) {
  196. access = O_CREAT | O_WRONLY;
  197. if (c->trunc)
  198. access |= O_TRUNC;
  199. } else {
  200. access = O_RDONLY;
  201. }
  202. #ifdef O_BINARY
  203. access |= O_BINARY;
  204. #endif
  205. fd = avpriv_open(filename, access, 0666);
  206. if (fd == -1)
  207. return AVERROR(errno);
  208. c->fd = fd;
  209. h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
  210. /* Buffer writes more than the default 32k to improve throughput especially
  211. * with networked file systems */
  212. if (!h->is_streamed && flags & AVIO_FLAG_WRITE)
  213. h->min_packet_size = h->max_packet_size = 262144;
  214. if (c->seekable >= 0)
  215. h->is_streamed = !c->seekable;
  216. return 0;
  217. }
  218. /* XXX: use llseek */
  219. static int64_t file_seek(URLContext *h, int64_t pos, int whence)
  220. {
  221. FileContext *c = h->priv_data;
  222. int64_t ret;
  223. if (whence == AVSEEK_SIZE) {
  224. struct stat st;
  225. ret = fstat(c->fd, &st);
  226. return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
  227. }
  228. ret = lseek(c->fd, pos, whence);
  229. return ret < 0 ? AVERROR(errno) : ret;
  230. }
  231. static int file_close(URLContext *h)
  232. {
  233. FileContext *c = h->priv_data;
  234. return close(c->fd);
  235. }
  236. static int file_open_dir(URLContext *h)
  237. {
  238. #if HAVE_LSTAT
  239. FileContext *c = h->priv_data;
  240. c->dir = opendir(h->filename);
  241. if (!c->dir)
  242. return AVERROR(errno);
  243. return 0;
  244. #else
  245. return AVERROR(ENOSYS);
  246. #endif /* HAVE_LSTAT */
  247. }
  248. static int file_read_dir(URLContext *h, AVIODirEntry **next)
  249. {
  250. #if HAVE_LSTAT
  251. FileContext *c = h->priv_data;
  252. struct dirent *dir;
  253. char *fullpath = NULL;
  254. *next = ff_alloc_dir_entry();
  255. if (!*next)
  256. return AVERROR(ENOMEM);
  257. do {
  258. errno = 0;
  259. dir = readdir(c->dir);
  260. if (!dir) {
  261. av_freep(next);
  262. return AVERROR(errno);
  263. }
  264. } while (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."));
  265. fullpath = av_append_path_component(h->filename, dir->d_name);
  266. if (fullpath) {
  267. struct stat st;
  268. if (!lstat(fullpath, &st)) {
  269. if (S_ISDIR(st.st_mode))
  270. (*next)->type = AVIO_ENTRY_DIRECTORY;
  271. else if (S_ISFIFO(st.st_mode))
  272. (*next)->type = AVIO_ENTRY_NAMED_PIPE;
  273. else if (S_ISCHR(st.st_mode))
  274. (*next)->type = AVIO_ENTRY_CHARACTER_DEVICE;
  275. else if (S_ISBLK(st.st_mode))
  276. (*next)->type = AVIO_ENTRY_BLOCK_DEVICE;
  277. else if (S_ISLNK(st.st_mode))
  278. (*next)->type = AVIO_ENTRY_SYMBOLIC_LINK;
  279. else if (S_ISSOCK(st.st_mode))
  280. (*next)->type = AVIO_ENTRY_SOCKET;
  281. else if (S_ISREG(st.st_mode))
  282. (*next)->type = AVIO_ENTRY_FILE;
  283. else
  284. (*next)->type = AVIO_ENTRY_UNKNOWN;
  285. (*next)->group_id = st.st_gid;
  286. (*next)->user_id = st.st_uid;
  287. (*next)->size = st.st_size;
  288. (*next)->filemode = st.st_mode & 0777;
  289. (*next)->modification_timestamp = INT64_C(1000000) * st.st_mtime;
  290. (*next)->access_timestamp = INT64_C(1000000) * st.st_atime;
  291. (*next)->status_change_timestamp = INT64_C(1000000) * st.st_ctime;
  292. }
  293. av_free(fullpath);
  294. }
  295. (*next)->name = av_strdup(dir->d_name);
  296. return 0;
  297. #else
  298. return AVERROR(ENOSYS);
  299. #endif /* HAVE_LSTAT */
  300. }
  301. static int file_close_dir(URLContext *h)
  302. {
  303. #if HAVE_LSTAT
  304. FileContext *c = h->priv_data;
  305. closedir(c->dir);
  306. return 0;
  307. #else
  308. return AVERROR(ENOSYS);
  309. #endif /* HAVE_LSTAT */
  310. }
  311. const URLProtocol ff_file_protocol = {
  312. .name = "file",
  313. .url_open = file_open,
  314. .url_read = file_read,
  315. .url_write = file_write,
  316. .url_seek = file_seek,
  317. .url_close = file_close,
  318. .url_get_file_handle = file_get_handle,
  319. .url_check = file_check,
  320. .url_delete = file_delete,
  321. .url_move = file_move,
  322. .priv_data_size = sizeof(FileContext),
  323. .priv_data_class = &file_class,
  324. .url_open_dir = file_open_dir,
  325. .url_read_dir = file_read_dir,
  326. .url_close_dir = file_close_dir,
  327. .default_whitelist = "file,crypto,data"
  328. };
  329. #endif /* CONFIG_FILE_PROTOCOL */
  330. #if CONFIG_PIPE_PROTOCOL
  331. static int pipe_open(URLContext *h, const char *filename, int flags)
  332. {
  333. FileContext *c = h->priv_data;
  334. int fd;
  335. char *final;
  336. av_strstart(filename, "pipe:", &filename);
  337. fd = strtol(filename, &final, 10);
  338. if((filename == final) || *final ) {/* No digits found, or something like 10ab */
  339. if (flags & AVIO_FLAG_WRITE) {
  340. fd = 1;
  341. } else {
  342. fd = 0;
  343. }
  344. }
  345. #if HAVE_SETMODE
  346. setmode(fd, O_BINARY);
  347. #endif
  348. c->fd = fd;
  349. h->is_streamed = 1;
  350. return 0;
  351. }
  352. const URLProtocol ff_pipe_protocol = {
  353. .name = "pipe",
  354. .url_open = pipe_open,
  355. .url_read = file_read,
  356. .url_write = file_write,
  357. .url_get_file_handle = file_get_handle,
  358. .url_check = file_check,
  359. .priv_data_size = sizeof(FileContext),
  360. .priv_data_class = &pipe_class,
  361. .default_whitelist = "crypto,data"
  362. };
  363. #endif /* CONFIG_PIPE_PROTOCOL */