follow eThread changes: mainly thread_running -> sync, hasStarted()
[enigma2.git] / lib / components / file_eraser.cpp
1 #include <lib/components/file_eraser.h>
2 #include <lib/base/eerror.h>
3 #include <lib/base/init.h>
4 #include <lib/base/init_num.h>
5 #include <sys/types.h>
6 #include <sys/wait.h>
7 #include <unistd.h>
8 #include <errno.h>
9
10 eBackgroundFileEraser *eBackgroundFileEraser::instance;
11
12 eBackgroundFileEraser::eBackgroundFileEraser()
13         :messages(this,1), stop_thread_timer(this)
14 {
15         if (!instance)
16                 instance=this;
17         CONNECT(messages.recv_msg, eBackgroundFileEraser::gotMessage);
18         CONNECT(stop_thread_timer.timeout, eBackgroundFileEraser::idle);
19 }
20
21 void eBackgroundFileEraser::idle()
22 {
23         quit(0);
24 }
25
26 eBackgroundFileEraser::~eBackgroundFileEraser()
27 {
28         messages.send(Message::quit);
29         if (instance==this)
30                 instance=0;
31 }
32
33 void eBackgroundFileEraser::thread()
34 {
35         hasStarted();
36         nice(5);
37         reset();
38         runLoop();
39         stop_thread_timer.stop();
40 }
41
42 void eBackgroundFileEraser::erase(const char *filename)
43 {
44         if (filename)
45         {
46                 char buf[255];
47                 snprintf(buf, 255, "%s.$$$", filename);
48                 if (rename(filename, buf)<0)
49                         ;/*perror("rename file failed !!!");*/
50                 else
51                 {
52                         messages.send(Message(Message::erase, strdup(buf)));
53                         run();
54                 }
55         }
56 }
57
58 void eBackgroundFileEraser::gotMessage(const Message &msg )
59 {
60         switch (msg.type)
61         {
62                 case Message::erase:
63                         if ( msg.filename )
64                         {
65                                 if ( ::unlink(msg.filename) < 0 )
66                                         eDebug("remove file %s failed (%m)", msg.filename);
67                                 else
68                                         eDebug("file %s erased", msg.filename);
69                                 free((char*)msg.filename);
70                         }
71                         stop_thread_timer.start(2000, true); // stop thread in two seconds
72                         break;
73                 case Message::quit:
74                         quit(0);
75                         break;
76                 default:
77                         eDebug("unhandled thread message");
78         }
79 }
80
81 eAutoInitP0<eBackgroundFileEraser> init_eBackgroundFilEraser(eAutoInitNumbers::configuration+1, "Background File Eraser");