-
Notifications
You must be signed in to change notification settings - Fork 2
/
dsrecurse.m
executable file
·318 lines (316 loc) · 10.2 KB
/
dsrecurse.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
% Author: Carl Doersch (cdoersch at cs dot cmu dot edu)
function [savestate,currds,respath]=dsrecurse(currds,basedir,currpath,savestate,matchstr,recurseon,task,brakidx,specialvars,pathprefix)
%try
%if(mystrcmp(basedir,currpath))
% disp(['dsrecurse:' matchstr]);
% currpath
%end
global ds;
respath={};
if(~isstruct(savestate))
savestate=struct();
%disp('setting empty savestate top')
end
if(~isstruct(currds))
currds=struct();
end
nams={};
if(ismember({'disk'},recurseon))
fils=cleandir(currpath);
disknams={fils.name};
if(strcmp(basedir,currpath))
%we are at the root--don't want to load the sys directory!
disknams(ismember(disknams,{'sys'}))=[];
end
for(i=1:numel(disknams))
nam=disknams{i};
dotpos=find(nam=='.');
if(numel(dotpos)>0)
nam=nam(1:(dotpos-1));
end
brakpos=find(nam=='[');
if(numel(brakpos)>0)
nam=nam(1:(brakpos(1)-1))
end
nams{i,1}=nam;
end
matcheddisknams=nams;
end
if(ismember({'savestate'},recurseon))
tmpnams=fieldnames(savestate);
if(mystrcmp(basedir,currpath))
%we are at the root--don't want to save the outdir!
tmpnams(ismember(tmpnams,{'outdir'}))=[];
end
nams=[nams;tmpnams];
end
if(ismember({'ds'},recurseon))
nams=[nams;fieldnames(currds)];
end
nams=unique(nams);
correspdisknams=cell(numel(nams),1);
for(i=1:numel(correspdisknams))
correspdisknams{i}={};
end
if(exist('disknams','var'))
for(i=1:numel(disknams))
[~,idx]=ismember(matcheddisknams{i},nams);
correspdisknams{idx}{end+1}=disknams{i};
end
end
%if(strcmp(task,'delete'))
%keyboard;
%end
%if(numel(recurseon)>1)
% idx(idx>numel(disknams))=numel(disknams)+1;
% disknams{end+1}=[];
% disknams=disknams(idx);
%end
dotpos=find(matchstr=='.');
if(numel(dotpos)==0)
currmatchstr=matchstr;
nextmatchstr='*';
matchisterminal=true;
else
currmatchstr=matchstr(1:(dotpos(1)-1));
nextmatchstr=matchstr((dotpos(1)+1):end);
matchisterminal=false;
end
% if(sum(currmatchstr=='{')>0)
% brakpos=find(currmatchstr=='{');
% idxstr=currmatchstr((brakpos(1)+1):(end-1));
% currmatchstr=currmatchstr(1:(brakpos-1));
% end
% task
for(i=1:numel(nams))
%nams{i}
if(~dsstringmatch(currmatchstr,nams{i}))
%currmatchstr
%'did not match'
continue;
end
%currmatchstr
%'matched'
clear filenm;
fnam=nams{i};
%if(strcmp(fnam,'initFeats'))
% keyboard;
%end
fieldisstruct=false;
if(ismember({'disk'},recurseon))
fieldisstruct=exist([currpath '/' fnam],'dir')&&~mystrcmp(fnam((end-1):end),'[]');
%if(fieldisstruct&&~isfield(savestate,fnam))
% savestate=setfield(savestate,fnam,struct());
%end
end
if(ismember({'savestate'},recurseon)&&isfield(savestate,fnam))
fieldisstruct=fieldisstruct||isstruct(getfield(savestate,fnam));
end
if(ismember({'ds'},recurseon)&&isfield(currds,fnam))
try
fieldisstruct=fieldisstruct||isstruct(getfield(currds,fnam))&&(numel(getfield(currds,fnam))==1);
catch,keyboard;end
end
%if(strcmp(fnam,'batch'))
% keyboard;
%end
if(fieldisstruct)
%fnam
%'field is struct'
if(strcmp(task(1:4),'save')&&(...
~isfield(savestate,fnam)||...
((isfield(getfield(savestate,fnam),'savestate')&&getfield(getfield(savestate,fnam),'savestate')==0))||...
isempty(getfield(savestate,fnam))))
%disp('setting empty savesatate')
if(strcmp(task,'savedistr'))
ds.sys.saved=[ds.sys.saved; {[pathprefix '.' fnam], struct()}];
end
mymkdir([currpath '/' fnam]);
if(~isfield(savestate,fnam))
savestate=setfield(savestate,fnam,struct());
end
end
if(strcmp(task,'loadsavestate')&&(~isfield(savestate,fnam)))
savestate=setfield(savestate,fnam,struct());
end
if(~isfield(currds,fnam))
%add it so that the operation can complete. The only operation that
%even reads the resulting currds is dsload, so it doesn't matter
%if we mess it up in other cases.
currds=setfield(currds,fnam,struct());
end
if(strcmp(task,'delete')&&matchisterminal)
if(isfield(savestate,fnam))
savestate=rmfield(savestate,fnam);
disp(['remove:' fnam]);
rmdir([currpath '/' fnam],'s');
end
if(isfield(currds,fnam))
currds=rmfield(currds,fnam);
end
elseif(strcmp(task,'expandpath')&&matchisterminal)
respath=[respath;{[pathprefix '.' fnam]}];
else
if(isfield(savestate,fnam))
savesttopass=getfield(savestate,fnam);
savestup=true;
else
%if we got here and the field hasn't been added to the savestate,
%we must be doing a delete and shouldn't add it.
savesttopass=struct();
savestup=false;
end
specialvars2=[];
for(j=1:numel(specialvars))
dotpos=find(specialvars(j).name=='.');
%'specialvarsrec'
%specialvars(j).name
if(isempty(dotpos)),firstdot=numel(specialvars(j).name)+1;else,firstdot=dotpos(1);end
if(dsstringmatch(specialvars(j).name(1:firstdot-1),fnam))
if(~isempty(dotpos))
specialvars2(end+1).name=specialvars(j).name(dotpos(1)+1:end);
specialvars2(end).type=specialvars(j).type;
else
error('found map/reduce variable pointing to a struct ?!?');
end
end
end
[savestate2 currds2 respath2]=dsrecurse(getfield(currds,fnam),basedir,[currpath '/' fnam],savesttopass,nextmatchstr,recurseon,task,brakidx,specialvars2,[pathprefix '.' fnam]);
if(savestup)
savestate=setfield(savestate,fnam,savestate2);
end
currds=setfield(currds,fnam,currds2);
%if(strcmp(task,'savedistr'))
% for(i=1:size(respath2,1))
% respath2{i,1}=[fnam '.' respath2{i,1}];
% end
% respath=[respath;respath2];
%end
end
if(strcmp(task,'expandpath')&&exist('respath2','var'))
%for(i=1:numel(respath2))
% respath2{i}=[fnam '.' respath2{i}];
%end
respath=[respath;respath2];
end
else
%disp(['got:' fnam]);
%fnam
%'field is not struct'
cfnam=correspdisknams{i};
mapsave=0;
reduceload=0;
docontinue=0;
for(j=1:numel(specialvars))
dotpos=find(specialvars(j).name=='.');
if(isempty(dotpos)),firstdot=numel(specialvars(j).name)+1;else,firstdot=dotpos(1);end
if(dsstringmatch(specialvars(j).name(1:firstdot-1),fnam))
if(isempty(dotpos))
if(strcmp(specialvars(j).type,'map'))
mapsave=1;
elseif(strcmp(specialvars(j).type,'reduce'))
reduceload=1;
elseif(strcmp(specialvars(j).type,'ignore'))
docontinue=1;
end
else
error('bad path for map/reduce variable.');
end
end
end
if(docontinue)
continue;
end
if(strcmp(task,'load')||strcmp(task,'forceload'))
[currds,savestate]=dsloadfield(fnam,currpath,currds,savestate,task,brakidx,reduceload);
elseif(strcmp(task,'loadsavestate'))
dsloadfieldsavestate;
elseif(strcmp(task(1:4),'save'))
%'saving'
if(strcmp(task(end-2:end),'chk'))
checksave=1;
else
checksave=0;
end
[savestate respath2]=dssavefield(getfield(currds,fnam),fnam,basedir,currpath,savestate,brakidx,task,checksave,mapsave,pathprefix);
%for(i=1:size(respath2,1))
% respath2{i,1}=[fnam '.' respath2{i,1}];
%end
respath=[respath;respath2];
elseif(strcmp(task,'move'))
%if(~isfield(savestate,fnam))
% 'empty savestae'
% fnam
%end
if(isfield(savestate,fnam))
f=getfield(savestate,fnam);
else
f=struct();
end
savestate=dsmovefield(f,fnam,basedir,currpath,savestate);
%if(~isfield(savestate,fnam))
% 'empty savestae after'
% fnam
%end
elseif(strcmp(task,'delete'))
%savestate=dsdeletefield(getfield(currds,fnam),fnam,basedir,currpath,savestate,brakidx);
for(i=1:numel(cfnam))
if(isfield(savestate,fnam)&&iscell(getfield(savestate,fnam))&&(~(isempty(brakidx{2}))))
if(~isempty(brakidx{1}))
%TODO: handle 2d arrays
disp('dsdelete does not yet support 2d array indexing...will delete memory/savestate, not disk');
else
typ=dsgettypeforvar([],fnam);
for(k=1:numel(brakidx{2}))
nms=dsgetdisknamtype(num2str(brakidx{2}(k)),typ,'');
for(l=1:numel(nms))
delete([currpath '/' cfnam{i} '/' nms{l} ]);
end
end
end
else
if(exist([currpath '/' cfnam{i}],'dir'))
rmdir([currpath '/' cfnam{i}],'s');
else
delete([currpath '/' cfnam{i}]);
end
end
end
if(isfield(savestate,fnam))
if(iscell(getfield(savestate,fnam))&&~(isempty(brakidx{2})&&isempty(brakidx{1})))
csst=getfield(savestate,fnam);
if(numel(csst)<2)
csst{2}=[];
end
if(isempty(brakidx{1}))
csst{2}(brakidx{2})=false;
else
csst{2}(brakidx{1},brakidx{2})=false;
end
savestate=setfield(savestate,fnam,csst);
else
savestate=rmfield(savestate,fnam);
end
end
if(isfield(currds,fnam))
if(iscell(getfield(currds,fnam))&&~(isempty(brakidx{2})&&isempty(brakidx{1})))
csst=getfield(currds,fnam);
if(isempty(brakidx{1}))
csst(brakidx{2})={[]};
else
csst(brakidx{1},brakidx{2})={[]};
end
currds=setfield(currds,fnam,csst);
else
currds=rmfield(currds,fnam);
end
end
elseif(strcmp(task,'expandpath'))
respath=[respath;{[pathprefix '.' fnam]}];
end
end
end
%catch ex
%dsprinterr
%end
end