-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_stores.clj
159 lines (132 loc) · 6.41 KB
/
file_stores.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
(ns pathological.file-stores
(:refer-clojure :exclude [type])
(:require
[pathological.utils :as u])
(:import
[clojure.lang Keyword]))
(declare ->file-store)
(defprotocol FileAttributeViewSupport
"A protocol allowing the file attribute view support of a file store to be
determined."
(supports-file-attribute-view [file-store class-or-type]
"Returns true if the file store supports the file attribute view
identified by the class or type; returns false otherwise.
The class or type argument should be:
- a [Class](https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html)
instance corresponding to a class implementing
[FileAttributeView](https://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/FileAttributeView.html),
- a string or keyword representing the view name of the file attribute
view, defined by the underlying file system implementation."))
(defprotocol FileStoreAttributeViewSupport
"A protocol allowing the file store attribute view support of a file store
to be determined, allowing attributes to be read in the case that file store
attribute views are supported.
By default, Java NIO2 does not include any file store attribute views.
However, the file store attribute view framework is extensible and
implementation dependent so this protocol is provided in the case that a file
system is in use that does provide file store attribute views."
(read-file-store-attribute-view [file-store class-or-type]
"Reads a file store attribute view of the given class or type from the
file store.
The class or type argument should be:
- a [Class](https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html)
instance corresponding to a class implementing
[FileStoreAttributeView](https://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/FileStoreAttributeView.html),
- a string or keyword representing the view name of the file store
attribute view, defined by the underlying file system implementation.")
(read-attribute [file-store attribute-spec]
"Reads the attribute defined by the given attribute spec from the file
store.
The attribute spec should be as defined in
[[pathological.attribute-specs/->attribute-spec]]."))
(defprotocol ReloadFileStoreAttributes
"A protocol allowing file store attribute views to be reloaded from the
backing file system."
(reload [view]
"Reloads all attributes of the view."))
(defmulti ^:private do-supports-file-attribute-view
(fn [_ class-or-type]
(clojure.core/type class-or-type)))
(defmethod ^:private do-supports-file-attribute-view String
[^java.nio.file.FileStore file-store ^String class-or-type]
(.supportsFileAttributeView file-store class-or-type))
(defmethod ^:private do-supports-file-attribute-view Class
[^java.nio.file.FileStore file-store ^Class class-or-type]
(.supportsFileAttributeView file-store class-or-type))
(defmethod ^:private do-supports-file-attribute-view Keyword
[^java.nio.file.FileStore file-store class-or-type]
(.supportsFileAttributeView file-store (name class-or-type)))
(def ^:dynamic *file-store-attribute-view-factories*
"A mapping of available file store attribute view types to factory functions
for materialised records.
By default, there are no factories defined since Java NIO2 does not include
any file store attribute views. However, since the file store attribute view
framework is extensible and implementation dependent in Java NIO2, this var is
dynamic and can be altered / rebound to support additional file store
attribute views."
{})
(defn ->file-store-attribute-view-factory
"Returns a file store attribute view factory for building materialised records
for the supplied file store attribute view type when available. If no factory
is available, an identity factory is returned which leaves the provided view
unchanged.
See [[*file-store-attribute-view-factories*]] for details on supported types."
[type]
(get *file-store-attribute-view-factories* type
(fn [_ view] view)))
(defrecord FileStore
[name
type
read-only?
total-space
usable-space
unallocated-space
block-size
delegate]
ReloadFileStoreAttributes
(reload [_]
(->file-store delegate))
FileAttributeViewSupport
(supports-file-attribute-view [_ class-or-type]
(let [class-or-type (u/->file-attribute-view-class class-or-type)]
(do-supports-file-attribute-view delegate class-or-type)))
FileStoreAttributeViewSupport
(read-file-store-attribute-view [_ class-or-type]
(let [type-class (u/->file-store-attribute-view-class class-or-type)
factory (->file-store-attribute-view-factory type)]
(factory (.getFileStoreAttributeView
^java.nio.file.FileStore delegate type-class))))
(read-attribute [_ attribute-spec]
(let [value (.getAttribute
^java.nio.file.FileStore delegate attribute-spec)]
(u/<-attribute-value attribute-spec value))))
(defn ->file-store
"Converts the [FileStore](https://docs.oracle.com/javase/7/docs/api/java/nio/file/FileStore.html)
instance to a materialised record.
The returned record includes:
- `:name`: the name of the file store,
- `:type`: the type of the file store,
- `:read-only?`: whether or not the file store is read only, as a boolean,
- `:total-space`: the total space in the file store, in bytes,
- `:usable-space`: the usable space for this JVM in the file store, in
bytes,
- `:unallocated-space`: the unallocated space in the file store, in bytes,
- `:block-size`: the block size of the file store, when available, in bytes;
`nil` when not available,
- `:delegate`: the underlying instance.
The returned record implements [[ReloadFileStoreAttributes]],
[[FileAttributeViewSupport]] and [[FileStoreAttributeViewSupport]]."
[^java.nio.file.FileStore file-store]
(let [block-size
(try (.getBlockSize file-store)
(catch UnsupportedOperationException _ nil)
(catch IllegalArgumentException _ nil))]
(map->FileStore
{:name (.name file-store)
:type (.type file-store)
:read-only? (.isReadOnly file-store)
:total-space (.getTotalSpace file-store)
:usable-space (.getUsableSpace file-store)
:unallocated-space (.getUnallocatedSpace file-store)
:block-size block-size
:delegate file-store})))