mdsplus icon indicating copy to clipboard operation
mdsplus copied to clipboard

The ~n expansion in paths is adding a trailing "+" character

Open mwinkel-cfs opened this issue 2 years ago • 3 comments

This issue was originally found in the old-trees branch. But also confirmed to be in the alpha branch.

Unclear if this is a bug in the source code or just an omission in the Wiki's documentation. If it is a bug in the source code, note that removing the trailing "+" might break the few customers that are relying on that behavior. Note that the documentation does not mention a trailing "+". https://www.mdsplus.org/index.php/Documentation:TreeAccess

The trailing "+" is added in "treeshr/TreeOpen.c" by the GetName() function (as shown by the following excerpt).

  if (STATUS_OK)
    status = (*TdiExecute)(&expression_d, &fname MDS_END_ARG);
  if (STATUS_OK)
  {
    ans = strncpy(malloc((size_t)fname.length + 2), fname.pointer,
                  fname.length);
    ans[fname.length] = '+';
    ans[fname.length + 1] = 0;
  }

Admittedly, this issue is an edge case scenario. Most sites will not use the ~n expansion in the manner shown below. (Stumbled across this issue just because was experimenting with the various tilde expansions done by MDSplus.) Here is a simple script that shows how to trigger the issue.

$ mkdir ~/fig_demo
$ export default_tree_path=~/fig_demo
$
$ mdstcl
TCL> edit fig /new
TCL> add node xyz/usage=text
TCL> write
TCL> close
TCL> set tree fig
TCL> create pulse 101
TCL> create pulse 102
TCL> create pulse 103
TCL> create pulse 104
TCL> quit
$
$cd ~/fig_demo
$ ls
fig_101.characteristics  fig_102.characteristics  fig_103.characteristics  fig_104.characteristics  fig_model.characteristics
fig_101.datafile         fig_102.datafile         fig_103.datafile         fig_104.datafile         fig_model.datafile
fig_101.tree             fig_102.tree             fig_103.tree             fig_104.tree             fig_model.tree
$
$ mkdir model shot_101 shot_102 shot_103+ shot_104+
$ mv *_model* model
$ mv fig_101* shot_101
$ mv fig_102* shot_102
$ mv fig_103* shot_103+
$ mv fig_104* shot_104+
$
$ ls */*
model/fig_model.characteristics   shot_101/fig_101.tree              shot_103+/fig_103.datafile
model/fig_model.datafile          shot_102/fig_102.characteristics   shot_103+/fig_103.tree
model/fig_model.tree              shot_102/fig_102.datafile          shot_104+/fig_104.characteristics
shot_101/fig_101.characteristics  shot_102/fig_102.tree              shot_104+/fig_104.datafile
shot_101/fig_101.datafile         shot_103+/fig_103.characteristics  shot_104+/fig_104.tree
$

$ # Now use an editor to create the "fig_tree_filename.fun" file.

$ cat fig_tree_filename.fun
PUBLIC FUN FIG_TREE_FILENAME(IN _shot) {
   _fname = "shot_" // TEXT(_shot, 3);
   RETURN(_fname);
}
$
$ cp fig_tree_filename.fun $MDSPLUS_DIR/tdi
$ cd ~
$
$ export default_tree_path=~/fig_demo/~n/
$
$ # Note can only open the two shot folders that have the trailing "+".
$ mdstcl
TCL> set tree fig /shot=101
Error: Failed to open tree 'fig' shot 101
Error message was: %TREE-E-FOPENR, Error opening file read-only.
mdsdcl: --> failed on line 'set tree fig /shot=101'
TCL>
TCL> set tree fig /shot=102
Error: Failed to open tree 'fig' shot 102
Error message was: %TREE-E-FOPENR, Error opening file read-only.
mdsdcl: --> failed on line 'set tree fig /shot=102'
TCL>
TCL> set tree fig /shot=103
TCL> dir

\FIG::TOP

 :XYZ

Total of 1 node.
TCL>
TCL>
TCL> set tree fig /shot=104
TCL> dir

\FIG::TOP

 :XYZ

Total of 1 node.
TCL>
TCL> quit

mwinkel-cfs avatar Jul 26 '22 20:07 mwinkel-cfs

I think I need a verbal explanation about this. I did not know anything about the plus science

Sent from my iPhone

On Jul 26, 2022, at 4:04 PM, Mark Winkel @.***> wrote:

 This issue was originally found in the old-trees branch. But also confirmed to be in the alpha branch.

Unclear if this is a bug in the source code or just an omission in the Wiki's documentation. If it is a bug in the source code, note that removing the trailing "+" might break the few customers that are relying on that behavior.

The trailing "+" is added in "tresshr/TreeOpen.c" by the GetName() function (as shown by the following excerpt).

if (STATUS_OK) status = (*TdiExecute)(&expression_d, &fname MDS_END_ARG); if (STATUS_OK) { ans = strncpy(malloc((size_t)fname.length + 2), fname.pointer, fname.length); ans[fname.length] = '+'; ans[fname.length + 1] = 0; } Admittedly, this is bug is an edge case scenario. Most sites will not use the ~n expansion in the manner shown below. (Stumbled across this issue just because was experimenting with the various tilde expansions done by MDSplus.) Here is a simple script that shows how to trigger the issue.

$ mkdir ~/fig_demo $ export default_tree_path=~/fig_demo $ $ mdstcl TCL> edit fig /new TCL> add node xyz/usage=text TCL> write TCL> close TCL> set tree fig TCL> create pulse 101 TCL> create pulse 102 TCL> create pulse 103 TCL> create pulse 104 TCL> quit $ $cd ~/fig_demo $ ls fig_101.characteristics fig_102.characteristics fig_103.characteristics fig_104.characteristics fig_model.characteristics fig_101.datafile fig_102.datafile fig_103.datafile fig_104.datafile fig_model.datafile fig_101.tree fig_102.tree fig_103.tree fig_104.tree fig_model.tree $ $ mkdir model shot_101 shot_102 shot_103+ shot_104+ $ mv _model model $ mv fig_101* shot_101 $ mv fig_102* shot_102 $ mv fig_103* shot_103+ $ mv fig_104* shot_104+ $ $ ls / model/fig_model.characteristics shot_101/fig_101.tree shot_103+/fig_103.datafile model/fig_model.datafile shot_102/fig_102.characteristics shot_103+/fig_103.tree model/fig_model.tree shot_102/fig_102.datafile shot_104+/fig_104.characteristics shot_101/fig_101.characteristics shot_102/fig_102.tree shot_104+/fig_104.datafile shot_101/fig_101.datafile shot_103+/fig_103.characteristics shot_104+/fig_104.tree $

$ # Now use an editor to create the "fig_tree_filename.fun" file.

$ cat fig_tree_filename.fun PUBLIC FUN FIG_TREE_FILENAME(IN _shot) { fname = "shot" // TEXT(_shot, 3); RETURN(_fname); } $ $ cp fig_tree_filename.fun $MDSPLUS_DIR/tdi $ cd ~ $ $ export default_tree_path=~/fig_demo/~n/ $ $ # Note can only open the two shot folders that have the trailing "+". $ mdstcl TCL> set tree fig /shot=101 Error: Failed to open tree 'fig' shot 101 Error message was: %TREE-E-FOPENR, Error opening file read-only. mdsdcl: --> failed on line 'set tree fig /shot=101' TCL> TCL> set tree fig /shot=102 Error: Failed to open tree 'fig' shot 102 Error message was: %TREE-E-FOPENR, Error opening file read-only. mdsdcl: --> failed on line 'set tree fig /shot=102' TCL> TCL> set tree fig /shot=103 TCL> dir

\FIG::TOP

:XYZ

Total of 1 node. TCL> TCL> TCL> set tree fig /shot=104 TCL> dir

\FIG::TOP

:XYZ

Total of 1 node. TCL> TCL> quit — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.

joshStillerman avatar Jul 26 '22 20:07 joshStillerman

Based on the history for treeshr/TreeOpen.c, it appears that the trailing "+" was present in GetFname() when the ~n expansion was originally added 21 years ago. See commit #150e93de57, 22-Jan-2001, "Add ~n to filemask". If this analysis is correct, then it likely implies that few sites are using the ~n feature (as the issue with the trailing "+" would surely have been reported years ago).

mwinkel-cfs avatar Jul 27 '22 04:07 mwinkel-cfs

On 29-Jul-2022, Josh sent a message stating that I should fix the issue in three branches: alpha, stable and old-trees.

mwinkel-cfs avatar Aug 01 '22 12:08 mwinkel-cfs