Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<!-- Import the phoenix browser virtual file system -->
<script src="thirdparty/idb/idb-min.js"></script>
<script src="thirdparty/filer/filer.min.js"></script>
<script src="thirdparty/Buffer/buffer-min.js"></script>
<script src="phoenix/shell.js" type="module"></script>
<script src="phoenix/nohost_server.js" type="module"></script>
<!-- Warn about failed cross origin requests in Chrome -->
Expand Down
49 changes: 35 additions & 14 deletions src/phoenix/fslib.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
*/

// jshint ignore: start
/*global fs, Phoenix, process*/
/*global process*/
/*eslint no-console: 0*/
/*eslint strict: ["error", "global"]*/

import {Errors} from "./errno.js";
import NativeFS from "./fslib_native.js";
import Constants from "./constants.js";
import Mounts from "./fslib_mounts.js";

let filerLib = null;

Expand Down Expand Up @@ -73,28 +74,48 @@ function _ensure_mount_directory() {

const fileSystemLib = {
mountNativeFolder: async function (...args) {
NativeFS.mountNativeFolder(...args);
return NativeFS.mountNativeFolder(...args);
},
readdir: function (...args) {
filerLib.fs.readdir(...args);
readdir: function (...args) { // (path, options, callback)
let path = args[0];
if(Mounts.isMountSubPath(path)) {
return NativeFS.readdir(...args);
}
return filerLib.fs.readdir(...args);
},
stat: function (...args) {
filerLib.fs.stat(...args);
stat: function (...args) { // (path, callback)
let path = args[0];
if(Mounts.isMountSubPath(path)) {
return NativeFS.stat(...args);
}
return filerLib.fs.stat(...args);
},
readFile: function (...args) {
filerLib.fs.readFile(...args);
readFile: function (...args) { // (path, options, callback)
let path = args[0];
if(Mounts.isMountSubPath(path)) {
return NativeFS.readFile(...args);
}
return filerLib.fs.readFile(...args);
},
writeFile: function (...args) {
filerLib.fs.writeFile(...args);
writeFile: function (...args) { // (path, data, options, callback)
let path = args[0];
if(Mounts.isMountSubPath(path)) {
return NativeFS.writeFile(...args);
}
return filerLib.fs.writeFile(...args);
},
mkdir: function (...args) {
filerLib.fs.mkdir(...args);
mkdir: function (...args) { // (path, mode, callback)
let path = args[0];
if(Mounts.isMountSubPath(path)) {
return NativeFS.mkdir(...args);
}
return filerLib.fs.mkdir(...args);
},
rename: function (...args) {
filerLib.fs.rename(...args);
return filerLib.fs.rename(...args);
},
unlink: function (...args) {
filerLib.fs.unlink(...args);
return filerLib.fs.unlink(...args);
},
showSaveDialog: function () {
throw new Errors.ENOSYS('Phoenix fs showSaveDialog function not yet supported.');
Expand Down
52 changes: 45 additions & 7 deletions src/phoenix/fslib_native.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/

// jshint ignore: start
/*global Blob, Response, TextDecoder*/
/*global Blob, Response, TextDecoder, buffer*/
/*eslint no-console: 0*/
/*eslint strict: ["error", "global"]*/

Expand Down Expand Up @@ -51,7 +51,11 @@ async function _mkdir(paretDirHandle, dirName, callback) {
}


function mkdir(context, path, mode, callback) {
function mkdir(path, mode, callback) {
if (arguments.length < 4) {
callback = mode;
}

path = window.path.normalize(path);
let dirname= window.path.dirname(path);
let subdirName= window.path.basename(path);
Expand All @@ -67,7 +71,7 @@ function mkdir(context, path, mode, callback) {
}


function readdir(context, path, options, callback) {
function readdir(path, options, callback) {
path = window.path.normalize(path);
if (typeof options !== 'function') {
throw new Errors.ENOSYS('Filer readdir options are not yet supported');
Expand Down Expand Up @@ -103,8 +107,23 @@ async function _getFileContents(fileHandle, encoding, callback) {
}
}

function readFile(context, path, options, callback) {
function _validateFileOptions(options, enc, fileMode){
if(!options) {
options = { encoding: enc, flag: fileMode };
} else if(typeof options === 'function') {
options = { encoding: enc, flag: fileMode };
} else if(typeof options === 'string') {
options = { encoding: options, flag: fileMode };
}
return options;
}

function readFile(path, options, callback) {
path = window.path.normalize(path);

callback = arguments[arguments.length - 1];
options = _validateFileOptions(options, null, 'r');

Mounts.getHandleFromPath(path, (err, handle) => {
if(err){
callback(err);
Expand All @@ -117,7 +136,7 @@ function readFile(context, path, options, callback) {
}


function stat(context, path, callback) {
function stat(path, callback) {
path = window.path.normalize(path);
Mounts.getHandleFromPath(path, (err, handle) => {
if(err){
Expand Down Expand Up @@ -146,7 +165,21 @@ async function _writeFileWithName(paretDirHandle, fileName, encoding, data, call
}
}

function writeFile (context, path, data, options, callback) {
function writeFile (path, data, options, callback) {
callback = arguments[arguments.length - 1];
options = _validateFileOptions(options, 'utf8', 'w');
if(!buffer.Buffer.isBuffer(data)) {
if(typeof data === 'number') {
data = '' + data;
}
data = data || '';
if(typeof data !== 'string') {
data = buffer.Buffer.from(data.toString());
} else {
data = buffer.Buffer.from(data || '', options.encoding || 'utf8');
}
}

path = window.path.normalize(path);
let dirname= window.path.dirname(path);
let fileName= window.path.basename(path);
Expand All @@ -171,7 +204,12 @@ function refreshMountPoints() {

const NativeFS = {
mountNativeFolder,
refreshMountPoints
refreshMountPoints,
mkdir,
readdir,
stat,
readFile,
writeFile
};

export default NativeFS;
21 changes: 21 additions & 0 deletions src/phoenix/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ function Stats(path, fileNode, devName) {
this.name = window.path.basename(path);
}

Stats.prototype.isFile = function() {
return this.type === Constants.NODE_TYPE_FILE;
};

Stats.prototype.isDirectory = function() {
return this.type === Constants.NODE_TYPE_DIRECTORY;
};

Stats.prototype.isSymbolicLink = function() {
return this.type === Constants.NODE_TYPE_SYMBOLIC_LINK;
};

// These will always be false in Filer.
Stats.prototype.isSocket =
Stats.prototype.isFIFO =
Stats.prototype.isCharacterDevice =
Stats.prototype.isBlockDevice =
function() {
return false;
};

function _getType(handle) {
switch (handle.kind) {
case Constants.KIND_FILE: return Constants.NODE_TYPE_FILE;
Expand Down
1 change: 1 addition & 0 deletions src/thirdparty/Buffer/buffer-min.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/thirdparty/Buffer/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
The buffer module from node.js, for the browser.


Git: https://github.com/feross/buffer
Copyright (C) Feross Aboukhadijeh, and other contributors.
Originally forked from an MIT-licensed module by Romain Beauxis.
MIT License