Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File/dir create/change watchers. #182

Merged
merged 1 commit into from
Oct 30, 2021
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
45 changes: 41 additions & 4 deletions src/phoenix/fslib.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,44 @@ const fileSystemLib = {
},
mkdir: function (...args) { // (path, mode, callback)
let path = args[0];
function callbackInterceptor(...interceptedArgs) {
let err = interceptedArgs.length >= 1 ? interceptedArgs[0] : null;
if(!err){
FsWatch.reportCreateEvent(path);
}
if(args.originalCallback){
args.originalCallback(...interceptedArgs);
}
}
let callbackIndex = _getFirstFunctionIndex(args);
if(callbackIndex !== -1) {
args.originalCallback = args[callbackIndex];
args[callbackIndex] = callbackInterceptor;
}

if(Mounts.isMountSubPath(path)) {
return NativeFS.mkdir(...args);
}
return filerLib.fs.mkdir(...args);
},
rename: function (oldPath, newPath, cb) {
function callbackInterceptor(...args) {
let err = args.length >= 1 ? args[0] : null;
if(!err){
FsWatch.reportUnlinkEvent(oldPath);
FsWatch.reportCreateEvent(newPath);
}
if(cb){
cb(...args);
}
}

if(Mounts.isMountPath(oldPath) || Mounts.isMountPath(newPath)) {
throw new Errors.EPERM('Mount root directory cannot be deleted.');
} else if(Mounts.isMountSubPath(oldPath) && Mounts.isMountSubPath(newPath)) {
return NativeFS.rename(oldPath, newPath, cb);
return NativeFS.rename(oldPath, newPath, callbackInterceptor);
}
return filerLib.fs.rename(oldPath, newPath, cb);
return filerLib.fs.rename(oldPath, newPath, callbackInterceptor);
},
unlink: function (path, cb) {
function callbackInterceptor(...args) {
Expand All @@ -154,16 +180,27 @@ const fileSystemLib = {
cb(...args);
}
}

if(Mounts.isMountPath(path)) {
throw new Errors.EPERM('Mount root directory cannot be deleted.');
} else if(Mounts.isMountSubPath(path)) {
return NativeFS.unlink(path, callbackInterceptor);
}
return filerShell.rm(path, { recursive: true }, callbackInterceptor);
},
copy: function (src, dst, callback) {
copy: function (src, dst, cb) {
function callbackInterceptor(...args) {
let err = args.length >= 1 ? args[0] : null;
if(!err){
FsWatch.reportCreateEvent(dst);
}
if(cb){
cb(...args);
}
}

if(Mounts.isMountSubPath(src) && Mounts.isMountSubPath(dst)) {
return NativeFS.copy(src, dst, callback);
return NativeFS.copy(src, dst, callbackInterceptor);
}
throw new Errors.ENOSYS('Phoenix fs copy on filer or across filer and native not yet supported');
},
Expand Down
30 changes: 16 additions & 14 deletions src/phoenix/fslib_watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let _watchListeners = [];
let _globmatch = null;

const WATCH_EVENT_NOTIFICATION = 'PHOENIX_WATCH_EVENT_NOTIFICATION';
// const WATCH_EVENT_CREATED = 'created';
const WATCH_EVENT_CREATED = 'created';
const WATCH_EVENT_DELETED = 'deleted';
const WATCH_EVENT_CHANGED = 'changed';

Expand Down Expand Up @@ -93,6 +93,7 @@ function _processFsWatchEvent(event, broadcast=true) {
function _listenToExternalFsWatchEvents() {
_setupBroadcastChannel();
_channel.onmessage = async function(event) {
console.log("External fs watch event: ", event.data);
_processFsWatchEvent(event.data, false);
};
}
Expand All @@ -108,28 +109,28 @@ function watch(path, ignoreGlobList, changeCallback, callback) {
callback();
}

function reportUnlinkEvent(path) {
function _triggerEvent(path, eventType) {
let pathLib = window.path;
path = pathLib.normalize(path);
let event = {
event: WATCH_EVENT_DELETED,
parentDirPath: pathLib.dirname(path),
event: eventType,
parentDirPath: `${pathLib.dirname(path)}/`,
entryName: pathLib.basename(path),
path: path
};
_processFsWatchEvent(event);
}

function reportUnlinkEvent(path) {
_triggerEvent(path, WATCH_EVENT_DELETED);
}

function reportChangeEvent(path) {
let pathLib = window.path;
path = pathLib.normalize(path);
let event = {
event: WATCH_EVENT_CHANGED,
parentDirPath: pathLib.dirname(path),
entryName: pathLib.basename(path),
path: path
};
_processFsWatchEvent(event);
_triggerEvent(path, WATCH_EVENT_CHANGED);
}

function reportCreateEvent(path) {
_triggerEvent(path, WATCH_EVENT_CREATED);
}

function unwatch(path, callback) {
Expand All @@ -151,7 +152,8 @@ const FsWatch = {
unwatch,
unwatchAll,
reportUnlinkEvent,
reportChangeEvent
reportChangeEvent,
reportCreateEvent
};

export default FsWatch;