version 1.1, 2013/10/14 10:32:48
|
version 1.1.1.2, 2016/11/02 10:35:00
|
Line 1
|
Line 1
|
|
#include "first.h" |
|
|
#include "stream.h" |
#include "stream.h" |
|
|
#include <sys/types.h> |
#include <sys/types.h> |
Line 12
|
Line 14
|
# define O_BINARY 0 |
# define O_BINARY 0 |
#endif |
#endif |
|
|
int stream_open(stream *f, buffer *fn) { | /* don't want to block when open()ing a fifo */ |
| #if defined(O_NONBLOCK) |
| # define FIFO_NONBLOCK O_NONBLOCK |
| #else |
| # define FIFO_NONBLOCK 0 |
| #endif |
| |
| int stream_open(stream *f, const buffer *fn) { |
| |
| #if !defined(__WIN32) |
| |
struct stat st; |
struct stat st; |
#ifdef HAVE_MMAP |
|
int fd; |
int fd; |
#elif defined __WIN32 |
|
HANDLE *fh, *mh; |
|
void *p; |
|
#endif |
|
|
|
f->start = NULL; |
f->start = NULL; |
|
f->size = 0; |
|
f->mapped = 0; |
|
|
if (-1 == stat(fn->ptr, &st)) { | if (-1 == (fd = open(fn->ptr, O_RDONLY | O_BINARY | FIFO_NONBLOCK))) { |
return -1; |
return -1; |
} |
} |
|
|
f->size = st.st_size; | if (-1 == fstat(fd, &st)) { |
| close(fd); |
#ifdef HAVE_MMAP | |
if (-1 == (fd = open(fn->ptr, O_RDONLY | O_BINARY))) { | |
return -1; |
return -1; |
} |
} |
|
|
f->start = mmap(NULL, f->size, PROT_READ, MAP_SHARED, fd, 0); | if (0 == st.st_size) { |
| /* empty file doesn't need a mapping */ |
| close(fd); |
| return 0; |
| } |
|
|
close(fd); | f->start = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); |
|
|
if (MAP_FAILED == f->start) { |
if (MAP_FAILED == f->start) { |
return -1; | f->start = malloc((size_t)st.st_size); |
| if (NULL == f->start |
| || st.st_size != read(fd, f->start, (size_t)st.st_size)) { |
| free(f->start); |
| f->start = NULL; |
| close(fd); |
| return -1; |
| } |
| } else { |
| f->mapped = 1; |
} |
} |
|
|
|
close(fd); |
|
|
|
f->size = st.st_size; |
|
return 0; |
|
|
#elif defined __WIN32 |
#elif defined __WIN32 |
|
|
|
HANDLE *fh, *mh; |
|
void *p; |
|
LARGE_INTEGER fsize; |
|
|
|
f->start = NULL; |
|
f->size = 0; |
|
|
fh = CreateFile(fn->ptr, |
fh = CreateFile(fn->ptr, |
GENERIC_READ, |
GENERIC_READ, |
FILE_SHARE_READ, |
FILE_SHARE_READ, |
Line 53 int stream_open(stream *f, buffer *fn) {
|
Line 86 int stream_open(stream *f, buffer *fn) {
|
|
|
if (!fh) return -1; |
if (!fh) return -1; |
|
|
|
if (0 != GetFileSizeEx(fh, &fsize)) { |
|
CloseHandle(fh); |
|
return -1; |
|
} |
|
|
|
if (0 == fsize) { |
|
CloseHandle(fh); |
|
return 0; |
|
} |
|
|
mh = CreateFileMapping( fh, |
mh = CreateFileMapping( fh, |
NULL, |
NULL, |
PAGE_READONLY, |
PAGE_READONLY, |
(sizeof(off_t) > 4) ? f->size >> 32 : 0, | (sizeof(off_t) > 4) ? fsize >> 32 : 0, |
f->size & 0xffffffff, | fsize & 0xffffffff, |
NULL); |
NULL); |
|
|
if (!mh) { |
if (!mh) { |
Line 72 int stream_open(stream *f, buffer *fn) {
|
Line 115 int stream_open(stream *f, buffer *fn) {
|
(LPTSTR) &lpMsgBuf, |
(LPTSTR) &lpMsgBuf, |
0, NULL ); |
0, NULL ); |
*/ |
*/ |
|
CloseHandle(fh); |
return -1; |
return -1; |
} |
} |
|
|
Line 84 int stream_open(stream *f, buffer *fn) {
|
Line 128 int stream_open(stream *f, buffer *fn) {
|
CloseHandle(fh); |
CloseHandle(fh); |
|
|
f->start = p; |
f->start = p; |
#else | f->size = (off_t)fsize; |
# error no mmap found | return 0; |
| |
#endif |
#endif |
|
|
return 0; |
|
} |
} |
|
|
int stream_close(stream *f) { |
int stream_close(stream *f) { |
#ifdef HAVE_MMAP |
#ifdef HAVE_MMAP |
if (f->start) munmap(f->start, f->size); | if (f->start) { |
| if (f->mapped) { |
| f->mapped = 0; |
| munmap(f->start, f->size); |
| } else { |
| free(f->start); |
| } |
| } |
#elif defined(__WIN32) |
#elif defined(__WIN32) |
if (f->start) UnmapViewOfFile(f->start); |
if (f->start) UnmapViewOfFile(f->start); |
#endif |
#endif |
|
|
f->start = NULL; |
f->start = NULL; |
|
f->size = 0; |
|
|
return 0; |
return 0; |
} |
} |