From 891e799e5e9073a38034c5255dae47f6c0a500ea Mon Sep 17 00:00:00 2001 From: ProgrammerIn-wonderland <3838shah@gmail.com> Date: Mon, 21 Oct 2024 17:00:28 -0400 Subject: [PATCH] WIP symlink, net_device virtio, move fixes, upload binary fixes --- src/emulator/src/main.js | 6 +- src/puter-js/src/modules/FileSystem/index.js | 2 + .../src/modules/FileSystem/operations/move.js | 16 +++++- .../modules/FileSystem/operations/symlink.js | 55 +++++++++++++++++++ .../modules/FileSystem/operations/upload.js | 2 +- submodules/v86 | 2 +- 6 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 src/puter-js/src/modules/FileSystem/operations/symlink.js diff --git a/src/emulator/src/main.js b/src/emulator/src/main.js index e5129cb0..2bdd8993 100644 --- a/src/emulator/src/main.js +++ b/src/emulator/src/main.js @@ -326,8 +326,10 @@ window.onload = async function() }, // bzimage_initrd_from_filesystem: true, autostart: true, - - network_relay_url: emu_config.network_relay ?? "wisp://127.0.0.1:4000", + net_device: { + relay_url: emu_config.network_relay ?? "wisp://127.0.0.1:4000", + type: "virtio" + }, virtio_console: true, }); diff --git a/src/puter-js/src/modules/FileSystem/index.js b/src/puter-js/src/modules/FileSystem/index.js index 55276991..eabe0325 100644 --- a/src/puter-js/src/modules/FileSystem/index.js +++ b/src/puter-js/src/modules/FileSystem/index.js @@ -10,6 +10,7 @@ import read from "./operations/read.js"; import move from "./operations/move.js"; import write from "./operations/write.js"; import sign from "./operations/sign.js"; +import symlink from './operations/symlink.js'; // Why is this called deleteFSEntry instead of just delete? because delete is // a reserved keyword in javascript import deleteFSEntry from "./operations/deleteFSEntry.js"; @@ -32,6 +33,7 @@ export class PuterJSFileSystemModule extends AdvancedBase { move = move; write = write; sign = sign; + symlink = symlink; static NARI_METHODS = { stat: { diff --git a/src/puter-js/src/modules/FileSystem/operations/move.js b/src/puter-js/src/modules/FileSystem/operations/move.js index eb87c245..a5a1b89d 100644 --- a/src/puter-js/src/modules/FileSystem/operations/move.js +++ b/src/puter-js/src/modules/FileSystem/operations/move.js @@ -1,9 +1,10 @@ import * as utils from '../../../lib/utils.js'; import getAbsolutePathForApp from '../utils/getAbsolutePathForApp.js'; +import stat from "./stat.js" +import path from "../../../lib/path.js" const move = function (...args) { let options; - // If first argument is an object, it's the options if (typeof args[0] === 'object' && args[0] !== null) { options = args[0]; @@ -36,6 +37,19 @@ const move = function (...args) { options.source = getAbsolutePathForApp(options.source); options.destination = getAbsolutePathForApp(options.destination); + if (!options.new_name) { + // Handler to check if dest is supposed to be a file or a folder + try { + const destStats = await stat.bind(this)(options.destination); // this is meant to error if it doesn't exist + if (!destStats.is_dir) { + throw "is not directory" // just a wuick way to just to the catch + } + } catch (e) { + options.new_name = path.basename(options.destination); + options.destination = path.dirname(options.destination); + } + } + // create xhr object const xhr = utils.initXhr('/move', this.APIOrigin, this.authToken); diff --git a/src/puter-js/src/modules/FileSystem/operations/symlink.js b/src/puter-js/src/modules/FileSystem/operations/symlink.js new file mode 100644 index 00000000..4ec96f92 --- /dev/null +++ b/src/puter-js/src/modules/FileSystem/operations/symlink.js @@ -0,0 +1,55 @@ +import getAbsolutePathForApp from '../utils/getAbsolutePathForApp.js'; +import pathLib from '../../../lib/path.js'; + +// This only works for absolute symlinks for now +const symlink = async function (target, linkPath) { + + + // If auth token is not provided and we are in the web environment, + // try to authenticate with Puter + if(!puter.authToken && puter.env === 'web'){ + try{ + await puter.ui.authenticateWithPuter(); + }catch(e){ + // if authentication fails, throw an error + throw 'Authentication failed.'; + } + } + + // convert path to absolute path + linkPath = getAbsolutePathForApp(linkPath); + target = getAbsolutePathForApp(target); + const name = pathLib.basename(linkPath); + const linkDir = pathLib.dirname(linkPath) + + const op = + { + op: 'symlink', + path: linkDir, + name: name, + target: target + }; + + const formData = new FormData(); + formData.append('operation', JSON.stringify(op)); + + try { + const response = await fetch(this.APIOrigin + "/batch", { + method: 'POST', + headers: { 'Authorization': `Bearer ${puter.authToken}` }, + body: formData + }); + if (response.status !== 200) { + const error = await response.text(); + console.error("[symlink] fetch error: ", error); + throw error; + } + } catch (e) { + console.error("[symlink] fetch error: ", e); + throw e; + } + + +} + +export default symlink; \ No newline at end of file diff --git a/src/puter-js/src/modules/FileSystem/operations/upload.js b/src/puter-js/src/modules/FileSystem/operations/upload.js index 8ca19c6d..cd4f6de7 100644 --- a/src/puter-js/src/modules/FileSystem/operations/upload.js +++ b/src/puter-js/src/modules/FileSystem/operations/upload.js @@ -104,7 +104,7 @@ const upload = async function(items, dirPath, options = {}){ // blob else if(items instanceof Blob){ // create a File object from the blob - let file = new File([items], options.name, { type: "text/plain" }); + let file = new File([items], options.name, { type: "application/octet-stream" }); entries = [file]; // add FullPath property to each entry for(let i=0; i