file.c 4.73 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1
/*
2
 * buffered file I/O
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"
23
#include "libavutil/opt.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
24
#include "avformat.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
25
#include <fcntl.h>
26
#if HAVE_IO_H
27 28
#include <io.h>
#endif
29
#if HAVE_UNISTD_H
Fabrice Bellard's avatar
Fabrice Bellard committed
30
#include <unistd.h>
31
#endif
32
#include <sys/stat.h>
33
#include <stdlib.h>
34
#include "os_support.h"
35
#include "url.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
36 37 38 39


/* standard file protocol */

40
typedef struct FileContext {
41
    const AVClass *class;
42
    int fd;
43
    int trunc;
44 45
} FileContext;

46 47 48 49 50 51 52 53 54 55 56 57
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,
};

58 59
static int file_read(URLContext *h, unsigned char *buf, int size)
{
60 61
    FileContext *c = h->priv_data;
    return read(c->fd, buf, size);
62 63 64 65
}

static int file_write(URLContext *h, const unsigned char *buf, int size)
{
66 67
    FileContext *c = h->priv_data;
    return write(c->fd, buf, size);
68 69 70 71
}

static int file_get_handle(URLContext *h)
{
72 73
    FileContext *c = h->priv_data;
    return c->fd;
74 75
}

76 77 78 79 80 81 82 83 84 85 86 87 88
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;
}

89 90
#if CONFIG_FILE_PROTOCOL

Fabrice Bellard's avatar
Fabrice Bellard committed
91 92
static int file_open(URLContext *h, const char *filename, int flags)
{
93
    FileContext *c = h->priv_data;
Fabrice Bellard's avatar
Fabrice Bellard committed
94 95 96
    int access;
    int fd;

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

99
    if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
100 101 102
        access = O_CREAT | O_RDWR;
        if (c->trunc)
            access |= O_TRUNC;
103
    } else if (flags & AVIO_FLAG_WRITE) {
104 105 106
        access = O_CREAT | O_WRONLY;
        if (c->trunc)
            access |= O_TRUNC;
Fabrice Bellard's avatar
Fabrice Bellard committed
107 108 109
    } else {
        access = O_RDONLY;
    }
110
#ifdef O_BINARY
Fabrice Bellard's avatar
Fabrice Bellard committed
111 112
    access |= O_BINARY;
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
113
    fd = open(filename, access, 0666);
114
    if (fd == -1)
115
        return AVERROR(errno);
116
    c->fd = fd;
Fabrice Bellard's avatar
Fabrice Bellard committed
117 118 119 120
    return 0;
}

/* XXX: use llseek */
121
static int64_t file_seek(URLContext *h, int64_t pos, int whence)
Fabrice Bellard's avatar
Fabrice Bellard committed
122
{
123
    FileContext *c = h->priv_data;
124
    int64_t ret;
125

126 127
    if (whence == AVSEEK_SIZE) {
        struct stat st;
128 129

        ret = fstat(c->fd, &st);
130 131
        return ret < 0 ? AVERROR(errno) : st.st_size;
    }
132 133 134 135

    ret = lseek(c->fd, pos, whence);

    return ret < 0 ? AVERROR(errno) : ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
136 137 138 139
}

static int file_close(URLContext *h)
{
140 141
    FileContext *c = h->priv_data;
    return close(c->fd);
Fabrice Bellard's avatar
Fabrice Bellard committed
142 143
}

144
URLProtocol ff_file_protocol = {
145 146 147 148 149 150
    .name                = "file",
    .url_open            = file_open,
    .url_read            = file_read,
    .url_write           = file_write,
    .url_seek            = file_seek,
    .url_close           = file_close,
151
    .url_get_file_handle = file_get_handle,
152
    .url_check           = file_check,
153
    .priv_data_size      = sizeof(FileContext),
154
    .priv_data_class     = &file_class,
Fabrice Bellard's avatar
Fabrice Bellard committed
155 156
};

157 158 159
#endif /* CONFIG_FILE_PROTOCOL */

#if CONFIG_PIPE_PROTOCOL
Fabrice Bellard's avatar
Fabrice Bellard committed
160 161 162

static int pipe_open(URLContext *h, const char *filename, int flags)
{
163
    FileContext *c = h->priv_data;
Fabrice Bellard's avatar
Fabrice Bellard committed
164
    int fd;
165
    char *final;
166
    av_strstart(filename, "pipe:", &filename);
Fabrice Bellard's avatar
Fabrice Bellard committed
167

168 169
    fd = strtol(filename, &final, 10);
    if((filename == final) || *final ) {/* No digits found, or something like 10ab */
170
        if (flags & AVIO_FLAG_WRITE) {
171 172 173 174
            fd = 1;
        } else {
            fd = 0;
        }
175
    }
176
#if HAVE_SETMODE
177 178
    setmode(fd, O_BINARY);
#endif
179
    c->fd = fd;
180
    h->is_streamed = 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
181 182 183
    return 0;
}

184
URLProtocol ff_pipe_protocol = {
185 186 187 188
    .name                = "pipe",
    .url_open            = pipe_open,
    .url_read            = file_read,
    .url_write           = file_write,
189
    .url_get_file_handle = file_get_handle,
190
    .url_check           = file_check,
191
    .priv_data_size      = sizeof(FileContext),
Fabrice Bellard's avatar
Fabrice Bellard committed
192
};
193 194

#endif /* CONFIG_PIPE_PROTOCOL */