Loading...
Searching...
No Matches
writecheck.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Answers whether a folder exists and is writeable
4 @details
5 It creates and removes a probe file in the folder, which is what the
6 deployment itself will do. The probe name carries the process id and a
7 random number and an existing file is never reused - fopen with 'O'
8 truncates.
9
10 The path arrives in the adapter table and is read back with symget, so its
11 characters are data: a value over 1000 characters, one carrying a control
12 character, or one that is not an absolute path is refused, and the shape is
13 then checked with mp_validatecol(ISPATH).
14
15 Access is whatever the platform grants on the admin folder: the deploy
16 screen calls it before the app is configured, so the service itself carries
17 no gate.
18
19 <h4> Data Inputs </h4>
20 <h5> fromjs </h5>
21 |PATH:$1000.|
22 |---|
23 |`/tmp`|
24
25 <h4> Data Outputs </h4>
26 <h5> checkwrite </h5>
27 |PATH:$1000.|PATH_EXISTS:$1.|IS_FOLDER:$1.|WRITEABLE:$1.|MSG:$200.|SYSUSERID:$32.|
28 |---|---|---|---|---|---|
29 |`/tmp`|`Y`|`Y`|`Y`| |`sasdemo`|
30
31 SYSUSERID is the account the probe was made as (`&sysuserid`), so a writeable
32 answer names who can write rather than only that somebody can.
33
34 <h4> SAS Macros </h4>
35 @li mp_abort.sas
36 @li mp_init.sas
37 @li mp_validatecol.sas
38
39 @version 9.3
40 @author 4GL Apps Ltd
41 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
42 and may not be re-distributed or re-sold without the express permission of
43 4GL Apps Ltd.
44
45 @sasjslint {"maxLineLength": 84}
46
47**/
48
49/* maxLineLength is 84 rather than 80 for the six-column data-output row above,
50 as viewtables and getcolvals raise their own cap for a table wider than eighty
51 characters. */
52
53%mp_init()
54
55/* the request tables arrive as sasjs params; FETCH puts them in WORK */
56%webout(FETCH)
57
58%mp_abort(iftrue=(%sysfunc(exist(work.fromjs))=0)
59 ,mac=&_program
60 ,msg=%str(Missing input table)
61)
62
63/* refuse what could break out of the statements, then shape check what is
64 left */
65data _null_;
66 set work.fromjs(obs=1);
67 length _path $32767;
68 _path=strip(coalescec(cats(path),''));
69 _bad=0;
70
71 if missing(_path) then _bad=2;
72 if length(_path)>1000 then _bad=1;
73 if anycntrl(_path)>0 then _bad=1;
74 /* only what can break the platform: a quote, a comma or a parenthesised
75 folder name ("C:\Program Files (x86)", "O'Brien", "/data/sas, archive") is
76 a legal path, and refusing it stops a site being deployed there. The value
77 is read back with symget and handed to filename(), so it never becomes
78 macro text and those characters are inert. */
79
80 if _bad=0 then do;
81 /* fail closed: an absent ISPATH rule must refuse the path, not accept it */
82 is_path=0;
83 %mp_validatecol(_path,ISPATH,is_path)
84 if is_path=0 then _bad=1;
85 end;
86
87 call symputx('br_path',_path);
88 call symputx('br_cwbad',put(_bad,best.));
89run;
90
91%mp_abort(iftrue=(&br_cwbad=2)
92 ,mac=&_program
93 ,msg=%str(Missing path)
94)
95%mp_abort(iftrue=(&br_cwbad=1)
96 ,mac=&_program
97 ,msg=%str(Invalid path)
98)
99
100/* only the columns in the header above leave the service: sep, probe and the
101 return codes the step works with are internal. SYSUSERID is the account the
102 probe ran as, which is the answer a reader of "is writeable" actually needs -
103 the deployment account is not always the one reading the screen. */
104data work.checkwrite(keep=path path_exists is_folder writeable msg sysuserid);
105 length path $1000 path_exists $1 is_folder $1 writeable $1 msg $200
106 sep $1 probe $2000 sysuserid $32;
107 path=symget('br_path');
108 path_exists='N';
109 is_folder='N';
110 writeable='N';
111 msg='';
112 sysuserid=symget('sysuserid');
113
114 /* a trailing separator would double up when the probe name is appended */
115 do while(length(path)>1);
116 if substr(path,length(path),1) not in ('/','\') then leave;
117 if length(path)=3 and substr(path,2,1)=':' then leave;
118 path=substr(path,1,length(path)-1);
119 end;
120
121 if fileexist(path)=1 then do;
122 path_exists='Y';
123 rc=filename('dc_chk',path);
124 did=dopen('dc_chk');
125 if did>0 then do;
126 is_folder='Y';
127 rc=dclose(did);
128 end;
129 else msg='Path exists but is not a folder';
130 rc=filename('dc_chk');
131 end;
132 else msg='Path does not exist';
133
134 if is_folder='Y' then do;
135 if index(path,'\')>0 then sep='\';
136 else sep='/';
137 /*
138 The probe name carries the process id and a random number, and an
139 existing file is never reused: fopen with 'O' truncates, so a fixed
140 name would destroy whatever was already called that.
141 */
142 probe='';
143 do _try=1 to 10;
144 probe=cats(path,sep,'.dcprobe_',symget('sysprocessid'),'_'
145 ,put(int(ranuni(0)*1e9),z10.));
146 if fileexist(probe)=0 then leave;
147 probe='';
148 end;
149 if missing(probe) then msg='No free name for the write probe';
150 else do;
151 rc=filename('dc_probe',probe);
152 fid=fopen('dc_probe','O');
153 if fid>0 then do;
154 rc=fclose(fid);
155 rc=fdelete('dc_probe');
156 writeable='Y';
157 msg='';
158 end;
159 else msg=sysmsg();
160 rc=filename('dc_probe');
161 end;
162 end;
163run;
164
165%webout(OPEN)
166%webout(OBJ,checkwrite)
167%webout(CLOSE)