-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notify.aplf
251 lines (215 loc) · 10.5 KB
/
Notify.aplf
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
{r}←Notify args
;type;path;oldpath;linked;link;i;folder;name;extn;nsname;ns;effect;z;affected;newname;oldname;resp;dispPath;dispNs;cap;text;⎕TRAP;links;above;regex
⍝ Notify Link system that an external file has changed
⍝ Usually called by FileSystemWatcher
:If U.debug=2
(1+⊃⎕LC)⎕STOP⊃⎕SI
:EndIf
:Trap DEBUG↓0
U.Check←→0⍴⍨¯1∊∘⎕NC'/'∘≠⊆⊢
r←⍬
(type path oldpath)←{⍵,(≢⍵)↓'' '' ''},⊆args
(path oldpath)←{(0≠≢⍵)/U.Norm ⍵}¨path oldpath
linked←{0::⍬ ⋄ U.Norm¨⎕SE.Link.Links.dir}⍬
→((≢linked)<i←1⍳⍨linked≡¨(≢¨linked)↑¨⊂path)⍴0 ⍝ not linked => done
link←i⊃⎕SE.Link.Links
(folder name extn)←⎕NPARTS path
U.Check name ⍝ invalid item name
regex←'^' '\*' '\?' '$'⎕R'^' '.*' '.' '$'⊢link.extn~'.' ⍝ convert glob to regex
→0~regex ⎕S 2⊢extn~'.' ⍝ check extn
folder←¯1↓folder ⍝ drop trailing /
nsname←(1+≢link.dir)↓folder
U.Check nsname/⍨link.flatten
((nsname='/')/nsname)←'.'
⍝ check visibility
Ancestry←(⊣,'/',⊢)\'/'(≠⊆⊢)∘⊃1∘⎕NPARTS
above←≢Ancestry link.(dir↓⍨-'/'=⊃⌽dir)
:Trap 0 ⍝ no access
→0/⍨∨/6 ⎕NINFO(above↓Ancestry name),⊂path
:Else
→0
:EndTrap
nsname←link.ns,(~link.flatten)/((0≠≢nsname)/'.'),nsname ⍝ target namespace
:If 3=⎕NC link.onRead ⍝ user exit defined?
:Trap 0
→((link⍎link.onRead)type path nsname)↓0 ⍝ return 1 to continue else 0
:If ~(type≡'deleted')∨(U.IsDir path)∨extn≡link.extn
Log'Ignoring due to extension: ',type,' of ',path
→0
:EndIf
:Else
⎕←'*** onRead callback ',link.onRead,' failed: ',⎕DMX.EM
∘∘∘
:EndTrap
:EndIf
:If 0=≢ns←U.GetRefTo nsname ⍝ We're going to need a ref to it
Log'Unable to create namespace ',nsname
→0 ⍝ might as well give up now
:EndIf
effect←0
:Select type
:Case 'created' ⍝ A new file
effect←0 ⍝ may not have an effect on existing objects
:If 0≠≢z←U.GetName path ⍝ try to extract the name
name←z
:EndIf
:If 0≠≢z←4⊃ns U.GetLinkInfo name ⍝ Redefines existing object with a source file
:AndIf ⎕NEXISTS z ⍝ ... and that file exists
affected←(⍕ns),'.',name
Log'(created): ignoring new file ',path
Log' attempts to redefine ',affected,' which is linked to ',z
→0
:EndIf
:Case 'changed' ⍝ Update to existing file?
:If 0≠≢z←4⊃ns U.GetLinkInfo name ⍝ name already linked
:AndIf name≡U.GetName path ⍝ name matches file contents
:AndIf (U.Normalise path)≡U.Normalise 4⊃z ⍝ to the same file name
effect←1 ⋄ affected←(⍕ns),name
:If (7⊃z)≡U.FileHash path
Log'(change) ignored, checksum unchanged: ',path
→0
:Else
Log'(change): ',affected,' updated in ',path
:EndIf ⍝ ... but the file did not change
:Else ⍝ apparent change to name with no current link
:If 0≠≢z←U.GetFileInfo path ⍝ Paranoia: check if file defined another name
:If 1≠≢z
⎕←'WARNING: user changed name of edited item - ]Link lost track!'
⎕←' Best course of action is:'
⎕←' ]Link ',(⍕link.ns),' -reset'
⎕←' ]Link ',(⍕link.ns),' ',link.dir
→0
:EndIf ⍝ Multiple names
z←⊃z
:AndIf 0≠(2⊃z).⎕NC⍕1⊃z ⍝ If the old name exists
(2⊃z).⎕EX⍕1⊃z ⍝ Expunge it
:EndIf
:If U.IsDir path
Log'(changed) ignored "change" to directory: ',path
→0
:EndIf
:If name≢newname←U.GetName path ⍝ repeated in create
:AndIf 0≠≢⊃z←ns U.GetLinkInfo newname ⍝ name inside is already linked
affected←(⍕ns),'.',name
Log'(created): ignoring changed file ',path
Log' attempts to redefine ',affected,' which is linked to ',4⊃z
→0
:EndIf
:If 0≠≢z←U.GetFileInfo path ⍝ Try to find an "affected" object
effect←1 ⋄ affected←(⍕ns),'.',name
Log'(changed): ',affected,' was previously linked to ',path
:ElseIf 0≠≢newname
effect←1 ⋄ affected←(⍕ns),'.',newname ⍝ So just accept it
Log'(changed): fixing ',affected,' from ',path
:Else
Log'(changed): No APL object found in ',path
→0
:EndIf
:EndIf
:Case 'renamed'
:If U.IsDir path ⍝ of a folder?
:AndIf 0=ns.⎕NC name ⍝ and new name is free and valid
:If 9=ns.⎕NC oldname←∊1↓⎕NPARTS oldpath ⍝ and old name looks like an existing namespace
ns⍎name,'←',oldname ⍝ copy it
(⍎(⍕ns),'.',name).⎕DF(⍕ns),'.',name ⍝ give it a nice name
ns.⎕EX oldname ⍝ expunge old
links←U.Norm¨U.GetInfos'file'
links←(((≢oldpath)↑¨links)∊⊂oldpath)/links
links←(⊂path),¨(≢oldpath)↓¨links
:For link :In links
Notify'changed'link
:EndFor
Log'NS rename ',oldname,' → ',(⍕ns),'.',name
:Else ⍝ no old ns
Log'NS rename ',oldname,' (not found) → ',name
:EndIf
→0
:EndIf
oldname←2⊃⎕NPARTS oldpath ⍝ rename - not of a folder
:If 0≠≢⊃z←ns U.GetLinkInfo oldname ⍝ old name *is* linked to this file
effect←1 ⋄ affected←(⍕ns),'.',name
Log'(rename) ',(⍕ns),'.',oldname,' → ',affected
:Else ⍝ old name not linked to old file
:If 0≠≢z←U.GetFileInfo path
affected←(⍕2⊃⊃z),'.',⊃⊃z
Log'(rename): ',affected,' was previously linked to ',path
:Else
:If 0≠≢name←U.GetName path
:If 0≠≢⊃z←ns U.GetLinkInfo name ⍝ but name inside is already linked
:AndIf ⎕NEXISTS 4⊃z ⍝ to a file which exists
affected←(⍕ns),'.',name
Log'(created): ignoring changed file ',path
Log' attempts to redefine ',affected,' which is linked to ',4⊃z
→0
:Else
effect←1 ⋄ affected←(⍕ns),'.',name
Log'(rename): re-definition of ',affected,' from ',path
:EndIf
:Else
Log'(rename): Unable to find changed object corresponding to ',path
:EndIf
:EndIf
:EndIf
:Case 'deleted'
:If 0≠≢ns U.GetLinkInfo name ⍝ existing name is linked to this file
:OrIf 9.1=ns.⎕NC⊂name ⍝ it identifies a namespace
effect←1 ⋄ affected←(⍕ns),'.',name
Log'(delete) of ',affected
:ElseIf 0≠≢z←U.FindLinkInfo path
effect←1 ⋄ affected←2⊃z
Log'(delete): ',affected,' was previously linked to ',path
:Else
Log'(delete): Unable to find changed object corresponding to ',path
:EndIf
:EndSelect
:If effect∨'created'≡type
:If ~resp←link.quiet
dispPath←U.WinSlash Strip path
dispNs←U.WinSlash⍕ns ⍝ / → \ on Windows
cap←'Namespace-Directory Synchroniser'
:If type≡'renamed'
text←⊂'"',(U.WinSlash oldpath),'" has been renamed to "',(U.WinSlash path),'". Would you like to synchronise?'
:Else
text←⊂'"',(U.WinSlash path),'" has been ',type,'. Would you like to synchronise?'
:EndIf
:Select type
:Case 'created'
text,←''('If you choose Yes, the contents of the file will be established in "',dispNs,'".')
text,←''('If you choose No, "',dispNs,'" will be out of sync with "',dispPath,'".')
:Case 'changed'
text,←''('If you choose Yes, "',affected,'" will be updated.')
text,←''('If you choose No, "',dispNs,'" will be out of sync with "',dispPath,'".')
:Case 'renamed'
text,←''('If you choose Yes, "',affected,'" will be re-mapped.')
text,←''('If you choose No, "',dispNs,'" will be mapped to a file which may no longer exist.')
:Case 'deleted'
text,←''('If you choose Yes, "',affected,'" will be DELETED!')
text,←''('If you choose No, "',dispNs,'" will contain items that are not on in the "',dispPath,'".')
:EndSelect
resp←⎕SE.SALTU.msgBox cap text'Query'
:EndIf
:If 1=resp
:If (⊂type)∊'renamed' 'deleted'
⎕EX affected
:EndIf
:If type≢'deleted'
:Trap 0
:If U.IsDir path
name ns.⎕NS'' ⍝ create namespace
:Else ⍝ file
2 ns.⎕FIX'file://',path
:EndIf
:Case 22
:If ~'tmp___'≡¯6↑path ⍝ PyCharm up to no good again
⎕←'(',type,') reported to unreadable file ',path
:EndIf
:Else
⎕←'Unable to fix file ',path
⎕←' '⎕DMX
:EndTrap
:EndIf
:EndIf
:EndIf
:Else
U.Resignal 1
:EndTrap