Skip to content

Commit 891e799

Browse files
WIP symlink, net_device virtio, move fixes, upload binary fixes
1 parent 13248a9 commit 891e799

File tree

6 files changed

+78
-5
lines changed

6 files changed

+78
-5
lines changed

src/emulator/src/main.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,10 @@ window.onload = async function()
326326
},
327327
// bzimage_initrd_from_filesystem: true,
328328
autostart: true,
329-
330-
network_relay_url: emu_config.network_relay ?? "wisp://127.0.0.1:4000",
329+
net_device: {
330+
relay_url: emu_config.network_relay ?? "wisp://127.0.0.1:4000",
331+
type: "virtio"
332+
},
331333
virtio_console: true,
332334
});
333335

src/puter-js/src/modules/FileSystem/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import read from "./operations/read.js";
1010
import move from "./operations/move.js";
1111
import write from "./operations/write.js";
1212
import sign from "./operations/sign.js";
13+
import symlink from './operations/symlink.js';
1314
// Why is this called deleteFSEntry instead of just delete? because delete is
1415
// a reserved keyword in javascript
1516
import deleteFSEntry from "./operations/deleteFSEntry.js";
@@ -32,6 +33,7 @@ export class PuterJSFileSystemModule extends AdvancedBase {
3233
move = move;
3334
write = write;
3435
sign = sign;
36+
symlink = symlink;
3537

3638
static NARI_METHODS = {
3739
stat: {

src/puter-js/src/modules/FileSystem/operations/move.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as utils from '../../../lib/utils.js';
22
import getAbsolutePathForApp from '../utils/getAbsolutePathForApp.js';
3+
import stat from "./stat.js"
4+
import path from "../../../lib/path.js"
35

46
const move = function (...args) {
57
let options;
6-
78
// If first argument is an object, it's the options
89
if (typeof args[0] === 'object' && args[0] !== null) {
910
options = args[0];
@@ -36,6 +37,19 @@ const move = function (...args) {
3637
options.source = getAbsolutePathForApp(options.source);
3738
options.destination = getAbsolutePathForApp(options.destination);
3839

40+
if (!options.new_name) {
41+
// Handler to check if dest is supposed to be a file or a folder
42+
try {
43+
const destStats = await stat.bind(this)(options.destination); // this is meant to error if it doesn't exist
44+
if (!destStats.is_dir) {
45+
throw "is not directory" // just a wuick way to just to the catch
46+
}
47+
} catch (e) {
48+
options.new_name = path.basename(options.destination);
49+
options.destination = path.dirname(options.destination);
50+
}
51+
}
52+
3953
// create xhr object
4054
const xhr = utils.initXhr('/move', this.APIOrigin, this.authToken);
4155

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import getAbsolutePathForApp from '../utils/getAbsolutePathForApp.js';
2+
import pathLib from '../../../lib/path.js';
3+
4+
// This only works for absolute symlinks for now
5+
const symlink = async function (target, linkPath) {
6+
7+
8+
// If auth token is not provided and we are in the web environment,
9+
// try to authenticate with Puter
10+
if(!puter.authToken && puter.env === 'web'){
11+
try{
12+
await puter.ui.authenticateWithPuter();
13+
}catch(e){
14+
// if authentication fails, throw an error
15+
throw 'Authentication failed.';
16+
}
17+
}
18+
19+
// convert path to absolute path
20+
linkPath = getAbsolutePathForApp(linkPath);
21+
target = getAbsolutePathForApp(target);
22+
const name = pathLib.basename(linkPath);
23+
const linkDir = pathLib.dirname(linkPath)
24+
25+
const op =
26+
{
27+
op: 'symlink',
28+
path: linkDir,
29+
name: name,
30+
target: target
31+
};
32+
33+
const formData = new FormData();
34+
formData.append('operation', JSON.stringify(op));
35+
36+
try {
37+
const response = await fetch(this.APIOrigin + "/batch", {
38+
method: 'POST',
39+
headers: { 'Authorization': `Bearer ${puter.authToken}` },
40+
body: formData
41+
});
42+
if (response.status !== 200) {
43+
const error = await response.text();
44+
console.error("[symlink] fetch error: ", error);
45+
throw error;
46+
}
47+
} catch (e) {
48+
console.error("[symlink] fetch error: ", e);
49+
throw e;
50+
}
51+
52+
53+
}
54+
55+
export default symlink;

src/puter-js/src/modules/FileSystem/operations/upload.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const upload = async function(items, dirPath, options = {}){
104104
// blob
105105
else if(items instanceof Blob){
106106
// create a File object from the blob
107-
let file = new File([items], options.name, { type: "text/plain" });
107+
let file = new File([items], options.name, { type: "application/octet-stream" });
108108
entries = [file];
109109
// add FullPath property to each entry
110110
for(let i=0; i<entries.length; i++){

0 commit comments

Comments
 (0)