• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/file.c

Go to the documentation of this file.
00001 /*
00002  * Buffered file io for ffmpeg system
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     return read(fd, buf, size);
00041 }
00042 
00043 static int file_write(URLContext *h, const unsigned char *buf, int size)
00044 {
00045     int fd = (intptr_t) h->priv_data;
00046     return write(fd, buf, size);
00047 }
00048 
00049 static int file_get_handle(URLContext *h)
00050 {
00051     return (intptr_t) h->priv_data;
00052 }
00053 
00054 static int file_check(URLContext *h, int mask)
00055 {
00056     struct stat st;
00057     int ret = stat(h->filename, &st);
00058     if (ret < 0)
00059         return AVERROR(errno);
00060 
00061     ret |= st.st_mode&S_IRUSR ? mask&AVIO_RDONLY : 0;
00062     ret |= st.st_mode&S_IWUSR ? mask&AVIO_WRONLY : 0;
00063     ret |= st.st_mode&S_IWUSR && st.st_mode&S_IRUSR ? mask&AVIO_RDWR : 0;
00064 
00065     return ret;
00066 }
00067 
00068 #if CONFIG_FILE_PROTOCOL
00069 
00070 static int file_open(URLContext *h, const char *filename, int flags)
00071 {
00072     int access;
00073     int fd;
00074 
00075     av_strstart(filename, "file:", &filename);
00076 
00077     if (flags & AVIO_RDWR) {
00078         access = O_CREAT | O_TRUNC | O_RDWR;
00079     } else if (flags & AVIO_WRONLY) {
00080         access = O_CREAT | O_TRUNC | O_WRONLY;
00081     } else {
00082         access = O_RDONLY;
00083     }
00084 #ifdef O_BINARY
00085     access |= O_BINARY;
00086 #endif
00087     fd = open(filename, access, 0666);
00088     if (fd == -1)
00089         return AVERROR(errno);
00090     h->priv_data = (void *) (intptr_t) fd;
00091     return 0;
00092 }
00093 
00094 /* XXX: use llseek */
00095 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
00096 {
00097     int fd = (intptr_t) h->priv_data;
00098     if (whence == AVSEEK_SIZE) {
00099         struct stat st;
00100         int ret = fstat(fd, &st);
00101         return ret < 0 ? AVERROR(errno) : st.st_size;
00102     }
00103     return lseek(fd, pos, whence);
00104 }
00105 
00106 static int file_close(URLContext *h)
00107 {
00108     int fd = (intptr_t) h->priv_data;
00109     return close(fd);
00110 }
00111 
00112 URLProtocol ff_file_protocol = {
00113     .name                = "file",
00114     .url_open            = file_open,
00115     .url_read            = file_read,
00116     .url_write           = file_write,
00117     .url_seek            = file_seek,
00118     .url_close           = file_close,
00119     .url_get_file_handle = file_get_handle,
00120     .url_check           = file_check,
00121 };
00122 
00123 #endif /* CONFIG_FILE_PROTOCOL */
00124 
00125 #if CONFIG_PIPE_PROTOCOL
00126 
00127 static int pipe_open(URLContext *h, const char *filename, int flags)
00128 {
00129     int fd;
00130     char *final;
00131     av_strstart(filename, "pipe:", &filename);
00132 
00133     fd = strtol(filename, &final, 10);
00134     if((filename == final) || *final ) {/* No digits found, or something like 10ab */
00135         if (flags & AVIO_WRONLY) {
00136             fd = 1;
00137         } else {
00138             fd = 0;
00139         }
00140     }
00141 #if HAVE_SETMODE
00142     setmode(fd, O_BINARY);
00143 #endif
00144     h->priv_data = (void *) (intptr_t) fd;
00145     h->is_streamed = 1;
00146     return 0;
00147 }
00148 
00149 URLProtocol ff_pipe_protocol = {
00150     .name                = "pipe",
00151     .url_open            = pipe_open,
00152     .url_read            = file_read,
00153     .url_write           = file_write,
00154     .url_get_file_handle = file_get_handle,
00155     .url_check           = file_check,
00156 };
00157 
00158 #endif /* CONFIG_PIPE_PROTOCOL */

Generated on Wed Apr 11 2012 07:31:36 for FFmpeg by  doxygen 1.7.1