2010-01-30 22:26:47 +00:00
|
|
|
/*
|
2013-07-12 20:11:08 +00:00
|
|
|
* Original authors: Albeu, probably Arpi
|
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
2017-05-11 06:07:09 +00:00
|
|
|
* mpv 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.
|
2010-01-30 22:26:47 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2010-01-30 22:26:47 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-05-11 06:07:09 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2010-01-30 22:26:47 +00:00
|
|
|
*
|
2017-05-11 06:07:09 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2010-01-30 22:26:47 +00:00
|
|
|
*/
|
2003-04-02 16:25:07 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2005-10-13 12:13:49 +00:00
|
|
|
#include <stdio.h>
|
2003-04-02 16:25:07 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2012-08-03 17:33:46 +00:00
|
|
|
#include <errno.h>
|
2003-04-02 16:25:07 +00:00
|
|
|
|
2024-05-01 19:39:19 +00:00
|
|
|
#ifndef _WIN32
|
2015-02-20 18:58:24 +00:00
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
|
|
|
|
2012-02-03 07:05:11 +00:00
|
|
|
#include "osdep/io.h"
|
|
|
|
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 20:21:56 +00:00
|
|
|
#include "common/common.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2018-05-17 18:58:49 +00:00
|
|
|
#include "misc/thread_tools.h"
|
2003-04-02 16:25:07 +00:00
|
|
|
#include "stream.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/m_option.h"
|
2014-01-08 20:46:42 +00:00
|
|
|
#include "options/path.h"
|
2003-04-02 16:25:07 +00:00
|
|
|
|
2014-02-17 11:51:38 +00:00
|
|
|
#if HAVE_BSD_FSTATFS
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#endif
|
|
|
|
|
2014-03-11 19:27:50 +00:00
|
|
|
#if HAVE_LINUX_FSTATFS
|
|
|
|
#include <sys/vfs.h>
|
|
|
|
#endif
|
|
|
|
|
2014-04-09 08:14:18 +00:00
|
|
|
#ifdef _WIN32
|
2014-02-18 07:11:59 +00:00
|
|
|
#include <windows.h>
|
2023-11-24 06:19:19 +00:00
|
|
|
#include <winioctl.h>
|
2014-04-09 08:14:18 +00:00
|
|
|
#include <winternl.h>
|
|
|
|
#include <io.h>
|
|
|
|
|
2024-05-01 22:44:26 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
// Those are defined only in Windows DDK
|
|
|
|
typedef struct _FILE_FS_DEVICE_INFORMATION {
|
|
|
|
DEVICE_TYPE DeviceType;
|
|
|
|
ULONG Characteristics;
|
|
|
|
} FILE_FS_DEVICE_INFORMATION, *PFILE_FS_DEVICE_INFORMATION;
|
|
|
|
|
|
|
|
typedef enum _FSINFOCLASS {
|
|
|
|
FileFsVolumeInformation = 1,
|
|
|
|
FileFsLabelInformation, // 2
|
|
|
|
FileFsSizeInformation, // 3
|
|
|
|
FileFsDeviceInformation, // 4
|
|
|
|
FileFsAttributeInformation, // 5
|
|
|
|
FileFsControlInformation, // 6
|
|
|
|
FileFsFullSizeInformation, // 7
|
|
|
|
FileFsObjectIdInformation, // 8
|
|
|
|
FileFsDriverPathInformation, // 9
|
|
|
|
FileFsVolumeFlagsInformation, // 10
|
|
|
|
FileFsSectorSizeInformation, // 11
|
|
|
|
FileFsDataCopyInformation, // 12
|
|
|
|
FileFsMetadataSizeInformation, // 13
|
|
|
|
FileFsFullSizeInformationEx, // 14
|
|
|
|
FileFsGuidInformation, // 15
|
|
|
|
FileFsMaximumInformation
|
|
|
|
} FS_INFORMATION_CLASS, *PFS_INFORMATION_CLASS;
|
|
|
|
#endif
|
|
|
|
|
2014-04-09 08:14:18 +00:00
|
|
|
#ifndef FILE_REMOTE_DEVICE
|
|
|
|
#define FILE_REMOTE_DEVICE (0x10)
|
|
|
|
#endif
|
2014-02-18 07:11:59 +00:00
|
|
|
#endif
|
|
|
|
|
stream: remove fd member
Stream implementations could set this to a unix file descriptor. The
generic stream code could use it as fallback for a few things. This
was confusing and insane. In most cases, the stream implementations
defined all callbacks, so setting the fd member didn't have any
advantages, other than avoiding defining a private struct to store it.
It appears that even if the stream implementation used close() on the
fd (or something equivalent), stream.c would close() it a second time
(and on windows, even would call closesocket()), which should be proof
for the insanity of this code.
For stream_file.c, additionally make sure we don't close stdin or
stdout if "-" is used as filename.
For stream_vcd.c, remove the control() code. This code most likely
didn't make the slightest sense, because it used a different type
for stream->priv. It also leaked memory. Maybe it worked, but it's
incorrect and insignificant anyway, so kill it. This code was added
with commit 9521c19 (svn commit 31019).
Untested for all protocols other than stream_file.c.
2013-07-12 20:07:07 +00:00
|
|
|
struct priv {
|
|
|
|
int fd;
|
|
|
|
bool close;
|
2016-10-14 23:10:17 +00:00
|
|
|
bool use_poll;
|
2018-02-21 16:02:18 +00:00
|
|
|
bool regular_file;
|
|
|
|
bool appending;
|
|
|
|
int64_t orig_size;
|
2018-05-17 19:45:17 +00:00
|
|
|
struct mp_cancel *cancel;
|
stream: remove fd member
Stream implementations could set this to a unix file descriptor. The
generic stream code could use it as fallback for a few things. This
was confusing and insane. In most cases, the stream implementations
defined all callbacks, so setting the fd member didn't have any
advantages, other than avoiding defining a private struct to store it.
It appears that even if the stream implementation used close() on the
fd (or something equivalent), stream.c would close() it a second time
(and on windows, even would call closesocket()), which should be proof
for the insanity of this code.
For stream_file.c, additionally make sure we don't close stdin or
stdout if "-" is used as filename.
For stream_vcd.c, remove the control() code. This code most likely
didn't make the slightest sense, because it used a different type
for stream->priv. It also leaked memory. Maybe it worked, but it's
incorrect and insignificant anyway, so kill it. This code was added
with commit 9521c19 (svn commit 31019).
Untested for all protocols other than stream_file.c.
2013-07-12 20:07:07 +00:00
|
|
|
};
|
|
|
|
|
2018-02-21 16:02:18 +00:00
|
|
|
// Total timeout = RETRY_TIMEOUT * MAX_RETRIES
|
|
|
|
#define RETRY_TIMEOUT 0.2
|
|
|
|
#define MAX_RETRIES 10
|
|
|
|
|
|
|
|
static int64_t get_size(stream_t *s)
|
|
|
|
{
|
|
|
|
struct priv *p = s->priv;
|
2020-02-16 22:51:21 +00:00
|
|
|
struct stat st;
|
|
|
|
if (fstat(p->fd, &st) == 0) {
|
|
|
|
if (st.st_size <= 0 && !s->seekable)
|
|
|
|
st.st_size = -1;
|
|
|
|
if (st.st_size >= 0)
|
|
|
|
return st.st_size;
|
2020-02-14 15:07:13 +00:00
|
|
|
}
|
2020-02-16 22:51:21 +00:00
|
|
|
return -1;
|
2018-02-21 16:02:18 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 14:28:50 +00:00
|
|
|
static int fill_buffer(stream_t *s, void *buffer, int max_len)
|
2013-08-22 16:08:46 +00:00
|
|
|
{
|
|
|
|
struct priv *p = s->priv;
|
2018-02-21 16:02:18 +00:00
|
|
|
|
2024-05-01 19:39:19 +00:00
|
|
|
#ifndef _WIN32
|
2016-10-14 23:10:17 +00:00
|
|
|
if (p->use_poll) {
|
2018-05-17 19:45:17 +00:00
|
|
|
int c = mp_cancel_get_fd(p->cancel);
|
2015-02-20 18:58:24 +00:00
|
|
|
struct pollfd fds[2] = {
|
|
|
|
{.fd = p->fd, .events = POLLIN},
|
|
|
|
{.fd = c, .events = POLLIN},
|
|
|
|
};
|
|
|
|
poll(fds, c >= 0 ? 2 : 1, -1);
|
|
|
|
if (fds[1].revents & POLLIN)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2018-02-21 16:02:18 +00:00
|
|
|
|
|
|
|
for (int retries = 0; retries < MAX_RETRIES; retries++) {
|
|
|
|
int r = read(p->fd, buffer, max_len);
|
|
|
|
if (r > 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
// Try to detect and handle files being appended during playback.
|
|
|
|
int64_t size = get_size(s);
|
|
|
|
if (p->regular_file && size > p->orig_size && !p->appending) {
|
|
|
|
MP_WARN(s, "File is apparently being appended to, will keep "
|
|
|
|
"retrying with timeouts.\n");
|
|
|
|
p->appending = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!p->appending || p->use_poll)
|
|
|
|
break;
|
|
|
|
|
2018-05-17 19:45:17 +00:00
|
|
|
if (mp_cancel_wait(p->cancel, RETRY_TIMEOUT))
|
2018-02-21 16:02:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2003-04-02 16:25:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 14:28:50 +00:00
|
|
|
static int write_buffer(stream_t *s, void *buffer, int len)
|
2013-08-22 16:08:46 +00:00
|
|
|
{
|
|
|
|
struct priv *p = s->priv;
|
2019-09-14 11:00:10 +00:00
|
|
|
return write(p->fd, buffer, len);
|
2003-04-02 16:25:07 +00:00
|
|
|
}
|
|
|
|
|
2013-08-22 16:08:46 +00:00
|
|
|
static int seek(stream_t *s, int64_t newpos)
|
|
|
|
{
|
|
|
|
struct priv *p = s->priv;
|
2013-08-22 16:23:33 +00:00
|
|
|
return lseek(p->fd, newpos, SEEK_SET) != (off_t)-1;
|
2003-04-02 16:25:07 +00:00
|
|
|
}
|
|
|
|
|
stream: remove fd member
Stream implementations could set this to a unix file descriptor. The
generic stream code could use it as fallback for a few things. This
was confusing and insane. In most cases, the stream implementations
defined all callbacks, so setting the fd member didn't have any
advantages, other than avoiding defining a private struct to store it.
It appears that even if the stream implementation used close() on the
fd (or something equivalent), stream.c would close() it a second time
(and on windows, even would call closesocket()), which should be proof
for the insanity of this code.
For stream_file.c, additionally make sure we don't close stdin or
stdout if "-" is used as filename.
For stream_vcd.c, remove the control() code. This code most likely
didn't make the slightest sense, because it used a different type
for stream->priv. It also leaked memory. Maybe it worked, but it's
incorrect and insignificant anyway, so kill it. This code was added
with commit 9521c19 (svn commit 31019).
Untested for all protocols other than stream_file.c.
2013-07-12 20:07:07 +00:00
|
|
|
static void s_close(stream_t *s)
|
|
|
|
{
|
2013-08-22 16:08:46 +00:00
|
|
|
struct priv *p = s->priv;
|
2017-05-11 06:07:09 +00:00
|
|
|
if (p->close)
|
2013-08-22 16:08:46 +00:00
|
|
|
close(p->fd);
|
stream: remove fd member
Stream implementations could set this to a unix file descriptor. The
generic stream code could use it as fallback for a few things. This
was confusing and insane. In most cases, the stream implementations
defined all callbacks, so setting the fd member didn't have any
advantages, other than avoiding defining a private struct to store it.
It appears that even if the stream implementation used close() on the
fd (or something equivalent), stream.c would close() it a second time
(and on windows, even would call closesocket()), which should be proof
for the insanity of this code.
For stream_file.c, additionally make sure we don't close stdin or
stdout if "-" is used as filename.
For stream_vcd.c, remove the control() code. This code most likely
didn't make the slightest sense, because it used a different type
for stream->priv. It also leaked memory. Maybe it worked, but it's
incorrect and insignificant anyway, so kill it. This code was added
with commit 9521c19 (svn commit 31019).
Untested for all protocols other than stream_file.c.
2013-07-12 20:07:07 +00:00
|
|
|
}
|
|
|
|
|
2014-01-08 20:46:42 +00:00
|
|
|
// If url is a file:// URL, return the local filename, otherwise return NULL.
|
|
|
|
char *mp_file_url_to_filename(void *talloc_ctx, bstr url)
|
|
|
|
{
|
|
|
|
bstr proto = mp_split_proto(url, &url);
|
|
|
|
if (bstrcasecmp0(proto, "file") != 0)
|
|
|
|
return NULL;
|
|
|
|
char *filename = bstrto0(talloc_ctx, url);
|
|
|
|
mp_url_unescape_inplace(filename);
|
|
|
|
#if HAVE_DOS_PATHS
|
|
|
|
// extract '/' from '/x:/path'
|
|
|
|
if (filename[0] == '/' && filename[1] && filename[2] == ':')
|
|
|
|
memmove(filename, filename + 1, strlen(filename)); // including \0
|
|
|
|
#endif
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2014-09-25 21:38:23 +00:00
|
|
|
// Return talloc_strdup's filesystem path if local, otherwise NULL.
|
|
|
|
// Unlike mp_file_url_to_filename(), doesn't return NULL if already local.
|
|
|
|
char *mp_file_get_path(void *talloc_ctx, bstr url)
|
|
|
|
{
|
|
|
|
if (mp_split_proto(url, &(bstr){0}).len) {
|
|
|
|
return mp_file_url_to_filename(talloc_ctx, url);
|
|
|
|
} else {
|
|
|
|
return bstrto0(talloc_ctx, url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 11:51:38 +00:00
|
|
|
#if HAVE_BSD_FSTATFS
|
2015-04-17 21:04:11 +00:00
|
|
|
static bool check_stream_network(int fd)
|
2014-02-17 11:51:38 +00:00
|
|
|
{
|
|
|
|
struct statfs fs;
|
2018-03-15 05:59:45 +00:00
|
|
|
const char *stypes[] = { "afpfs", "nfs", "smbfs", "webdav", "osxfusefs",
|
2021-04-24 10:57:06 +00:00
|
|
|
"fuse", "fusefs.sshfs", "macfuse", NULL };
|
2015-04-17 21:04:11 +00:00
|
|
|
if (fstatfs(fd, &fs) == 0)
|
2014-02-17 11:51:38 +00:00
|
|
|
for (int i=0; stypes[i]; i++)
|
|
|
|
if (strcmp(stypes[i], fs.f_fstypename) == 0)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
|
2014-03-11 19:27:50 +00:00
|
|
|
}
|
|
|
|
#elif HAVE_LINUX_FSTATFS
|
2015-04-17 21:04:11 +00:00
|
|
|
static bool check_stream_network(int fd)
|
2014-03-11 19:27:50 +00:00
|
|
|
{
|
|
|
|
struct statfs fs;
|
|
|
|
const uint32_t stypes[] = {
|
|
|
|
0x5346414F /*AFS*/, 0x61756673 /*AUFS*/, 0x00C36400 /*CEPH*/,
|
|
|
|
0xFF534D42 /*CIFS*/, 0x73757245 /*CODA*/, 0x19830326 /*FHGFS*/,
|
|
|
|
0x65735546 /*FUSEBLK*/,0x65735543 /*FUSECTL*/,0x1161970 /*GFS*/,
|
|
|
|
0x47504653 /*GPFS*/, 0x6B414653 /*KAFS*/, 0x0BD00BD0 /*LUSTRE*/,
|
|
|
|
0x564C /*NCP*/, 0x6969 /*NFS*/, 0x6E667364 /*NFSD*/,
|
|
|
|
0xAAD7AAEA /*PANFS*/, 0x50495045 /*PIPEFS*/, 0x517B /*SMB*/,
|
|
|
|
0xBEEFDEAD /*SNFS*/, 0xBACBACBC /*VMHGFS*/, 0x7461636f /*OCFS2*/,
|
2018-03-15 05:47:24 +00:00
|
|
|
0xFE534D42 /*SMB2*/, 0x61636673 /*ACFS*/, 0x013111A8 /*IBRIX*/,
|
2014-03-11 19:27:50 +00:00
|
|
|
0
|
|
|
|
};
|
2015-04-17 21:04:11 +00:00
|
|
|
if (fstatfs(fd, &fs) == 0) {
|
2014-03-11 19:27:50 +00:00
|
|
|
for (int i=0; stypes[i]; i++) {
|
|
|
|
if (stypes[i] == fs.f_type)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
2014-02-17 11:51:38 +00:00
|
|
|
}
|
2014-04-09 08:14:18 +00:00
|
|
|
#elif defined(_WIN32)
|
2015-04-17 21:04:11 +00:00
|
|
|
static bool check_stream_network(int fd)
|
2014-02-18 07:11:59 +00:00
|
|
|
{
|
2014-04-09 08:14:18 +00:00
|
|
|
NTSTATUS (NTAPI *pNtQueryVolumeInformationFile)(HANDLE,
|
|
|
|
PIO_STATUS_BLOCK, PVOID, ULONG, FS_INFORMATION_CLASS) = NULL;
|
2014-02-18 07:11:59 +00:00
|
|
|
|
2014-04-09 08:14:18 +00:00
|
|
|
// NtQueryVolumeInformationFile is an internal Windows function. It has
|
|
|
|
// been present since Windows XP, however this code should fail gracefully
|
|
|
|
// if it's removed from a future version of Windows.
|
2015-08-01 18:51:52 +00:00
|
|
|
HMODULE ntdll = GetModuleHandleW(L"ntdll.dll");
|
2014-04-09 08:14:18 +00:00
|
|
|
pNtQueryVolumeInformationFile = (NTSTATUS (NTAPI*)(HANDLE,
|
|
|
|
PIO_STATUS_BLOCK, PVOID, ULONG, FS_INFORMATION_CLASS))
|
|
|
|
GetProcAddress(ntdll, "NtQueryVolumeInformationFile");
|
2014-02-18 07:11:59 +00:00
|
|
|
|
2014-04-09 08:14:18 +00:00
|
|
|
if (!pNtQueryVolumeInformationFile)
|
|
|
|
return false;
|
|
|
|
|
2015-04-17 21:04:11 +00:00
|
|
|
HANDLE h = (HANDLE)_get_osfhandle(fd);
|
2014-04-09 08:14:18 +00:00
|
|
|
if (h == INVALID_HANDLE_VALUE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
FILE_FS_DEVICE_INFORMATION info = { 0 };
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
NTSTATUS status = pNtQueryVolumeInformationFile(h, &io, &info,
|
|
|
|
sizeof(info), FileFsDeviceInformation);
|
|
|
|
if (!NT_SUCCESS(status))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return info.DeviceType == FILE_DEVICE_NETWORK_FILE_SYSTEM ||
|
|
|
|
(info.Characteristics & FILE_REMOTE_DEVICE);
|
2014-02-18 07:11:59 +00:00
|
|
|
}
|
2014-02-17 11:51:38 +00:00
|
|
|
#else
|
2015-04-17 21:04:11 +00:00
|
|
|
static bool check_stream_network(int fd)
|
2014-02-17 11:51:38 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-04-05 21:00:12 +00:00
|
|
|
static int open_f(stream_t *stream, const struct stream_open_args *args)
|
2013-07-11 19:10:42 +00:00
|
|
|
{
|
2015-07-10 10:45:49 +00:00
|
|
|
struct priv *p = talloc_ptrtype(stream, p);
|
|
|
|
*p = (struct priv) {
|
2020-02-14 15:07:13 +00:00
|
|
|
.fd = -1,
|
2013-08-22 16:08:46 +00:00
|
|
|
};
|
2015-07-10 10:45:49 +00:00
|
|
|
stream->priv = p;
|
2017-02-02 17:24:27 +00:00
|
|
|
stream->is_local_file = true;
|
2013-08-22 16:08:46 +00:00
|
|
|
|
2020-05-10 14:44:35 +00:00
|
|
|
bool strict_fs = args->flags & STREAM_LOCAL_FS_ONLY;
|
2014-05-24 12:06:13 +00:00
|
|
|
bool write = stream->mode == STREAM_WRITE;
|
|
|
|
int m = O_CLOEXEC | (write ? O_RDWR | O_CREAT | O_TRUNC : O_RDONLY);
|
2003-04-02 16:25:07 +00:00
|
|
|
|
2020-05-10 14:44:35 +00:00
|
|
|
char *filename = stream->path;
|
|
|
|
char *url = "";
|
|
|
|
if (!strict_fs) {
|
|
|
|
char *fn = mp_file_url_to_filename(stream, bstr0(stream->url));
|
|
|
|
if (fn)
|
|
|
|
filename = stream->path = fn;
|
|
|
|
url = stream->url;
|
2014-01-08 20:46:42 +00:00
|
|
|
}
|
2008-02-20 08:20:49 +00:00
|
|
|
|
2020-05-10 14:44:35 +00:00
|
|
|
bool is_fdclose = strncmp(url, "fdclose://", 10) == 0;
|
|
|
|
if (strncmp(url, "fd://", 5) == 0 || is_fdclose) {
|
2017-06-16 20:40:42 +00:00
|
|
|
char *begin = strstr(stream->url, "://") + 3, *end = NULL;
|
|
|
|
p->fd = strtol(begin, &end, 0);
|
2024-05-13 19:55:51 +00:00
|
|
|
if (!end || end == begin || end[0] || p->fd < 0) {
|
|
|
|
MP_ERR(stream, "Invalid FD number: %s\n", stream->url);
|
2015-07-09 13:51:31 +00:00
|
|
|
return STREAM_ERROR;
|
|
|
|
}
|
2024-05-13 19:55:51 +00:00
|
|
|
#ifdef F_SETFD
|
|
|
|
if (fcntl(p->fd, F_GETFD) == -1) {
|
|
|
|
MP_ERR(stream, "Invalid FD: %d\n", p->fd);
|
|
|
|
return STREAM_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
2017-06-16 20:40:42 +00:00
|
|
|
if (is_fdclose)
|
|
|
|
p->close = true;
|
2020-05-10 14:44:35 +00:00
|
|
|
} else if (!strict_fs && !strcmp(filename, "-")) {
|
2014-05-24 12:06:13 +00:00
|
|
|
if (!write) {
|
2013-12-21 19:36:45 +00:00
|
|
|
MP_INFO(stream, "Reading from stdin...\n");
|
2015-07-10 10:47:53 +00:00
|
|
|
p->fd = 0;
|
2013-08-22 16:08:46 +00:00
|
|
|
} else {
|
2014-05-24 12:06:18 +00:00
|
|
|
MP_INFO(stream, "Writing to stdout...\n");
|
2015-07-10 10:47:53 +00:00
|
|
|
p->fd = 1;
|
2013-08-22 16:08:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-02-21 16:02:18 +00:00
|
|
|
if (bstr_startswith0(bstr0(stream->url), "appending://"))
|
|
|
|
p->appending = true;
|
|
|
|
|
2013-08-22 16:08:46 +00:00
|
|
|
mode_t openmode = S_IRUSR | S_IWUSR;
|
2006-12-21 22:40:51 +00:00
|
|
|
#ifndef __MINGW32__
|
2013-08-22 16:08:46 +00:00
|
|
|
openmode |= S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
|
2015-02-20 18:58:24 +00:00
|
|
|
if (!write)
|
|
|
|
m |= O_NONBLOCK;
|
2006-12-21 22:40:51 +00:00
|
|
|
#endif
|
2015-07-10 10:47:53 +00:00
|
|
|
p->fd = open(filename, m | O_BINARY, openmode);
|
|
|
|
if (p->fd < 0) {
|
2013-12-21 19:36:45 +00:00
|
|
|
MP_ERR(stream, "Cannot open file '%s': %s\n",
|
2015-07-10 10:47:53 +00:00
|
|
|
filename, mp_strerror(errno));
|
2013-08-22 16:08:46 +00:00
|
|
|
return STREAM_ERROR;
|
|
|
|
}
|
2018-05-17 10:40:33 +00:00
|
|
|
p->close = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat st;
|
2024-03-13 16:05:06 +00:00
|
|
|
bool is_sock_or_fifo = false;
|
2018-05-17 10:40:33 +00:00
|
|
|
if (fstat(p->fd, &st) == 0) {
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
stream->is_directory = true;
|
|
|
|
} else if (S_ISREG(st.st_mode)) {
|
|
|
|
p->regular_file = true;
|
2024-05-01 19:39:19 +00:00
|
|
|
#ifndef _WIN32
|
2018-05-17 10:40:33 +00:00
|
|
|
// O_NONBLOCK has weird semantics on file locks; remove it.
|
|
|
|
int val = fcntl(p->fd, F_GETFL) & ~(unsigned)O_NONBLOCK;
|
|
|
|
fcntl(p->fd, F_SETFL, val);
|
2015-02-20 18:58:24 +00:00
|
|
|
#endif
|
2018-05-17 10:40:33 +00:00
|
|
|
} else {
|
2024-03-13 16:05:06 +00:00
|
|
|
#ifndef __MINGW32__
|
|
|
|
is_sock_or_fifo = S_ISSOCK(st.st_mode) || S_ISFIFO(st.st_mode);
|
|
|
|
#endif
|
2018-05-17 10:40:33 +00:00
|
|
|
p->use_poll = true;
|
2013-08-22 16:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
2003-04-02 16:25:07 +00:00
|
|
|
|
2024-05-01 19:39:19 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-06 12:08:13 +00:00
|
|
|
setmode(p->fd, O_BINARY);
|
|
|
|
#endif
|
|
|
|
|
2015-07-10 10:47:53 +00:00
|
|
|
off_t len = lseek(p->fd, 0, SEEK_END);
|
|
|
|
lseek(p->fd, 0, SEEK_SET);
|
2014-05-24 12:06:18 +00:00
|
|
|
if (len != (off_t)-1) {
|
2013-08-22 16:08:46 +00:00
|
|
|
stream->seek = seek;
|
2014-05-24 12:04:09 +00:00
|
|
|
stream->seekable = true;
|
2013-08-22 16:08:46 +00:00
|
|
|
}
|
|
|
|
|
2014-05-24 12:06:18 +00:00
|
|
|
stream->fast_skip = true;
|
2013-08-22 16:08:46 +00:00
|
|
|
stream->fill_buffer = fill_buffer;
|
|
|
|
stream->write_buffer = write_buffer;
|
2019-11-07 14:54:34 +00:00
|
|
|
stream->get_size = get_size;
|
2013-08-22 16:08:46 +00:00
|
|
|
stream->close = s_close;
|
|
|
|
|
2024-03-13 16:05:06 +00:00
|
|
|
if (is_sock_or_fifo || check_stream_network(p->fd)) {
|
2014-02-17 11:51:38 +00:00
|
|
|
stream->streaming = true;
|
2020-11-14 13:33:17 +00:00
|
|
|
#if HAVE_COCOA
|
|
|
|
if (fcntl(p->fd, F_RDAHEAD, 0) < 0) {
|
|
|
|
MP_VERBOSE(stream, "Cannot disable read ahead on file '%s': %s\n",
|
|
|
|
filename, mp_strerror(errno));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2014-02-17 11:51:38 +00:00
|
|
|
|
2018-02-21 16:02:18 +00:00
|
|
|
p->orig_size = get_size(stream);
|
|
|
|
|
2018-05-17 19:45:17 +00:00
|
|
|
p->cancel = mp_cancel_new(p);
|
|
|
|
if (stream->cancel)
|
2018-05-19 15:06:00 +00:00
|
|
|
mp_cancel_set_parent(p->cancel, stream->cancel);
|
2018-05-17 19:45:17 +00:00
|
|
|
|
2013-08-22 16:08:46 +00:00
|
|
|
return STREAM_OK;
|
2003-04-02 16:25:07 +00:00
|
|
|
}
|
|
|
|
|
2007-12-02 13:22:53 +00:00
|
|
|
const stream_info_t stream_info_file = {
|
stream: fix url_options field, make protocols field not fixed length
The way the url_options field was handled was not entirely sane: it's
actually a flexible array member, so it points to garbage for streams
which do not initialize this member (it just points to the data right
after the struct, which is garbage in theory and practice). This was
not actually a problem, since the field is only used if priv_size is
set (due to how this stuff is used). But it doesn't allow setting
priv_size only, which might be useful in some cases.
Also, make the protocols array not a fixed size array. Most stream
implementations have only 1 protocol prefix, but stream_lavf.c has
over 10 (whitelists ffmpeg protocols). The high size of the fixed
size protocol array wastes space, and it is _still_ annoying to
add new prefixes to stream_lavf (have to bump the maximum length),
so make it arbitrary length.
The two changes (plus some more cosmetic changes) arte conflated into
one, because it was annoying going over all the stream implementations.
2013-08-25 20:49:27 +00:00
|
|
|
.name = "file",
|
2020-05-10 14:44:35 +00:00
|
|
|
.open2 = open_f,
|
2020-03-08 17:43:31 +00:00
|
|
|
.protocols = (const char*const[]){ "file", "", "appending", NULL },
|
2014-05-24 12:06:13 +00:00
|
|
|
.can_write = true,
|
2020-05-10 14:44:35 +00:00
|
|
|
.local_fs = true,
|
stream, demux: redo origin policy thing
mpv has a very weak and very annoying policy that determines whether a
playlist should be used or not. For example, if you play a remote
playlist, you usually don't want it to be able to read local filesystem
entries. (Although for a media player the impact is small I guess.)
It's weak and annoying as in that it does not prevent certain cases
which could be interpreted as bad in some cases, such as allowing
playlists on the local filesystem to reference remote URLs. It probably
barely makes sense, but we just want to exclude some other "definitely
not a good idea" things, all while playlists generally just work, so
whatever.
The policy is:
- from the command line anything is played
- local playlists can reference anything except "unsafe" streams
("unsafe" means special stream inputs like libavfilter graphs)
- remote playlists can reference only remote URLs
- things like "memory://" and archives are "transparent" to this
This commit does... something. It replaces the weird stream flags with a
slightly clearer "origin" value, which is now consequently passed down
and used everywhere. It fixes some deviations from the described policy.
I wanted to force archives to reference only content within them, but
this would probably have been more complicated (or required different
abstractions), and I'm too lazy to figure it out, so archives are now
"transparent" (playlists within archives behave the same outside).
There may be a lot of bugs in this.
This is unfortunately a very noisy commit because:
- every stream open call now needs to pass the origin
- so does every demuxer open call (=> params param. gets mandatory)
- most stream were changed to provide the "origin" value
- the origin value needed to be passed along in a lot of places
- I was too lazy to split the commit
Fixes: #7274
2019-12-20 08:41:42 +00:00
|
|
|
.stream_origin = STREAM_ORIGIN_FS,
|
2003-04-02 16:25:07 +00:00
|
|
|
};
|
2020-03-08 17:43:31 +00:00
|
|
|
|
|
|
|
const stream_info_t stream_info_fd = {
|
|
|
|
.name = "fd",
|
2020-05-10 14:44:35 +00:00
|
|
|
.open2 = open_f,
|
2020-03-08 17:43:31 +00:00
|
|
|
.protocols = (const char*const[]){ "fd", "fdclose", NULL },
|
|
|
|
.can_write = true,
|
|
|
|
.stream_origin = STREAM_ORIGIN_UNSAFE,
|
|
|
|
};
|