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