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.

401 lines
10KB

  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. ret = unlink(filename);
  160. if (ret < 0)
  161. return AVERROR(errno);
  162. return ret;
  163. #else
  164. return AVERROR(ENOSYS);
  165. #endif /* HAVE_UNISTD_H */
  166. }
  167. static int file_move(URLContext *h_src, URLContext *h_dst)
  168. {
  169. const char *filename_src = h_src->filename;
  170. const char *filename_dst = h_dst->filename;
  171. av_strstart(filename_src, "file:", &filename_src);
  172. av_strstart(filename_dst, "file:", &filename_dst);
  173. if (rename(filename_src, filename_dst) < 0)
  174. return AVERROR(errno);
  175. return 0;
  176. }
  177. #if CONFIG_FILE_PROTOCOL
  178. static int file_open(URLContext *h, const char *filename, int flags)
  179. {
  180. FileContext *c = h->priv_data;
  181. int access;
  182. int fd;
  183. struct stat st;
  184. av_strstart(filename, "file:", &filename);
  185. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  186. access = O_CREAT | O_RDWR;
  187. if (c->trunc)
  188. access |= O_TRUNC;
  189. } else if (flags & AVIO_FLAG_WRITE) {
  190. access = O_CREAT | O_WRONLY;
  191. if (c->trunc)
  192. access |= O_TRUNC;
  193. } else {
  194. access = O_RDONLY;
  195. }
  196. #ifdef O_BINARY
  197. access |= O_BINARY;
  198. #endif
  199. fd = avpriv_open(filename, access, 0666);
  200. if (fd == -1)
  201. return AVERROR(errno);
  202. c->fd = fd;
  203. h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
  204. return 0;
  205. }
  206. /* XXX: use llseek */
  207. static int64_t file_seek(URLContext *h, int64_t pos, int whence)
  208. {
  209. FileContext *c = h->priv_data;
  210. int64_t ret;
  211. if (whence == AVSEEK_SIZE) {
  212. struct stat st;
  213. ret = fstat(c->fd, &st);
  214. return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
  215. }
  216. ret = lseek(c->fd, pos, whence);
  217. return ret < 0 ? AVERROR(errno) : ret;
  218. }
  219. static int file_close(URLContext *h)
  220. {
  221. FileContext *c = h->priv_data;
  222. return close(c->fd);
  223. }
  224. static int file_open_dir(URLContext *h)
  225. {
  226. #if HAVE_LSTAT
  227. FileContext *c = h->priv_data;
  228. c->dir = opendir(h->filename);
  229. if (!c->dir)
  230. return AVERROR(errno);
  231. return 0;
  232. #else
  233. return AVERROR(ENOSYS);
  234. #endif /* HAVE_LSTAT */
  235. }
  236. static int file_read_dir(URLContext *h, AVIODirEntry **next)
  237. {
  238. #if HAVE_LSTAT
  239. FileContext *c = h->priv_data;
  240. struct dirent *dir;
  241. char *fullpath = NULL;
  242. *next = ff_alloc_dir_entry();
  243. if (!*next)
  244. return AVERROR(ENOMEM);
  245. do {
  246. errno = 0;
  247. dir = readdir(c->dir);
  248. if (!dir) {
  249. av_freep(next);
  250. return AVERROR(errno);
  251. }
  252. } while (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."));
  253. fullpath = av_append_path_component(h->filename, dir->d_name);
  254. if (fullpath) {
  255. struct stat st;
  256. if (!lstat(fullpath, &st)) {
  257. if (S_ISDIR(st.st_mode))
  258. (*next)->type = AVIO_ENTRY_DIRECTORY;
  259. else if (S_ISFIFO(st.st_mode))
  260. (*next)->type = AVIO_ENTRY_NAMED_PIPE;
  261. else if (S_ISCHR(st.st_mode))
  262. (*next)->type = AVIO_ENTRY_CHARACTER_DEVICE;
  263. else if (S_ISBLK(st.st_mode))
  264. (*next)->type = AVIO_ENTRY_BLOCK_DEVICE;
  265. else if (S_ISLNK(st.st_mode))
  266. (*next)->type = AVIO_ENTRY_SYMBOLIC_LINK;
  267. else if (S_ISSOCK(st.st_mode))
  268. (*next)->type = AVIO_ENTRY_SOCKET;
  269. else if (S_ISREG(st.st_mode))
  270. (*next)->type = AVIO_ENTRY_FILE;
  271. else
  272. (*next)->type = AVIO_ENTRY_UNKNOWN;
  273. (*next)->group_id = st.st_gid;
  274. (*next)->user_id = st.st_uid;
  275. (*next)->size = st.st_size;
  276. (*next)->filemode = st.st_mode & 0777;
  277. (*next)->modification_timestamp = INT64_C(1000000) * st.st_mtime;
  278. (*next)->access_timestamp = INT64_C(1000000) * st.st_atime;
  279. (*next)->status_change_timestamp = INT64_C(1000000) * st.st_ctime;
  280. }
  281. av_free(fullpath);
  282. }
  283. (*next)->name = av_strdup(dir->d_name);
  284. return 0;
  285. #else
  286. return AVERROR(ENOSYS);
  287. #endif /* HAVE_LSTAT */
  288. }
  289. static int file_close_dir(URLContext *h)
  290. {
  291. #if HAVE_LSTAT
  292. FileContext *c = h->priv_data;
  293. closedir(c->dir);
  294. return 0;
  295. #else
  296. return AVERROR(ENOSYS);
  297. #endif /* HAVE_LSTAT */
  298. }
  299. const URLProtocol ff_file_protocol = {
  300. .name = "file",
  301. .url_open = file_open,
  302. .url_read = file_read,
  303. .url_write = file_write,
  304. .url_seek = file_seek,
  305. .url_close = file_close,
  306. .url_get_file_handle = file_get_handle,
  307. .url_check = file_check,
  308. .url_delete = file_delete,
  309. .url_move = file_move,
  310. .priv_data_size = sizeof(FileContext),
  311. .priv_data_class = &file_class,
  312. .url_open_dir = file_open_dir,
  313. .url_read_dir = file_read_dir,
  314. .url_close_dir = file_close_dir,
  315. .default_whitelist = "file,crypto"
  316. };
  317. #endif /* CONFIG_FILE_PROTOCOL */
  318. #if CONFIG_PIPE_PROTOCOL
  319. static int pipe_open(URLContext *h, const char *filename, int flags)
  320. {
  321. FileContext *c = h->priv_data;
  322. int fd;
  323. char *final;
  324. av_strstart(filename, "pipe:", &filename);
  325. fd = strtol(filename, &final, 10);
  326. if((filename == final) || *final ) {/* No digits found, or something like 10ab */
  327. if (flags & AVIO_FLAG_WRITE) {
  328. fd = 1;
  329. } else {
  330. fd = 0;
  331. }
  332. }
  333. #if HAVE_SETMODE
  334. setmode(fd, O_BINARY);
  335. #endif
  336. c->fd = fd;
  337. h->is_streamed = 1;
  338. return 0;
  339. }
  340. const URLProtocol ff_pipe_protocol = {
  341. .name = "pipe",
  342. .url_open = pipe_open,
  343. .url_read = file_read,
  344. .url_write = file_write,
  345. .url_get_file_handle = file_get_handle,
  346. .url_check = file_check,
  347. .priv_data_size = sizeof(FileContext),
  348. .priv_data_class = &pipe_class,
  349. .default_whitelist = "crypto"
  350. };
  351. #endif /* CONFIG_PIPE_PROTOCOL */