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.

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