dev(puterfs): move get_recursive_size to extension

This commit is contained in:
KernelDeimos
2025-11-12 15:55:29 -05:00
committed by Eric Dubé
parent c9c745740d
commit a50866ec76
4 changed files with 29 additions and 8 deletions

View File

@@ -112,6 +112,7 @@ export default class PuterFSProvider {
capabilities.READDIR_UUID_MODE,
capabilities.COPY_TREE,
capabilities.GET_RECURSIVE_SIZE,
capabilities.READ,
capabilities.WRITE,
@@ -777,6 +778,27 @@ export default class PuterFSProvider {
return node;
}
async get_recursive_size ({ node }) {
const uuid = await node.get('uid');
const cte_query = `
WITH RECURSIVE descendant_cte AS (
SELECT uuid, parent_uid, size
FROM fsentries
WHERE parent_uid = ?
UNION ALL
SELECT f.uuid, f.parent_uid, f.size
FROM fsentries f
INNER JOIN descendant_cte d
ON f.parent_uid = d.uuid
)
SELECT SUM(size) AS total_size FROM descendant_cte
`;
const rows = await db.read(cte_query, [uuid]);
return rows[0].total_size;
}
/**
* @param {Object} param
* @param {File} param.file: The file to write.

View File

@@ -531,15 +531,13 @@ module.exports = class FSNodeContext {
* already fetched.
*/
async fetchSize() {
const { fsEntryService } = Context.get('services').values;
// we already have the size for files
if ( ! this.entry.is_dir ) {
await this.fetchEntry();
return this.entry.size;
}
this.entry.size = await fsEntryService.get_recursive_size(this.entry.uuid);
this.entry.size = await this.provider.get_recursive_size({ node: this });
return this.entry.size;
}

View File

@@ -1,18 +1,18 @@
/*
* Copyright (C) 2024-present Puter Technologies Inc.
*
*
* This file is part of Puter.
*
*
* Puter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
@@ -35,6 +35,7 @@ const capabilityNames = [
'copy-tree',
'move-tree',
'remove-tree',
'get-recursive-size',
// Behavior Capabilities
'case-sensitive',

View File

@@ -108,7 +108,7 @@ class SizeService extends BaseService {
let sz;
if ( node.entry.is_dir ) {
if ( node.entry.uuid ) {
sz = await fsEntryService.get_recursive_size(node.entry.uuid);
sz = await node.fetchSize();
} else {
// very unlikely, but a warning is better than a throw right now
// TODO: remove this once we're sure this is never hit