file.c 4.09 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 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg 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
 * FFmpeg 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 FFmpeg; 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
static int file_read(URLContext *h, unsigned char *buf, int size)
{
    int fd = (intptr_t) h->priv_data;
40 41
    int r = read(fd, buf, size);
    return (-1 == r)?AVERROR(errno):r;
42 43 44 45 46
}

static int file_write(URLContext *h, const unsigned char *buf, int size)
{
    int fd = (intptr_t) h->priv_data;
47 48
    int r = write(fd, buf, size);
    return (-1 == r)?AVERROR(errno):r;
49 50 51 52 53 54 55
}

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

56 57 58 59 60 61 62 63 64 65 66 67 68
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;
}

69 70
#if CONFIG_FILE_PROTOCOL

Fabrice Bellard's avatar
Fabrice Bellard committed
71 72 73 74
static int file_open(URLContext *h, const char *filename, int flags)
{
    int access;
    int fd;
75
    struct stat st;
Fabrice Bellard's avatar
Fabrice Bellard committed
76

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

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

94
    h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
95

Fabrice Bellard's avatar
Fabrice Bellard committed
96 97 98 99
    return 0;
}

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

static int file_close(URLContext *h)
{
113
    int fd = (intptr_t) h->priv_data;
Fabrice Bellard's avatar
Fabrice Bellard committed
114 115 116
    return close(fd);
}

117
URLProtocol ff_file_protocol = {
118 119 120 121 122 123
    .name                = "file",
    .url_open            = file_open,
    .url_read            = file_read,
    .url_write           = file_write,
    .url_seek            = file_seek,
    .url_close           = file_close,
124
    .url_get_file_handle = file_get_handle,
125
    .url_check           = file_check,
Fabrice Bellard's avatar
Fabrice Bellard committed
126 127
};

128 129 130
#endif /* CONFIG_FILE_PROTOCOL */

#if CONFIG_PIPE_PROTOCOL
Fabrice Bellard's avatar
Fabrice Bellard committed
131 132 133 134

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

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

154
URLProtocol ff_pipe_protocol = {
155 156 157 158
    .name                = "pipe",
    .url_open            = pipe_open,
    .url_read            = file_read,
    .url_write           = file_write,
159
    .url_get_file_handle = file_get_handle,
160
    .url_check           = file_check,
Fabrice Bellard's avatar
Fabrice Bellard committed
161
};
162 163

#endif /* CONFIG_PIPE_PROTOCOL */