2012-03-26 09:34:32 +08:00
|
|
|
/*
|
|
|
|
* Copyright 1996, University Corporation for Atmospheric Research
|
|
|
|
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
|
|
|
|
*/
|
|
|
|
|
2017-03-12 03:03:17 +08:00
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2012-03-26 09:34:32 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2018-02-26 12:45:31 +08:00
|
|
|
#include <stdio.h>
|
2012-03-26 09:34:32 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2017-03-09 08:01:10 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2012-03-26 09:34:32 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
2018-02-26 12:45:31 +08:00
|
|
|
#ifdef _MSC_VER /* Microsoft Compilers */
|
|
|
|
#include <windows.h>
|
|
|
|
#include <winbase.h>
|
|
|
|
#include <io.h>
|
|
|
|
#define access(path,mode) _access(path,mode)
|
|
|
|
#endif
|
2017-03-09 08:01:10 +08:00
|
|
|
|
2015-05-29 05:03:02 +08:00
|
|
|
#include "ncdispatch.h"
|
2012-09-07 03:44:03 +08:00
|
|
|
#include "nc3internal.h"
|
2018-02-26 12:45:31 +08:00
|
|
|
#include "netcdf_mem.h"
|
|
|
|
#include "ncwinpath.h"
|
2012-03-26 09:34:32 +08:00
|
|
|
|
2012-05-16 01:48:27 +08:00
|
|
|
#undef DEBUG
|
2012-04-12 10:06:28 +08:00
|
|
|
|
2012-03-26 09:34:32 +08:00
|
|
|
#ifndef HAVE_SSIZE_T
|
2017-03-17 04:34:33 +08:00
|
|
|
typedef int ssize_t;
|
2012-03-26 09:34:32 +08:00
|
|
|
#endif
|
|
|
|
|
2012-04-12 10:06:28 +08:00
|
|
|
#ifdef DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2012-03-26 09:34:32 +08:00
|
|
|
#ifndef SEEK_SET
|
|
|
|
#define SEEK_SET 0
|
|
|
|
#define SEEK_CUR 1
|
|
|
|
#define SEEK_END 2
|
|
|
|
#endif
|
|
|
|
|
2012-04-23 04:34:21 +08:00
|
|
|
/* Define the mode flags for create: let umask rule */
|
2012-06-29 01:12:02 +08:00
|
|
|
#define OPENMODE 0666
|
2012-04-09 06:47:38 +08:00
|
|
|
|
2012-03-26 09:34:32 +08:00
|
|
|
#include "ncio.h"
|
|
|
|
#include "fbits.h"
|
|
|
|
#include "rnd.h"
|
|
|
|
|
|
|
|
/* #define INSTRUMENT 1 */
|
|
|
|
#if INSTRUMENT /* debugging */
|
|
|
|
#undef NDEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "instr.h"
|
|
|
|
#endif
|
|
|
|
|
2012-04-09 06:47:38 +08:00
|
|
|
#ifndef MEMIO_MAXBLOCKSIZE
|
|
|
|
#define MEMIO_MAXBLOCKSIZE 268435456 /* sanity check, about X_SIZE_T_MAX/8 */
|
|
|
|
#endif
|
|
|
|
|
2012-03-26 09:34:32 +08:00
|
|
|
#undef MIN /* system may define MIN somewhere and complain */
|
|
|
|
#define MIN(mm,nn) (((mm) < (nn)) ? (mm) : (nn))
|
|
|
|
|
|
|
|
#if !defined(NDEBUG) && !defined(X_INT_MAX)
|
|
|
|
#define X_INT_MAX 2147483647
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0 /* !defined(NDEBUG) && !defined(X_ALIGN) */
|
|
|
|
#define X_ALIGN 4
|
|
|
|
#else
|
|
|
|
#undef X_ALIGN
|
|
|
|
#endif
|
|
|
|
|
2018-02-26 12:45:31 +08:00
|
|
|
#undef REALLOCBUG
|
|
|
|
#ifdef REALLOCBUG
|
|
|
|
/* There is some kind of realloc bug that I cannot solve yet */
|
|
|
|
#define reallocx(m,new,old) realloc(m,new)
|
|
|
|
#else
|
|
|
|
static void*
|
|
|
|
reallocx(void* mem, size_t newsize, size_t oldsize)
|
|
|
|
{
|
|
|
|
void* m = malloc(newsize);
|
|
|
|
memcpy(m,mem,oldsize);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-26 09:34:32 +08:00
|
|
|
/* Private data for memio */
|
|
|
|
|
|
|
|
typedef struct NCMEMIO {
|
2018-02-26 12:45:31 +08:00
|
|
|
int locked; /* => we cannot realloc or free*/
|
|
|
|
int modified; /* => we realloc'd memory at least once */
|
2012-03-26 09:34:32 +08:00
|
|
|
int persist; /* => save to a file; triggered by NC_WRITE */
|
|
|
|
char* memory;
|
2018-02-26 12:45:31 +08:00
|
|
|
size_t alloc;
|
|
|
|
size_t size;
|
|
|
|
size_t pos;
|
|
|
|
/* Convenience flags */
|
|
|
|
int diskless;
|
|
|
|
int inmemory; /* assert(inmemory iff !diskless */
|
2012-03-26 09:34:32 +08:00
|
|
|
} NCMEMIO;
|
|
|
|
|
|
|
|
/* Forward */
|
|
|
|
static int memio_rel(ncio *const nciop, off_t offset, int rflags);
|
|
|
|
static int memio_get(ncio *const nciop, off_t offset, size_t extent, int rflags, void **const vpp);
|
|
|
|
static int memio_move(ncio *const nciop, off_t to, off_t from, size_t nbytes, int rflags);
|
|
|
|
static int memio_sync(ncio *const nciop);
|
|
|
|
static int memio_filesize(ncio* nciop, off_t* filesizep);
|
|
|
|
static int memio_pad_length(ncio* nciop, off_t length);
|
|
|
|
static int memio_close(ncio* nciop, int);
|
2018-02-26 12:45:31 +08:00
|
|
|
static int readfile(const char* path, NC_memio*);
|
|
|
|
static int writefile(const char* path, NCMEMIO*);
|
|
|
|
static int fileiswriteable(const char* path);
|
2012-04-09 06:47:38 +08:00
|
|
|
|
|
|
|
/* Mnemonic */
|
|
|
|
#define DOOPEN 1
|
2012-03-26 09:34:32 +08:00
|
|
|
|
2018-02-26 12:45:31 +08:00
|
|
|
static size_t pagesize = 0;
|
2012-04-12 10:06:28 +08:00
|
|
|
|
2015-09-23 05:42:52 +08:00
|
|
|
/*! Create a new ncio struct to hold info about the file. */
|
2018-02-26 12:45:31 +08:00
|
|
|
static int
|
|
|
|
memio_new(const char* path, int ioflags, off_t initialsize, ncio** nciopp, NCMEMIO** memiop)
|
2012-03-26 09:34:32 +08:00
|
|
|
{
|
2012-04-09 06:47:38 +08:00
|
|
|
int status = NC_NOERR;
|
2012-03-26 09:34:32 +08:00
|
|
|
ncio* nciop = NULL;
|
|
|
|
NCMEMIO* memio = NULL;
|
2018-02-26 12:45:31 +08:00
|
|
|
size_t minsize = (size_t)initialsize;
|
2012-04-12 10:06:28 +08:00
|
|
|
|
2015-05-29 05:03:02 +08:00
|
|
|
/* use asserts because this is an internal function */
|
2018-02-26 12:45:31 +08:00
|
|
|
assert(fIsSet(ioflags,NC_INMEMORY));
|
2015-05-29 05:03:02 +08:00
|
|
|
assert(memiop != NULL && nciopp != NULL);
|
2018-02-26 12:45:31 +08:00
|
|
|
assert(path != NULL);
|
2012-07-03 06:59:59 +08:00
|
|
|
|
2015-05-29 05:03:02 +08:00
|
|
|
if(pagesize == 0) {
|
2012-07-03 06:59:59 +08:00
|
|
|
#if defined (_WIN32) || defined(_WIN64)
|
|
|
|
SYSTEM_INFO info;
|
|
|
|
GetSystemInfo (&info);
|
|
|
|
pagesize = info.dwPageSize;
|
|
|
|
#elif defined HAVE_SYSCONF
|
2018-02-27 03:18:01 +08:00
|
|
|
long pgval = -1;
|
|
|
|
pgval = sysconf(_SC_PAGE_SIZE);
|
|
|
|
if(pgval < 0) {
|
|
|
|
status = NC_EIO;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
pagesize = (size_t)pgval;
|
2012-04-09 06:47:38 +08:00
|
|
|
#elif defined HAVE_GETPAGESIZE
|
2018-02-27 03:18:01 +08:00
|
|
|
pagesize = (size_t)getpagesize();
|
2012-04-09 06:47:38 +08:00
|
|
|
#else
|
2018-02-27 03:18:01 +08:00
|
|
|
pagesize = 4096; /* good guess */
|
2012-04-09 06:47:38 +08:00
|
|
|
#endif
|
2012-04-12 10:06:28 +08:00
|
|
|
}
|
|
|
|
|
2012-04-09 06:47:38 +08:00
|
|
|
errno = 0;
|
2012-03-26 09:34:32 +08:00
|
|
|
|
2012-04-10 06:03:02 +08:00
|
|
|
/* Always force the allocated size to be a multiple of pagesize */
|
|
|
|
if(initialsize == 0) initialsize = pagesize;
|
|
|
|
if((initialsize % pagesize) != 0)
|
|
|
|
initialsize += (pagesize - (initialsize % pagesize));
|
|
|
|
|
2012-03-26 09:34:32 +08:00
|
|
|
nciop = (ncio* )calloc(1,sizeof(ncio));
|
2012-04-09 06:47:38 +08:00
|
|
|
if(nciop == NULL) {status = NC_ENOMEM; goto fail;}
|
2015-09-23 05:35:30 +08:00
|
|
|
|
2012-03-26 09:34:32 +08:00
|
|
|
nciop->ioflags = ioflags;
|
2012-04-09 06:47:38 +08:00
|
|
|
*((int*)&nciop->fd) = -1; /* caller will fix */
|
2012-03-26 09:34:32 +08:00
|
|
|
|
|
|
|
*((ncio_relfunc**)&nciop->rel) = memio_rel;
|
|
|
|
*((ncio_getfunc**)&nciop->get) = memio_get;
|
|
|
|
*((ncio_movefunc**)&nciop->move) = memio_move;
|
|
|
|
*((ncio_syncfunc**)&nciop->sync) = memio_sync;
|
|
|
|
*((ncio_filesizefunc**)&nciop->filesize) = memio_filesize;
|
|
|
|
*((ncio_pad_lengthfunc**)&nciop->pad_length) = memio_pad_length;
|
|
|
|
*((ncio_closefunc**)&nciop->close) = memio_close;
|
2015-09-23 05:35:30 +08:00
|
|
|
|
2012-03-26 09:34:32 +08:00
|
|
|
memio = (NCMEMIO*)calloc(1,sizeof(NCMEMIO));
|
2012-04-09 06:47:38 +08:00
|
|
|
if(memio == NULL) {status = NC_ENOMEM; goto fail;}
|
2012-03-26 09:34:32 +08:00
|
|
|
*((void* *)&nciop->pvt) = memio;
|
|
|
|
|
2015-05-29 05:03:02 +08:00
|
|
|
*((char**)&nciop->path) = strdup(path);
|
|
|
|
if(nciop->path == NULL) {status = NC_ENOMEM; goto fail;}
|
2018-02-26 12:45:31 +08:00
|
|
|
|
2015-05-29 05:03:02 +08:00
|
|
|
if(memiop && memio) *memiop = memio; else free(memio);
|
|
|
|
if(nciopp && nciop) *nciopp = nciop;
|
Refactored read_scale(), memio_new(), var_create_dataset() and makespecial()
to clean up resources properly on failure.
Refactored doubly-linked list code for objects in the libsrc4 directory,
cleaning up the add/del routines, breaking out the common next/prev
pointers into a struct and extracting the add/del operations on them,
changed the list of dims to add new dims in the same order as the other
types, made all add routines able to optionally return a pointer to the
newly created object.
Removed some dead code (pg_var(), nc4_pg_var1(), nc4_pg_varm(), misc. small
routines, etc)
Fixed fill value handling for string types in nc4_get_vara().
Changed many malloc()+strcpy() pairs into calls to strdup().
Cleaned up misc. other minor Coverity issues.
2013-12-08 17:29:26 +08:00
|
|
|
else {
|
2015-05-29 05:03:02 +08:00
|
|
|
if(nciop->path != NULL) free((char*)nciop->path);
|
Refactored read_scale(), memio_new(), var_create_dataset() and makespecial()
to clean up resources properly on failure.
Refactored doubly-linked list code for objects in the libsrc4 directory,
cleaning up the add/del routines, breaking out the common next/prev
pointers into a struct and extracting the add/del operations on them,
changed the list of dims to add new dims in the same order as the other
types, made all add routines able to optionally return a pointer to the
newly created object.
Removed some dead code (pg_var(), nc4_pg_var1(), nc4_pg_varm(), misc. small
routines, etc)
Fixed fill value handling for string types in nc4_get_vara().
Changed many malloc()+strcpy() pairs into calls to strdup().
Cleaned up misc. other minor Coverity issues.
2013-12-08 17:29:26 +08:00
|
|
|
free(nciop);
|
|
|
|
}
|
2018-02-26 12:45:31 +08:00
|
|
|
memio->alloc = (size_t)initialsize;
|
|
|
|
memio->pos = 0;
|
|
|
|
memio->size = minsize;
|
|
|
|
memio->memory = NULL; /* filled in by caller */
|
|
|
|
|
|
|
|
if(fIsSet(ioflags,NC_DISKLESS))
|
|
|
|
memio->diskless = 1;
|
|
|
|
if(fIsSet(ioflags,NC_INMEMORY) && !memio->diskless)
|
|
|
|
memio->inmemory = 1;
|
|
|
|
if(fIsSet(ioflags,NC_WRITE) && !fIsSet(ioflags,NC_NOCLOBBER) && memio->diskless)
|
|
|
|
memio->persist = 1;
|
2012-04-09 06:47:38 +08:00
|
|
|
|
|
|
|
done:
|
|
|
|
return status;
|
2012-03-26 09:34:32 +08:00
|
|
|
|
|
|
|
fail:
|
2015-05-29 05:03:02 +08:00
|
|
|
if(memio != NULL) free(memio);
|
2012-03-26 09:34:32 +08:00
|
|
|
if(nciop != NULL) {
|
|
|
|
if(nciop->path != NULL) free((char*)nciop->path);
|
Refactored read_scale(), memio_new(), var_create_dataset() and makespecial()
to clean up resources properly on failure.
Refactored doubly-linked list code for objects in the libsrc4 directory,
cleaning up the add/del routines, breaking out the common next/prev
pointers into a struct and extracting the add/del operations on them,
changed the list of dims to add new dims in the same order as the other
types, made all add routines able to optionally return a pointer to the
newly created object.
Removed some dead code (pg_var(), nc4_pg_var1(), nc4_pg_varm(), misc. small
routines, etc)
Fixed fill value handling for string types in nc4_get_vara().
Changed many malloc()+strcpy() pairs into calls to strdup().
Cleaned up misc. other minor Coverity issues.
2013-12-08 17:29:26 +08:00
|
|
|
free(nciop);
|
2012-03-26 09:34:32 +08:00
|
|
|
}
|
2012-04-09 06:47:38 +08:00
|
|
|
goto done;
|
2012-03-26 09:34:32 +08:00
|
|
|
}
|
|
|
|
|
2015-05-29 05:03:02 +08:00
|
|
|
/* Create a file, and the ncio struct to go with it.
|
2012-03-26 09:34:32 +08:00
|
|
|
|
|
|
|
path - path of file to create.
|
|
|
|
ioflags - flags from nc_create
|
|
|
|
initialsz - From the netcdf man page: "The argument
|
2015-05-29 05:03:02 +08:00
|
|
|
initialsize sets the initial size of the file at creation time."
|
2015-09-23 05:35:30 +08:00
|
|
|
igeto -
|
|
|
|
igetsz -
|
2012-03-26 09:34:32 +08:00
|
|
|
sizehintp - the size of a page of data for buffered reads and writes.
|
2015-09-23 05:35:30 +08:00
|
|
|
parameters - arbitrary data
|
2012-03-26 09:34:32 +08:00
|
|
|
nciopp - pointer to a pointer that will get location of newly
|
|
|
|
created and inited ncio struct.
|
|
|
|
mempp - pointer to pointer to the initial memory read.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
memio_create(const char* path, int ioflags,
|
|
|
|
size_t initialsz,
|
|
|
|
off_t igeto, size_t igetsz, size_t* sizehintp,
|
2018-02-26 12:45:31 +08:00
|
|
|
void* parameters /*ignored*/,
|
2012-03-26 09:34:32 +08:00
|
|
|
ncio* *nciopp, void** const mempp)
|
|
|
|
{
|
|
|
|
ncio* nciop;
|
|
|
|
int fd;
|
|
|
|
int status;
|
2012-04-09 06:47:38 +08:00
|
|
|
NCMEMIO* memio = NULL;
|
2012-03-26 09:34:32 +08:00
|
|
|
|
|
|
|
if(path == NULL ||* path == 0)
|
|
|
|
return NC_EINVAL;
|
2018-02-26 12:45:31 +08:00
|
|
|
|
|
|
|
status = memio_new(path, ioflags, initialsz, &nciop, &memio);
|
2012-04-09 06:47:38 +08:00
|
|
|
if(status != NC_NOERR)
|
|
|
|
return status;
|
2012-08-03 02:43:21 +08:00
|
|
|
|
2018-02-26 12:45:31 +08:00
|
|
|
if(memio->persist) {
|
|
|
|
/* Verify the file is writeable */
|
|
|
|
if(!fileiswriteable(path))
|
|
|
|
{status = EPERM; goto unwind_open;}
|
|
|
|
}
|
2012-04-09 06:47:38 +08:00
|
|
|
|
2018-02-26 12:45:31 +08:00
|
|
|
/* Allocate the memory for this file */
|
|
|
|
memio->memory = (char*)malloc((size_t)memio->alloc);
|
|
|
|
if(memio->memory == NULL) {status = NC_ENOMEM; goto unwind_open;}
|
|
|
|
memio->locked = 0;
|
2012-03-26 09:34:32 +08:00
|
|
|
|
2012-04-12 10:06:28 +08:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,"memio_create: initial memory: %lu/%lu\n",(unsigned long)memio->memory,(unsigned long)memio->alloc);
|
|
|
|
#endif
|
|
|
|
|
2012-03-26 09:34:32 +08:00
|
|
|
fd = nc__pseudofd();
|
2015-09-23 05:35:30 +08:00
|
|
|
*((int* )&nciop->fd) = fd;
|
2012-03-26 09:34:32 +08:00
|
|
|
|
2012-04-09 06:47:38 +08:00
|
|
|
fSet(nciop->ioflags, NC_WRITE);
|
|
|
|
|
2012-03-26 09:34:32 +08:00
|
|
|
if(igetsz != 0)
|
|
|
|
{
|
|
|
|
status = nciop->get(nciop,
|
|
|
|
igeto, igetsz,
|
|
|
|
RGN_WRITE,
|
|
|
|
mempp);
|
|
|
|
if(status != NC_NOERR)
|
|
|
|
goto unwind_open;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pick a default sizehint */
|
2018-02-26 12:45:31 +08:00
|
|
|
if(sizehintp) *sizehintp = (size_t)pagesize;
|
2012-03-26 09:34:32 +08:00
|
|
|
|
|
|
|
*nciopp = nciop;
|
|
|
|
return NC_NOERR;
|
|
|
|
|
|
|
|
unwind_open:
|
|
|
|
memio_close(nciop,1);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-02-26 12:45:31 +08:00
|
|
|
/* This function opens the data file or inmemory data
|
2012-03-26 09:34:32 +08:00
|
|
|
path - path of data file.
|
|
|
|
ioflags - flags passed into nc_open.
|
|
|
|
igeto - looks like this function can do an initial page get, and
|
|
|
|
igeto is going to be the offset for that. But it appears to be
|
2015-09-23 05:35:30 +08:00
|
|
|
unused
|
2012-03-26 09:34:32 +08:00
|
|
|
igetsz - the size in bytes of initial page get (a.k.a. extent). Not
|
|
|
|
ever used in the library.
|
|
|
|
sizehintp - the size of a page of data for buffered reads and writes.
|
2015-09-23 05:35:30 +08:00
|
|
|
parameters - arbitrary data
|
2012-03-26 09:34:32 +08:00
|
|
|
nciopp - pointer to pointer that will get address of newly created
|
|
|
|
and inited ncio struct.
|
|
|
|
mempp - pointer to pointer to the initial memory read.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
memio_open(const char* path,
|
|
|
|
int ioflags,
|
|
|
|
off_t igeto, size_t igetsz, size_t* sizehintp,
|
2015-05-29 05:03:02 +08:00
|
|
|
void* parameters,
|
2012-03-26 09:34:32 +08:00
|
|
|
ncio* *nciopp, void** const mempp)
|
|
|
|
{
|
2012-11-15 02:24:45 +08:00
|
|
|
ncio* nciop = NULL;
|
2015-05-29 05:03:02 +08:00
|
|
|
int fd = -1;
|
|
|
|
int status = NC_NOERR;
|
|
|
|
size_t sizehint = 0;
|
2018-02-26 12:45:31 +08:00
|
|
|
NC_memio meminfo; /* use struct to avoid worrying about free'ing it */
|
|
|
|
NCMEMIO* memio = NULL;
|
|
|
|
size_t initialsize;
|
|
|
|
/* Should be the case that diskless => inmemory but not converse */
|
|
|
|
int diskless = (fIsSet(ioflags,NC_DISKLESS));
|
|
|
|
int inmemory = (fIsSet(ioflags,NC_INMEMORY) && !diskless);
|
|
|
|
int locked = 0;
|
2012-03-26 09:34:32 +08:00
|
|
|
|
2015-05-29 05:03:02 +08:00
|
|
|
if(path == NULL || strlen(path) == 0)
|
|
|
|
return NC_EINVAL;
|
2012-03-26 09:34:32 +08:00
|
|
|
|
2012-04-09 06:47:38 +08:00
|
|
|
assert(sizehintp != NULL);
|
2018-02-26 12:45:31 +08:00
|
|
|
|
2015-09-23 05:35:30 +08:00
|
|
|
sizehint = *sizehintp;
|
2012-03-26 09:34:32 +08:00
|
|
|
|
2018-02-26 12:45:31 +08:00
|
|
|
if(inmemory) { /* parameters provide the memory chunk */
|
|
|
|
NC_memio* memparams = (NC_memio*)parameters;
|
|
|
|
meminfo = *memparams;
|
|
|
|
locked = fIsSet(meminfo.flags,NC_MEMIO_LOCKED);
|
|
|
|
/* As a safeguard, if !locked and NC_WRITE is set,
|
|
|
|
then we must take control of the incoming memory */
|
|
|
|
if(!locked && fIsSet(ioflags,NC_WRITE)) {
|
|
|
|
memparams->memory = NULL;
|
|
|
|
}
|
|
|
|
} else { /* read the file into a chunk of memory*/
|
|
|
|
assert(diskless);
|
|
|
|
status = readfile(path,&meminfo);
|
|
|
|
if(status != NC_NOERR)
|
|
|
|
{goto unwind_open;}
|
2015-05-29 05:03:02 +08:00
|
|
|
}
|
2012-04-09 06:47:38 +08:00
|
|
|
|
2018-02-26 12:45:31 +08:00
|
|
|
/* Fix up initial size */
|
|
|
|
initialsize = meminfo.size;
|
|
|
|
|
|
|
|
/* create the NCMEMIO structure */
|
|
|
|
status = memio_new(path, ioflags, initialsize, &nciop, &memio);
|
|
|
|
if(status != NC_NOERR)
|
|
|
|
{goto unwind_open;}
|
|
|
|
memio->locked = locked;
|
|
|
|
|
|
|
|
/* Initialize the memio memory */
|
|
|
|
memio->memory = meminfo.memory;
|
|
|
|
|
|
|
|
/* memio_new may have modified the allocated size, in which case,
|
|
|
|
reallocate the memory unless the memory is locked. */
|
|
|
|
if(memio->alloc > meminfo.size) {
|
|
|
|
if(memio->locked)
|
|
|
|
memio->alloc = meminfo.size; /* force it back to what it was */
|
|
|
|
else {
|
|
|
|
void* oldmem = memio->memory;
|
|
|
|
memio->memory = reallocx(oldmem,memio->alloc,meminfo.size);
|
|
|
|
if(memio->memory == NULL)
|
|
|
|
{status = NC_ENOMEM; goto unwind_open;}
|
|
|
|
}
|
2013-07-20 05:32:27 +08:00
|
|
|
}
|
2012-04-09 06:47:38 +08:00
|
|
|
|
2012-04-12 10:06:28 +08:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,"memio_open: initial memory: %lu/%lu\n",(unsigned long)memio->memory,(unsigned long)memio->alloc);
|
|
|
|
#endif
|
|
|
|
|
2018-02-26 12:45:31 +08:00
|
|
|
if(memio->persist) {
|
|
|
|
/* Verify the file is writeable */
|
|
|
|
if(!fileiswriteable(path))
|
|
|
|
{status = EPERM; goto unwind_open;}
|
2012-04-10 06:03:02 +08:00
|
|
|
}
|
2012-04-09 06:47:38 +08:00
|
|
|
|
2015-05-29 05:03:02 +08:00
|
|
|
/* Use half the filesize as the blocksize ; why? */
|
2018-02-26 12:45:31 +08:00
|
|
|
sizehint = (size_t)(memio->alloc/2);
|
2012-04-09 06:47:38 +08:00
|
|
|
|
2017-05-03 11:22:54 +08:00
|
|
|
/* sizehint must be multiple of 8 */
|
|
|
|
sizehint = (sizehint / 8) * 8;
|
|
|
|
if(sizehint < 8) sizehint = 8;
|
|
|
|
|
2012-04-09 06:47:38 +08:00
|
|
|
fd = nc__pseudofd();
|
2015-09-23 05:35:30 +08:00
|
|
|
*((int* )&nciop->fd) = fd;
|
2012-03-26 09:34:32 +08:00
|
|
|
|
|
|
|
if(igetsz != 0)
|
|
|
|
{
|
|
|
|
status = nciop->get(nciop,
|
|
|
|
igeto, igetsz,
|
|
|
|
0,
|
|
|
|
mempp);
|
|
|
|
if(status != NC_NOERR)
|
|
|
|
goto unwind_open;
|
|
|
|
}
|
|
|
|
|
2015-05-29 05:03:02 +08:00
|
|
|
if(sizehintp) *sizehintp = sizehint;
|
|
|
|
if(nciopp) *nciopp = nciop; else {ncio_close(nciop,0);}
|
2012-03-26 09:34:32 +08:00
|
|
|
return NC_NOERR;
|
|
|
|
|
|
|
|
unwind_open:
|
2013-08-10 05:47:42 +08:00
|
|
|
if(fd >= 0)
|
|
|
|
close(fd);
|
2012-04-09 06:47:38 +08:00
|
|
|
memio_close(nciop,0);
|
2012-03-26 09:34:32 +08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2015-09-23 05:35:30 +08:00
|
|
|
/*
|
2012-03-26 09:34:32 +08:00
|
|
|
* Get file size in bytes.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
memio_filesize(ncio* nciop, off_t* filesizep)
|
|
|
|
{
|
|
|
|
NCMEMIO* memio;
|
|
|
|
if(nciop == NULL || nciop->pvt == NULL) return NC_EINVAL;
|
|
|
|
memio = (NCMEMIO*)nciop->pvt;
|
|
|
|
if(filesizep != NULL) *filesizep = memio->size;
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sync any changes to disk, then truncate or extend file so its size
|
|
|
|
* is length. This is only intended to be called before close, if the
|
|
|
|
* file is open for writing and the actual size does not match the
|
|
|
|
* calculated size, perhaps as the result of having been previously
|
|
|
|
* written in NOFILL mode.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
memio_pad_length(ncio* nciop, off_t length)
|
|
|
|
{
|
|
|
|
NCMEMIO* memio;
|
2018-02-26 12:45:31 +08:00
|
|
|
size_t len = (size_t)length;
|
2012-03-26 09:34:32 +08:00
|
|
|
if(nciop == NULL || nciop->pvt == NULL) return NC_EINVAL;
|
|
|
|
memio = (NCMEMIO*)nciop->pvt;
|
|
|
|
|
2018-02-26 12:45:31 +08:00
|
|
|
if(!memio->persist)
|
2012-03-26 09:34:32 +08:00
|
|
|
return EPERM; /* attempt to write readonly file*/
|
2018-02-26 12:45:31 +08:00
|
|
|
if(memio->locked)
|
2012-03-26 09:34:32 +08:00
|
|
|
return NC_EDISKLESS;
|
2012-04-09 06:47:38 +08:00
|
|
|
|
2018-02-26 12:45:31 +08:00
|
|
|
if(len > memio->alloc) {
|
2012-04-12 10:06:28 +08:00
|
|
|
/* Realloc the allocated memory to a multiple of the pagesize*/
|
2018-02-26 12:45:31 +08:00
|
|
|
size_t newsize = (size_t)len;
|
2012-04-12 10:06:28 +08:00
|
|
|
void* newmem = NULL;
|
|
|
|
/* Round to a multiple of pagesize */
|
|
|
|
if((newsize % pagesize) != 0)
|
|
|
|
newsize += (pagesize - (newsize % pagesize));
|
2012-04-09 06:47:38 +08:00
|
|
|
|
2018-02-26 12:45:31 +08:00
|
|
|
newmem = (char*)reallocx(memio->memory,newsize,memio->alloc);
|
2012-04-10 06:03:02 +08:00
|
|
|
if(newmem == NULL) return NC_ENOMEM;
|
2018-02-26 12:45:31 +08:00
|
|
|
/* If not copy is set, then fail if the newmem address is different
|
|
|
|
from old address */
|
|
|
|
if(newmem != memio->memory) {
|
|
|
|
memio->modified++;
|
|
|
|
if(memio->locked) {
|
|
|
|
free(newmem);
|
|
|
|
return NC_EINMEMORY;
|
|
|
|
}
|
|
|
|
}
|
2012-04-10 06:03:02 +08:00
|
|
|
/* zero out the extra memory */
|
2018-02-25 11:36:24 +08:00
|
|
|
memset((void*)((char*)newmem+memio->alloc),0,(size_t)(newsize - memio->alloc));
|
2012-04-10 06:03:02 +08:00
|
|
|
|
2012-04-12 10:06:28 +08:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,"realloc: %lu/%lu -> %lu/%lu\n",
|
|
|
|
(unsigned long)memio->memory,(unsigned long)memio->alloc,
|
|
|
|
(unsigned long)newmem,(unsigned long)newsize);
|
|
|
|
#endif
|
2018-02-26 12:45:31 +08:00
|
|
|
if(memio->memory != NULL && (!memio->locked || memio->modified))
|
|
|
|
free(memio->memory);
|
2012-03-26 09:34:32 +08:00
|
|
|
memio->memory = newmem;
|
2012-04-09 06:47:38 +08:00
|
|
|
memio->alloc = newsize;
|
2018-02-26 12:45:31 +08:00
|
|
|
memio->modified = 1;
|
2015-09-23 05:35:30 +08:00
|
|
|
}
|
2018-02-26 12:45:31 +08:00
|
|
|
memio->size = len;
|
2012-03-26 09:34:32 +08:00
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
2015-09-23 05:35:30 +08:00
|
|
|
/*! Write out any dirty buffers to disk.
|
|
|
|
|
|
|
|
Write out any dirty buffers to disk and ensure that next read will get data from disk. Sync any changes, then close the open file associated with the ncio struct, and free its memory.
|
|
|
|
|
|
|
|
@param[in] nciop pointer to ncio to close.
|
|
|
|
@param[in] doUnlink if true, unlink file
|
|
|
|
@return NC_NOERR on success, error code on failure.
|
2012-03-26 09:34:32 +08:00
|
|
|
*/
|
|
|
|
|
2015-09-23 05:35:30 +08:00
|
|
|
static int
|
2012-03-26 09:34:32 +08:00
|
|
|
memio_close(ncio* nciop, int doUnlink)
|
|
|
|
{
|
|
|
|
int status = NC_NOERR;
|
2015-09-23 05:35:30 +08:00
|
|
|
NCMEMIO* memio ;
|
2015-05-29 05:03:02 +08:00
|
|
|
|
2012-03-26 09:34:32 +08:00
|
|
|
if(nciop == NULL || nciop->pvt == NULL) return NC_NOERR;
|
2012-04-09 06:47:38 +08:00
|
|
|
|
2012-03-26 09:34:32 +08:00
|
|
|
memio = (NCMEMIO*)nciop->pvt;
|
2012-04-09 06:47:38 +08:00
|
|
|
assert(memio != NULL);
|
|
|
|
|
2012-04-10 06:03:02 +08:00
|
|
|
/* See if the user wants the contents persisted to a file */
|
2018-02-26 12:45:31 +08:00
|
|
|
if(memio->persist && memio->memory != NULL) {
|
|
|
|
status = writefile(nciop->path,memio);
|
|
|
|
}
|
2012-04-09 06:47:38 +08:00
|
|
|
|
2018-02-26 12:45:31 +08:00
|
|
|
/* We only free the memio memory if file is not locked or has been modified */
|
|
|
|
if(memio->memory != NULL && (!memio->locked || memio->modified)) {
|
2012-08-03 02:43:21 +08:00
|
|
|
free(memio->memory);
|
2018-02-26 12:45:31 +08:00
|
|
|
memio->memory = NULL;
|
|
|
|
}
|
2012-04-09 06:47:38 +08:00
|
|
|
/* do cleanup */
|
|
|
|
if(memio != NULL) free(memio);
|
2012-03-26 09:34:32 +08:00
|
|
|
if(nciop->path != NULL) free((char*)nciop->path);
|
|
|
|
free(nciop);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-02-26 12:45:31 +08:00
|
|
|
guarantee(ncio* nciop, off_t endpoint0)
|
2012-03-26 09:34:32 +08:00
|
|
|
{
|
|
|
|
NCMEMIO* memio = (NCMEMIO*)nciop->pvt;
|
2018-02-26 12:45:31 +08:00
|
|
|
size_t endpoint = (size_t)endpoint0;
|
2012-04-09 06:47:38 +08:00
|
|
|
if(endpoint > memio->alloc) {
|
2012-03-26 09:34:32 +08:00
|
|
|
/* extend the allocated memory and size */
|
|
|
|
int status = memio_pad_length(nciop,endpoint);
|
|
|
|
if(status != NC_NOERR) return status;
|
|
|
|
}
|
|
|
|
if(memio->size < endpoint)
|
|
|
|
memio->size = endpoint;
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Request that the region (offset, extent)
|
|
|
|
* be made available through *vpp.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
memio_get(ncio* const nciop, off_t offset, size_t extent, int rflags, void** const vpp)
|
|
|
|
{
|
|
|
|
int status = NC_NOERR;
|
|
|
|
NCMEMIO* memio;
|
|
|
|
if(nciop == NULL || nciop->pvt == NULL) return NC_EINVAL;
|
|
|
|
memio = (NCMEMIO*)nciop->pvt;
|
2018-02-25 11:36:24 +08:00
|
|
|
status = guarantee(nciop, offset+(off_t)extent);
|
2012-03-26 09:34:32 +08:00
|
|
|
memio->locked++;
|
|
|
|
if(status != NC_NOERR) return status;
|
|
|
|
if(vpp) *vpp = memio->memory+offset;
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Like memmove(), safely move possibly overlapping data.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
memio_move(ncio* const nciop, off_t to, off_t from, size_t nbytes, int ignored)
|
|
|
|
{
|
|
|
|
int status = NC_NOERR;
|
|
|
|
NCMEMIO* memio;
|
|
|
|
|
|
|
|
if(nciop == NULL || nciop->pvt == NULL) return NC_EINVAL;
|
|
|
|
memio = (NCMEMIO*)nciop->pvt;
|
|
|
|
if(from < to) {
|
|
|
|
/* extend if "to" is not currently allocated */
|
2018-02-25 11:36:24 +08:00
|
|
|
status = guarantee(nciop,to+(off_t)nbytes);
|
2012-03-26 09:34:32 +08:00
|
|
|
if(status != NC_NOERR) return status;
|
|
|
|
}
|
|
|
|
/* check for overlap */
|
2018-02-25 11:36:24 +08:00
|
|
|
if((to + (off_t)nbytes) > from || (from + (off_t)nbytes) > to) {
|
2012-03-26 09:34:32 +08:00
|
|
|
/* Ranges overlap */
|
|
|
|
#ifdef HAVE_MEMMOVE
|
|
|
|
memmove((void*)(memio->memory+to),(void*)(memio->memory+from),nbytes);
|
|
|
|
#else
|
|
|
|
off_t overlap;
|
|
|
|
off_t nbytes1;
|
|
|
|
if((from + nbytes) > to) {
|
|
|
|
overlap = ((from + nbytes) - to); /* # bytes of overlap */
|
|
|
|
nbytes1 = (nbytes - overlap); /* # bytes of non-overlap */
|
|
|
|
/* move the non-overlapping part */
|
|
|
|
memcpy((void*)(memio->memory+(to+overlap)),
|
2012-03-29 01:46:25 +08:00
|
|
|
(void*)(memio->memory+(from+overlap)),
|
|
|
|
nbytes1);
|
2012-03-26 09:34:32 +08:00
|
|
|
/* move the overlapping part */
|
|
|
|
memcpy((void*)(memio->memory+to),
|
|
|
|
(void*)(memio->memory+from),
|
|
|
|
overlap);
|
|
|
|
} else { /*((to + nbytes) > from) */
|
|
|
|
overlap = ((to + nbytes) - from); /* # bytes of overlap */
|
|
|
|
nbytes1 = (nbytes - overlap); /* # bytes of non-overlap */
|
|
|
|
/* move the non-overlapping part */
|
2012-03-29 01:46:25 +08:00
|
|
|
memcpy((void*)(memio->memory+to),
|
|
|
|
(void*)(memio->memory+from),
|
|
|
|
nbytes1);
|
2012-03-26 09:34:32 +08:00
|
|
|
/* move the overlapping part */
|
|
|
|
memcpy((void*)(memio->memory+(to+nbytes1)),
|
|
|
|
(void*)(memio->memory+(from+nbytes1)),
|
|
|
|
overlap);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else {/* no overlap */
|
|
|
|
memcpy((void*)(memio->memory+to),(void*)(memio->memory+from),nbytes);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
memio_rel(ncio* const nciop, off_t offset, int rflags)
|
|
|
|
{
|
|
|
|
NCMEMIO* memio;
|
|
|
|
if(nciop == NULL || nciop->pvt == NULL) return NC_EINVAL;
|
|
|
|
memio = (NCMEMIO*)nciop->pvt;
|
|
|
|
memio->locked--;
|
|
|
|
return NC_NOERR; /* do nothing */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write out any dirty buffers to disk and
|
|
|
|
* ensure that next read will get data from disk.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
memio_sync(ncio* const nciop)
|
|
|
|
{
|
|
|
|
return NC_NOERR; /* do nothing */
|
|
|
|
}
|
2018-02-26 12:45:31 +08:00
|
|
|
|
|
|
|
/* "Hidden" Internal function to extract a copy of
|
|
|
|
the size and/or contents of the memory
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
memio_extract(ncio* const nciop, size_t* sizep, void** memoryp)
|
|
|
|
{
|
|
|
|
int status = NC_NOERR;
|
|
|
|
NCMEMIO* memio = NULL;
|
|
|
|
|
|
|
|
if(nciop == NULL || nciop->pvt == NULL) return NC_NOERR;
|
|
|
|
memio = (NCMEMIO*)nciop->pvt;
|
|
|
|
assert(memio != NULL);
|
|
|
|
if(sizep) *sizep = memio->size;
|
|
|
|
|
|
|
|
if(memoryp && memio->memory != NULL) {
|
|
|
|
*memoryp = memio->memory;
|
|
|
|
memio->memory = NULL; /* make sure it does not get free'd */
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
fileiswriteable(const char* path)
|
|
|
|
{
|
|
|
|
int ok;
|
|
|
|
ok = access(path,O_RDWR);
|
|
|
|
if(ok < 0)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read contents of a disk file into a memory chunk */
|
|
|
|
static int
|
|
|
|
readfile(const char* path, NC_memio* memio)
|
|
|
|
{
|
|
|
|
int status = NC_NOERR;
|
|
|
|
FILE* f = NULL;
|
|
|
|
size_t filesize = 0;
|
|
|
|
size_t count = 0;
|
|
|
|
char* memory = NULL;
|
|
|
|
char* p = NULL;
|
|
|
|
|
|
|
|
/* Open the file for reading */
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
f = NCfopen(path,"rb");
|
|
|
|
#else
|
|
|
|
f = NCfopen(path,"r");
|
|
|
|
#endif
|
|
|
|
if(f == NULL)
|
|
|
|
{status = errno; goto done;}
|
|
|
|
/* get current filesize */
|
|
|
|
if(fseek(f,0,SEEK_END) < 0)
|
|
|
|
{status = errno; goto done;}
|
|
|
|
filesize = (size_t)ftell(f);
|
|
|
|
/* allocate memory */
|
|
|
|
memory = malloc((size_t)filesize);
|
|
|
|
if(memory == NULL)
|
|
|
|
{status = NC_ENOMEM; goto done;}
|
|
|
|
/* move pointer back to beginning of file */
|
|
|
|
rewind(f);
|
|
|
|
count = filesize;
|
|
|
|
p = memory;
|
|
|
|
while(count > 0) {
|
|
|
|
size_t actual;
|
|
|
|
actual = fread(p,1,count,f);
|
|
|
|
if(actual == 0 || ferror(f))
|
|
|
|
{status = NC_EIO; goto done;}
|
|
|
|
count -= actual;
|
|
|
|
p += actual;
|
|
|
|
}
|
|
|
|
if(memio) {
|
|
|
|
memio->size = (size_t)filesize;
|
|
|
|
memio->memory = memory;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
if(status != NC_NOERR && memory != NULL)
|
|
|
|
free(memory);
|
|
|
|
if(f != NULL) fclose(f);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write contents of a memory chunk back into a disk file */
|
|
|
|
static int
|
|
|
|
writefile(const char* path, NCMEMIO* memio)
|
|
|
|
{
|
|
|
|
int status = NC_NOERR;
|
|
|
|
FILE* f = NULL;
|
|
|
|
size_t count = 0;
|
|
|
|
char* p = NULL;
|
|
|
|
|
|
|
|
/* Open the file for writing*/
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
f = NCfopen(path,"rwb");
|
|
|
|
#else
|
|
|
|
f = NCfopen(path,"rw");
|
|
|
|
#endif
|
|
|
|
if(f == NULL)
|
|
|
|
{status = errno; goto done;}
|
|
|
|
rewind(f);
|
|
|
|
count = memio->size;
|
|
|
|
p = memio->memory;
|
|
|
|
while(count > 0) {
|
|
|
|
size_t actual;
|
|
|
|
actual = fwrite(p,1,count,f);
|
|
|
|
if(actual == 0 || ferror(f))
|
|
|
|
{status = NC_EIO; goto done;}
|
|
|
|
count -= actual;
|
|
|
|
p += actual;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
if(f != NULL) fclose(f);
|
|
|
|
return status;
|
|
|
|
}
|