trunk 20080912
[gitenigma.git] / lib / system / file_eraser.cpp
blob59a69846b22df4e9741c59e43f42ce7d5344bec6
1 #include <lib/system/file_eraser.h>
2 #include <lib/system/init.h>
3 #include <lib/system/init_num.h>
4 #include <sys/types.h>
5 #include <sys/wait.h>
6 #include <unistd.h>
7 #include <errno.h>
9 eBackgroundFileEraser *eBackgroundFileEraser::instance;
11 eBackgroundFileEraser::eBackgroundFileEraser()
12 :messages(this,1)
14 if (!instance)
15 instance=this;
16 CONNECT(messages.recv_msg, eBackgroundFileEraser::gotMessage);
17 run();
20 eBackgroundFileEraser::~eBackgroundFileEraser()
22 messages.send(Message::quit);
23 if ( thread_running() )
24 kill();
25 if (instance==this)
26 instance=0;
29 void eBackgroundFileEraser::thread()
31 nice(5);
32 exec();
35 void eBackgroundFileEraser::erase(const char *filename)
37 messages.send(Message(Message::erase, filename?strdup(filename):0));
40 void eBackgroundFileEraser::gotMessage(const Message &msg )
42 switch (msg.type)
44 case Message::erase:
45 if ( msg.filename )
47 if ( ::unlink(msg.filename) < 0 )
48 eDebug("remove file %s failed (%m)", msg.filename);
49 else
50 eDebug("file %s erased", msg.filename);
51 free((char*)msg.filename);
53 break;
54 case Message::quit:
55 quit(0);
56 break;
57 default:
58 eDebug("unhandled thread message");
62 eAutoInitP0<eBackgroundFileEraser> init_eBackgroundFilEraser(eAutoInitNumbers::configuration+1, "Background File Eraser");