-
Notifications
You must be signed in to change notification settings - Fork 4
/
item_info.clj
184 lines (154 loc) · 6.23 KB
/
item_info.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
(ns clj-jargon.item-info
(:use [clj-jargon.validations])
(:require [clojure-commons.file-utils :as ft]
[otel.otel :as otel])
(:import [org.irods.jargon.core.pub.domain ObjStat$SpecColType
Collection
DataObject
Quota]
[org.irods.jargon.core.exception FileNotFoundException]
[org.irods.jargon.core.query CollectionAndDataObjectListingEntry$ObjectType]
[org.irods.jargon.core.pub IRODSFileSystemAO
DataObjectAO
CollectionAO
QuotaAO]
[org.irods.jargon.core.pub.io IRODSFile
IRODSFileFactory]))
(def collection-type CollectionAndDataObjectListingEntry$ObjectType/COLLECTION)
(def dataobject-type CollectionAndDataObjectListingEntry$ObjectType/DATA_OBJECT)
(def unknown-type CollectionAndDataObjectListingEntry$ObjectType/UNKNOWN)
(defn ^String trash-base-dir
"Returns the base trash folder for a specified user.
Parameters:
zone - he name of the authenication zone
user - the username of the user's trash folder to look up.
Returns:
It returns the absolute path to the trash folder."
[^String zone ^String user]
(ft/path-join "/" zone "trash" "home" user))
(defn ^IRODSFile file
"Returns an instance of IRODSFile representing 'path'. Note that path
can point to either a file or a directory.
Parameters:
path - String containing a path.
Returns: An instance of IRODSFile representing 'path'."
[{^IRODSFileFactory file-factory :fileFactory} ^String path]
(validate-path-lengths path)
(.instanceIRODSFile file-factory path))
(defn ^Boolean exists?
"Returns true if 'path' exists in iRODS and false otherwise.
Parameters:
path - String containing a path.
Returns: true if the path exists in iRODS and false otherwise."
[cm ^String path]
(validate-path-lengths path)
(.exists (file cm path)))
(defn ^Boolean paths-exist?
"Returns true if the paths exist in iRODS.
Parameters:
paths - A sequence of strings containing paths.
Returns: Boolean"
[cm paths]
(doseq [p paths] (validate-path-lengths p))
(zero? (count (filter #(not (exists? cm %)) paths))))
(defn object-type
[{^IRODSFileSystemAO cm-ao :fileSystemAO} ^String path]
(otel/with-span [s ["object-type"]]
(try
(condp = (.getObjectType (.getObjStat cm-ao path))
collection-type :dir
dataobject-type :file
:none)
(catch FileNotFoundException _ :none))))
(defn- ^Boolean jargon-type-check
[cm check-type ^String path]
(= check-type (object-type cm path)))
(defn ^Boolean is-file?
"Returns true if the path is a file in iRODS, false otherwise."
[cm ^String path]
(validate-path-lengths path)
(jargon-type-check cm :file path))
(defn ^Boolean is-dir?
"Returns true if the path is a directory in iRODS, false otherwise."
[cm ^String path]
(validate-path-lengths path)
(jargon-type-check cm :dir path))
(defn ^Boolean is-linked-dir?
"Indicates whether or not a directory (collection) is actually a link to a
directory (linked collection).
Parameters:
cm - the context map
path - the absolute path to the directory to check.
Returns:
It returns true if the path points to a linked directory, otherwise it
returns false."
[{^IRODSFileFactory file-factory :fileFactory} ^String path]
(validate-path-lengths path)
(= ObjStat$SpecColType/LINKED_COLL
(.. file-factory
(instanceIRODSFile (ft/rm-last-slash path))
initializeObjStatForFile
getSpecColType)))
(defn ^DataObject data-object
"Returns an instance of DataObject representing 'path'."
[{^DataObjectAO data-ao :dataObjectAO} ^String path]
(validate-path-lengths path)
(.findByAbsolutePath data-ao path))
(defn ^Collection collection
"Returns an instance of Collection (the Jargon version) representing
a directory in iRODS."
[{^CollectionAO collection-ao :collectionAO} ^String path]
(validate-path-lengths path)
(.findByAbsolutePath collection-ao (ft/rm-last-slash path)))
(defn lastmod-date
"Returns the date that the file/directory was last modified."
[{^IRODSFileSystemAO cm-ao :fileSystemAO} ^String path]
(validate-path-lengths path)
(str (long (.getTime (.getModifiedAt (.getObjStat cm-ao path))))))
(defn created-date
"Returns the date that the file/directory was created."
[{^IRODSFileSystemAO cm-ao :fileSystemAO} ^String path]
(validate-path-lengths path)
(str (long (.getTime (.getCreatedAt (.getObjStat cm-ao path))))))
(defn file-size
"Returns the size of the file in bytes."
[{^IRODSFileSystemAO cm-ao :fileSystemAO} ^String path]
(validate-path-lengths path)
(.getObjSize (.getObjStat cm-ao path)))
(defn stat
"Returns status information for a path."
[{^IRODSFileSystemAO cm-ao :fileSystemAO} ^String path]
(otel/with-span [s ["stat"]]
(validate-path-lengths path)
(try
(let [objstat (.getObjStat cm-ao path)]
(condp = (.getObjectType objstat)
collection-type
{:id path
:path path
:type :dir
:date-created (long (.getTime (.getCreatedAt objstat)))
:date-modified (long (.getTime (.getModifiedAt objstat)))}
dataobject-type
{:id path
:path path
:type :file
:file-size (.getObjSize objstat)
:md5 (.getChecksum objstat)
:date-created (long (.getTime (.getCreatedAt objstat)))
:date-modified (long (.getTime (.getModifiedAt objstat)))
}
{:type :none}))
(catch FileNotFoundException _ {:type :none}))))
(defn quota-map
[^Quota quota-entry]
(hash-map
:resource (.getResourceName quota-entry)
:zone (.getZoneName quota-entry)
:user (.getUserName quota-entry)
:updated (str (.getTime (.getUpdatedAt quota-entry)))
:limit (str (.getQuotaLimit quota-entry))
:over (str (.getQuotaOver quota-entry))))
(defn quota
[{^QuotaAO quota-ao :quotaAO} ^String user]
(mapv quota-map (.listQuotaForAUser quota-ao user)))