Author Topic: My entire C file cleared and lost by CB  (Read 18610 times)

Offline @nthony

  • Multiple posting newcomer
  • *
  • Posts: 17
My entire C file cleared and lost by CB
« on: August 27, 2007, 01:19:19 pm »
Please help me, C::B has just crashed and with it, ate my ENTIRE project's C file (over 4000 lines of code). This represents MONTHS of work for me (my last back up was a while ago) and I am completely lost as to what I can do now.
It crashed while I was switching tabs between files within the editor, the file I was switching from was unscathed, but the file I was switching to was completely emptied. I need to know if there is ANY place that C::B maintains its own back-ups or history copies of files it has opened, or for that matter even Windows?
I've just lost it right now, and I won't shutdown my computer until I've exhausted every resource, so please if anyone knows any way I can retrieve the last saved copy please let me know. My mind is blown right now, days and hours of work have been lost...

I've been going over what just happened in my mind as its spinning and I just cant understand for the life of me WHY it would choose to eat my file... even though code:blocks rarely crashes, it could be unfortunate if you have forgotten to save, but why in god's name would it maliciously EAT everything you've already had before the save point... I just dont get it, it almost seems deliberately evil. I managed to find a recently created file named codeblocks.rpt after the crash, I hope this can shed some light on where my file might be (and I hope to god it still exists).

[attachment deleted by admin]
« Last Edit: August 27, 2007, 01:54:07 pm by @nthony »

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: My entire C file cleared and lost by CB
« Reply #1 on: August 27, 2007, 02:42:45 pm »
If you have the autosave plugin installed, you should have a recent backup created automatically. By default, if you have not changed the settings, that will be a file with the same name as your original source, and .save appended.
Other than that, Code::Blocks never saves anything or writes to any files (other than its config file) unless you explicitely tell it to do so.

Saving files is done atomically using temporary files and switching file pointers after completion, so if anything crashes while saving, the original file will always be either 100% ok, or 100% untouched, or found in the temp folder (under a random name).

You may want to check if your files are really empty. It is quite possible that your sources are still there, and you only have the encoding messing up for some reason (look at file info in Explorer, if the file size is not 0, then it is not empty!).
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline @nthony

  • Multiple posting newcomer
  • *
  • Posts: 17
Re: My entire C file cleared and lost by CB
« Reply #2 on: August 28, 2007, 03:24:40 am »
File size/contents is completely 0 in explorer. I don't even have the heart to re-open it one more time, its just killing me right now.
I will continue checking for a .save file, but I do not believe auto save was on (as I often save my own work frequently), and nothing has turned up yet.

Even in the application I was creating in CB, writing to a user file only occured after writing to a temp file completed succesfully. I did this explicitly to prevent eating the user's file in case of a crash during writing.
I am not so certain CB does the same on all occasions, for it makes no sense as to why a 4000+ line C file would magically be emptied of its contents, even on a crash. I've experienced the odd crash on CB and, at most, have lost the last couple of lines I wrote before saving, but never has it emptied my source file. I've been trying to avoid thinking about this ordeal, but everytime I do it's as to "why? how could this happen?", and I can only come up with one logicial explanation: Somewhere, somehow, someone has carelessly fopen'd the original file with write access, hoping that the transaction would complete normally, but in this case it crashed shortly after opening it, but promptly before the contents could be written. In an even more malicous twist, this whole process occured inbetween rebuilding the project, so as if to taunt me even more, CB has deleted my last working binary file and object file, so there's not even a chance for a "final", or an attempt at reverse engineering it.
Maybe there's a more complex, better explanation, but as for now, I'm more concerned about retreiving my file. What about Window's facilities, such as system restore, etc? might it have saved a back-up somewhere?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9508
Re: My entire C file cleared and lost by CB
« Reply #3 on: August 28, 2007, 07:55:09 am »
What about Window's facilities, such as system restore, etc? might it have saved a back-up somewhere?
You might try undelete tools for NTFS, but they are usually not free. (I'm not sure if this one is, maybe: http://ntfsundelete.com/). Keep in mind that installing a new software might overwrite the part in the partition with the data that got lost. So at least install in another partition. This also applies to any application that creates temp-files for example. There might also be a *.cbTemp file around or in your either application data or my files folder might be a new folder called cb_[any_number] with data saved from the crash.
With regards, Morten.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ

Offline @nthony

  • Multiple posting newcomer
  • *
  • Posts: 17
Re: My entire C file cleared and lost by CB
« Reply #4 on: August 28, 2007, 11:33:50 am »
Unfortunately, the file hasn't just been deleted, but CB has written all zeroes to it, which I think is the bane of most undelete utilities.. but in desperation, its worth a try. Since the incident, there have been a number of other (temp) files created by other programs as well, so the prospects don't look good. I've searched for any and everything modified and/or created during the time of the crash, and nothing helpful has surfaced (just the file itself, and some .layout, .depend, etc CB files, no saved data). So I guess that's that... after something like this happens, you just don't even have the heart to start over again...

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: My entire C file cleared and lost by CB
« Reply #5 on: August 28, 2007, 01:55:09 pm »
I've looked at everything that opens, read, writes or otherwise touches files, found nothing that could possibly truncate or write a bunch of zeroes to a file under normal conditions (of course everything is possible in a crash...). :(

The algorithm used to save files is as this:
1. if file.c does not exist, write data directly to file, end of operation
2. write data to file.c.cbTemp
3. rename file.c to file.c.backup
4. rename file.c.cbTemp to file.c
5. delete file.c.backup

So, .cbTemp and .backup are the file types to look for, when doing recovery.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: My entire C file cleared and lost by CB
« Reply #6 on: August 28, 2007, 04:37:49 pm »
Acutally the above is not 100% correct, since some wizards will indeed overwrite existing files without warning. But since you were not running a wizard, I guess that is not related.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9508
Re: My entire C file cleared and lost by CB
« Reply #7 on: August 28, 2007, 05:04:05 pm »
Acutally the above is not 100% correct, since some wizards will indeed overwrite existing files without warning.
:shock: Which one? Are there still wizards that do so? I thought we have fixed that finally?!
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ

Offline @nthony

  • Multiple posting newcomer
  • *
  • Posts: 17
Re: My entire C file cleared and lost by CB
« Reply #8 on: August 28, 2007, 05:14:51 pm »
If step 4, renaming of file.c.cbTemp, is not succesful, what happens to .cbTemp? Is it deleted along with .backup, or left in place? Also, even more scary is the possibility that the writing operation did complete successfully, instead CB might have cleared the file through the editor. So in that situation, the file algorithm would have functioned properly, but whatever decided to clear the contents of the editor, then save it, and then crash on top of that, would be at fault. I don't believe any wizards and/or third-party plugins were running at the time.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: My entire C file cleared and lost by CB
« Reply #9 on: August 28, 2007, 05:51:24 pm »
Quote
:shock: Which one? Are there still wizards that do so? I thought we have fixed that finally?!
All wizards that generate content do. Wizards that copy files do not.

1. Make a folder, call it for example overwrite
2. Copy a file into this folder, for example foo.cpp  (make sure this is NOT a file you still need!)
3. Start the Plugin wizard, tell it that your project name is overwrite
4. Click OK until you get to name your plugin, name it foo
5. Click OK
6. Your file foo.cpp is gone.

Quote
If step 4, renaming of file.c.cbTemp, is not succesful, what happens to .cbTemp?
If it is successful, a delayed delete is issued on the backup file. This is an extra security measure, deleting the file only after a grace period of 3 seconds. The reasoning behind this is that if you have a power failure at the precise moment of file renames and both the old and new versions are screwed up for some reason, the backup isn't deleted too. It is assumed that this "extra time" is good enough to give the hard disk time to physically lay down changes before anything is deleted.

If the rename fails, the backup file is renamed to the original file name (this is significantly different from the wx "safe write" implementation, which actually loses the data in this case).

Quote
Also, even more scary is the possibility that the writing operation did complete successfully, instead CB might have cleared the file through the editor.
Hmm yes, if this happens (I don't see a reason why it should, but you never know... during a crash anything can happen) then obviously your data will be overwritten. But I see no way to prevent this, we have no way of telling.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline @nthony

  • Multiple posting newcomer
  • *
  • Posts: 17
Re: My entire C file cleared and lost by CB
« Reply #10 on: August 28, 2007, 06:38:49 pm »
Unfortunately the undelete utility was able to find everything, including files I had deleted years ago, except any of the .c, .cbTemp, or .backup files I was looking for.
If it helps, here is some more information on the situation:
I was using a Nightly build from a month or two ago (SVN 4154 Jun 22, 07). After putting some finishing touches on what was to be my next release of the program, I proceeded to rebuild the project (at this time CodeBlocks had been running for some time, nearly 8-10 hours). It began giving me strange errors about undefined references in my object files, which I knew were clearly defined and had not changed any code that would make them undefined. I tried rebuilding again, and after the same errors appeared, I tried looking at the other .c and .h files to see if something had gone wrong there. As I had thought, nothing had changed to produce the error (since I was only working on the one c file at a time), and quite stumped, I went to switch tabs back to the first file again. This is when it locked up and promptly crashed. At first, I was not alarmed as CB has crashed on me before, and had saved my work (via the rebuild) before it crashed anyways, so I simply re-opened it. I was a little disturbed to see an empty file, but sometimes the file tabs don't load correctly in C::B so I just found the file under the "Sources" folder in the project tree-list and double-clicked it. The full realization of what had happened (and with it the feeling of total nausea, the one you might get after realizing you left your wallet and everything in it at a bar the night before), occurred after, when repeatedly opening the file, each time it came up empty.

Quote
Hmm yes, if this happens (I don't see a reason why it should, but you never know... during a crash anything can happen) then obviously your data will be overwritten. But I see no way to prevent this, we have no way of telling.
Well, if there is, say, code that refreshes the editor between switching file tabs, then perhaps if it fails to refresh properly, any pending file writing should be cancelled. Still I'm not sure why CB would decide to save after switching file tabs, unless it was responding to the crash by attempting to save all data before being terminated. In which case, if the unstable state of the program has caused the contents of the editor to be cleared, then it might have ended up doing more harm then good. If this is what is occuring, (although it is a long-shot assumption on my part) then one way of prevention would be that, in the event that CB does try to save data before crashing, the editor (or some internal monitoring mechanism) should flag an exception when ever it has had its contents changed adversely (i.e. if it failed to refresh), which would then suspend any pending file writing until the exception has been dealt with.
But as I said, this is all just a guess as to what did happen, though I think an "overwriting" flaw in the method of file writing is more probable than the above scenario. Did you guys have a look at the .RPT file, any much info from that?
« Last Edit: August 28, 2007, 06:59:49 pm by @nthony »

Offline manmach

  • Multiple posting newcomer
  • *
  • Posts: 39
Re: My entire C file cleared and lost by CB
« Reply #11 on: August 28, 2007, 08:15:20 pm »
Although this won't cure your current predicament, now might be a good time to consider a version control tool. There really is no excuse for any developer not to use it. Might I suggest Subversion and something like Second Copy for you automated backups? You will at least prevent a similar catastrophe in the future.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: My entire C file cleared and lost by CB
« Reply #12 on: August 29, 2007, 12:21:12 am »
Although this won't cure your current predicament, now might be a good time to consider a version control tool. There really is no excuse for any developer not to use it. Might I suggest Subversion and something like Second Copy for you automated backups? You will at least prevent a similar catastrophe in the future.
This is a wise man, listen to him! Seriously. Subversion rules.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline @nthony

  • Multiple posting newcomer
  • *
  • Posts: 17
Re: My entire C file cleared and lost by CB
« Reply #13 on: August 29, 2007, 04:45:17 am »
I have another question:
If I can obtain a copy of the executable from a recent version, is it worth attempting to reverse engineer it? Exactly how accurate-to-original can I expect the code to be?

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: My entire C file cleared and lost by CB
« Reply #14 on: August 29, 2007, 09:51:40 am »
Probably as much work as writing it again. Maybe the tools have significantly improved in the last couple of years, but in my experience, reverse engineering optimized C/C++ code is not worth the effort.

The only language where reverse engineering works in a decent way is Java (which is not a surprise). You can usually restore 99.99% of your original code in 2 minutes (put aside variable names).
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."