00001 /* 00002 * buffered file I/O 00003 * Copyright (c) 2001 Fabrice Bellard 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #include "libavutil/avstring.h" 00023 #include "avformat.h" 00024 #include <fcntl.h> 00025 #if HAVE_SETMODE 00026 #include <io.h> 00027 #endif 00028 #include <unistd.h> 00029 #include <sys/stat.h> 00030 #include <stdlib.h> 00031 #include "os_support.h" 00032 #include "url.h" 00033 00034 00035 /* standard file protocol */ 00036 00037 static int file_read(URLContext *h, unsigned char *buf, int size) 00038 { 00039 int fd = (intptr_t) h->priv_data; 00040 int r = read(fd, buf, size); 00041 return (-1 == r)?AVERROR(errno):r; 00042 } 00043 00044 static int file_write(URLContext *h, const unsigned char *buf, int size) 00045 { 00046 int fd = (intptr_t) h->priv_data; 00047 int r = write(fd, buf, size); 00048 return (-1 == r)?AVERROR(errno):r; 00049 } 00050 00051 static int file_get_handle(URLContext *h) 00052 { 00053 return (intptr_t) h->priv_data; 00054 } 00055 00056 static int file_check(URLContext *h, int mask) 00057 { 00058 struct stat st; 00059 int ret = stat(h->filename, &st); 00060 if (ret < 0) 00061 return AVERROR(errno); 00062 00063 ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0; 00064 ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0; 00065 00066 return ret; 00067 } 00068 00069 #if CONFIG_FILE_PROTOCOL 00070 00071 static int file_open(URLContext *h, const char *filename, int flags) 00072 { 00073 int access; 00074 int fd; 00075 00076 av_strstart(filename, "file:", &filename); 00077 00078 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) { 00079 access = O_CREAT | O_TRUNC | O_RDWR; 00080 } else if (flags & AVIO_FLAG_WRITE) { 00081 access = O_CREAT | O_TRUNC | O_WRONLY; 00082 } else { 00083 access = O_RDONLY; 00084 } 00085 #ifdef O_BINARY 00086 access |= O_BINARY; 00087 #endif 00088 fd = open(filename, access, 0666); 00089 if (fd == -1) 00090 return AVERROR(errno); 00091 h->priv_data = (void *) (intptr_t) fd; 00092 return 0; 00093 } 00094 00095 /* XXX: use llseek */ 00096 static int64_t file_seek(URLContext *h, int64_t pos, int whence) 00097 { 00098 int fd = (intptr_t) h->priv_data; 00099 if (whence == AVSEEK_SIZE) { 00100 struct stat st; 00101 int ret = fstat(fd, &st); 00102 return ret < 0 ? AVERROR(errno) : st.st_size; 00103 } 00104 return lseek(fd, pos, whence); 00105 } 00106 00107 static int file_close(URLContext *h) 00108 { 00109 int fd = (intptr_t) h->priv_data; 00110 return close(fd); 00111 } 00112 00113 URLProtocol ff_file_protocol = { 00114 .name = "file", 00115 .url_open = file_open, 00116 .url_read = file_read, 00117 .url_write = file_write, 00118 .url_seek = file_seek, 00119 .url_close = file_close, 00120 .url_get_file_handle = file_get_handle, 00121 .url_check = file_check, 00122 }; 00123 00124 #endif /* CONFIG_FILE_PROTOCOL */ 00125 00126 #if CONFIG_PIPE_PROTOCOL 00127 00128 static int pipe_open(URLContext *h, const char *filename, int flags) 00129 { 00130 int fd; 00131 char *final; 00132 av_strstart(filename, "pipe:", &filename); 00133 00134 fd = strtol(filename, &final, 10); 00135 if((filename == final) || *final ) {/* No digits found, or something like 10ab */ 00136 if (flags & AVIO_FLAG_WRITE) { 00137 fd = 1; 00138 } else { 00139 fd = 0; 00140 } 00141 } 00142 #if HAVE_SETMODE 00143 setmode(fd, O_BINARY); 00144 #endif 00145 h->priv_data = (void *) (intptr_t) fd; 00146 h->is_streamed = 1; 00147 return 0; 00148 } 00149 00150 URLProtocol ff_pipe_protocol = { 00151 .name = "pipe", 00152 .url_open = pipe_open, 00153 .url_read = file_read, 00154 .url_write = file_write, 00155 .url_get_file_handle = file_get_handle, 00156 .url_check = file_check, 00157 }; 00158 00159 #endif /* CONFIG_PIPE_PROTOCOL */