-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathrepo.js
More file actions
224 lines (196 loc) · 6.82 KB
/
repo.js
File metadata and controls
224 lines (196 loc) · 6.82 KB
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
"use strict";
const g_db = require("@arangodb").db;
const g_lib = require("./support");
const { errors } = require("@arangodb");
const error = require("./lib/error_codes");
const pathModule = require("./posix_path");
/**
* All DataFed repositories have the following path structure on a POSIX file system
*
* E.g.
* /mnt/science/datafed/project/foo/904u42
* /mnt/science/datafed/user/bob/352632
*
* In these cases
*
* PROJECT_PATH = /mnt/science/datafed/project/foo
* USER_PATH = /mnt/science/datafed/user/bob
*
* USER_RECORD_PATH = /mnt/science/datafed/user/bob/352632
* and
* PROJECT_RECORD_PATH = /mnt/science/datafed/project/foo/904u42
*
* REPO_BASE_PATH = /mnt/science
* REPO_ROOT_PATH = /mnt/science/datafed
* REPO_PATH = /mnt/science/datafed/project
* REPO_PATH = /mnt/science/datafed/user
**/
const PathType = {
USER_PATH: "USER_PATH",
USER_RECORD_PATH: "USER_RECORD_PATH",
PROJECT_PATH: "PROJECT_PATH",
PROJECT_RECORD_PATH: "PROJECT_RECORD_PATH",
REPO_BASE_PATH: "REPO_BASE_PATH",
REPO_ROOT_PATH: "REPO_ROOT_PATH",
REPO_PATH: "REPO_PATH",
UNKNOWN: "UNKNOWN",
};
class Repo {
// ERROR code
#error = null;
// Error message should be a string if defined
#err_msg = null;
// Boolean value that determines if the repo exists in the database
#exists = false;
// The repo id simply the key prepended with 'repo/'
#repo_id = null;
#repo_key = null;
/**
* Constructs a Repo object and checks if the key exists in the database.
*
* @param {string} a_key or id - The unique identifier for the repo, of repo key.
* e.g. can be either
* repo/repo_name
* or
* repo_name
*/
constructor(a_key) {
// Define the collection
const collection = g_db._collection("repo");
// This function is designed to check if the provided key exists in the
// database as a record. Searches are only made in the 'd' collection
//
// Will return true if it does and false if it does not.
if (a_key && a_key !== "repo/") {
if (a_key.startsWith("repo/")) {
this.#repo_id = a_key;
this.#repo_key = a_key.slice("repo/".length);
} else {
this.#repo_id = "repo/" + a_key;
this.#repo_key = a_key;
}
// Check if the repo document exists
try {
if (collection.exists(this.#repo_key)) {
this.#exists = true;
} else {
this.#exists = false;
this.#error = error.ERR_NOT_FOUND;
this.#err_msg = "Invalid repo: (" + a_key + "). No record found.";
}
} catch (e) {
this.#exists = false;
this.#error = error.ERR_INTERNAL_FAULT;
this.#err_msg = "Unknown error encountered.";
console.log(e);
}
}
}
/**
* Checks if the repo exists in the database.
*
* @returns {boolean} True if the repo exists, otherwise false.
*/
exists() {
return this.#exists;
}
key() {
return this.#repo_key;
}
id() {
return this.#repo_id;
}
/**
* Will return error code of last run method.
* @returns {number} - If no error code, will return null
**/
error() {
return this.#error;
}
/**
* Retrieves the error code of the last run method.
*
* @returns {string|null} Error code or null if no error.
*/
errorMessage() {
return this.#err_msg;
}
/**
* Detect what kind of POSIX path has been provided
*
* @param {string} a_path - the POSIX path that is supposed to exist on the repo
* @returns {string} - posix path type
**/
pathType(a_path) {
// Ensure the repo exists
if (!this.#exists) {
throw [error.ERR_PERM_DENIED, "Repo does not exist " + this.#repo_id];
}
let repo = g_db._document(this.#repo_id);
if (!repo.path) {
throw [error.ERR_INTERNAL_FAULT, "Repo document is missing path: " + this.#repo_id];
}
// Get and sanitize the repo root path by removing the trailing slash if one exists
let repo_root_path = repo.path.replace(/\/$/, "");
let sanitized_path = a_path.replace(/\/$/, "");
// Check if the sanitized path is exactly the repo root path
if (sanitized_path === repo_root_path) {
return PathType.REPO_ROOT_PATH;
}
// Check if the sanitized path is a valid base path
if (
sanitized_path.length < repo_root_path.length &&
repo_root_path.startsWith(sanitized_path + "/")
) {
return PathType.REPO_BASE_PATH;
}
// Ensure the sanitized path starts with the repo root path
if (!sanitized_path.startsWith(repo_root_path + "/")) {
return PathType.UNKNOWN;
}
// Get the relative path and its components
const relative_path = sanitized_path.substr(repo_root_path.length);
const relative_path_components = pathModule.splitPOSIXPath(relative_path);
// Map the first component to its corresponding PathType
const pathMapping = {
project: [PathType.REPO_PATH, PathType.PROJECT_PATH, PathType.PROJECT_RECORD_PATH],
user: [PathType.REPO_PATH, PathType.USER_PATH, PathType.USER_RECORD_PATH],
};
const firstComponent = relative_path_components[0];
if (pathMapping[firstComponent]) {
return (
pathMapping[firstComponent][relative_path_components.length - 1] || PathType.UNKNOWN
);
}
return PathType.UNKNOWN;
}
static resolveFromPath(file_path) {
var canonical = pathModule.normalizePOSIXPath(file_path);
if (canonical !== file_path) {
throw [error.ERR_PERM_DENIED, "Path contains invalid sequences: " + file_path];
}
var repos = g_db.repo.all().toArray();
var best_match = null;
var best_length = 0;
for (var i = 0; i < repos.length; i++) {
var repo_path = repos[i].path;
if (repo_path.charAt(repo_path.length - 1) !== "/") {
repo_path += "/";
}
if (canonical.indexOf(repo_path) === 0 || canonical === repo_path.slice(0, -1)) {
if (repo_path.length > best_length) {
best_match = repos[i];
best_length = repo_path.length;
}
}
}
if (!best_match) {
throw [
error.ERR_PERM_DENIED,
"File path does not match any known repository: " + file_path,
];
}
return new Repo(best_match._id);
}
}
module.exports = { Repo, PathType };