file.c 3.92 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1 2
/*
 * Buffered file io for ffmpeg system
3
 * Copyright (c) 2001 Fabrice Bellard
Fabrice Bellard's avatar
Fabrice Bellard committed
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
11
 *
12
 * Libav is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Fabrice Bellard's avatar
Fabrice Bellard committed
20
 */
21 22

#include "libavutil/avstring.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
23
#include "avformat.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
24
#include <fcntl.h>
25
#if HAVE_SETMODE
26 27
#include <io.h>
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
28
#include <unistd.h>
29
#include <sys/stat.h>
30
#include <stdlib.h>
31
#include "os_support.h"
32
#include "url.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
33 34 35 36


/* standard file protocol */

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
static int file_read(URLContext *h, unsigned char *buf, int size)
{
    int fd = (intptr_t) h->priv_data;
    return read(fd, buf, size);
}

static int file_write(URLContext *h, const unsigned char *buf, int size)
{
    int fd = (intptr_t) h->priv_data;
    return write(fd, buf, size);
}

static int file_get_handle(URLContext *h)
{
    return (intptr_t) h->priv_data;
}

54 55 56 57 58 59 60 61 62 63 64 65 66
static int file_check(URLContext *h, int mask)
{
    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;

    return ret;
}

67 68
#if CONFIG_FILE_PROTOCOL

Fabrice Bellard's avatar
Fabrice Bellard committed
69 70 71 72 73
static int file_open(URLContext *h, const char *filename, int flags)
{
    int access;
    int fd;

74
    av_strstart(filename, "file:", &filename);
Fabrice Bellard's avatar
Fabrice Bellard committed
75

76
    if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
77
        access = O_CREAT | O_TRUNC | O_RDWR;
78
    } else if (flags & AVIO_FLAG_WRITE) {
Fabrice Bellard's avatar
Fabrice Bellard committed
79 80 81 82
        access = O_CREAT | O_TRUNC | O_WRONLY;
    } else {
        access = O_RDONLY;
    }
83
#ifdef O_BINARY
Fabrice Bellard's avatar
Fabrice Bellard committed
84 85
    access |= O_BINARY;
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
86
    fd = open(filename, access, 0666);
87
    if (fd == -1)
88
        return AVERROR(errno);
89
    h->priv_data = (void *) (intptr_t) fd;
Fabrice Bellard's avatar
Fabrice Bellard committed
90 91 92 93
    return 0;
}

/* XXX: use llseek */
94
static int64_t file_seek(URLContext *h, int64_t pos, int whence)
Fabrice Bellard's avatar
Fabrice Bellard committed
95
{
96
    int fd = (intptr_t) h->priv_data;
97 98 99 100 101
    if (whence == AVSEEK_SIZE) {
        struct stat st;
        int ret = fstat(fd, &st);
        return ret < 0 ? AVERROR(errno) : st.st_size;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
102 103 104 105 106
    return lseek(fd, pos, whence);
}

static int file_close(URLContext *h)
{
107
    int fd = (intptr_t) h->priv_data;
Fabrice Bellard's avatar
Fabrice Bellard committed
108 109 110
    return close(fd);
}

111
URLProtocol ff_file_protocol = {
112 113 114 115 116 117
    .name                = "file",
    .url_open            = file_open,
    .url_read            = file_read,
    .url_write           = file_write,
    .url_seek            = file_seek,
    .url_close           = file_close,
118
    .url_get_file_handle = file_get_handle,
119
    .url_check           = file_check,
Fabrice Bellard's avatar
Fabrice Bellard committed
120 121
};

122 123 124
#endif /* CONFIG_FILE_PROTOCOL */

#if CONFIG_PIPE_PROTOCOL
Fabrice Bellard's avatar
Fabrice Bellard committed
125 126 127 128

static int pipe_open(URLContext *h, const char *filename, int flags)
{
    int fd;
129
    char *final;
130
    av_strstart(filename, "pipe:", &filename);
Fabrice Bellard's avatar
Fabrice Bellard committed
131

132 133
    fd = strtol(filename, &final, 10);
    if((filename == final) || *final ) {/* No digits found, or something like 10ab */
134
        if (flags & AVIO_FLAG_WRITE) {
135 136 137 138
            fd = 1;
        } else {
            fd = 0;
        }
139
    }
140
#if HAVE_SETMODE
141 142
    setmode(fd, O_BINARY);
#endif
143
    h->priv_data = (void *) (intptr_t) fd;
144
    h->is_streamed = 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
145 146 147
    return 0;
}

148
URLProtocol ff_pipe_protocol = {
149 150 151 152
    .name                = "pipe",
    .url_open            = pipe_open,
    .url_read            = file_read,
    .url_write           = file_write,
153
    .url_get_file_handle = file_get_handle,
154
    .url_check           = file_check,
Fabrice Bellard's avatar
Fabrice Bellard committed
155
};
156 157

#endif /* CONFIG_PIPE_PROTOCOL */