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.

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