Fixed EXDEV error on rename.

Ylian Saint-Hilaire committed Jan 31, 2020 at 14:44 UTC a466c2fed10485a2cd5cca5d657d6f569111057d
3 files changed +28 -8
common.js
+10
@@ -258,4 +258,14 @@ module.exports.translationsToJson = function(t) {
258 }
259 arr2.sort(function (a, b) { if (a.en > b.en) return 1; if (a.en < b.en) return -1; return 0; });
260 return JSON.stringify({ strings: arr2 }, null, ' ');
261 +}
262 +
263 +module.exports.copyFile = function(source, target, cb) {
264 + var cbCalled = false, rd = fs.createReadStream(source);
265 + rd.on('error', function (err) { done(err); });
266 + var wr = fs.createWriteStream(target);
267 + wr.on('error', function (err) { done(err); });
268 + wr.on('close', function (ex) { done(); });
269 + rd.pipe(wr);
270 + function done(err) { if (!cbCalled) { cb(err); cbCalled = true; } }
271 }
\ No newline at end of file
package.json
+1 -1
@@ -1,6 +1,6 @@
1 {
2 "name": "meshcentral",
3 - "version": "0.4.8-g",
3 + "version": "0.4.8-h",
4 "keywords": [
5 "Remote Management",
6 "Intel AMT",
webserver.js
+17 -7
@@ -1657,7 +1657,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
1657 if (domain == null) { parent.debug('web', 'handleTermsRequest: Bad domain'); res.end("Not Found"); return; }
1658 if ((domain.loginkey != null) && (domain.loginkey.indexOf(req.query.key) == -1)) { res.end("Not Found"); return; } // Check 3FA URL key
1659 parent.debug('web', 'handleRootPostRequest, action: ' + req.body.action);
1660 -
1660 +
1661 switch (req.body.action) {
1662 case 'login': { handleLoginRequest(req, res, true); break; }
1663 case 'tokenlogin': {
@@ -1677,7 +1677,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
1677 }
1678
1679 // Return true if it looks like we are using a real TLS certificate.
1680 - obj.isTrustedCert = function(domain) {
1680 + obj.isTrustedCert = function (domain) {
1681 if (obj.args.notls == true) return false; // We are not using TLS, so not trusted cert.
1682 if ((domain != null) && (typeof domain.trustedcert == 'boolean')) return domain.trustedcert; // If the status of the cert specified, use that.
1683 if (typeof obj.args.trustedcert == 'boolean') return obj.args.trustedcert; // If the status of the cert specified, use that.
@@ -2261,8 +2261,18 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
2261 try { obj.fs.mkdirSync(obj.parent.path.join(obj.parent.filespath, domainx)); } catch (e) { }
2262 try { obj.fs.mkdirSync(xfile.fullpath); } catch (e) { }
2263
2264 - obj.fs.rename(file.path, fpath, function () {
2265 - obj.parent.DispatchEvent([user._id], obj, 'updatefiles'); // Fire an event causing this user to update this files
2264 + // Rename the file
2265 + obj.fs.rename(file.path, fpath, function (err) {
2266 + if (err && (err.code === 'EXDEV') && fs.copyFile) {
2267 + // On some Linux, the rename will fail with a "EXDEV" error, do a copy+unlink instead.
2268 + obj.common.copyFile(file.path, fpath, function (err) {
2269 + obj.fs.unlink(file.path, function (err) {
2270 + obj.parent.DispatchEvent([user._id], obj, 'updatefiles'); // Fire an event causing this user to update this files
2271 + });
2272 + });
2273 + } else {
2274 + obj.parent.DispatchEvent([user._id], obj, 'updatefiles'); // Fire an event causing this user to update this files
2275 + }
2276 });
2277 } else {
2278 try { obj.fs.unlink(file.path, function (err) { }); } catch (e) { }
@@ -3440,7 +3450,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
3450 } else {
3451 // Use default security headers
3452 var geourl = (domain.geolocation ? ' *.openstreetmap.org' : '');
3443 - var selfurl = ((args.notls !== true) ? (' wss://' + req.headers.host) : (' ws://' + req.headers.host));
3453 + var selfurl = ((args.notls !== true) ? (' wss://' + req.headers.host) : (' ws://' + req.headers.host));
3454 var headers = {
3455 'Referrer-Policy': 'no-referrer',
3456 'X-XSS-Protection': '1; mode=block',
@@ -3621,7 +3631,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
3631 obj.app.use(function (req, res, next) {
3632 parent.debug('web', '404 Error ' + req.url);
3633 var domain = getDomain(req);
3624 - res.status(404).render(getRenderPage('error404', req), getRenderArgs({ }, domain));
3634 + res.status(404).render(getRenderPage('error404', req), getRenderArgs({}, domain));
3635 });
3636 }
3637
@@ -4364,7 +4374,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
4374 if (exists) { args.lang = acceptLanguages[i]; res.render(fileOptions[acceptLanguages[i]], args); } else { res.render(filename, args); }
4375 });
4376 return;
4367 - }
4377 + }
4378 }
4379 }
4380 }