file.c 10.9 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"
23
#include "libavutil/internal.h"
24
#include "libavutil/opt.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
25
#include "avformat.h"
26
#if HAVE_DIRENT_H
27
#include <dirent.h>
28
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
29
#include <fcntl.h>
30
#if HAVE_IO_H
31 32
#include <io.h>
#endif
33
#if HAVE_UNISTD_H
Fabrice Bellard's avatar
Fabrice Bellard committed
34
#include <unistd.h>
35
#endif
36
#include <sys/stat.h>
37
#include <stdlib.h>
38
#include "os_support.h"
39
#include "url.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
40

41 42 43 44 45 46 47 48
/* 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
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/* Not available in POSIX.1-1996 */
#ifndef S_ISLNK
#  ifdef S_IFLNK
#    define S_ISLNK(m) (((m) & S_IFLNK) == S_IFLNK)
#  else
#    define S_ISLNK(m) 0
#  endif
#endif

/* Not available in POSIX.1-1996 */
#ifndef S_ISSOCK
#  ifdef S_IFSOCK
#    define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
#  else
#    define S_ISSOCK(m) 0
#  endif
#endif

Fabrice Bellard's avatar
Fabrice Bellard committed
68 69
/* standard file protocol */

70
typedef struct FileContext {
71
    const AVClass *class;
72
    int fd;
73
    int trunc;
74
    int blocksize;
75
    int follow;
76
    int seekable;
77
#if HAVE_DIRENT_H
78
    DIR *dir;
79
#endif
80 81
} FileContext;

82
static const AVOption file_options[] = {
83
    { "truncate", "truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
84
    { "blocksize", "set I/O operation maximum block size", offsetof(FileContext, blocksize), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
85
    { "follow", "Follow a file as it is being written", offsetof(FileContext, follow), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
86
    { "seekable", "Sets if the file is seekable", offsetof(FileContext, seekable), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 0, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
87 88 89 90 91
    { NULL }
};

static const AVOption pipe_options[] = {
    { "blocksize", "set I/O operation maximum block size", offsetof(FileContext, blocksize), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
92 93 94 95 96 97 98 99 100 101
    { NULL }
};

static const AVClass file_class = {
    .class_name = "file",
    .item_name  = av_default_item_name,
    .option     = file_options,
    .version    = LIBAVUTIL_VERSION_INT,
};

102 103 104 105 106 107 108
static const AVClass pipe_class = {
    .class_name = "pipe",
    .item_name  = av_default_item_name,
    .option     = pipe_options,
    .version    = LIBAVUTIL_VERSION_INT,
};

109 110
static int file_read(URLContext *h, unsigned char *buf, int size)
{
111
    FileContext *c = h->priv_data;
112
    int ret;
113
    size = FFMIN(size, c->blocksize);
114
    ret = read(c->fd, buf, size);
115 116
    if (ret == 0 && c->follow)
        return AVERROR(EAGAIN);
117 118
    if (ret == 0)
        return AVERROR_EOF;
119
    return (ret == -1) ? AVERROR(errno) : ret;
120 121 122 123
}

static int file_write(URLContext *h, const unsigned char *buf, int size)
{
124
    FileContext *c = h->priv_data;
125
    int ret;
126
    size = FFMIN(size, c->blocksize);
127
    ret = write(c->fd, buf, size);
128
    return (ret == -1) ? AVERROR(errno) : ret;
129 130 131 132
}

static int file_get_handle(URLContext *h)
{
133 134
    FileContext *c = h->priv_data;
    return c->fd;
135 136
}

137 138
static int file_check(URLContext *h, int mask)
{
139
    int ret = 0;
140 141 142 143 144 145
    const char *filename = h->filename;
    av_strstart(filename, "file:", &filename);

    {
#if HAVE_ACCESS && defined(R_OK)
    if (access(filename, F_OK) < 0)
146
        return AVERROR(errno);
147
    if (mask&AVIO_FLAG_READ)
148
        if (access(filename, R_OK) >= 0)
149 150
            ret |= AVIO_FLAG_READ;
    if (mask&AVIO_FLAG_WRITE)
151
        if (access(filename, W_OK) >= 0)
152
            ret |= AVIO_FLAG_WRITE;
153 154
#else
    struct stat st;
155
#   ifndef _WIN32
156
    ret = stat(filename, &st);
157 158 159
#   else
    ret = win32_stat(filename, &st);
#   endif
160 161 162 163 164 165
    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
166
    }
167 168 169
    return ret;
}

170 171 172 173 174 175 176 177
static int file_delete(URLContext *h)
{
#if HAVE_UNISTD_H
    int ret;
    const char *filename = h->filename;
    av_strstart(filename, "file:", &filename);

    ret = rmdir(filename);
178 179 180 181 182
    if (ret < 0 && (errno == ENOTDIR
#   ifdef _WIN32
        || errno == EINVAL
#   endif
        ))
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        ret = unlink(filename);
    if (ret < 0)
        return AVERROR(errno);

    return ret;
#else
    return AVERROR(ENOSYS);
#endif /* HAVE_UNISTD_H */
}

static int file_move(URLContext *h_src, URLContext *h_dst)
{
    const char *filename_src = h_src->filename;
    const char *filename_dst = h_dst->filename;
    av_strstart(filename_src, "file:", &filename_src);
198
    av_strstart(filename_dst, "file:", &filename_dst);
199 200 201 202 203 204 205

    if (rename(filename_src, filename_dst) < 0)
        return AVERROR(errno);

    return 0;
}

206 207
#if CONFIG_FILE_PROTOCOL

Fabrice Bellard's avatar
Fabrice Bellard committed
208 209
static int file_open(URLContext *h, const char *filename, int flags)
{
210
    FileContext *c = h->priv_data;
Fabrice Bellard's avatar
Fabrice Bellard committed
211 212
    int access;
    int fd;
213
    struct stat st;
Fabrice Bellard's avatar
Fabrice Bellard committed
214

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

217
    if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
218 219 220
        access = O_CREAT | O_RDWR;
        if (c->trunc)
            access |= O_TRUNC;
221
    } else if (flags & AVIO_FLAG_WRITE) {
222 223 224
        access = O_CREAT | O_WRONLY;
        if (c->trunc)
            access |= O_TRUNC;
Fabrice Bellard's avatar
Fabrice Bellard committed
225 226 227
    } else {
        access = O_RDONLY;
    }
228
#ifdef O_BINARY
Fabrice Bellard's avatar
Fabrice Bellard committed
229 230
    access |= O_BINARY;
#endif
231
    fd = avpriv_open(filename, access, 0666);
232
    if (fd == -1)
233
        return AVERROR(errno);
234
    c->fd = fd;
235

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

238 239 240 241 242
    /* Buffer writes more than the default 32k to improve throughput especially
     * with networked file systems */
    if (!h->is_streamed && flags & AVIO_FLAG_WRITE)
        h->min_packet_size = h->max_packet_size = 262144;

243 244 245
    if (c->seekable >= 0)
        h->is_streamed = !c->seekable;

Fabrice Bellard's avatar
Fabrice Bellard committed
246 247 248 249
    return 0;
}

/* XXX: use llseek */
250
static int64_t file_seek(URLContext *h, int64_t pos, int whence)
Fabrice Bellard's avatar
Fabrice Bellard committed
251
{
252
    FileContext *c = h->priv_data;
253
    int64_t ret;
254

255 256
    if (whence == AVSEEK_SIZE) {
        struct stat st;
257
        ret = fstat(c->fd, &st);
258
        return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
259
    }
260 261 262 263

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

    return ret < 0 ? AVERROR(errno) : ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
264 265 266 267
}

static int file_close(URLContext *h)
{
268 269
    FileContext *c = h->priv_data;
    return close(c->fd);
Fabrice Bellard's avatar
Fabrice Bellard committed
270 271
}

272 273
static int file_open_dir(URLContext *h)
{
274
#if HAVE_LSTAT
275 276 277 278 279 280 281
    FileContext *c = h->priv_data;

    c->dir = opendir(h->filename);
    if (!c->dir)
        return AVERROR(errno);

    return 0;
282 283
#else
    return AVERROR(ENOSYS);
284
#endif /* HAVE_LSTAT */
285 286 287 288
}

static int file_read_dir(URLContext *h, AVIODirEntry **next)
{
289
#if HAVE_LSTAT
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
    FileContext *c = h->priv_data;
    struct dirent *dir;
    char *fullpath = NULL;

    *next = ff_alloc_dir_entry();
    if (!*next)
        return AVERROR(ENOMEM);
    do {
        errno = 0;
        dir = readdir(c->dir);
        if (!dir) {
            av_freep(next);
            return AVERROR(errno);
        }
    } while (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."));

    fullpath = av_append_path_component(h->filename, dir->d_name);
    if (fullpath) {
        struct stat st;
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
        if (!lstat(fullpath, &st)) {
            if (S_ISDIR(st.st_mode))
                (*next)->type = AVIO_ENTRY_DIRECTORY;
            else if (S_ISFIFO(st.st_mode))
                (*next)->type = AVIO_ENTRY_NAMED_PIPE;
            else if (S_ISCHR(st.st_mode))
                (*next)->type = AVIO_ENTRY_CHARACTER_DEVICE;
            else if (S_ISBLK(st.st_mode))
                (*next)->type = AVIO_ENTRY_BLOCK_DEVICE;
            else if (S_ISLNK(st.st_mode))
                (*next)->type = AVIO_ENTRY_SYMBOLIC_LINK;
            else if (S_ISSOCK(st.st_mode))
                (*next)->type = AVIO_ENTRY_SOCKET;
            else if (S_ISREG(st.st_mode))
                (*next)->type = AVIO_ENTRY_FILE;
            else
                (*next)->type = AVIO_ENTRY_UNKNOWN;

327 328 329 330 331 332 333 334 335 336 337 338 339
            (*next)->group_id = st.st_gid;
            (*next)->user_id = st.st_uid;
            (*next)->size = st.st_size;
            (*next)->filemode = st.st_mode & 0777;
            (*next)->modification_timestamp = INT64_C(1000000) * st.st_mtime;
            (*next)->access_timestamp =  INT64_C(1000000) * st.st_atime;
            (*next)->status_change_timestamp = INT64_C(1000000) * st.st_ctime;
        }
        av_free(fullpath);
    }

    (*next)->name = av_strdup(dir->d_name);
    return 0;
340 341
#else
    return AVERROR(ENOSYS);
342
#endif /* HAVE_LSTAT */
343 344 345 346
}

static int file_close_dir(URLContext *h)
{
347
#if HAVE_LSTAT
348 349 350
    FileContext *c = h->priv_data;
    closedir(c->dir);
    return 0;
351 352
#else
    return AVERROR(ENOSYS);
353
#endif /* HAVE_LSTAT */
354 355
}

356
const URLProtocol ff_file_protocol = {
357 358 359 360 361 362
    .name                = "file",
    .url_open            = file_open,
    .url_read            = file_read,
    .url_write           = file_write,
    .url_seek            = file_seek,
    .url_close           = file_close,
363
    .url_get_file_handle = file_get_handle,
364
    .url_check           = file_check,
365 366
    .url_delete          = file_delete,
    .url_move            = file_move,
367
    .priv_data_size      = sizeof(FileContext),
368
    .priv_data_class     = &file_class,
369 370 371
    .url_open_dir        = file_open_dir,
    .url_read_dir        = file_read_dir,
    .url_close_dir       = file_close_dir,
372
    .default_whitelist   = "file,crypto"
Fabrice Bellard's avatar
Fabrice Bellard committed
373 374
};

375 376 377
#endif /* CONFIG_FILE_PROTOCOL */

#if CONFIG_PIPE_PROTOCOL
Fabrice Bellard's avatar
Fabrice Bellard committed
378 379 380

static int pipe_open(URLContext *h, const char *filename, int flags)
{
381
    FileContext *c = h->priv_data;
Fabrice Bellard's avatar
Fabrice Bellard committed
382
    int fd;
383
    char *final;
384
    av_strstart(filename, "pipe:", &filename);
Fabrice Bellard's avatar
Fabrice Bellard committed
385

386 387
    fd = strtol(filename, &final, 10);
    if((filename == final) || *final ) {/* No digits found, or something like 10ab */
388
        if (flags & AVIO_FLAG_WRITE) {
389 390 391 392
            fd = 1;
        } else {
            fd = 0;
        }
393
    }
394
#if HAVE_SETMODE
395 396
    setmode(fd, O_BINARY);
#endif
397
    c->fd = fd;
398
    h->is_streamed = 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
399 400 401
    return 0;
}

402
const URLProtocol ff_pipe_protocol = {
403 404 405 406
    .name                = "pipe",
    .url_open            = pipe_open,
    .url_read            = file_read,
    .url_write           = file_write,
407
    .url_get_file_handle = file_get_handle,
408
    .url_check           = file_check,
409
    .priv_data_size      = sizeof(FileContext),
410
    .priv_data_class     = &pipe_class,
411
    .default_whitelist   = "crypto"
Fabrice Bellard's avatar
Fabrice Bellard committed
412
};
413 414

#endif /* CONFIG_PIPE_PROTOCOL */