• 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 I/O
00003  * Copyright (c) 2001 Fabrice Bellard
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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_FLAG_READ  : 0;
00062     ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
00063 
00064     return ret;
00065 }
00066 
00067 #if CONFIG_FILE_PROTOCOL
00068 
00069 static int file_open(URLContext *h, const char *filename, int flags)
00070 {
00071     int access;
00072     int fd;
00073 
00074     av_strstart(filename, "file:", &filename);
00075 
00076     if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
00077         access = O_CREAT | O_TRUNC | O_RDWR;
00078     } else if (flags & AVIO_FLAG_WRITE) {
00079         access = O_CREAT | O_TRUNC | O_WRONLY;
00080     } else {
00081         access = O_RDONLY;
00082     }
00083 #ifdef O_BINARY
00084     access |= O_BINARY;
00085 #endif
00086     fd = open(filename, access, 0666);
00087     if (fd == -1)
00088         return AVERROR(errno);
00089     h->priv_data = (void *) (intptr_t) fd;
00090     return 0;
00091 }
00092 
00093 /* XXX: use llseek */
00094 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
00095 {
00096     int fd = (intptr_t) h->priv_data;
00097     if (whence == AVSEEK_SIZE) {
00098         struct stat st;
00099         int ret = fstat(fd, &st);
00100         return ret < 0 ? AVERROR(errno) : st.st_size;
00101     }
00102     return lseek(fd, pos, whence);
00103 }
00104 
00105 static int file_close(URLContext *h)
00106 {
00107     int fd = (intptr_t) h->priv_data;
00108     return close(fd);
00109 }
00110 
00111 URLProtocol ff_file_protocol = {
00112     .name                = "file",
00113     .url_open            = file_open,
00114     .url_read            = file_read,
00115     .url_write           = file_write,
00116     .url_seek            = file_seek,
00117     .url_close           = file_close,
00118     .url_get_file_handle = file_get_handle,
00119     .url_check           = file_check,
00120 };
00121 
00122 #endif /* CONFIG_FILE_PROTOCOL */
00123 
00124 #if CONFIG_PIPE_PROTOCOL
00125 
00126 static int pipe_open(URLContext *h, const char *filename, int flags)
00127 {
00128     int fd;
00129     char *final;
00130     av_strstart(filename, "pipe:", &filename);
00131 
00132     fd = strtol(filename, &final, 10);
00133     if((filename == final) || *final ) {/* No digits found, or something like 10ab */
00134         if (flags & AVIO_FLAG_WRITE) {
00135             fd = 1;
00136         } else {
00137             fd = 0;
00138         }
00139     }
00140 #if HAVE_SETMODE
00141     setmode(fd, O_BINARY);
00142 #endif
00143     h->priv_data = (void *) (intptr_t) fd;
00144     h->is_streamed = 1;
00145     return 0;
00146 }
00147 
00148 URLProtocol ff_pipe_protocol = {
00149     .name                = "pipe",
00150     .url_open            = pipe_open,
00151     .url_read            = file_read,
00152     .url_write           = file_write,
00153     .url_get_file_handle = file_get_handle,
00154     .url_check           = file_check,
00155 };
00156 
00157 #endif /* CONFIG_PIPE_PROTOCOL */
Generated on Thu Jul 11 2013 15:38:23 for Libav by doxygen 1.7.1