fix: is_empty sometimes being wrong

is_empty wasn't working correctly while running concurrently with
fetchEntry because because the check in fetchIsEmpty was too
specific. This commit fixes this issue while also adding support for
a 'path' parameter to is_empty.
This commit is contained in:
KernelDeimos
2025-09-15 18:24:48 -04:00
parent f4fc24bce3
commit 3af84c0647
2 changed files with 27 additions and 10 deletions

View File

@@ -534,11 +534,11 @@ module.exports = class FSNodeContext {
}
async fetchIsEmpty () {
if ( ! this.entry ) return;
if ( ! this.entry.is_dir ) return;
if ( ! this.uid ) return;
this.entry.is_empty = await is_empty(this.uid);
if ( ! this.uid && ! this.path ) return;
this.entry.is_empty = await is_empty({
uid: this.uid,
path: this.path,
});
}
async fetchAll(fsEntryFetcher, user, force) {

View File

@@ -38,12 +38,29 @@ const tmp_provide_services = async ss => {
async function is_empty(dir_uuid){
/** @type BaseDatabaseAccessService */
const db = services.get('database').get(DB_READ, 'filesystem');
let rows;
// first check if this entry is shared
let rows = await db.read(
`SELECT EXISTS(SELECT 1 FROM fsentries WHERE parent_uid = ? LIMIT 1) AS not_empty`,
[dir_uuid]
);
if ( typeof dir_uuid === 'object' ) {
if ( typeof dir_uuid.path === 'string' && dir_uuid.path !== '' ) {
console.log('it is the path branch');
rows = await db.read(
`SELECT EXISTS(SELECT 1 FROM fsentries WHERE path LIKE ${db.case({
sqlite: `? || '%'`,
otherwise: `CONCAT(?, '%')`,
})} LIMIT 1) AS not_empty`,
[dir_uuid.path + '/']
);
} else dir_uuid = dir_uuid.uid;
}
if ( typeof dir_uuid === 'string' ) {
console.log('it is the uuid branchj');
rows = await db.read(
`SELECT EXISTS(SELECT 1 FROM fsentries WHERE parent_uid = ? LIMIT 1) AS not_empty`,
[dir_uuid]
);
}
return !rows[0].not_empty;
}