@@ -66,23 +66,23 @@ class Record {
6666 /**
6767 * Generates the full path to the record as it should appear in the repository.
6868 *
69- * @param {object } loc - The location object which specifies the owner of the record.
70- * @param {string } basePath - The base path where the record is stored.
71- *
72- * @returns {string } - the path to the record or null if error
69+ * @param {string } uid - The owner uid (e.g. "u/bob" or "p/myproject")
70+ * @param {string } basePath - The base path of the repository.
71+ * @returns {string|null } - the path to the record or null if error
7372 */
74- _pathToRecord ( loc , basePath ) {
73+ _pathToRecord ( uid , basePath ) {
7574 const path = basePath . endsWith ( "/" ) ? basePath : basePath + "/" ;
76- if ( loc . uid . charAt ( 0 ) == "u" ) {
77- return path + "user/" + loc . uid . substr ( 2 ) + "/" + this . #key;
78- } else if ( loc . uid . charAt ( 0 ) == "p" ) {
79- return path + "project/" + loc . uid . substr ( 2 ) + "/" + this . #key;
75+ if ( uid . charAt ( 0 ) = == "u" ) {
76+ return path + "user/" + uid . substr ( 2 ) + "/" + this . #key;
77+ } else if ( uid . charAt ( 0 ) = == "p" ) {
78+ return path + "project/" + uid . substr ( 2 ) + "/" + this . #key;
8079 } else {
8180 this . #error = error . ERR_INTERNAL_FAULT ;
82- this . #err_msg = "Provided path does not fit within supported directory " ;
83- this . #err_msg += "structure for repository, no user or project folder has" ;
84- this . #err_msg += " been determined for the record." ;
85- console . log ( e ) ;
81+ this . #err_msg =
82+ "Provided uid does not fit within supported directory " +
83+ "structure for repository, no user or project folder has " +
84+ "been determined for the record. uid: " +
85+ uid ;
8686 return null ;
8787 }
8888 }
@@ -174,77 +174,71 @@ class Record {
174174 return ! ! this . #alloc;
175175 }
176176
177- /**
178- * Validates if the provided record path is consistent with the database.
179- *
180- * @param {string } a_path - The path to validate.
181- * @returns {boolean } True if consistent, otherwise false.
182- */
183177 isPathConsistent ( a_path ) {
184- // This function will populate the this.#loc member and the this.#alloc
185- // member
186178 if ( ! this . isManaged ( ) ) {
187179 return false ;
188180 }
189181
190- // If there is a new repo we need to check the path there and use that
182+ if ( ! a_path . startsWith ( "/" ) ) {
183+ a_path = "/" + a_path ;
184+ }
185+
186+ // If record is in flight, only check new location
191187 if ( this . #loc. hasOwnProperty ( "new_repo" ) && this . #loc. new_repo ) {
192- // Below we get the allocation associated with data item by
193- // 1. Checking if the data item is in flight, is in the process
194- // of being moved to a new location or new owner and using that
195- // oweners id.
196- // 2. Using the loc.uid parameter if not inflight to get the owner
197- // id.
188+ const new_uid = this . #loc. new_owner ? this . #loc. new_owner : this . #loc. uid ;
198189 const new_alloc = g_db . alloc . firstExample ( {
199- _from : this . #loc . new_owner ? this . #loc . new_owner : this . #loc . uid ,
190+ _from : new_uid ,
200191 _to : this . #loc. new_repo ,
201192 } ) ;
202193
203- // If no allocation is found for the item throw an error
204- // if the paths do not align also throw an error.
205194 if ( ! new_alloc ) {
206195 this . #error = error . ERR_PERM_DENIED ;
207196 this . #err_msg =
208- "Permission denied, '" + this . #key + "' is not part of an allocation '" ;
197+ "Permission denied, '" + this . #key + "' is not part of an allocation'" ;
209198 return false ;
210199 }
211200
212- this . #repo = g_db . _document ( this . #loc. new_repo ) ;
213-
214- if ( ! this . #repo) {
201+ const new_repo = g_db . _document ( this . #loc. new_repo ) ;
202+ if ( ! new_repo ) {
215203 this . #error = error . ERR_INTERNAL_FAULT ;
216204 this . #err_msg =
217- "Unable to find repo that record is meant to be allocated too, '" +
205+ "Unable to find repo '" +
218206 this . #loc. new_repo +
219- "' record '" +
220- this . #data_id;
207+ "' for record '" +
208+ this . #data_id +
209+ "'" ;
221210 return false ;
222211 }
223212
224- // If path is missing the starting "/" add it back in
225- if ( ! a_path . startsWith ( "/" ) && this . #repo . path . startsWith ( "/" ) ) {
226- a_path = "/" + a_path ;
213+ let new_path = this . _pathToRecord ( new_uid , new_repo . path ) ;
214+ if ( new_path === a_path ) {
215+ return true ;
227216 }
228217
229- let stored_path = this . _pathToRecord ( this . #loc, this . #repo. path ) ;
230-
231- if ( ! this . _comparePaths ( stored_path , a_path ) ) {
232- return false ;
233- }
234- } else {
235- this . #repo = g_db . _document ( this . #loc. _to ) ;
218+ this . #error = error . ERR_PERM_DENIED ;
219+ this . #err_msg =
220+ "Record path is not consistent with repo. Expected: " +
221+ new_path +
222+ " but got: " +
223+ a_path ;
224+ return false ;
225+ }
236226
237- if ( ! a_path . startsWith ( "/" ) && this . #repo. path . startsWith ( "/" ) ) {
238- a_path = "/" + a_path ;
239- }
240- let stored_path = this . _pathToRecord ( this . #loc, this . #repo. path ) ;
227+ // No in-flight move — check current location
228+ this . #repo = g_db . _document ( this . #loc. _to ) ;
229+ let current_path = this . _pathToRecord ( this . #loc. uid , this . #repo. path ) ;
241230
242- // If there is no new repo check that the paths align
243- if ( ! this . _comparePaths ( stored_path , a_path ) ) {
244- return false ;
245- }
231+ if ( current_path === a_path ) {
232+ return true ;
246233 }
247- return true ;
234+
235+ this . #error = error . ERR_PERM_DENIED ;
236+ this . #err_msg =
237+ "Record path is not consistent with repo. Expected: " +
238+ current_path +
239+ " but got: " +
240+ a_path ;
241+ return false ;
248242 }
249243}
250244
0 commit comments