Skip to content

Commit

Permalink
Fix corrupt .ice file loading crash and implement safe save at -Save …
Browse files Browse the repository at this point in the history
…and quit- dialog
  • Loading branch information
cavearr committed Jan 22, 2025
1 parent 00ef2cb commit 351d535
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
24 changes: 16 additions & 8 deletions app/scripts/controllers/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ angular

//-- If there was a .ice file given
if (filepath) {

//-- Check the filepath
if (fs.existsSync(filepath)) {
console.log('OPEN PROJECT',filepath);

//-- Open the file
project.open(filepath);
Expand Down Expand Up @@ -392,7 +392,7 @@ angular



$scope.saveProject = function () {
$scope.saveProject = function (afterSaveProjectAction) {
if (
(typeof common.isEditingSubmodule !== "undefined" &&
common.isEditingSubmodule === true) || graph.breadcrumbs.length > 1
Expand All @@ -412,10 +412,16 @@ angular

var filepath = project.path;
if (filepath) {
project.save(filepath, function () {
reloadCollectionsIfRequired(filepath);
});
resetChangedStack();
project.save(filepath, () => {
reloadCollectionsIfRequired(filepath);
resetChangedStack();
if(afterSaveProjectAction){
afterSaveProjectAction();

}

});

} else {
$scope.saveProjectAs();
}
Expand All @@ -428,6 +434,7 @@ angular

project.save(filepath, function () {
reloadCollectionsIfRequired(filepath);

});
resetChangedStack();
if (localCallback) {
Expand Down Expand Up @@ -650,8 +657,9 @@ angular
callback: function (closeEvent) {
switch (closeEvent.index) {
case 0:
$scope.saveProject();
win.close(true);
$scope.saveProject(()=>{
win.close(true);
});
break;
case 1:
win.close(true);
Expand Down
38 changes: 20 additions & 18 deletions app/scripts/services/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,13 @@ angular.module('icestudio')
utils.beginBlockingTask();
//-- Timeout needed in windows systems
if(this.waitForIcestudioReady()){
//setTimeout(function(){
_this._decoupledOpen(filepath,emptyPath);
}else{
setTimeout(function(){

_this.open(filepath,emptyPath);
},300);
}
//},500);
};

this._decoupledOpen = function(filepath,emptyPath){
Expand All @@ -93,6 +91,8 @@ angular.module('icestudio')

})
.catch(function () {

utils.endBlockingTask();
alertify.error(gettextCatalog.getString('Invalid project format'), 30);
});
};
Expand Down Expand Up @@ -394,7 +394,23 @@ angular.module('icestudio')
this.save = function (filepath, callback) {
var backupProject = false;
var name = utils.basename(filepath);

let self=this;
const doSaveProject = ()=>{
utils.saveFile(filepath, pruneProject(project))
.then(() =>{
let bdir=utils.filepath2buildpath(self.filepath);
common.setBuildDir(bdir);
alertify.success(gettextCatalog.getString('Project {{name}} saved', { name: utils.bold(name) }));
if (callback) {
callback();
}

})
.catch(function (error) {
alertify.error(error, 30);
});
};//doSaveProject

if (subModuleActive) {

backupProject = utils.clone(project);
Expand Down Expand Up @@ -452,21 +468,7 @@ angular.module('icestudio')
this.path = filepath;
this.filepath = filepath;
}
let self=this;
function doSaveProject() {
utils.saveFile(filepath, pruneProject(project))
.then(function () {
if (callback) {
callback();
}
let bdir=utils.filepath2buildpath(self.filepath);
common.setBuildDir(bdir);
alertify.success(gettextCatalog.getString('Project {{name}} saved', { name: utils.bold(name) }));
})
.catch(function (error) {
alertify.error(error, 30);
});
}


};

Expand Down
1 change: 0 additions & 1 deletion app/scripts/services/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,6 @@ angular.module('icestudio')
// If not exists we need to create empty file to block it until write it
nodeFs.writeFileSync(filepath, '');
}

// Try to get the file ownership
const release = await fsLock.lock(filepath, { retries: 10 }); // Retry 10 times if is locked

Expand Down

0 comments on commit 351d535

Please sign in to comment.