|
| 1 | +/* |
| 2 | + * Copyright (C) 2024-present Puter Technologies Inc. |
| 3 | + * |
| 4 | + * This file is part of Puter. |
| 5 | + * |
| 6 | + * Puter is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as published |
| 8 | + * by the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +const FSNodeContext = require("../../../filesystem/FSNodeContext"); |
| 21 | +const _path = require('path'); |
| 22 | +const { Context } = require("../../../util/context"); |
| 23 | + |
| 24 | +class MemoryFSProvider { |
| 25 | + constructor(mountpoint) { |
| 26 | + this.mountpoint = mountpoint; |
| 27 | + |
| 28 | + // key: relative path from the mountpoint, always starts with `/` |
| 29 | + // value: file content |
| 30 | + this.files = new Map(); |
| 31 | + |
| 32 | + // key: relative path from the mountpoint, always starts with `/` |
| 33 | + // value: directory content |
| 34 | + this.directories = new Set(); |
| 35 | + |
| 36 | + // The root directory (of the mountpoint) always exists. |
| 37 | + this.directories.add('/'); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Normalize the path to be relative to the mountpoint. Returns `/` if the path is empty/undefined. |
| 42 | + * |
| 43 | + * @param {string} path - The path to normalize. |
| 44 | + * @returns {string} - The normalized path, always starts with `/`. |
| 45 | + */ |
| 46 | + _normalize_path (path) { |
| 47 | + if ( ! path ) { |
| 48 | + return '/'; |
| 49 | + } |
| 50 | + |
| 51 | + if ( path.startsWith(this.mountpoint) ) { |
| 52 | + path = path.slice(this.mountpoint.length); |
| 53 | + } |
| 54 | + |
| 55 | + if ( ! path.startsWith('/') ) { |
| 56 | + path = '/' + path; |
| 57 | + } |
| 58 | + |
| 59 | + return path; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Performs a stat operation on the given FSNode. |
| 64 | + * |
| 65 | + * @param {Object} param |
| 66 | + * @param {FSNodeContext} param.node - The node to stat. |
| 67 | + * @returns {Promise<Object|null>} - The result of the stat operation, or `null` if the node doesn't exist. |
| 68 | + * |
| 69 | + * If the result is not null, the returned object includes following fields: |
| 70 | + * - `is_dir` {boolean} — `true` if the node is a directory. |
| 71 | + * - `public` {boolean} — `true` if the node is public (read/write access for everyone). |
| 72 | + * - `user_id` {number} — The ID of the user who owns the node. |
| 73 | + * |
| 74 | + * (ref: https://github.com/HeyPuter/puter/blob/8e58fabb7156d02c0e396ad26788e25ab0138db8/src/backend/src/services/database/sqlite_setup/0001_create-tables.sql#L70-L99) |
| 75 | + */ |
| 76 | + async stat ({ |
| 77 | + node, |
| 78 | + }) { |
| 79 | + const inner_path = this._normalize_path(node?.path); |
| 80 | + |
| 81 | + // for now, assume the path is a dir |
| 82 | + if ( this.directories.has(inner_path) ) { |
| 83 | + const full_path = _path.join(this.mountpoint, inner_path); |
| 84 | + |
| 85 | + return { |
| 86 | + is_public: true, |
| 87 | + |
| 88 | + path: full_path, |
| 89 | + |
| 90 | + name: _path.basename(full_path), |
| 91 | + |
| 92 | + // TODO (xiaochen): get the user id from database, the `user_id` must be set no |
| 93 | + // matter what. |
| 94 | + user_id: 1, |
| 95 | + |
| 96 | + is_dir: true, |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Create a new directory. |
| 105 | + * |
| 106 | + * @param {Object} param |
| 107 | + * @param {Context} param.context - The context of the operation. |
| 108 | + * @param {FSNodeContext} param.parent - The parent node to create the directory in. Must exist and be a directory. |
| 109 | + * @param {string} param.name - The name of the new directory. |
| 110 | + * @returns {Promise<FSNodeContext>} - The new directory node. |
| 111 | + */ |
| 112 | + async mkdir({ context, parent, name }) { |
| 113 | + const inner_path = this._normalize_path(_path.join(parent.path, name)); |
| 114 | + const full_path = _path.join(this.mountpoint, inner_path); |
| 115 | + |
| 116 | + this.directories.add(inner_path); |
| 117 | + |
| 118 | + // create the node |
| 119 | + const fs = context.get('services').get('filesystem'); |
| 120 | + const node = await fs.node(full_path); |
| 121 | + return node; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +module.exports = { |
| 126 | + MemoryFSProvider, |
| 127 | +}; |
0 commit comments