/src/netcdf-c/libsrc/ncio.c
| Line | Count | Source | 
| 1 |  | /* | 
| 2 |  |  *  Copyright 2018, University Corporation for Atmospheric Research | 
| 3 |  |  *      See netcdf/COPYRIGHT file for copying and redistribution conditions. | 
| 4 |  |  */ | 
| 5 |  |  | 
| 6 |  | #ifdef HAVE_CONFIG_H | 
| 7 |  | #include "config.h" | 
| 8 |  | #endif | 
| 9 |  |  | 
| 10 |  | #include <stdlib.h> | 
| 11 |  |  | 
| 12 |  | #include "netcdf.h" | 
| 13 |  | #include "ncio.h" | 
| 14 |  | #include "fbits.h" | 
| 15 |  | #include "ncuri.h" | 
| 16 |  | #include "ncrc.h" | 
| 17 |  | #include "ncutil.h" | 
| 18 |  |  | 
| 19 |  | /* With the advent of diskless io, we need to provide | 
| 20 |  |    for multiple ncio packages at the same time, | 
| 21 |  |    so we have multiple versions of ncio_create. | 
| 22 |  | */ | 
| 23 |  |  | 
| 24 |  | /* Define known ncio packages */ | 
| 25 |  | extern int posixio_create(const char*,int,size_t,off_t,size_t,size_t*,void*,ncio**,void** const); | 
| 26 |  | extern int posixio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const); | 
| 27 |  |  | 
| 28 |  | extern int stdio_create(const char*,int,size_t,off_t,size_t,size_t*,void*,ncio**,void** const); | 
| 29 |  | extern int stdio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const); | 
| 30 |  |  | 
| 31 |  | #ifdef USE_FFIO | 
| 32 |  | extern int ffio_create(const char*,int,size_t,off_t,size_t,size_t*,void*,ncio**,void** const); | 
| 33 |  | extern int ffio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const); | 
| 34 |  | #endif | 
| 35 |  |  | 
| 36 |  | #  ifdef USE_MMAP | 
| 37 |  |      extern int mmapio_create(const char*,int,size_t,off_t,size_t,size_t*,void*,ncio**,void** const); | 
| 38 |  |      extern int mmapio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const); | 
| 39 |  | #  endif | 
| 40 |  |  | 
| 41 |  | #ifdef NETCDF_ENABLE_BYTERANGE | 
| 42 |  |     extern int httpio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const); | 
| 43 |  | #endif | 
| 44 |  |  | 
| 45 |  | #ifdef NETCDF_ENABLE_S3 | 
| 46 |  |     extern int s3io_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const); | 
| 47 |  | #endif | 
| 48 |  |  | 
| 49 |  |      extern int memio_create(const char*,int,size_t,off_t,size_t,size_t*,void*,ncio**,void** const); | 
| 50 |  |      extern int memio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const); | 
| 51 |  |  | 
| 52 |  | /* Forward */ | 
| 53 |  | #ifdef NETCDF_ENABLE_BYTERANGE | 
| 54 |  | static int urlmodetest(const char* path); | 
| 55 |  | #endif | 
| 56 |  |  | 
| 57 |  | int | 
| 58 |  | ncio_create(const char *path, int ioflags, size_t initialsz, | 
| 59 |  |                        off_t igeto, size_t igetsz, size_t *sizehintp, | 
| 60 |  |            void* parameters, | 
| 61 |  |                        ncio** iopp, void** const mempp) | 
| 62 | 0 | { | 
| 63 | 0 |     if(fIsSet(ioflags,NC_DISKLESS)) { | 
| 64 | 0 |         return memio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp); | 
| 65 | 0 |     } else if(fIsSet(ioflags,NC_INMEMORY)) { | 
| 66 | 0 |         return memio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp); | 
| 67 | 0 |     } | 
| 68 | 0 | #  ifdef USE_MMAP | 
| 69 | 0 |     else if(fIsSet(ioflags,NC_MMAP)) { | 
| 70 | 0 |         return mmapio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp); | 
| 71 | 0 |     } | 
| 72 | 0 | #  endif /*USE_MMAP*/ | 
| 73 |  |  | 
| 74 |  | #ifdef USE_STDIO | 
| 75 |  |     return stdio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp); | 
| 76 |  | #elif defined(USE_FFIO) | 
| 77 |  |     return ffio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp); | 
| 78 |  | #else | 
| 79 | 0 |     return posixio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp); | 
| 80 | 0 | #endif | 
| 81 | 0 | } | 
| 82 |  |  | 
| 83 |  | int | 
| 84 |  | ncio_open(const char *path, int ioflags, | 
| 85 |  |                      off_t igeto, size_t igetsz, size_t *sizehintp, | 
| 86 |  |          void* parameters, | 
| 87 |  |                      ncio** iopp, void** const mempp) | 
| 88 | 291 | { | 
| 89 |  | #ifdef NETCDF_ENABLE_BYTERANGE | 
| 90 |  |     int modetest = urlmodetest(path); | 
| 91 |  | #endif | 
| 92 |  |  | 
| 93 |  |     /* Diskless open has the following constraints: | 
| 94 |  |        1. file must be classic version 1 or 2 or 5 | 
| 95 |  |      */ | 
| 96 | 291 |     if(fIsSet(ioflags,NC_DISKLESS)) { | 
| 97 | 0 |         return memio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp); | 
| 98 | 0 |     } | 
| 99 | 291 |     if(fIsSet(ioflags,NC_INMEMORY)) { | 
| 100 | 291 |         return memio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp); | 
| 101 | 291 |     } | 
| 102 | 0 | #  ifdef USE_MMAP | 
| 103 | 0 |     if(fIsSet(ioflags,NC_MMAP)) { | 
| 104 | 0 |         return mmapio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp); | 
| 105 | 0 |     } | 
| 106 | 0 | #  endif /*USE_MMAP*/ | 
| 107 |  | #  ifdef NETCDF_ENABLE_BYTERANGE | 
| 108 |  |    if(modetest == NC_HTTP) { | 
| 109 |  |         return httpio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp); | 
| 110 |  |    } | 
| 111 |  | #  ifdef NETCDF_ENABLE_S3 | 
| 112 |  |    if(modetest == NC_S3SDK) { | 
| 113 |  |        return s3io_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp); | 
| 114 |  |    } | 
| 115 |  | #  endif | 
| 116 |  | #  endif /*NETCDF_ENABLE_BYTERANGE*/ | 
| 117 |  |  | 
| 118 |  | #ifdef USE_STDIO | 
| 119 |  |     return stdio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp); | 
| 120 |  | #elif defined(USE_FFIO) | 
| 121 |  |     return ffio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp); | 
| 122 |  | #else | 
| 123 | 0 |     return posixio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp); | 
| 124 | 0 | #endif | 
| 125 | 0 | } | 
| 126 |  |  | 
| 127 |  | /**************************************************/ | 
| 128 |  | /* wrapper functions for the ncio dispatch table */ | 
| 129 |  |  | 
| 130 |  | int | 
| 131 |  | ncio_rel(ncio* const nciop, off_t offset, int rflags) | 
| 132 | 4.79k | { | 
| 133 | 4.79k |     return nciop->rel(nciop,offset,rflags); | 
| 134 | 4.79k | } | 
| 135 |  |  | 
| 136 |  | int | 
| 137 |  | ncio_get(ncio* const nciop, off_t offset, size_t extent, | 
| 138 |  |       int rflags, void **const vpp) | 
| 139 | 4.99k | { | 
| 140 | 4.99k |     return nciop->get(nciop,offset,extent,rflags,vpp); | 
| 141 | 4.99k | } | 
| 142 |  |  | 
| 143 |  | int | 
| 144 |  | ncio_move(ncio* const nciop, off_t to, off_t from, size_t nbytes, int rflags) | 
| 145 | 0 | { | 
| 146 | 0 |     return nciop->move(nciop,to,from,nbytes,rflags); | 
| 147 | 0 | } | 
| 148 |  |  | 
| 149 |  | int | 
| 150 |  | ncio_sync(ncio* const nciop) | 
| 151 | 291 | { | 
| 152 | 291 |     return nciop->sync(nciop); | 
| 153 | 291 | } | 
| 154 |  |  | 
| 155 |  | int | 
| 156 |  | ncio_filesize(ncio* const nciop, off_t *filesizep) | 
| 157 | 302 | { | 
| 158 | 302 |     return nciop->filesize(nciop,filesizep); | 
| 159 | 302 | } | 
| 160 |  |  | 
| 161 |  | int | 
| 162 |  | ncio_pad_length(ncio* const nciop, off_t length) | 
| 163 | 0 | { | 
| 164 | 0 |     return nciop->pad_length(nciop,length); | 
| 165 | 0 | } | 
| 166 |  |  | 
| 167 |  | int | 
| 168 |  | ncio_close(ncio* const nciop, int doUnlink) | 
| 169 | 291 | { | 
| 170 |  |     /* close and release all resources associated | 
| 171 |  |        with nciop, including nciop | 
| 172 |  |     */ | 
| 173 | 291 |     int status = nciop->close(nciop,doUnlink); | 
| 174 | 291 |     return status; | 
| 175 | 291 | } | 
| 176 |  |  | 
| 177 |  | /* URL utilities */ | 
| 178 |  |  | 
| 179 |  | /* | 
| 180 |  | Check mode flags and return: | 
| 181 |  | NC_HTTP => byterange | 
| 182 |  | NC_S3SDK => s3 | 
| 183 |  | 0 => Not URL | 
| 184 |  | */ | 
| 185 |  | #ifdef NETCDF_ENABLE_BYTERANGE | 
| 186 |  | static int | 
| 187 |  | urlmodetest(const char* path) | 
| 188 |  | { | 
| 189 |  |     int kind = 0; | 
| 190 |  |     NCURI* uri = NULL; | 
| 191 |  |      | 
| 192 |  |     ncuriparse(path,&uri); | 
| 193 |  |     if(uri == NULL) return 0; /* Not URL */ | 
| 194 |  |     if(NC_testmode(uri, "bytes")) { | 
| 195 |  |         /* NC_S3SDK takes priority over NC_HTTP */ | 
| 196 |  |         if(NC_testmode(uri, "s3")) kind = NC_S3SDK; else kind = NC_HTTP; | 
| 197 |  |     } else | 
| 198 |  |         kind = 0; | 
| 199 |  |     ncurifree(uri); | 
| 200 |  |     return kind; | 
| 201 |  | } | 
| 202 |  | #endif |