Loading...
Searching...
No Matches
dirlist.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Lists the folders below a parent, for the deploy screens
4 @details
5 A blank parent is the platform root, which on Windows is answered with the
6 drives that exist - that is mp_dirlist's job, so no filesystem logic lives
7 here. DEPTH (1-3, default 1) is how many levels come back, so a parent with
8 tens of thousands of subfolders is never transferred in full, and MAXOBS
9 (default 2000) caps the rows with a TRUNCATED flag. Only rows mp_dirlist
10 classified as a folder or as `locked` come back; a `locked` entry is one that
11 could not be opened, kept so the administrator can see it is there.
12
13 The parent arrives in the adapter table and is read back with symget, so its
14 characters are data: a value over 1000 characters, one carrying a control
15 character, or one that is not an absolute path is refused, and the shape is
16 then checked with mp_validatecol(ISPATH).
17
18 Access is whatever the platform grants on the admin folder: the deploy
19 screen calls it before the app is configured, so the service itself carries
20 no gate.
21
22 <h4> Data Inputs </h4>
23 <h5> fromjs </h5>
24 |PARENT:$1000.|DEPTH:best.|MAXOBS:best.|
25 |---|---|---|
26 |`/tmp`|1|2000|
27
28 <h4> Data Outputs </h4>
29 <h5> dirinfo </h5>
30 |PARENT:$1000.|SEP:$1.|DEPTH:best.|TRUNCATED:$1.|PLATFORM:$3.|
31 |---|---|---|---|---|
32 |`/tmp`|`/`|1|`N`|`UNIX`|
33
34 <h5> dirlist </h5>
35 |FILEPATH:$2000.|FILENAME:$255.|FILE_OR_FOLDER:$6.|LEVEL:best.|MSG:$200.|
36 |---|---|---|---|---|
37 |`/tmp/dc`|`dc`|`folder`|0| |
38
39 <h4> SAS Macros </h4>
40 @li mp_abort.sas
41 @li mp_init.sas
42 @li mp_dirlist.sas
43 @li mp_validatecol.sas
44
45 @version 9.3
46 @author 4GL Apps Ltd
47 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
48 and may not be re-distributed or re-sold without the express permission of
49 4GL Apps Ltd.
50
51 @sasjslint {"maxLineLength": 80}
52
53**/
54
55
56%global br_parent br_depth br_maxobs br_bad br_trunc;
57%let br_parent=;
58%let br_depth=1;
59%let br_maxobs=2000;
60%let br_bad=0;
61%let br_trunc=N;
62
63%mp_init()
64
65/* the request tables arrive as sasjs params; FETCH puts them in WORK */
66%webout(FETCH)
67
68%mp_abort(iftrue=(%sysfunc(exist(work.fromjs))=0)
69 ,mac=&_program
70 ,msg=%str(Missing input table)
71)
72
73/*
74 Refuse a parent that could break out of the statements it is passed into,
75 then check that what is left is an absolute path this platform accepts.
76 The value is read from the table, so it is never resolved as macro text.
77*/
78data _null_;
79 set work.fromjs(obs=1);
80 length _parent $32767;
81 _parent=strip(coalescec(cats(parent),''));
82 _bad=0;
83
84 if length(_parent)>1000 then _bad=1;
85 if anycntrl(_parent)>0 then _bad=1;
86 /* only what can break the platform or the statement: a quote, a comma or a
87 parenthesised folder name ("C:\Program Files (x86)") is a legal path, and
88 refusing it stops a site being deployed there. The value reaches mp_dirlist
89 as data below, via %superq, so no character in it is read as macro text. */
90
91 /* blank is the root, so only a supplied parent is shape checked. The flag
92 starts at 0 so a core release without the ISPATH rule refuses the path
93 rather than accepting it - the value comes from the request */
94 if not missing(_parent) then do;
95 is_path=0;
96 %mp_validatecol(_parent,ISPATH,is_path)
97 if is_path=0 then _bad=1;
98 end;
99
100 _d=input(cats(depth),best.);
101 if _d=. or _d<1 then _d=1;
102 if _d>3 then _d=3;
103 _m=input(cats(maxobs),best.);
104 if _m=. or _m<1 then _m=2000;
105 if _m>10000 then _m=10000;
106
107 if not missing(_parent) then call symputx('br_parent',_parent);
108 call symputx('br_depth',put(_d,best.));
109 call symputx('br_maxobs',put(_m,best.));
110 call symputx('br_bad',put(_bad,best.));
111run;
112
113%mp_abort(iftrue=(&br_bad=1)
114 ,mac=&_program
115 ,msg=%str(Invalid parent)
116)
117
118%mp_dirlist(path=%superq(br_parent)
119 ,outds=work.dirraw
120 ,maxdepth=%eval(&br_depth-1)
121)
122
123/*
124 The picker only offers folders; one that cannot be opened is kept so the
125 administrator can see it is there. A blank parent has already been turned
126 into the platform roots by mp_dirlist.
127*/
128/* only the five columns in the header above leave the service; _kept is the
129 row counter, not part of the listing */
130data work.dirlist(keep=filepath filename file_or_folder level msg);
131 set work.dirraw(where=(upcase(file_or_folder) in ('FOLDER','LOCKED')));
132 retain _kept 0;
133 _kept=_kept+1;
134 if _kept>&br_maxobs then do;
135 call symputx('br_trunc','Y');
136 stop;
137 end;
138run;
139
140/*
141 mp_dirlist numbers a folder's children with the folder's own level, so the
142 contents of a non-blank PARENT arrive at level 0 - the level the picker
143 reserves for the parent row it does not draw (it keeps LEVEL>0 for a non-root
144 listing). Shift the children to start at 1, or the picker is handed rows it
145 discards and a subfolder can never be browsed. A blank parent lists the
146 platform roots, which are the top level and stay at 0.
147*/
148/* one for a non-blank parent, none for the platform roots - as data, because
149 open-code %if needs SAS 9.4M5 */
150%let br_shift=%eval(%length(%superq(br_parent))>0);
151data work.dirlist;
152 set work.dirlist;
153 level=level+&br_shift;
154run;
155
156/* a predictable order makes the picker stable between calls */
157proc sort data=work.dirlist;
158 by filepath;
159run;
160
161data work.dirinfo;
162 length parent $1000 sep $1 truncated $1 platform $3;
163 parent=symget('br_parent');
164 platform=ifc("%upcase(&sysscp)" in ('WIN','DNTHOST'),'WIN','UNIX');
165 sep=ifc(platform='WIN','\','/');
166 depth=input(symget('br_depth'),best.);
167 truncated="&br_trunc";
168run;
169
170%webout(OPEN)
171%webout(OBJ,dirinfo)
172%webout(OBJ,dirlist)
173%webout(CLOSE)