summaryrefslogtreecommitdiff
path: root/src/ddhardrescue.c
blob: 67c061be47a988186d5f20e207c6b54674aaceeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <signal.h>
#include <pthread.h>
#include <string.h> /* For memset() */

#include "slices_evt.h"
#include "utils.h"
#include "recover.h"
#include "cursesview.h"

// Global variable shared by all threads to say "finish current operation and go away"
int end=0;

void sigHookAbrt() {
	end=1;
}

// Main thread procedures declaration and a struct for passing arguments in a clean way
void *procWorker(void *a);
void *procViewer(void *a);

struct threadArgs {
	struct progArgs *progArgs;
	slices_evt_t *slices;
};

int main(int argc, char **argv) {
	// System structures
	struct sigaction sa;
	pthread_t tWorker;

	// Main data structure
	slices_evt_t slices;

	// Progam and threads arguments
	struct progArgs args;
	struct threadArgs tArgs;

	// Algorithmic needs
	int res, i;
	
	// Parse command-line arguments	
	res=parseArgs(argc, argv, &args);
	if (res!=0) {
		usage(argv[0]);
		return 1;
	}

	// Set signals behavior
	memset(&sa,0,sizeof(sa));
	sa.sa_handler=sigHookAbrt;
	res=sigaction(SIGABRT, &sa, NULL);
	if (res!=0) {
		return 2;
	}

	//XXX Remove srand : only for simulation
	srand(4);

	// Initialize main data structure
	//XXX provide a method to do that ?
	memset(&slices, 0, sizeof(slices));
	slices.data=slicesNewSingleton(args.beginSector, args.endSector, S_UNKNOWN);
	if ( slices.data == NULL ) {
		return 3;
	}
	slices.data->min=args.beginSector;
	slices.data->max=args.endSector;
	res=pthread_mutex_init(&(slices.eventListenerMutex), NULL);
	if (res!=0) {
		return 4;
	}

	// Threads preparation, creation and start
	memset(&tArgs, 0, sizeof(tArgs));
	tArgs.progArgs=&args;
	tArgs.slices=&slices;


	res=pthread_create(&tWorker, NULL, procWorker, &tArgs);
	if (res!=0) {
		return 5;
	}

	// Ncurses interface run in the main thread
	(void) procViewer((void*)&tArgs);

	// Thread join point (wait worker thread when viewer is done)
	(void) pthread_join(tWorker, NULL);

	//Final dump of datas	
	address_t blockSize=0;
	char *dump;
	dump=slicesDump(slices.data, &blockSize, 10000, args.beginSector, args.endSector);
	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.eventListenerMutex));

	//XXX Method for that
	slice_t *curr, *toFree;
	i=0; curr=slices.data->first;
	while (curr!=NULL) {
		i++;
		toFree=curr;
		curr=curr->next;
		free(toFree);
	}
	free(slices.data);

	return 0;
}

void *procWorker(void *a) {
	struct threadArgs *tArgs = (struct threadArgs *)a;

	//XXX : We will need something more controlable than just a blocking call to the main algorithm
	recover(
		tArgs->slices,
		tArgs->progArgs->src,
		tArgs->progArgs->dst,
		tArgs->progArgs->ddOpts
	);
	
	return a;
}


void *procViewer(void *a) {
	struct threadArgs *tArgs = (struct threadArgs *)a;
	
	cursesMainLoop(
		tArgs->slices
	);

	return a;
}