2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
gbx_stream_direct.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __STREAM_IMPL_C
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gbx_value.h"
|
|
|
|
#include "gb_limit.h"
|
2011-09-18 17:21:11 +02:00
|
|
|
#include "gbx_number.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "gbx_stream.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define FD (stream->direct.fd)
|
|
|
|
|
|
|
|
|
|
|
|
static int stream_open(STREAM *stream, const char *path, int mode)
|
|
|
|
{
|
2011-09-18 17:21:11 +02:00
|
|
|
int fd;
|
|
|
|
struct stat info;
|
2011-09-18 17:38:43 +02:00
|
|
|
int fmode, omode;
|
2011-09-18 17:36:30 +02:00
|
|
|
VALUE val;
|
|
|
|
|
2011-09-18 17:21:11 +02:00
|
|
|
if (mode & ST_CREATE)
|
2017-12-18 13:02:03 +01:00
|
|
|
fmode = O_CREAT | O_TRUNC; // | O_EXCL;
|
2011-09-18 17:21:11 +02:00
|
|
|
else if (mode & ST_APPEND)
|
|
|
|
fmode = O_APPEND | O_CREAT;
|
|
|
|
else
|
|
|
|
fmode = 0;
|
|
|
|
|
|
|
|
switch (mode & ST_MODE)
|
2008-03-11 14:29:47 +01:00
|
|
|
{
|
2011-09-18 17:21:11 +02:00
|
|
|
case ST_READ: fmode |= O_RDONLY; break;
|
|
|
|
case ST_WRITE: fmode |= O_WRONLY; break;
|
|
|
|
case ST_READ_WRITE: fmode |= O_RDWR; break;
|
|
|
|
default: fmode |= O_RDONLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path[0] == '.' && isdigit(path[1]))
|
|
|
|
{
|
2011-09-18 17:36:30 +02:00
|
|
|
if ((mode & ST_CREATE) || (mode & ST_APPEND))
|
|
|
|
THROW(E_ACCESS);
|
|
|
|
|
2011-09-18 17:21:11 +02:00
|
|
|
if (NUMBER_from_string(NB_READ_INTEGER, &path[1], strlen(path) - 1, &val) || val._integer.value < 0)
|
|
|
|
{
|
|
|
|
errno = ENOENT;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = val._integer.value;
|
2011-09-18 17:38:43 +02:00
|
|
|
omode = fcntl(fd, F_GETFL, NULL);
|
|
|
|
if (omode < 0)
|
|
|
|
return TRUE;
|
2011-09-18 17:36:30 +02:00
|
|
|
|
2011-09-28 02:57:05 +02:00
|
|
|
if (((mode & ST_MODE) == ST_READ && (omode & O_ACCMODE) == O_WRONLY)
|
|
|
|
|| ((mode & ST_MODE) == ST_WRITE && (omode & O_ACCMODE) == O_RDONLY)
|
|
|
|
|| ((mode & ST_MODE) == ST_READ_WRITE && (omode & O_ACCMODE) != O_RDWR))
|
2011-09-18 17:36:30 +02:00
|
|
|
THROW(E_ACCESS);
|
|
|
|
|
2011-09-18 17:21:11 +02:00
|
|
|
stream->direct.watch = TRUE;
|
2015-03-20 07:48:04 +01:00
|
|
|
stream->common.no_read_ahead = TRUE;
|
2008-03-11 14:29:47 +01:00
|
|
|
}
|
2008-11-22 13:58:36 +01:00
|
|
|
else
|
2011-09-18 17:21:11 +02:00
|
|
|
{
|
|
|
|
stream->direct.watch = FALSE;
|
|
|
|
|
|
|
|
fd = open(path, fmode, 0666);
|
|
|
|
if (fd < 0)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (fstat(fd, &info) < 0)
|
2013-01-05 00:24:37 +01:00
|
|
|
{
|
|
|
|
close(fd);
|
2011-09-18 17:21:11 +02:00
|
|
|
return TRUE;
|
2013-01-05 00:24:37 +01:00
|
|
|
}
|
2011-09-18 17:21:11 +02:00
|
|
|
|
|
|
|
if (S_ISDIR(info.st_mode))
|
|
|
|
{
|
2013-01-05 00:24:37 +01:00
|
|
|
close(fd);
|
2011-09-18 17:21:11 +02:00
|
|
|
errno = EISDIR;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISREG(info.st_mode))
|
|
|
|
{
|
|
|
|
stream->common.available_now = FALSE;
|
2015-03-20 00:23:47 +01:00
|
|
|
stream->common.no_read_ahead = TRUE;
|
2011-09-18 17:21:11 +02:00
|
|
|
fcntl(fd, F_SETFL, O_NONBLOCK);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
stream->common.available_now = TRUE;
|
|
|
|
}
|
2008-03-11 14:29:47 +01:00
|
|
|
|
2011-09-18 17:21:11 +02:00
|
|
|
FD = fd;
|
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int stream_close(STREAM *stream)
|
|
|
|
{
|
2011-09-18 17:21:11 +02:00
|
|
|
if (!stream->direct.watch)
|
|
|
|
{
|
|
|
|
if (close(FD) < 0)
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-18 17:21:11 +02:00
|
|
|
FD = -1;
|
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static int stream_read(STREAM *stream, char *buffer, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2017-09-27 02:48:00 +02:00
|
|
|
return read(FD, buffer, len);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static int stream_write(STREAM *stream, char *buffer, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2017-09-27 02:48:00 +02:00
|
|
|
return write(FD, buffer, len);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static int stream_seek(STREAM *stream, int64_t pos, int whence)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-18 17:21:11 +02:00
|
|
|
return (lseek(FD, pos, whence) < 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static int stream_tell(STREAM *stream, int64_t *pos)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-18 17:21:11 +02:00
|
|
|
*pos = lseek(FD, 0, SEEK_CUR);
|
|
|
|
return (*pos < 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int stream_flush(STREAM *stream)
|
|
|
|
{
|
2011-09-18 17:21:11 +02:00
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define stream_eof NULL
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static int stream_lof(STREAM *stream, int64_t *len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-11-02 03:40:10 +01:00
|
|
|
struct stat info;
|
|
|
|
|
2008-11-22 13:58:36 +01:00
|
|
|
if (!stream->common.available_now)
|
2008-03-11 14:29:47 +01:00
|
|
|
return TRUE;
|
|
|
|
|
2008-11-02 03:40:10 +01:00
|
|
|
if (fstat(FD, &info) < 0)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
*len = info.st_size;
|
2011-09-18 17:21:11 +02:00
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2011-09-18 17:21:11 +02:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
static int stream_handle(STREAM *stream)
|
|
|
|
{
|
2011-09-18 17:21:11 +02:00
|
|
|
return FD;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DECLARE_STREAM(STREAM_direct);
|
|
|
|
|