-
Notifications
You must be signed in to change notification settings - Fork 0
/
attributes.clj
389 lines (346 loc) · 12.4 KB
/
attributes.clj
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
(ns pathological.attributes
(:refer-clojure :exclude [name])
(:require
[pathological.principals :as pr]
[pathological.utils :as u]
[pathological.attribute-specs :as as])
(:import
[java.nio.file Path]
[java.nio ByteBuffer]
[java.nio.file.attribute BasicFileAttributes
DosFileAttributes
FileOwnerAttributeView
PosixFileAttributes]))
(declare
->basic-file-attribute-view
->owner-file-attribute-view
->posix-file-attribute-view
->dos-file-attribute-view
->user-defined-file-attribute-view
->acl-file-attribute-view)
(defn register-conversion [direction selector-spec converter]
(let [selector-view (first selector-spec)
selector-name (second selector-spec)
conversions-var
(cond
(= direction :to) #'u/*->attribute-value-conversions*
(= direction :from) #'u/*<-attribute-value-conversions*
:else (throw
(IllegalArgumentException.
(format
"Unknown direction: %s. Must be one of [:to :from]."
direction))))]
(alter-var-root
conversions-var
#(assoc-in % [selector-view selector-name] converter))))
(defprotocol ReloadFileAttributes
(reload [view]))
(defprotocol UpdateFileTimes
(set-times [view last-modified-time last-access-time creation-time])
(set-last-modified-time [view last-modified-time])
(set-last-access-time [view last-access-time])
(set-creation-time [view creation-time]))
(defprotocol UpdateFileOwner
(set-owner [view owner]))
(defprotocol UpdateFileGroup
(set-group [view group]))
(defprotocol UpdatePosixFilePermissions
(set-permissions [_ permissions]))
(defprotocol UpdateFileAcl
(set-acl [view acl-entries]))
(defprotocol UpdateDosFileAttributes
(set-read-only [view value])
(set-hidden [view value])
(set-system [view value])
(set-archive [view value]))
(defprotocol UpdateUserDefinedFileAttributes
(write-attribute [view name value])
(delete-attribute [view name]))
(defrecord BasicFileAttributeView
[path
file-key
size
last-modified-time
last-access-time
creation-time
regular-file?
directory?
symbolic-link?
other?
delegate]
ReloadFileAttributes
(reload [view]
(->basic-file-attribute-view path (:delegate view)))
UpdateFileTimes
(set-times
[view new-last-modified-time new-last-access-time new-creation-time]
(.setTimes
^java.nio.file.attribute.BasicFileAttributeView (:delegate view)
(u/->file-time new-last-modified-time)
(u/->file-time new-last-access-time)
(u/->file-time new-creation-time))
(reload view))
(set-last-modified-time [view new-last-modified-time]
(set-times view new-last-modified-time nil nil))
(set-last-access-time [view new-last-access-time]
(set-times view nil new-last-access-time nil))
(set-creation-time [view new-creation-time]
(set-times view nil nil new-creation-time)))
(defrecord OwnerFileAttributeView
[path
owner
delegate]
ReloadFileAttributes
(reload [view]
(->owner-file-attribute-view path (:delegate view)))
UpdateFileOwner
(set-owner [view new-owner]
(.setOwner
^FileOwnerAttributeView (:delegate view)
new-owner)
(reload view)))
(defrecord PosixFileAttributeView
[path
file-key
size
owner
group
permissions
last-modified-time
last-access-time
creation-time
regular-file?
directory?
symbolic-link?
other?
delegate]
ReloadFileAttributes
(reload [view]
(->posix-file-attribute-view path (:delegate view)))
UpdateFileTimes
(set-times
[view new-last-modified-time new-last-access-time new-creation-time]
(.setTimes
^java.nio.file.attribute.PosixFileAttributeView (:delegate view)
(u/->file-time new-last-modified-time)
(u/->file-time new-last-access-time)
(u/->file-time new-creation-time))
(reload view))
(set-last-modified-time [view new-last-modified-time]
(set-times view new-last-modified-time nil nil))
(set-last-access-time [view new-last-access-time]
(set-times view nil new-last-access-time nil))
(set-creation-time [view new-creation-time]
(set-times view nil nil new-creation-time))
UpdateFileOwner
(set-owner [view new-owner]
(.setOwner
^java.nio.file.attribute.PosixFileAttributeView (:delegate view)
new-owner)
(reload view))
UpdateFileGroup
(set-group [view new-group]
(.setGroup
^java.nio.file.attribute.PosixFileAttributeView (:delegate view)
new-group)
(reload view))
UpdatePosixFilePermissions
(set-permissions [view new-permissions]
(.setPermissions
^java.nio.file.attribute.PosixFileAttributeView (:delegate view)
(u/->posix-file-permissions new-permissions))
(reload view)))
(defrecord DosFileAttributeView
[path
file-key
size
last-modified-time
last-access-time
creation-time
regular-file?
directory?
symbolic-link?
other?
delegate]
ReloadFileAttributes
(reload [view]
(->dos-file-attribute-view path (:delegate view)))
UpdateFileTimes
(set-times
[view new-last-modified-time new-last-access-time new-creation-time]
(.setTimes
^java.nio.file.attribute.DosFileAttributeView (:delegate view)
(u/->file-time new-last-modified-time)
(u/->file-time new-last-access-time)
(u/->file-time new-creation-time))
(reload view))
(set-last-modified-time [view new-last-modified-time]
(set-times view new-last-modified-time nil nil))
(set-last-access-time [view new-last-access-time]
(set-times view nil new-last-access-time nil))
(set-creation-time [view new-creation-time]
(set-times view nil nil new-creation-time))
UpdateDosFileAttributes
(set-read-only [view new-value]
(.setReadOnly
^java.nio.file.attribute.DosFileAttributeView (:delegate view)
new-value)
(reload view))
(set-hidden [view new-value]
(.setHidden
^java.nio.file.attribute.DosFileAttributeView (:delegate view)
new-value)
(reload view))
(set-system [view new-value]
(.setSystem
^java.nio.file.attribute.DosFileAttributeView (:delegate view)
new-value)
(reload view))
(set-archive [view new-value]
(.setArchive
^java.nio.file.attribute.DosFileAttributeView (:delegate view)
new-value)
(reload view)))
(defrecord UserDefinedFileAttributeView
[path
attributes
delegate]
ReloadFileAttributes
(reload [view]
(->user-defined-file-attribute-view path (:delegate view)))
UpdateUserDefinedFileAttributes
(write-attribute [view name value]
(.write
^java.nio.file.attribute.UserDefinedFileAttributeView (:delegate view)
(clojure.core/name name)
(u/->byte-buffer value))
(reload view))
(delete-attribute [view name]
(.delete
^java.nio.file.attribute.UserDefinedFileAttributeView (:delegate view)
(clojure.core/name name))
(reload view)))
(defrecord AclFileAttributeView
[path
owner
acl
delegate]
ReloadFileAttributes
(reload [view]
(->acl-file-attribute-view path (:delegate view)))
UpdateFileOwner
(set-owner [view new-owner]
(.setOwner
^java.nio.file.attribute.AclFileAttributeView (:delegate view)
new-owner)
(reload view))
UpdateFileAcl
(set-acl [view new-acl-entries]
(.setAcl
^java.nio.file.attribute.AclFileAttributeView (:delegate view)
(map u/->acl-entry new-acl-entries))
(reload view)))
(defn ->basic-file-attribute-map [^BasicFileAttributes attributes]
{:file-key (.fileKey attributes)
:size (.size attributes)
:last-modified-time (u/<-file-time (.lastModifiedTime attributes))
:last-access-time (u/<-file-time (.lastAccessTime attributes))
:creation-time (u/<-file-time (.creationTime attributes))
:regular-file? (.isRegularFile attributes)
:directory? (.isDirectory attributes)
:symbolic-link? (.isSymbolicLink attributes)
:other? (.isOther attributes)})
(defn ->basic-file-attribute-view
[^Path path ^java.nio.file.attribute.BasicFileAttributeView view]
(when view
(let [^BasicFileAttributes attributes (.readAttributes view)]
(map->BasicFileAttributeView
(merge
(->basic-file-attribute-map attributes)
{:path path
:delegate view})))))
(defn ->owner-file-attribute-view
[^Path path ^FileOwnerAttributeView view]
(when view
(let [owner (pr/<-user-principal (.getOwner view))]
(map->OwnerFileAttributeView
{:path path
:owner owner
:delegate view}))))
(defn ->posix-file-attribute-view
[^Path path ^java.nio.file.attribute.PosixFileAttributeView view]
(when view
(let [^PosixFileAttributes posix-attributes (.readAttributes view)]
(map->PosixFileAttributeView
{:path path
:file-key (.fileKey posix-attributes)
:size (.size posix-attributes)
:owner (pr/<-user-principal (.owner posix-attributes))
:group (pr/<-group-principal (.group posix-attributes))
:permissions (u/<-posix-file-permissions
(.permissions posix-attributes))
:last-modified-time (u/<-file-time
(.lastModifiedTime posix-attributes))
:last-access-time (u/<-file-time (.lastAccessTime posix-attributes))
:creation-time (u/<-file-time (.creationTime posix-attributes))
:regular-file? (.isRegularFile posix-attributes)
:directory? (.isDirectory posix-attributes)
:symbolic-link? (.isSymbolicLink posix-attributes)
:other? (.isOther posix-attributes)
:delegate view}))))
(defn ->dos-file-attribute-view
[^Path path ^java.nio.file.attribute.DosFileAttributeView view]
(when view
(let [^DosFileAttributes dos-attributes (.readAttributes view)]
(map->DosFileAttributeView
{:path path
:file-key (.fileKey dos-attributes)
:size (.size dos-attributes)
:last-modified-time (u/<-file-time (.lastModifiedTime dos-attributes))
:last-access-time (u/<-file-time (.lastAccessTime dos-attributes))
:creation-time (u/<-file-time (.creationTime dos-attributes))
:regular-file? (.isRegularFile dos-attributes)
:directory? (.isDirectory dos-attributes)
:symbolic-link? (.isSymbolicLink dos-attributes)
:other? (.isOther dos-attributes)
:read-only? (.isReadOnly dos-attributes)
:hidden? (.isHidden dos-attributes)
:archive? (.isArchive dos-attributes)
:system? (.isSystem dos-attributes)
:delegate view}))))
(defn ->user-defined-file-attribute-view
[^Path path ^java.nio.file.attribute.UserDefinedFileAttributeView view]
(when view
(let [names (.list view)
attributes
(into {}
(mapv
(fn [name]
(let [byte-buffer (ByteBuffer/allocate (.size view name))]
(.read view name byte-buffer)
[(keyword name) (.array byte-buffer)]))
names))]
(map->UserDefinedFileAttributeView
{:path path
:attributes attributes
:delegate view}))))
(defn ->acl-file-attribute-view
[^Path path ^java.nio.file.attribute.AclFileAttributeView view]
(when view
(let [owner (pr/<-user-principal (.getOwner view))
acl (mapv u/<-acl-entry (.getAcl view))]
(map->AclFileAttributeView
{:path path
:owner owner
:acl acl
:delegate view}))))
(def ^:dynamic *file-attribute-view-factories*
{:basic ->basic-file-attribute-view
:owner ->owner-file-attribute-view
:posix ->posix-file-attribute-view
:dos ->dos-file-attribute-view
:user ->user-defined-file-attribute-view
:acl ->acl-file-attribute-view})
(defn ->file-attribute-view-factory [type]
(get *file-attribute-view-factories* type
(fn [_ view] view)))