re-commit new NFIFlash plugin (bug #480)
[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(eTimer::create(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         kill();  // i dont understand why this is needed .. in ~eThread::eThread is a kill() to..
33 }
34
35 void eBackgroundFileEraser::thread()
36 {
37         hasStarted();
38
39         nice(5);
40
41         setIoPrio(IOPRIO_CLASS_BE, 7);
42
43         reset();
44
45         runLoop();
46
47         stop_thread_timer->stop();
48 }
49
50 void eBackgroundFileEraser::erase(const char *filename)
51 {
52         if (filename)
53         {
54                 char buf[255];
55                 snprintf(buf, 255, "%s.del", filename);
56                 if (rename(filename, buf)<0)
57                         ;/*perror("rename file failed !!!");*/
58                 else
59                 {
60                         messages.send(Message(Message::erase, strdup(buf)));
61                         run();
62                 }
63         }
64 }
65
66 void eBackgroundFileEraser::gotMessage(const Message &msg )
67 {
68         switch (msg.type)
69         {
70                 case Message::erase:
71                         if ( msg.filename )
72                         {
73                                 if ( ::unlink(msg.filename) < 0 )
74                                         eDebug("remove file %s failed (%m)", msg.filename);
75                                 else
76                                         eDebug("file %s erased", msg.filename);
77                                 free((char*)msg.filename);
78                         }
79                         stop_thread_timer->start(1000, true); // stop thread in one seconds
80                         break;
81                 case Message::quit:
82                         quit(0);
83                         break;
84                 default:
85                         eDebug("unhandled thread message");
86         }
87 }
88
89 eAutoInitP0<eBackgroundFileEraser> init_eBackgroundFilEraser(eAutoInitNumbers::configuration+1, "Background File Eraser");