summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ddhardrescue.c112
1 files changed, 77 insertions, 35 deletions
diff --git a/src/ddhardrescue.c b/src/ddhardrescue.c
index de89c26..ba6781e 100644
--- a/src/ddhardrescue.c
+++ b/src/ddhardrescue.c
@@ -9,10 +9,10 @@
#include "recover.h"
#include "utils.h"
-
+// Global variable shared by all threads to say "finish current operation and go away"
int end=0;
-//FIXME remove me, only for tests
-unsigned long c=0;
+WINDOW *winUpdateSliceDump=NULL;
+static pthread_mutex_t ncursesWriteMutex = PTHREAD_MUTEX_INITIALIZER;
struct threadArgs {
struct progArgs *progArgs;
@@ -24,18 +24,7 @@ void sigHookAbrt() {
end=1;
}
-/*TODO : remove that when procViewer is done
-void dumper(slices_evt_t *slicesEvt, slice_t *modifiedSlice) {
- address_t blockSize=0;
- char *dump;
- dump=slicesDump(slicesEvt->data, &blockSize, 3000, 0, 21474836480ULL);
- system("clear");
- puts(dump);
- free(dump);
-
-}
-*/
-
+// Main procedures of each thread
void *procWorker(void *a);
void *procViewer(void *a);
@@ -76,8 +65,7 @@ int main(int argc, char **argv) {
// Data structure initialization
memset(&slices, 0, sizeof(slices));
slices.data=slicesNewSingleton(args.beginSector, args.endSector, S_UNKNOWN);
-// slices.eventListener=dumper;
- res=pthread_mutex_init(&(slices.mutex), NULL);
+ res=pthread_mutex_init(&(slices.eventListenerMutex), NULL);
if (res!=0) {
return 3;
}
@@ -87,6 +75,7 @@ int main(int argc, char **argv) {
tArgs.progArgs=&args;
tArgs.slices=&slices;
+
res=pthread_create(&tWorker, NULL, procWorker, &tArgs);
if (res!=0) {
return 4;
@@ -98,18 +87,20 @@ int main(int argc, char **argv) {
// Thread join point
(void) pthread_join(tWorker, NULL);
- /*Final dump of datas
+ //Final dump of datas
address_t blockSize=0;
char *dump;
dump=slicesDump(slices.data, &blockSize, 1000, args.beginSector, args.endSector);
- puts(dump);
- free(dump);
+ if (dump != NULL) {
+ puts(dump);
+ free(dump);
+ }
printf("blockSize==%lld\n", blockSize);
printf("slices->count==%d\n", slices.data->count);
- */
+
//Resources desallocation
- (void) pthread_mutex_destroy(&(slices.mutex));
+ (void) pthread_mutex_destroy(&(slices.eventListenerMutex));
slice_t *curr, *toFree;
i=0; curr=slices.data->first;
@@ -124,7 +115,7 @@ int main(int argc, char **argv) {
return 0;
}
-void * procWorker(void *a) {
+void *procWorker(void *a) {
struct threadArgs *tArgs = (struct threadArgs *)a;
recover(
@@ -137,34 +128,85 @@ void * procWorker(void *a) {
return a;
}
+void cursesUpdateSliceDump(slices_evt_t *slicesEvt, slice_t *modifiedSlice) {
+// char *strProgress="|/-\\";
+// static int progress=0;
+ char *toPrint;
+ address_t blockSize=0;
+
+//FIXME : do that realy
+ pthread_mutex_lock(&ncursesWriteMutex);
+
+
+ toPrint=slicesDump(slicesEvt->data, &blockSize, 1000, 0, 1000);
+ if (toPrint != NULL) {
+ attron(COLOR_PAIR(4));
+ mvwprintw(winUpdateSliceDump, 1, 0, toPrint);
+// attroff(COLOR_PAIR(4));
+
+ update_panels();
+ doupdate();
+
+ free(toPrint);
+ }
+
+/* sprintf(toPrint, "%c - %p %p", strProgress[progress], slicesEvt, modifiedSlice);
+ progress=(progress+1)%strlen(strProgress);
+*/
+ pthread_mutex_unlock(&ncursesWriteMutex);
+}
+
#define CURSESWIN_COUNT 3
void *procViewer(void *a) {
WINDOW *wins[CURSESWIN_COUNT];
PANEL *panels[CURSESWIN_COUNT];
- int ch;
- //struct threadArgs *tArgs = (struct threadArgs *)a;
+ int ch, i;
+ char str[2];
+ struct threadArgs *tArgs = (struct threadArgs *)a;
+
cursesInit(wins, panels, CURSESWIN_COUNT);
- /* Show it on the screen */
- attron(COLOR_PAIR(4));
- mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F2 to Exit)");
- attroff(COLOR_PAIR(4));
+ wattron(wins[CURSESWIN_COUNT-1], COLOR_PAIR(4));
+ mvwprintw(wins[CURSESWIN_COUNT-1], 1, 0, "(F2 to Exit)");
+// wattroff(wins[CURSESWIN_COUNT-1], COLOR_PAIR(4));
+
+ update_panels();
doupdate();
+ /* Enable worker listener */
+ //FIXME : check pointers ?
+ winUpdateSliceDump=wins[1];
+ tArgs->slices->eventListener=cursesUpdateSliceDump;
+
while((ch = getch()) != KEY_F(2)) {
+
+ pthread_mutex_lock(&ncursesWriteMutex);
+
switch(ch) {
- case 1:
- case 2:
- case 3:
- top_panel(panels[ch]);
+ case '1':
+ case '2':
+ case '3':
+ str[0]=ch; str[1]='\0'; // int (ascii value+flags) to string
+ i=strtol(str, (char **)NULL, 10); // string to real integer
+ if (i>0 && i<=CURSESWIN_COUNT) {
+ top_panel(panels[i-1]);
+ update_panels();
+ }
break;
}
- update_panels();
doupdate();
+
+ pthread_mutex_unlock(&ncursesWriteMutex);
}
- endwin();
+
+ pthread_mutex_lock(&(tArgs->slices->eventListenerMutex));
+ tArgs->slices->eventListener=NULL;
+ pthread_mutex_unlock(&(tArgs->slices->eventListenerMutex));
+
+ end=1;
+ cursesUnInit(wins, panels, CURSESWIN_COUNT);
return a;
}