file.c 12 KB

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