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.

369 lines
9.4KB

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