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.

410 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. #if HAVE_DIRENT_H
  71. DIR *dir;
  72. #endif
  73. } FileContext;
  74. static const AVOption file_options[] = {
  75. { "truncate", "truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
  76. { "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 },
  77. { "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 },
  78. { NULL }
  79. };
  80. static const AVOption pipe_options[] = {
  81. { "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 },
  82. { NULL }
  83. };
  84. static const AVClass file_class = {
  85. .class_name = "file",
  86. .item_name = av_default_item_name,
  87. .option = file_options,
  88. .version = LIBAVUTIL_VERSION_INT,
  89. };
  90. static const AVClass pipe_class = {
  91. .class_name = "pipe",
  92. .item_name = av_default_item_name,
  93. .option = pipe_options,
  94. .version = LIBAVUTIL_VERSION_INT,
  95. };
  96. static int file_read(URLContext *h, unsigned char *buf, int size)
  97. {
  98. FileContext *c = h->priv_data;
  99. int ret;
  100. size = FFMIN(size, c->blocksize);
  101. ret = read(c->fd, buf, size);
  102. if (ret == 0 && c->follow)
  103. return AVERROR(EAGAIN);
  104. if (ret == 0)
  105. return AVERROR_EOF;
  106. return (ret == -1) ? AVERROR(errno) : ret;
  107. }
  108. static int file_write(URLContext *h, const unsigned char *buf, int size)
  109. {
  110. FileContext *c = h->priv_data;
  111. int ret;
  112. size = FFMIN(size, c->blocksize);
  113. ret = write(c->fd, buf, size);
  114. return (ret == -1) ? AVERROR(errno) : ret;
  115. }
  116. static int file_get_handle(URLContext *h)
  117. {
  118. FileContext *c = h->priv_data;
  119. return c->fd;
  120. }
  121. static int file_check(URLContext *h, int mask)
  122. {
  123. int ret = 0;
  124. const char *filename = h->filename;
  125. av_strstart(filename, "file:", &filename);
  126. {
  127. #if HAVE_ACCESS && defined(R_OK)
  128. if (access(filename, F_OK) < 0)
  129. return AVERROR(errno);
  130. if (mask&AVIO_FLAG_READ)
  131. if (access(filename, R_OK) >= 0)
  132. ret |= AVIO_FLAG_READ;
  133. if (mask&AVIO_FLAG_WRITE)
  134. if (access(filename, W_OK) >= 0)
  135. ret |= AVIO_FLAG_WRITE;
  136. #else
  137. struct stat st;
  138. # ifndef _WIN32
  139. ret = stat(filename, &st);
  140. # else
  141. ret = win32_stat(filename, &st);
  142. # endif
  143. if (ret < 0)
  144. return AVERROR(errno);
  145. ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
  146. ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
  147. #endif
  148. }
  149. return ret;
  150. }
  151. static int file_delete(URLContext *h)
  152. {
  153. #if HAVE_UNISTD_H
  154. int ret;
  155. const char *filename = h->filename;
  156. av_strstart(filename, "file:", &filename);
  157. ret = rmdir(filename);
  158. if (ret < 0 && (errno == ENOTDIR
  159. # ifdef _WIN32
  160. || errno == EINVAL
  161. # endif
  162. ))
  163. ret = unlink(filename);
  164. if (ret < 0)
  165. return AVERROR(errno);
  166. return ret;
  167. #else
  168. return AVERROR(ENOSYS);
  169. #endif /* HAVE_UNISTD_H */
  170. }
  171. static int file_move(URLContext *h_src, URLContext *h_dst)
  172. {
  173. const char *filename_src = h_src->filename;
  174. const char *filename_dst = h_dst->filename;
  175. av_strstart(filename_src, "file:", &filename_src);
  176. av_strstart(filename_dst, "file:", &filename_dst);
  177. if (rename(filename_src, filename_dst) < 0)
  178. return AVERROR(errno);
  179. return 0;
  180. }
  181. #if CONFIG_FILE_PROTOCOL
  182. static int file_open(URLContext *h, const char *filename, int flags)
  183. {
  184. FileContext *c = h->priv_data;
  185. int access;
  186. int fd;
  187. struct stat st;
  188. av_strstart(filename, "file:", &filename);
  189. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  190. access = O_CREAT | O_RDWR;
  191. if (c->trunc)
  192. access |= O_TRUNC;
  193. } else if (flags & AVIO_FLAG_WRITE) {
  194. access = O_CREAT | O_WRONLY;
  195. if (c->trunc)
  196. access |= O_TRUNC;
  197. } else {
  198. access = O_RDONLY;
  199. }
  200. #ifdef O_BINARY
  201. access |= O_BINARY;
  202. #endif
  203. fd = avpriv_open(filename, access, 0666);
  204. if (fd == -1)
  205. return AVERROR(errno);
  206. c->fd = fd;
  207. h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
  208. /* Buffer writes more than the default 32k to improve throughput especially
  209. * with networked file systems */
  210. if (!h->is_streamed && flags & AVIO_FLAG_WRITE)
  211. h->min_packet_size = h->max_packet_size = 262144;
  212. return 0;
  213. }
  214. /* XXX: use llseek */
  215. static int64_t file_seek(URLContext *h, int64_t pos, int whence)
  216. {
  217. FileContext *c = h->priv_data;
  218. int64_t ret;
  219. if (whence == AVSEEK_SIZE) {
  220. struct stat st;
  221. ret = fstat(c->fd, &st);
  222. return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
  223. }
  224. ret = lseek(c->fd, pos, whence);
  225. return ret < 0 ? AVERROR(errno) : ret;
  226. }
  227. static int file_close(URLContext *h)
  228. {
  229. FileContext *c = h->priv_data;
  230. return close(c->fd);
  231. }
  232. static int file_open_dir(URLContext *h)
  233. {
  234. #if HAVE_LSTAT
  235. FileContext *c = h->priv_data;
  236. c->dir = opendir(h->filename);
  237. if (!c->dir)
  238. return AVERROR(errno);
  239. return 0;
  240. #else
  241. return AVERROR(ENOSYS);
  242. #endif /* HAVE_LSTAT */
  243. }
  244. static int file_read_dir(URLContext *h, AVIODirEntry **next)
  245. {
  246. #if HAVE_LSTAT
  247. FileContext *c = h->priv_data;
  248. struct dirent *dir;
  249. char *fullpath = NULL;
  250. *next = ff_alloc_dir_entry();
  251. if (!*next)
  252. return AVERROR(ENOMEM);
  253. do {
  254. errno = 0;
  255. dir = readdir(c->dir);
  256. if (!dir) {
  257. av_freep(next);
  258. return AVERROR(errno);
  259. }
  260. } while (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."));
  261. fullpath = av_append_path_component(h->filename, dir->d_name);
  262. if (fullpath) {
  263. struct stat st;
  264. if (!lstat(fullpath, &st)) {
  265. if (S_ISDIR(st.st_mode))
  266. (*next)->type = AVIO_ENTRY_DIRECTORY;
  267. else if (S_ISFIFO(st.st_mode))
  268. (*next)->type = AVIO_ENTRY_NAMED_PIPE;
  269. else if (S_ISCHR(st.st_mode))
  270. (*next)->type = AVIO_ENTRY_CHARACTER_DEVICE;
  271. else if (S_ISBLK(st.st_mode))
  272. (*next)->type = AVIO_ENTRY_BLOCK_DEVICE;
  273. else if (S_ISLNK(st.st_mode))
  274. (*next)->type = AVIO_ENTRY_SYMBOLIC_LINK;
  275. else if (S_ISSOCK(st.st_mode))
  276. (*next)->type = AVIO_ENTRY_SOCKET;
  277. else if (S_ISREG(st.st_mode))
  278. (*next)->type = AVIO_ENTRY_FILE;
  279. else
  280. (*next)->type = AVIO_ENTRY_UNKNOWN;
  281. (*next)->group_id = st.st_gid;
  282. (*next)->user_id = st.st_uid;
  283. (*next)->size = st.st_size;
  284. (*next)->filemode = st.st_mode & 0777;
  285. (*next)->modification_timestamp = INT64_C(1000000) * st.st_mtime;
  286. (*next)->access_timestamp = INT64_C(1000000) * st.st_atime;
  287. (*next)->status_change_timestamp = INT64_C(1000000) * st.st_ctime;
  288. }
  289. av_free(fullpath);
  290. }
  291. (*next)->name = av_strdup(dir->d_name);
  292. return 0;
  293. #else
  294. return AVERROR(ENOSYS);
  295. #endif /* HAVE_LSTAT */
  296. }
  297. static int file_close_dir(URLContext *h)
  298. {
  299. #if HAVE_LSTAT
  300. FileContext *c = h->priv_data;
  301. closedir(c->dir);
  302. return 0;
  303. #else
  304. return AVERROR(ENOSYS);
  305. #endif /* HAVE_LSTAT */
  306. }
  307. const URLProtocol ff_file_protocol = {
  308. .name = "file",
  309. .url_open = file_open,
  310. .url_read = file_read,
  311. .url_write = file_write,
  312. .url_seek = file_seek,
  313. .url_close = file_close,
  314. .url_get_file_handle = file_get_handle,
  315. .url_check = file_check,
  316. .url_delete = file_delete,
  317. .url_move = file_move,
  318. .priv_data_size = sizeof(FileContext),
  319. .priv_data_class = &file_class,
  320. .url_open_dir = file_open_dir,
  321. .url_read_dir = file_read_dir,
  322. .url_close_dir = file_close_dir,
  323. .default_whitelist = "file,crypto"
  324. };
  325. #endif /* CONFIG_FILE_PROTOCOL */
  326. #if CONFIG_PIPE_PROTOCOL
  327. static int pipe_open(URLContext *h, const char *filename, int flags)
  328. {
  329. FileContext *c = h->priv_data;
  330. int fd;
  331. char *final;
  332. av_strstart(filename, "pipe:", &filename);
  333. fd = strtol(filename, &final, 10);
  334. if((filename == final) || *final ) {/* No digits found, or something like 10ab */
  335. if (flags & AVIO_FLAG_WRITE) {
  336. fd = 1;
  337. } else {
  338. fd = 0;
  339. }
  340. }
  341. #if HAVE_SETMODE
  342. setmode(fd, O_BINARY);
  343. #endif
  344. c->fd = fd;
  345. h->is_streamed = 1;
  346. return 0;
  347. }
  348. const URLProtocol ff_pipe_protocol = {
  349. .name = "pipe",
  350. .url_open = pipe_open,
  351. .url_read = file_read,
  352. .url_write = file_write,
  353. .url_get_file_handle = file_get_handle,
  354. .url_check = file_check,
  355. .priv_data_size = sizeof(FileContext),
  356. .priv_data_class = &pipe_class,
  357. .default_whitelist = "crypto"
  358. };
  359. #endif /* CONFIG_PIPE_PROTOCOL */