From 0ec1a0bf80ad09587672a5a13d21971a65785b0e Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Thu, 27 Dec 2018 17:13:51 +0000 Subject: [PATCH] SUP-2586: Contribution for Windows (memfs part) --- memfs.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/memfs.py b/memfs.py index 88c41dbe6..01f71c754 100755 --- a/memfs.py +++ b/memfs.py @@ -8,7 +8,7 @@ import binascii assert len(sys.argv) > 2 # For now exclude GRIB3 as it is still experimental -EXCLUDED = 'grib3' +EXCLUDED = ['grib3'] dirs = [os.path.realpath(x) for x in sys.argv[1:-1]] print(dirs) @@ -35,17 +35,19 @@ for directory in dirs: NAMES.append(dname) for dirpath, dirnames, files in os.walk(directory, followlinks=True): - if EXCLUDED in dirnames: - print('Note: %s/%s will not be included.' % (dirpath,EXCLUDED)) + for ex in EXCLUDED: + if ex in dirnames: + print('Note: %s/%s will not be included.' % (dirpath,ex)) # Prune the walk by modifying the dirnames in-place - dirnames[:] = [dirname for dirname in dirnames if dirname != EXCLUDED] + dirnames[:] = [dirname for dirname in dirnames if dirname not in EXCLUDED] for name in files: full = '%s/%s' % (dirpath, name) _, ext = os.path.splitext(full) if ext not in ['.def', '.table', '.tmpl']: continue + full = full.replace("\\","/") fname = full[full.find("/%s/" % (dname,)):] #print("MEMFS add", fname) name = re.sub(r'\W', '_', fname) @@ -171,6 +173,49 @@ static FILE* fmemopen(const char* buffer, size_t size, const char* mode){ return funopen(f, &read_mem, &write_mem, &seek_mem, &close_mem); } +#elif defined(ECCODES_ON_WINDOWS) + +#include +#include +#include + +static FILE *fmemopen(void* buffer, size_t size, const char* mode) { + char path[MAX_PATH - 13]; + if (!GetTempPath(sizeof(path), path)) + return NULL; + + char filename[MAX_PATH + 1]; + if (!GetTempFileName(path, "eccodes", 0, filename)) + return NULL; + + HANDLE h = CreateFile(filename, + GENERIC_READ | GENERIC_WRITE, + 0, + NULL, + OPEN_ALWAYS, + FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, + NULL); + + if (h == INVALID_HANDLE_VALUE) + return NULL; + + int fd = _open_osfhandle((intptr_t)h, _O_RDWR); + if (fd < 0) { + CloseHandle(h); + return NULL; + } + + FILE* f = _fdopen(fd, "w+"); + if (!f) { + _close(fd); + return NULL; + } + + fwrite(buffer, size, 1, f); + rewind(f); + return f; +} + #endif static size_t entries_count = sizeof(entries)/sizeof(entries[0]);