|
| 1 | +/* |
| 2 | + * GNU AGPL-3.0 License |
| 3 | + * |
| 4 | + * Copyright (c) 2021 - present core.ai . All rights reserved. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify it under |
| 7 | + * the terms of the GNU Affero General Public License as published by the Free |
| 8 | + * Software Foundation, either version 3 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 11 | + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License along |
| 15 | + * with this program. If not, see https://opensource.org/licenses/AGPL-3.0. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +// jshint ignore: start |
| 20 | +/*global process*/ |
| 21 | +/*eslint no-console: 0*/ |
| 22 | +/*eslint strict: ["error", "global"]*/ |
| 23 | + |
| 24 | +const {ERR_CODES, Errors} = require('./errno'); |
| 25 | +const {NativeFS} = require('./fslib_native'); |
| 26 | +const {Constants} = require('./constants'); |
| 27 | +const {Mounts} = require('./fslib_mounts'); |
| 28 | +const {FsWatch} = require('./fslib_watch'); |
| 29 | +const {filerCopy} = require('./filerlib_copy.js'); |
| 30 | + |
| 31 | +let filerLib = null; |
| 32 | +let filerShell = null; |
| 33 | + |
| 34 | +/** |
| 35 | + * Offers functionality similar to mkdir -p |
| 36 | + * |
| 37 | + * Asynchronous operation. No arguments other than a possible exception |
| 38 | + * are given to the completion callback. |
| 39 | + */ |
| 40 | +function mkdir_p (fsLib, path, mode, callback, position) { |
| 41 | + const osSep = '/'; |
| 42 | + const parts = filerLib.path.normalize(path).split(osSep); |
| 43 | + |
| 44 | + mode = mode || process.umask(); |
| 45 | + position = position || 0; |
| 46 | + |
| 47 | + if (position >= parts.length) { |
| 48 | + return callback(null); |
| 49 | + } |
| 50 | + |
| 51 | + var directory = parts.slice(0, position + 1).join(osSep) || osSep; |
| 52 | + fsLib.stat(directory, function(err) { |
| 53 | + if (err === null) { |
| 54 | + mkdir_p(fsLib, path, mode, callback, position + 1); |
| 55 | + } else { |
| 56 | + fsLib.mkdir(directory, mode, function (error) { |
| 57 | + if (error && error.code !== 'EEXIST') { |
| 58 | + return callback(error); |
| 59 | + } else { |
| 60 | + mkdir_p(fsLib, path, mode, callback, position + 1); |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +function _ensure_mount_directory() { |
| 68 | + fileSystemLib.mkdirs(Constants.MOUNT_POINT_ROOT); |
| 69 | + NativeFS.refreshMountPoints(); |
| 70 | +} |
| 71 | + |
| 72 | +function _getFirstFunctionIndex(argsArray) { |
| 73 | + for(let i=0; i<argsArray.length; i++){ |
| 74 | + if (typeof argsArray[i] === 'function') { |
| 75 | + return i; |
| 76 | + } |
| 77 | + } |
| 78 | + return -1; |
| 79 | +} |
| 80 | + |
| 81 | +const fileSystemLib = { |
| 82 | + mountNativeFolder: async function (...args) { |
| 83 | + return NativeFS.mountNativeFolder(...args); |
| 84 | + }, |
| 85 | + readdir: function (...args) { // (path, options, callback) |
| 86 | + let path = args[0]; |
| 87 | + if(Mounts.isMountPath(path) || Mounts.isMountSubPath(path)) { |
| 88 | + return NativeFS.readdir(...args); |
| 89 | + } |
| 90 | + return filerLib.fs.readdir(...args); |
| 91 | + }, |
| 92 | + stat: function (...args) { // (path, callback) |
| 93 | + let path = args[0]; |
| 94 | + if(Mounts.isMountSubPath(path)) { |
| 95 | + return NativeFS.stat(...args); |
| 96 | + } |
| 97 | + return filerLib.fs.stat(...args); |
| 98 | + }, |
| 99 | + readFile: function (...args) { // (path, options, callback) |
| 100 | + let path = args[0]; |
| 101 | + if(Mounts.isMountSubPath(path)) { |
| 102 | + return NativeFS.readFile(...args); |
| 103 | + } |
| 104 | + return filerLib.fs.readFile(...args); |
| 105 | + }, |
| 106 | + writeFile: function (...args) { // (path, data, options, callback) |
| 107 | + let path = args[0]; |
| 108 | + function callbackInterceptor(...interceptedArgs) { |
| 109 | + let err = interceptedArgs.length >= 1 ? interceptedArgs[0] : null; |
| 110 | + if(!err){ |
| 111 | + FsWatch.reportChangeEvent(path); |
| 112 | + } |
| 113 | + if(args.originalCallback){ |
| 114 | + args.originalCallback(...interceptedArgs); |
| 115 | + } |
| 116 | + } |
| 117 | + let callbackIndex = _getFirstFunctionIndex(args); |
| 118 | + if(callbackIndex !== -1) { |
| 119 | + args.originalCallback = args[callbackIndex]; |
| 120 | + args[callbackIndex] = callbackInterceptor; |
| 121 | + } |
| 122 | + |
| 123 | + if(Mounts.isMountSubPath(path)) { |
| 124 | + return NativeFS.writeFile(...args); |
| 125 | + } |
| 126 | + return filerLib.fs.writeFile(...args); |
| 127 | + }, |
| 128 | + mkdir: function (...args) { // (path, mode, callback) |
| 129 | + let path = args[0]; |
| 130 | + function callbackInterceptor(...interceptedArgs) { |
| 131 | + let err = interceptedArgs.length >= 1 ? interceptedArgs[0] : null; |
| 132 | + if(!err){ |
| 133 | + FsWatch.reportCreateEvent(path); |
| 134 | + } |
| 135 | + if(args.originalCallback){ |
| 136 | + args.originalCallback(...interceptedArgs); |
| 137 | + } |
| 138 | + } |
| 139 | + let callbackIndex = _getFirstFunctionIndex(args); |
| 140 | + if(callbackIndex !== -1) { |
| 141 | + args.originalCallback = args[callbackIndex]; |
| 142 | + args[callbackIndex] = callbackInterceptor; |
| 143 | + } |
| 144 | + |
| 145 | + if(Mounts.isMountSubPath(path)) { |
| 146 | + return NativeFS.mkdir(...args); |
| 147 | + } |
| 148 | + return filerLib.fs.mkdir(...args); |
| 149 | + }, |
| 150 | + rename: function (oldPath, newPath, cb) { |
| 151 | + function callbackInterceptor(...args) { |
| 152 | + let err = args.length >= 1 ? args[0] : null; |
| 153 | + if(!err){ |
| 154 | + FsWatch.reportUnlinkEvent(oldPath); |
| 155 | + FsWatch.reportCreateEvent(newPath); |
| 156 | + } |
| 157 | + if(cb){ |
| 158 | + cb(...args); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + if(Mounts.isMountPath(oldPath) || Mounts.isMountPath(newPath)) { |
| 163 | + throw new Errors.EPERM('Mount root directory cannot be deleted.'); |
| 164 | + } else if(Mounts.isMountSubPath(oldPath) && Mounts.isMountSubPath(newPath)) { |
| 165 | + return NativeFS.rename(oldPath, newPath, callbackInterceptor); |
| 166 | + } |
| 167 | + return filerLib.fs.rename(oldPath, newPath, callbackInterceptor); |
| 168 | + }, |
| 169 | + unlink: function (path, cb) { |
| 170 | + function callbackInterceptor(...args) { |
| 171 | + let err = args.length >= 1 ? args[0] : null; |
| 172 | + if(!err){ |
| 173 | + FsWatch.reportUnlinkEvent(path); |
| 174 | + } |
| 175 | + if(cb){ |
| 176 | + cb(...args); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + if(Mounts.isMountPath(path)) { |
| 181 | + throw new Errors.EPERM('Mount root directory cannot be deleted.'); |
| 182 | + } else if(Mounts.isMountSubPath(path)) { |
| 183 | + return NativeFS.unlink(path, callbackInterceptor); |
| 184 | + } |
| 185 | + if (typeof path !== 'string') { |
| 186 | + callbackInterceptor(new Errors.EINVAL('Invalid arguments.')); |
| 187 | + return; |
| 188 | + } |
| 189 | + return filerShell.rm(path, { recursive: true }, callbackInterceptor); |
| 190 | + }, |
| 191 | + copy: function (src, dst, cb) { |
| 192 | + function callbackInterceptor(...args) { |
| 193 | + let err = args.length >= 1 ? args[0] : null; |
| 194 | + if(!err){ |
| 195 | + FsWatch.reportCreateEvent(dst); |
| 196 | + } |
| 197 | + if(cb){ |
| 198 | + cb(...args); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + if(Mounts.isMountSubPath(src) && Mounts.isMountSubPath(dst)) { |
| 203 | + return NativeFS.copy(src, dst, callbackInterceptor); |
| 204 | + } else if(!Mounts.isMountSubPath(src) && !Mounts.isMountSubPath(dst)) { |
| 205 | + return filerCopy(src, dst, callbackInterceptor); |
| 206 | + } |
| 207 | + throw new Errors.ENOSYS('Phoenix fs copy across filer and native not yet supported'); |
| 208 | + }, |
| 209 | + showSaveDialog: function () { |
| 210 | + throw new Errors.ENOSYS('Phoenix fs showSaveDialog function not yet supported.'); |
| 211 | + }, |
| 212 | + watch: function (...args) { |
| 213 | + return FsWatch.watch(...args); |
| 214 | + }, |
| 215 | + unwatch: function (...args) { |
| 216 | + return FsWatch.unwatch(...args); |
| 217 | + }, |
| 218 | + unwatchAll: function (...args) { |
| 219 | + return FsWatch.unwatchAll(...args); |
| 220 | + }, |
| 221 | + moveToTrash: function () { |
| 222 | + throw new Errors.ENOSYS('Phoenix fs moveToTrash function not yet supported.'); |
| 223 | + }, |
| 224 | + mkdirs: function (path, mode, recursive, callback) { |
| 225 | + if (typeof recursive !== 'boolean') { |
| 226 | + callback = recursive; |
| 227 | + recursive = false; |
| 228 | + } |
| 229 | + |
| 230 | + if (typeof callback !== 'function') { |
| 231 | + callback = function () { |
| 232 | + // Do Nothing |
| 233 | + }; |
| 234 | + } |
| 235 | + |
| 236 | + if (!recursive) { |
| 237 | + fileSystemLib.mkdir(path, mode, callback); |
| 238 | + } else { |
| 239 | + mkdir_p(fileSystemLib, path, mode, callback); |
| 240 | + } |
| 241 | + }, |
| 242 | + BYTE_ARRAY_ENCODING: NativeFS.BYTE_ARRAY_ENCODING, |
| 243 | + ERR_NOT_FOUND: ERR_CODES.ERROR_CODES.ENOENT, |
| 244 | + ERR_EISDIR: ERR_CODES.ERROR_CODES.EISDIR, |
| 245 | + ERR_EINVAL: ERR_CODES.ERROR_CODES.EINVAL, |
| 246 | + ERR_FILE_EXISTS: ERR_CODES.ERROR_CODES.EEXIST |
| 247 | +}; |
| 248 | + |
| 249 | +fileSystemLib.copyFile = fileSystemLib.copy; |
| 250 | + |
| 251 | +function initFsLib(FilerLib) { |
| 252 | + filerLib = FilerLib; |
| 253 | + filerShell = new filerLib.fs.Shell(); |
| 254 | + window.path = FilerLib.path; |
| 255 | + window.fs = fileSystemLib; |
| 256 | + |
| 257 | + _ensure_mount_directory(); |
| 258 | +} |
| 259 | + |
| 260 | +module.exports ={ |
| 261 | + initFsLib |
| 262 | +}; |
0 commit comments