Skip to content
Snippets Groups Projects
file.c 5.44 KiB
Newer Older
  • Learn to ignore specific revisions
  • Fabrice Bellard's avatar
    Fabrice Bellard committed
    /*
    
     * buffered file I/O
    
     * Copyright (c) 2001 Fabrice Bellard
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     *
    
     * This file is part of FFmpeg.
     *
     * FFmpeg is free software; you can redistribute it and/or
    
     * modify it under the terms of the GNU Lesser General Public
     * License as published by the Free Software Foundation; either
    
     * version 2.1 of the License, or (at your option) any later version.
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     *
    
     * FFmpeg is distributed in the hope that it will be useful,
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
    
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     * Lesser General Public License for more details.
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     *
    
     * You should have received a copy of the GNU Lesser General Public
    
     * License along with FFmpeg; if not, write to the Free Software
    
     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     */
    
    
    #include "libavutil/avstring.h"
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    #include "avformat.h"
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    #include <fcntl.h>
    
    #if HAVE_UNISTD_H
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    #include <unistd.h>
    
    #include <stdlib.h>
    
    #include "url.h"
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
    /* Some systems may not have S_ISFIFO */
    #ifndef S_ISFIFO
    #  ifdef S_IFIFO
    #    define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
    #  else
    #    define S_ISFIFO(m) 0
    #  endif
    #endif
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    /* standard file protocol */
    
    
    static const AVOption file_options[] = {
        { "truncate", "Truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
        { NULL }
    };
    
    static const AVClass file_class = {
        .class_name = "file",
        .item_name  = av_default_item_name,
        .option     = file_options,
        .version    = LIBAVUTIL_VERSION_INT,
    };
    
    
    static int file_read(URLContext *h, unsigned char *buf, int size)
    {
    
        int r = read(c->fd, buf, size);
    
        return (-1 == r)?AVERROR(errno):r;
    
    }
    
    static int file_write(URLContext *h, const unsigned char *buf, int size)
    {
    
        int r = write(c->fd, buf, size);
    
        return (-1 == r)?AVERROR(errno):r;
    
    }
    
    static int file_get_handle(URLContext *h)
    {
    
        FileContext *c = h->priv_data;
        return c->fd;
    
    static int file_check(URLContext *h, int mask)
    {
    
    #if HAVE_ACCESS && defined(R_OK)
    
        int ret = 0;
        if (access(h->filename, F_OK) < 0)
    
            return AVERROR(errno);
    
        if (mask&AVIO_FLAG_READ)
            if (access(h->filename, R_OK) >= 0)
                ret |= AVIO_FLAG_READ;
        if (mask&AVIO_FLAG_WRITE)
            if (access(h->filename, W_OK) >= 0)
                ret |= AVIO_FLAG_WRITE;
    
    #else
        struct stat st;
        int ret = stat(h->filename, &st);
        if (ret < 0)
            return AVERROR(errno);
    
        ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ  : 0;
        ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
    #endif
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    static int file_open(URLContext *h, const char *filename, int flags)
    {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        int access;
        int fd;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
        av_strstart(filename, "file:", &filename);
    
        if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
    
            access = O_CREAT | O_RDWR;
            if (c->trunc)
                access |= O_TRUNC;
    
        } else if (flags & AVIO_FLAG_WRITE) {
    
            access = O_CREAT | O_WRONLY;
            if (c->trunc)
                access |= O_TRUNC;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        } else {
            access = O_RDONLY;
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        access |= O_BINARY;
    #endif
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        fd = open(filename, access, 0666);
    
        h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        return 0;
    }
    
    /* XXX: use llseek */
    
    static int64_t file_seek(URLContext *h, int64_t pos, int whence)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        if (whence == AVSEEK_SIZE) {
            struct stat st;
    
            ret = fstat(c->fd, &st);
    
            return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
    
    
        ret = lseek(c->fd, pos, whence);
    
        return ret < 0 ? AVERROR(errno) : ret;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    static int file_close(URLContext *h)
    {
    
        FileContext *c = h->priv_data;
        return close(c->fd);
    
        .name                = "file",
        .url_open            = file_open,
        .url_read            = file_read,
        .url_write           = file_write,
        .url_seek            = file_seek,
        .url_close           = file_close,
    
        .url_get_file_handle = file_get_handle,
    
        .priv_data_size      = sizeof(FileContext),
    
    #endif /* CONFIG_FILE_PROTOCOL */
    
    #if CONFIG_PIPE_PROTOCOL
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    static int pipe_open(URLContext *h, const char *filename, int flags)
    {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        int fd;
    
        av_strstart(filename, "pipe:", &filename);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
        fd = strtol(filename, &final, 10);
        if((filename == final) || *final ) {/* No digits found, or something like 10ab */
    
    Vincent Fourmond's avatar
    Vincent Fourmond committed
                fd = 1;
            } else {
                fd = 0;
            }
    
        setmode(fd, O_BINARY);
    #endif
    
        h->is_streamed = 1;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        return 0;
    }
    
    
        .name                = "pipe",
        .url_open            = pipe_open,
        .url_read            = file_read,
        .url_write           = file_write,
    
        .url_get_file_handle = file_get_handle,
    
        .priv_data_size      = sizeof(FileContext),
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    };
    
    
    #endif /* CONFIG_PIPE_PROTOCOL */