-
Notifications
You must be signed in to change notification settings - Fork 2
/
dsmv.m
executable file
·159 lines (152 loc) · 5.22 KB
/
dsmv.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
% Author: Carl Doersch (cdoersch at cs dot cmu dot edu)
%
% Move a variable or set of variables from one location to another within
% the ds struct. The syntax is similar to unix mv. src can be a variable
% or set of variables (absolute or relative paths, *-wildcards supported, no
% bracket indexing). dest can be an absolute or relative path, either specifying
% an existing struct within the
% ds (in which case all src variables are moved into that struct), or a path/name that
% does not yet exist, in which case the variable will be renamed. Changes in the
% workspace are mirrored on disk if the variable appears on disk, and dsmv will still
% function if the variables are not loaded in memory.
function dsmv(src,dest)
global ds;
src=dsexpandpath(src);
try
for(i=1:numel(src))
[pathsrc{i},~,srctype]=dsdiskpath(src{i});
if(~isempty(pathsrc{i}))
[tmppathdest, pathexist, desttype]=dsdiskpath(dest,true);
%if(isempty(pathdest))
% dotpos=find(desc=='.');
% dotpos=dotpos(end);
% pathdest=dsdiskpath(dest(1:(dotpos-1));
%end
if(pathexist&&strcmp(desttype,'struct'))%in this case, we move the results into the struct
for(j=1:numel(pathsrc{i}))
fnam=pathsrc{i}{j};
spos=find(fnam=='/');
fnam=fnam((spos(end)+1):end);
pathdest{i}{j}=[tmppathdest{1} filesep fnam];
end
else
if(numel(src)>1)
%if we get here, we can't have made any filesystem changes yet
throw MException('ds:cantmove','cant move multiple ds variables to a single result variable');
end
if(pathexist)%we're overwriting something
for(j=1:numel(tmppathdest))
%TODO: come up with a way to recover this stuff on error
delete(tmppathdest{j});
end
end
pathdest{i}=dsdiskpath(dest,true,srctype);
if(isempty(pathdest{i}))
throw(MException('ds:cantmove',['cannot move to ' dest '. No such path in dswork.']));
end
%if(pathexist&&strcmp(desttype,'cell'))%in this case, we need to delete what's there or movefile will do the wrong thing
% delete(tmppathdest{1});
% pathdest{i}=tmppathdest;
%else
% pathdest{i}=tmppathdest;
end
%for(i=1:numel(pathsrc))
% if(numel(pathdest)>1)
% resnm=pathdest{i};
% else
% resnm=pathdest{1};
% end
movedidx=0;
for(j=1:numel(pathsrc{i}))
disp(['movefile(' pathsrc{i}{j} ',' pathdest{i}{j} ');']);
movefile(pathsrc{i}{j},pathdest{i}{j});
movedidx=movedidx+1;
end
else
pathdest{i}=[];
end
end
if(dsfield(ds,'sys','currpath'))
origcurrpath=ds.sys.currpath;
else
origcurrpath='';
end
origcurrpath=['ds' origcurrpath];
absdest=dsabspath(dest)
absdest=absdest(4:end);
for(i=1:numel(src))
abssrc{i}=dsabspath(src{i});
abssrc{i}=abssrc{i}(4:end);
end
dscd('.ds');
ds2=ds;
sys=ds.sys;
if(isempty(absdest)||(dsfield(ds2,absdest(2:end))&&eval(['isstruct(ds2' absdest ')'])))
for(i=1:numel(src))
fnam=src{i};
dotpos=find(fnam=='.')
fnam=fnam((dotpos(end)+1):end);
absdest2{i}=[absdest '.' fnam];
end
else
if(numel(abssrc)>1)
throw MException('ds:cantmove','cant move multiple ds variables to a single result variable');
end
absdest2{1}=absdest;
end
for(i=1:numel(src))
if(dshasprefix(absdest2{i},abssrc{i})&&...
((numel(abssrc{i})==numel(absdest2{i}))||(absdest2{i}(numel(abssrc{i})+1)=='.')))
disp(['cannot move ' abssrc{i} ' into a substructure of itself']);
continue;
end
absdest2{i}
abssrc{i}
if(dsfield(ds2,abssrc{i}(2:end)))
eval(['ds2' absdest2{i} '=ds2' abssrc{i} ';']);
end
if(dsfield(sys,['savestate' abssrc{i}]))
movesst=1;
else
movesst=0;
end
if(movesst)
eval(['sys.savestate' absdest2{i} '=sys.savestate' abssrc{i} ';']);
elseif(dsfield(sys,['savestate' absdest2{i}]))
dp2=find(absdest2{i}=='.');
destvarparent=absdest2{i}(1:(dp2(end)-1));
eval(['sys.savestate' destvarparent '=rmfield(sys.savestate' destvarparent ',''' absdest2{i}((dp2(end)+1):end) ''')']);
end
matchstr=abssrc{i};
dotpos=find(matchstr=='.');
dotpos=dotpos(end);
parent=matchstr(1:(dotpos-1))
child=matchstr((dotpos+1):end)
if(dsfield(ds2,abssrc{i}(2:end)))
eval(['ds2' parent '=rmfield(ds2' parent ',''' child ''')']);
end
if(movesst)
eval(['sys.savestate' parent '=rmfield(sys.savestate' parent ',''' child ''')']);
end
if(dshasprefix(origcurrpath,['.' abssrc{i}]))
origcurrpath=[absdest2{i} origcurrpath(numel(abssrc{i})+2:end)];
end
end
ds=ds2;
ds.sys=sys;
dscd(origcurrpath);
catch ex
le=lasterror;
moveback=1'
dsprinterr;
try
dscd(origcurrpath);
catch
end
if(moveback&&exist('movedidx','var'))
for(j=movedidx:-1:numel(pathsrc{i}))
movefile(pathdest{i}{j},pathsrc{i}{j});
end
end
end
end