diff --git a/mdstcpip/mdsip_connections.h b/mdstcpip/mdsip_connections.h index e9de943d7f..4afb047d80 100644 --- a/mdstcpip/mdsip_connections.h +++ b/mdstcpip/mdsip_connections.h @@ -332,7 +332,7 @@ EXPORT Connection *PopConnection(int id); client_t GetConnectionClientType(int id); -int GetConnectionVersion(int id); +int MdsIpGetConnectionVersion(int id); extern void FlipData(Message *m); extern void FlipHeader(MsgHdr *header); diff --git a/mdstcpip/mdsipshr/Connections.c b/mdstcpip/mdsipshr/Connections.c index 5fd2ebbefa..297614d235 100644 --- a/mdstcpip/mdsipshr/Connections.c +++ b/mdstcpip/mdsipshr/Connections.c @@ -116,13 +116,11 @@ Connection *FindConnectionSending(int id) return c; } -EXPORT int GetConnectionVersion(int id) +EXPORT int MdsIpGetConnectionVersion(int id) { MDSIPTHREADSTATIC_INIT; - int version; Connection *c = _FindConnection(id, NULL, MDSIPTHREADSTATIC_VAR); - version = c ? (int)c->version : -1; - return version; + return (c) ? (int)c->version : -1; } Connection *FindConnectionWithLock(int id, con_t state) @@ -483,6 +481,31 @@ int AddConnection(Connection *c) return c->id; } +static inline int get_max_version() +{ +#define MDSIP_MAX_VERSION "MDSIP_MAX_VERSION" + static int max_version = -1; + static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + pthread_mutex_lock(&mutex); + if (max_version < 0) + { + // if env not specified, use max + max_version = 0xFFFF; + char *tmp = getenv(MDSIP_MAX_VERSION); + if (tmp) + { + long max_long = strtol(tmp, &tmp, 0); + if (!*tmp && max_long >= 0 && max_long < max_version) + { + max_version = (int)max_long; + } + MDSMSG(MDSIP_MAX_VERSION " = %d", max_version); + } + } + pthread_mutex_unlock(&mutex); + return max_version; +} + int AcceptConnection(char *protocol, char *info_name, SOCKET readfd, void *info, size_t info_len, int *id, char **usr) { @@ -516,10 +539,12 @@ int AcceptConnection(char *protocol, char *info_name, SOCKET readfd, void *info, // SET COMPRESSION // if (STATUS_OK) { + const int max_version = get_max_version(); c->compression_level = msg->h.status & 0xf; c->client_type = msg->h.client_type; - if (msg->h.ndims > 0) - c->version = msg->h.dims[0]; + c->version = msg->h.ndims > 0 ? msg->h.dims[0] : 0; + if (c->version > max_version) + c->version = max_version; fprintf(stderr, "Connected: %s\n", user_p); } else diff --git a/mdstcpip/mdsipshr/MdsValue.c b/mdstcpip/mdsipshr/MdsValue.c index 843b7938fb..da69585666 100644 --- a/mdstcpip/mdsipshr/MdsValue.c +++ b/mdstcpip/mdsipshr/MdsValue.c @@ -59,7 +59,7 @@ EXPORT int MdsIpGetDescriptor(int id, const char *expression, int nargs, { int dim = 0; int i, status; - int version = GetConnectionVersion(id); + int version = MdsIpGetConnectionVersion(id); const int expect_serial = version >= MDSIP_VERSION_DSC_ARGS; if (expect_serial) { @@ -104,12 +104,15 @@ EXPORT int MdsIpGetDescriptor(int id, const char *expression, int nargs, status = GetAnswerInfoTS(id, (char *)&ser.dtype, (short int *)&ser.length, &ndims, dims, (int *)&ser.arsize, (void **)&ser.pointer, &mem); - ser.class = CLASS_A; - if ((expect_serial && ser.dtype == DTYPE_SERIAL) || ser.dtype == DTYPE_B) - status = MdsSerializeDscIn(ser.pointer, ans_ptr); - else - status = MdsCopyDxXd((mdsdsc_t *)&ser, ans_ptr); - free(mem); + if(STATUS_OK) + { + ser.class = CLASS_A; + if ((expect_serial && ser.dtype == DTYPE_SERIAL) || ser.dtype == DTYPE_B) + status = MdsSerializeDscIn(ser.pointer, ans_ptr); + else + status = MdsCopyDxXd((mdsdsc_t *)&ser, ans_ptr); + free(mem); + } } return status; } diff --git a/tditest/testing/Makefile.am b/tditest/testing/Makefile.am index ab5d3e4302..dfeeae685c 100644 --- a/tditest/testing/Makefile.am +++ b/tditest/testing/Makefile.am @@ -17,6 +17,7 @@ TESTS = \ test-tdishr.tdi \ test-treeshr.tdi \ test-mdsip.tdi \ + test-mdsip0.tdi \ test-mdsmisc.tdi \ test-dev-py.tdi \ test-tab.tdi @@ -45,7 +46,7 @@ VALGRIND_FLAGS = \ MOSTLYCLEANFILES = \ ./*.log ./*.tap \ tditst.tmp shotid.sys \ - main_*.characteristics* main_*.datafile* main_*.tree* + *_*.characteristics* *_*.datafile* *_*.tree* ## ////////////////////////////////////////////////////////////////////////// ## ## // TARGETS ////////////////////////////////////////////////////////////// ## ## ////////////////////////////////////////////////////////////////////////// ## @@ -53,5 +54,5 @@ MOSTLYCLEANFILES = \ #clean-local: clean-local-tests -check_PROGRAMS = +check_PROGRAMS = check_SCRIPTS = $(TESTS) diff --git a/tditest/testing/do_tditests.sh b/tditest/testing/do_tditests.sh index 07a9ae95e4..b0b7040bcd 100755 --- a/tditest/testing/do_tditests.sh +++ b/tditest/testing/do_tditests.sh @@ -24,8 +24,9 @@ status=0 run() { eval $1 2>&1 | tee ${2-/dev/null} | - grep -v -e '^[DIWE],' \ - -e '^\s*Data inserted:' \ + grep -v -e '^[DIWE],'\ + -e '^\s*Data inserted:'\ + -e '^Connected:'\ -e 'Length:' } @@ -35,7 +36,7 @@ if [ ! -z $1 ]; then # fixes [?1034h for old readline verisons, rhel5/6/7, fc17/18/20 export TERM=vt100 else - cmd="$TDITEST $zdrv$srcdir/$test.tdi 1 2 3" + cmd="$TDITEST $zdrv$srcdir/$test.tdi $zdrv$(pwd) 2 3" fi if [ -z ${MDSPLUS_DIR} ]; then diff --git a/tditest/testing/test-mdsip.ans b/tditest/testing/test-mdsip.ans index d7288f9014..a75e6fc7cf 100644 --- a/tditest/testing/test-mdsip.ans +++ b/tditest/testing/test-mdsip.ans @@ -1,5 +1,5 @@ -mdsconnect("local://1") -0 +mdsconnect("thread://0")>=0 +1BU mdsvalue("$",[1,2,3]) [1,2,3] mdsvalue("DECOMPILE($)",[[1,2],[3,4]]) @@ -10,6 +10,19 @@ mdsvalue("$",[[[[[[[[1]]]]]]]]) [[[[[[[[1]]]]]]]] mdsvalue("$",[[[[[[[[1,2]]]]]]],[[[[[[[3,4]]]]]]]]) [[[[[[[[1,2]]]]]]], [[[[[[[3,4]]]]]]]] +FOR(_i=1;_i<=3;_i++){setenv("mdsip_path=thread://"//char(0x30+_i)//"::.");treeopennew('MDSIP', _i);} +3 +treeaddnode('A', _, 0) +265389633 +treewrite() +265389633 +treeputrecord('A',RANGE(3)) +265389633 +tcl('deco A') +RANGE(3) +1 +WHILE(treeclose() & 1);mdsdisconnect() +65545 cat - > /tmp/test-mdsip.hosts << EOF unknown@* | MAP_TO_LOCAL known@1.* | SELF diff --git a/tditest/testing/test-mdsip.tdi b/tditest/testing/test-mdsip.tdi index a96fdff816..05ea6d6c87 100644 --- a/tditest/testing/test-mdsip.tdi +++ b/tditest/testing/test-mdsip.tdi @@ -1,4 +1,4 @@ -mdsconnect("local://1") +mdsconnect("thread://0")>=0 # 0 dims ; TODO: locks up next mdsvalue: CONNECTIONLIST_LOCK #mdsvalue("$",[]) # 1 dim @@ -11,6 +11,16 @@ mdsvalue("$",[[[[[[[1]]]]]]]) mdsvalue("$",[[[[[[[[1]]]]]]]]) # 8 dims dim[i] != 1 for first and last mdsvalue("$",[[[[[[[[1,2]]]]]]],[[[[[[[3,4]]]]]]]]) +FOR(_i=1;_i<=3;_i++)\ +{\ +setenv("mdsip_path=thread://"//char(0x30+_i)//"::.");\ +treeopennew('MDSIP', _i);\ +} +treeaddnode('A', _, 0) +treewrite() +treeputrecord('A',RANGE(3)) +tcl('deco A') +WHILE(treeclose() & 1);mdsdisconnect() # test CheckClient @public _hostfile = "/tmp/test-mdsip.hosts";\ fun set_hostfile(in _lines) {\ diff --git a/tditest/testing/test-mdsip0.ans b/tditest/testing/test-mdsip0.ans new file mode 100644 index 0000000000..06953cd15d --- /dev/null +++ b/tditest/testing/test-mdsip0.ans @@ -0,0 +1,27 @@ +setenv("MDSIP_MAX_VERSION=0") +65545 +mdsconnect("thread://0")>=0 +1BU +mdsvalue("$",[1,2,3]) +[1,2,3] +mdsvalue("DECOMPILE($)",[[1,2],[3,4]]) +"[[1,2], [3,4]]" +mdsvalue("$",[[[[[[[1]]]]]]]) +[[[[[[[1]]]]]]] +mdsvalue("$",[[[[[[[[1]]]]]]]]) +[[[[[[[[1]]]]]]]] +mdsvalue("$",[[[[[[[[1,2]]]]]]],[[[[[[[3,4]]]]]]]]) +[[[[[[[[1,2]]]]]]], [[[[[[[3,4]]]]]]]] +FOR(_i=1;_i<=3;_i++){setenv("mdsip0_path=thread://"//char(0x30+_i)//"::.");treeopennew('MDSIP0', _i);} +3 +treeaddnode('A', _, 0) +265389633 +treewrite() +265389633 +treeputrecord('A',RANGE(3)) +265389633 +tcl('deco A') +RANGE(3) +1 +WHILE(treeclose() & 1);mdsdisconnect() +65545 diff --git a/tditest/testing/test-mdsip0.tdi b/tditest/testing/test-mdsip0.tdi new file mode 100644 index 0000000000..37ee852993 --- /dev/null +++ b/tditest/testing/test-mdsip0.tdi @@ -0,0 +1,24 @@ +setenv("MDSIP_MAX_VERSION=0") +mdsconnect("thread://0")>=0 +# 0 dims ; TODO: locks up next mdsvalue: CONNECTIONLIST_LOCK +#mdsvalue("$",[]) +# 1 dim +mdsvalue("$",[1,2,3]) +# 2 dims; deco +mdsvalue("DECOMPILE($)",[[1,2],[3,4]]) +# 7 dims +mdsvalue("$",[[[[[[[1]]]]]]]) +# 8 dims dim[i] == 1 for all i<8 +mdsvalue("$",[[[[[[[[1]]]]]]]]) +# 8 dims dim[i] != 1 for first and last +mdsvalue("$",[[[[[[[[1,2]]]]]]],[[[[[[[3,4]]]]]]]]) +FOR(_i=1;_i<=3;_i++)\ +{\ +setenv("mdsip0_path=thread://"//char(0x30+_i)//"::.");\ +treeopennew('MDSIP0', _i);\ +} +treeaddnode('A', _, 0) +treewrite() +treeputrecord('A',RANGE(3)) +tcl('deco A') +WHILE(treeclose() & 1);mdsdisconnect() diff --git a/tditest/testing/test-tdishr.ans b/tditest/testing/test-tdishr.ans index ed520cfcfb..80abe6ea71 100644 --- a/tditest/testing/test-tdishr.ans +++ b/tditest/testing/test-tdishr.ans @@ -1,7 +1,7 @@ EXTRACT(len(_$0)-16,16,_$0) "/test-tdishr.tdi" -deallocate(_$0) -1 +deallocate(_$0)+deallocate(_$1) +2 _a=1 1 allocated(_a) @@ -10,11 +10,10 @@ show_private(_a) Private _A = 1 1 show_private() -Private _$1 = "1" Private _$2 = "2" Private _$3 = "3" Private _A = 1 -4 +3 deallocate() * allocated(_a) diff --git a/tditest/testing/test-tdishr.tdi b/tditest/testing/test-tdishr.tdi index 515035d6a1..7ebc26b560 100644 --- a/tditest/testing/test-tdishr.tdi +++ b/tditest/testing/test-tdishr.tdi @@ -1,7 +1,7 @@ # allocations _$0 should end with "/test-tdishr.tdi" EXTRACT(len(_$0)-16,16,_$0) -# deallocate _$0 as it varies with os in tests -deallocate(_$0) +# deallocate _$0 and _$1 as they vary with os in tests +deallocate(_$0)+deallocate(_$1) _a=1 allocated(_a) show_private(_a) diff --git a/treeshr/RemoteAccess.c b/treeshr/RemoteAccess.c index 19ac223359..6864c93096 100644 --- a/treeshr/RemoteAccess.c +++ b/treeshr/RemoteAccess.c @@ -78,7 +78,7 @@ static inline char *replaceBackslashes(char *filename) return filename; } -static int remote_connect(char *server, int inc_count) +static int remote_connect(char *server) { #define CONMSG(TYP, PRI, ...) TYP("Host(conid=%d, links=?, unique='%s'), server='%s'" PRI, conid, unique, server, __VA_ARGS__) int conid = -1; @@ -120,7 +120,7 @@ static int remote_connect(char *server, int inc_count) { host = malloc(sizeof(Host)); host->conid = conid; - host->links = !!inc_count; + host->links = 1; host->unique = strdup(unique); host->next = TREE_HOSTLIST; TREE_HOSTLIST = host; @@ -246,7 +246,7 @@ int ConnectTreeRemote(PINO_DATABASE *dblist, char const *tree, int conid; logname[strlen(logname) - 2] = '\0'; int status = TreeSUCCESS; - conid = remote_connect(logname, 1); + conid = remote_connect(logname); if (conid != -1) { status = tree_open(dblist, conid, subtree_list ? subtree_list : tree); @@ -282,7 +282,7 @@ int ConnectTreeRemote(PINO_DATABASE *dblist, char const *tree, } } else - remote_disconnect(conid, 0); + remote_disconnect(conid, B_FALSE); } else status = TreeCONNECTFAIL; @@ -763,29 +763,28 @@ int PutRecordRemote(PINO_DATABASE *dblist, int nid_in, struct descriptor *dsc, int utility_update) { int status; - EMPTYXD(ans); + EMPTYXD(xd); char exp[80]; if (dsc) { sprintf(exp, "TreeShr->TreePutRecord(val(%d),xd($),val(%d))", nid_in, utility_update); - status = MdsValueDsc(dblist->tree_info->channel, exp, dsc, &ans, NULL); + status = MdsValueDsc(dblist->tree_info->channel, exp, dsc, &xd, NULL); } else { sprintf(exp, "TreeShr->TreePutRecord(val(%d),val(0),val(%d))", nid_in, utility_update); - status = MdsValueDsc(dblist->tree_info->channel, exp, &ans, NULL); + status = MdsValueDsc(dblist->tree_info->channel, exp, &xd, NULL); } - if (ans.pointer) + if (xd.pointer) { - if (ans.pointer->dtype == DTYPE_L) - status = *(int *)ans.pointer->pointer; + if (xd.pointer->dtype == DTYPE_L) + status = *(int *)xd.pointer->pointer; else if (STATUS_OK) status = 0; - MdsFree1Dx(&ans, NULL); } - MdsIpFreeDsc(&ans); + MdsIpFreeDsc(&xd); return status; } @@ -926,13 +925,14 @@ int TreeTurnOffRemote(PINO_DATABASE *dblist, int nid) int TreeGetCurrentShotIdRemote(const char *treearg, char *path, int *shot) { int status = TreeFAILURE; - int channel = remote_connect(path, 0); - if (channel > 0) + int conid = remote_connect(path); + if (conid > 0) { struct descrip ans = {0}; struct descrip tree = STR2DESCRIP(treearg); - status = MdsValue(channel, "TreeShr->TreeGetCurrentShotId(ref($))", &tree, + status = MdsValue(conid, "TreeShr->TreeGetCurrentShotId(ref($))", &tree, &ans, NULL); + remote_disconnect(conid, B_FALSE); if (ans.ptr) { if (ans.dtype == DTYPE_L) @@ -948,14 +948,15 @@ int TreeGetCurrentShotIdRemote(const char *treearg, char *path, int *shot) int TreeSetCurrentShotIdRemote(const char *treearg, char *path, int shot) { int status = 0; - int channel = remote_connect(path, 0); - if (channel > 0) + int conid = remote_connect(path); + if (conid > 0) { struct descrip ans = {0}; struct descrip tree = STR2DESCRIP(treearg); char exp[64]; sprintf(exp, "TreeShr->TreeSetCurrentShotId(ref($),val(%d))", shot); - status = MdsValue(channel, exp, &tree, &ans, NULL); + status = MdsValue(conid, exp, &tree, &ans, NULL); + remote_disconnect(conid, B_FALSE); if (ans.ptr) { status = (ans.dtype == DTYPE_L) ? *(int *)ans.ptr : 0; @@ -1127,7 +1128,7 @@ static inline int mds_io_request(int conid, mds_io_mode idx, size_t size, if (idx != MDS_IO_CLOSE_K) fprintf(stderr, "Error in GetAnswerInfoTS: mode = %d, status = %d\n", idx, status); - remote_disconnect(conid, 0); + remote_disconnect(conid, B_FALSE); } } return status; @@ -1194,7 +1195,7 @@ inline static int io_open_remote(char *host, char *filename, int options, if (options & O_RDWR) mdsio.open.options |= MDS_IO_O_RDWR; if (*conid == -1) - *conid = remote_connect(host, 1); + *conid = remote_connect(host); if (*conid != -1) { fd = @@ -1261,7 +1262,7 @@ inline static int io_close_remote(int conid, int fd) int status = MdsIoRequest(conid, MDS_IO_CLOSE_K, sizeof(mdsio.close), &mdsio, NULL, &len, &dout, &msg); if (STATUS_OK) - remote_disconnect(conid, 0); + remote_disconnect(conid, B_FALSE); if (STATUS_OK && sizeof(int) == len) { ret = *(int *)dout; @@ -1574,7 +1575,7 @@ inline static int io_exists_remote(char *host, char *filename) { int ret; INIT_AND_FREE_ON_EXIT(void *, msg); - int conid = remote_connect(host, 1); + int conid = remote_connect(host); if (conid != -1) { mdsio_t mdsio = {.exists = {.length = strlen(filename) + 1}}; @@ -1582,6 +1583,7 @@ inline static int io_exists_remote(char *host, char *filename) char *dout; int status = MdsIoRequest(conid, MDS_IO_EXISTS_K, sizeof(mdsio.exists), &mdsio, filename, &len, &dout, &msg); + remote_disconnect(conid, B_FALSE); if (STATUS_OK && len == sizeof(int)) ret = *(int *)dout; else @@ -1615,7 +1617,7 @@ inline static int io_remove_remote(char *host, char *filename) { int ret; INIT_AND_FREE_ON_EXIT(void *, msg); - int conid = remote_connect(host, 1); + int conid = remote_connect(host); if (conid != -1) { mdsio_t mdsio = {.remove = {.length = strlen(filename) + 1}}; @@ -1623,6 +1625,7 @@ inline static int io_remove_remote(char *host, char *filename) char *dout; int status = MdsIoRequest(conid, MDS_IO_REMOVE_K, sizeof(mdsio.remove), &mdsio, filename, &len, &dout, &msg); + remote_disconnect(conid, B_FALSE); if (STATUS_OK && len == sizeof(int)) ret = *(int *)dout; else @@ -1653,7 +1656,7 @@ inline static int io_rename_remote(char *host, char *filename_old, { int ret; int conid; - conid = remote_connect(host, 1); + conid = remote_connect(host); if (conid != -1) { INIT_AND_FREE_ON_EXIT(char *, names); @@ -1666,6 +1669,7 @@ inline static int io_rename_remote(char *host, char *filename_old, char *dout; int status = MdsIoRequest(conid, MDS_IO_RENAME_K, sizeof(mdsio.rename), &mdsio, names, &len, &dout, &msg); + remote_disconnect(conid, B_FALSE); if (STATUS_OK && len == sizeof(int)) ret = *(int *)dout; else @@ -1801,15 +1805,15 @@ inline static int io_open_one_remote(char *host, char *filepath, int *enhanced) { int status; - static int (*GetConnectionVersion)(int) = NULL; - status = LibFindImageSymbol_C("MdsIpShr", "GetConnectionVersion", - &GetConnectionVersion); + static int (*MdsIpGetConnectionVersion)(int) = NULL; + status = LibFindImageSymbol_C("MdsIpShr", "MdsIpGetConnectionVersion", + &MdsIpGetConnectionVersion); do { - *conid = remote_connect(host, 1); + *conid = remote_connect(host); if (*conid != -1) { - if (GetConnectionVersion(*conid) < MDSIP_VERSION_OPEN_ONE) + if (MdsIpGetConnectionVersion(*conid) < MDSIP_VERSION_OPEN_ONE) { if (*filepath && !strstr(filepath, "::")) { diff --git a/treeshr/TreeAddNode.c b/treeshr/TreeAddNode.c index edf47046a7..6e4575e70b 100644 --- a/treeshr/TreeAddNode.c +++ b/treeshr/TreeAddNode.c @@ -890,6 +890,7 @@ int _TreeWriteTree(void **dbid, char const *exp_ptr, int shotid) ((size_t)info_ptr->header->externals * 4u + 511u) / 512u; strcat(nfilenam, "#"); ntreefd = MDS_IO_OPEN(nfilenam, O_WRONLY | O_CREAT | O_TRUNC, 0664); +#define GOTO_ERROR_CLOSE do{MDS_IO_CLOSE(ntreefd);goto error_exit;}while(0) if (ntreefd != -1) { status = MDSplusERROR; @@ -899,24 +900,22 @@ int _TreeWriteTree(void **dbid, char const *exp_ptr, int shotid) FreeHeaderOut(header); status = TreeWRITETREEERR; if (num != (ssize_t)(header_pages * 512)) - { - goto error_exit; - } + GOTO_ERROR_CLOSE; num = MDS_IO_WRITE(ntreefd, info_ptr->node, 512 * node_pages); if (num != (ssize_t)(node_pages * 512)) - goto error_exit; + GOTO_ERROR_CLOSE; num = MDS_IO_WRITE(ntreefd, info_ptr->tags, 512 * tags_pages); if (num != (ssize_t)(tags_pages * 512)) - goto error_exit; + GOTO_ERROR_CLOSE; num = MDS_IO_WRITE(ntreefd, info_ptr->tag_info, 512 * tag_info_pages); if (num != (ssize_t)(tag_info_pages * 512)) - goto error_exit; + GOTO_ERROR_CLOSE; num = MDS_IO_WRITE(ntreefd, info_ptr->external, 512 * external_pages); if (num != (ssize_t)(external_pages * 512)) - goto error_exit; + GOTO_ERROR_CLOSE; status = TreeWriteNci(info_ptr); if (STATUS_NOT_OK) - goto error_exit; + GOTO_ERROR_CLOSE; status = TreeSUCCESS; if (info_ptr->channel > -1) status = MDS_IO_CLOSE(info_ptr->channel); diff --git a/treeshr/TreeOpen.c b/treeshr/TreeOpen.c index b6680c872d..f38ff6de52 100644 --- a/treeshr/TreeOpen.c +++ b/treeshr/TreeOpen.c @@ -73,7 +73,7 @@ inline static char *strndup(const char *src, size_t n) int treeshr_errno = 0; extern int MDSEventCan(); static void RemoveBlanksAndUpcase(char *out, char const *in); -static int CloseTopTree(PINO_DATABASE *dblist, int call_hook); +static int close_top_tree(PINO_DATABASE *dblist, int call_hook); static int ConnectTree(PINO_DATABASE *dblist, char *tree, NODE *parent, char *subtree_list); static int CreateDbSlot(PINO_DATABASE **dblist, char *tree, int shot, @@ -317,7 +317,7 @@ int _TreeClose(void **dbid, char const *tree, int shot) } else if ((*dblist)->open) { - status = CloseTopTree(*dblist, 1); + status = close_top_tree(*dblist, 1); if (STATUS_OK) free_top_db(dblist); } @@ -328,7 +328,7 @@ int _TreeClose(void **dbid, char const *tree, int shot) return status; } -static int CloseTopTree(PINO_DATABASE *dblist, int call_hook) +static int close_top_tree(PINO_DATABASE *dblist, int call_hook) { int status = TreeSUCCESS; if (!dblist) @@ -396,7 +396,6 @@ static int CloseTopTree(PINO_DATABASE *dblist, int call_hook) } free(local_info->edit); } - /******************************************************** For each tree in the linked list, first the pages must be remapped to the pagefile using sys$cretva before they can @@ -733,7 +732,7 @@ static int CreateDbSlot(PINO_DATABASE **dblist, char *tree, int shot, case CLOSE: move_to_top(prev_db, db); - CloseTopTree(*dblist, 1); + close_top_tree(*dblist, 1); status = TreeSUCCESS; break; case ERROR_DIRTY: @@ -762,7 +761,7 @@ static int CreateDbSlot(PINO_DATABASE **dblist, char *tree, int shot, if (useable_db) { move_to_top(saved_prev_db, useable_db); - CloseTopTree(*dblist, 1); + close_top_tree(*dblist, 1); status = TreeSUCCESS; } else @@ -1254,22 +1253,26 @@ int TreeReopenDatafile(struct tree_info *info) int TreeReopenNci(struct tree_info *info) { - int status = 1, reopen_get, reopen_put; + int status = 1; WRLOCKINFO(info); if (info->nci_file) { - reopen_get = info->nci_file->get > 0; - reopen_put = info->nci_file->put > 0; - if (reopen_get) - MDS_IO_CLOSE(info->nci_file->get); - if (reopen_put) - MDS_IO_CLOSE(info->nci_file->put); + int reopen_get = info->nci_file->get; + int reopen_put = info->nci_file->put; free(info->nci_file); info->nci_file = 0; if (reopen_get) + { status = _TreeOpenNciR(info); - if (STATUS_OK && reopen_put) - status = _TreeOpenNciW(info, 0); + MDS_IO_CLOSE(reopen_get); + } + if (reopen_put) + { + if (STATUS_OK) + status = _TreeOpenNciW(info, 0); + MDS_IO_CLOSE(reopen_put); + } + } UNLOCKINFO(info); return status; @@ -1353,6 +1356,7 @@ int _TreeOpenEdit(void **dbid, char const *tree_in, int shot_in) int _TreeOpenNew(void **dbid, char const *tree_in, int shot_in) { + int fd_keepalive = -1; // in case tree is remote TREE_INFO *info; char *tree = strdup(tree_in); int shot; @@ -1381,17 +1385,22 @@ int _TreeOpenNew(void **dbid, char const *tree_in, int shot_in) &info->filespec, &fd); if (fd > -1) { - MDS_IO_CLOSE(fd); + fd_keepalive = fd; info->channel = -2; status = OpenOne(info, (*dblist)->tree_info, TREE_NCIFILE_TYPE, 1, 0, NULL, &fd); + if (fd > -1) { - MDS_IO_CLOSE(fd); + MDS_IO_CLOSE(fd_keepalive); + fd_keepalive = fd; status = OpenOne(info, (*dblist)->tree_info, TREE_DATAFILE_TYPE, 1, 0, NULL, &fd); if (fd > -1) - MDS_IO_CLOSE(fd); + { + MDS_IO_CLOSE(fd_keepalive); + fd_keepalive = fd; + } } } if (STATUS_OK) @@ -1452,7 +1461,10 @@ int _TreeOpenNew(void **dbid, char const *tree_in, int shot_in) } } if (STATUS_OK) + { _TreeWriteTree(dbid, 0, 0); + MDS_IO_CLOSE(fd_keepalive); + } return status; } @@ -1463,7 +1475,7 @@ void TreeFreeDbid(void *dbid) MDSDBG("Destroyed DB %" PRIxPTR "\n", (uintptr_t)dbid); PINO_DATABASE *db = (PINO_DATABASE *)dbid; TreeFreeDbid(db->next); - CloseTopTree(db, 1); + close_top_tree(db, 1); free_xd(&db->timecontext.start); free_xd(&db->timecontext.end); free_xd(&db->timecontext.delta); diff --git a/treeshr/TreePutRecord.c b/treeshr/TreePutRecord.c index f45c99d46d..44cb67fb26 100644 --- a/treeshr/TreePutRecord.c +++ b/treeshr/TreePutRecord.c @@ -457,22 +457,25 @@ int _TreeOpenDatafileW(TREE_INFO *info, int *stv_ptr, int tmpfile) } if (STATUS_OK) { - int old_get = df_ptr->get; char *filename = tree_to_datafile(info->filespec, tmpfile); + int old_fd = df_ptr->get; df_ptr->get = MDS_IO_OPEN( filename, tmpfile ? O_RDWR | O_CREAT | O_TRUNC | O_EXCL : O_RDONLY, 0664); status = (df_ptr->get == -1) ? TreeFAILURE : TreeSUCCESS; if (df_ptr->get == -1) - df_ptr->get = old_get; - else if (df_ptr->get > 0) - MDS_IO_CLOSE(old_get); + df_ptr->get = old_fd; + else if (old_fd > 0) + MDS_IO_CLOSE(old_fd); if (STATUS_OK) { + old_fd = df_ptr->put; df_ptr->put = MDS_IO_OPEN(filename, O_RDWR, 0); status = (df_ptr->put == -1) ? TreeFAILURE : TreeSUCCESS; if (df_ptr->put == -1) - df_ptr->put = 0; + df_ptr->put = old_fd; + else if (old_fd > 0) + MDS_IO_CLOSE(old_fd); if (STATUS_OK) df_ptr->open_for_write = 1; }