Fails with the rm Command

Yesterday I was working on making a video game and by accident I deleted the player class with the rm command so I looked around if I could recover the files and I found this

http://www.linuxforums.org/forum/newbie/69673-not-possible-recover-files-deleted-using-rm.html

It is a cool shell script so I decided to enhance it with c++.

Note: I do use the system command (Sorry)

rm.sh

1
2
3
4
5
6
7
8
#!/bin/bash

mkdir ~/.Trash &> /dev/null

while [ ! -z "$1" ]; do
    mv "$1" ~/.Trash/
    shift
done


rm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>

#include <cstdlib>

/*
  This is a simple program that uses the shell script found at
  http://www.linuxforums.org/forum/newbie/69673-not-possible-recover-files-deleted-using-rm.html
  For it to work we need the script in /usr/bin/ under the name of rm.sh

  Then place this in /usr/bin/ under any name you want.

  Hope this helps someone :)
*/

const std::string Rm = "sh /usr/bin/rm.sh";

int main(int argc , char* argv[])
{
    int i = 0;

    std::string Choice = "";
    std::cout << "Using the rm command with following arguments : " << std::endl;

    for (i = 0 ; i < argc ; i++)
    {
	std::cout << argv [i] << " ";
    }

    std::cout << std::endl;;

    std::cout << "Is that okay? (Yes or No)" << std::endl;
    std::cin >> Choice;

    if (Choice == "Yes" || Choice == "yes")
    {
	for (i = 1 ; i < argc ; i++)
	{
	    std::string Temp = argv [i];
	    std::string Command = Rm + " " + Temp;
	    system (Command.c_str ());
	}
    }
    else
    {
	std::cout << "Oh!! Okay have a great day :)" << std::endl;
    }

    return 0;
}


I hope I can at least help someone.

~PS I eventually had to rewrite the player class again lesson learned.
Last edited on
The third time that you delete all your documents you start being more cautious, xP.
Still, git saved me the other day.

I think that the loops are superfluous in both codes.
There was an old thread (by chrisname) about a trash implementation. http://www.cplusplus.com/forum/lounge/15031/ (it derrails several times)
Last edited on
@ne555 Lol. The thread was a nice read. Also thanks for the great idea of using git I am totally making an account now. Maybe I can write a script to update my repository every 5 minutes for me XD. Also I agree 100% the loops are unnecessary I have to admit the idea was half baked.
Last edited on
When something like that happens, it's a good idea to draw the plug immediately and try an undelete utility (using a live CD). If that fails, you can search your disk for a specific string using dd or an already existing tool like foremost.
@Athar Will do!! Thanks for the tip. :)
closed account (1yR4jE8b)
This is why I use version control for everything: accidentally deleting a file means at most a few dozen patch hunks of changes since my last commit is lost.
Last edited on
If your file is less <= 4096 bytes (i.e., occupies only one block on the hard disk) you might be able to find it with grep if your OS hasn't reallocated the block already.
Last edited on
@darkestfright Yeah I just made a github account I am now using it as much as I can

@chrisname I never knew that I guess I should have just greped around for the file. Anyway I did rewrite the class I have to say the new one is better than the old.
Sometimes a rewrite is a good thing, sometimes it's a bad thing. *derails* Rewriting software can set you behind the competition and reintroduce bugs that you had fixed before, but can also allow you to fix design problems in previous versions. Anyone agree?
The decision to undertake a re-write hinges on a few factors. First, is that program so critical it must be rectified heavily to make it workable? Can end users make do with the minor hiccups? Is that program an important component in the whole working IT system? Is that program related to earning monies for the companies? Is that program non-critical so as to speak? Are there work-around to achieve the same outcome?

So yes re-write is a major undertaking depending on the program complexity. It should not be overlooked leniently.
I was using polymorphism and inheritance so I did not have to rewrite a lot of the functions.
I gave up the project I lost inspiration, it was going to be a galaga clonse but I am tired of clones . So I am working on an original game. Besides the old project had a lot of mistakes.
If you would like to see I have the project on github.

http://github.com/TheMassiveChipmunk/Epic-Quest

Note: I did not upload the resources because some of them were copyrighted.

If you see any bad programming practices please tell me I am still new to game development.
A couple weeks ago my co-worker accidentally did rm -rf in the root directory of our beta server... He noticed after it deleted the boot, bin, and part of etc.
Topic archived. No new replies allowed.