diff options
-rw-r--r-- | Makefile | 2 | ||||
-rwxr-xr-x | inc/slices.h | 2 | ||||
-rwxr-xr-x | inc/util.h | 10 | ||||
-rwxr-xr-x | inc/utils.h | 15 | ||||
-rwxr-xr-x | src/ddhardrescue.c | 128 | ||||
-rwxr-xr-x | src/recover.c | 3 | ||||
-rwxr-xr-x | src/slices.c | 25 | ||||
-rwxr-xr-x | src/util.c | 24 | ||||
-rwxr-xr-x | src/utils.c | 24 |
9 files changed, 171 insertions, 62 deletions
@@ -1,6 +1,6 @@ BIN=ddhardrescue CFLAGS=-Iinc -Wall -g -pg -LIBS=-lpanel -lncurses +LIBS=-lpanel -lncurses -lpthread #FIXME : liste des dépendances = tous les .c trouvés. C'est nul. diff --git a/inc/slices.h b/inc/slices.h index 5e56269..78fb4c7 100755 --- a/inc/slices.h +++ b/inc/slices.h @@ -31,7 +31,7 @@ int sliceSplit(slices_t *slices, slice_t *initialSlice, address_t splitAt, slice slices_t *slicesNewEmpty(); -slices_t *sliceNewSingleton(address_t begin, address_t end, sliceStatus_t status); +slices_t *slicesNewSingleton(address_t begin, address_t end, sliceStatus_t status); void slicesAppend(slices_t *slices, slice_t *slice); diff --git a/inc/util.h b/inc/util.h deleted file mode 100755 index 0724e72..0000000 --- a/inc/util.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef UTIL_H -#define UTIL_H - -#include "slices.h" - -int parseArgs(int argc, char **argv, char *src, char *dst, char *ddOpts, address_t *beginSector, address_t *endSector); - -void usage(); - -#endif /*UTIL_H*/ diff --git a/inc/utils.h b/inc/utils.h new file mode 100755 index 0000000..c045b83 --- /dev/null +++ b/inc/utils.h @@ -0,0 +1,15 @@ +#ifndef UTILS_H +#define UTILS_H + +#include "slices.h" + +struct progArgs { + char *src, *dst, *ddOpts, *dump; + address_t beginSector, endSector, blockSize; +}; + +int parseArgs(int argc, char **argv, struct progArgs *args); + +void usage(char *progname); + +#endif /*UTILS_H*/ diff --git a/src/ddhardrescue.c b/src/ddhardrescue.c index df61bc6..486ea41 100755 --- a/src/ddhardrescue.c +++ b/src/ddhardrescue.c @@ -1,49 +1,137 @@ #include <stdio.h> #include <stdlib.h> +#include <signal.h> #include <stdint.h> - +#include <string.h> +#include <pthread.h> + #include "slices.h" #include "recover.h" +#include "utils.h" int end=0; unsigned long c=0; +struct threadArgs { + struct progArgs *progArgs; + struct slices_manage_t *slices; +}; + +typedef enum { E_BOUND, E_TYPE } sliceEventKind_t; + +typedef struct { + slices_t *data; + pthread_mutex_t mutex; + void (*slices_listener)(sliceEventKind_t eventKind, slices_t *slices, slice_t *slice); +} slices_manage_t; + + void sigHookAbrt() { end=1; } -int main() { - char *src, *dst, *ddOpts, *dump; - address_t beginSector, endSector, blockSize; - int /*depth,*/i; - slices_t *slices; - slice_t *curr, *toFree; +void *procWorker(void *a); +void *procViewer(void *a); + +int main(int argc, char **argv) { + // System structures + struct sigaction sa; + pthread_t tWorker, tViewer; + + // Main data structures + slices_manage_t slices; + + // Progam and threads arguments + struct progArgs args; + struct threadArgs tArgs; + + // Algorithmic needs + int res, i; + - //TODO Parse args - //TODO signal... + // 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); - slices=recover(src,dst,ddOpts,beginSector,endSector/*,depth*/); - - blockSize=0; - dump=slicesDump(slices, &blockSize, 1000, beginSector, endSector); + + // Data structure initialization + memset(&slices, 0, sizeof(slices)); + slices.data=slicesNewSingleton(args.beginSector, args.endSector, S_UNKNOWN); + res=pthread_mutex_init(&(slices.mutex), NULL); + if (res!=0) { + return 3; + } + + // 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 4; + } + + res=pthread_create(&tViewer, NULL, procViewer, &tArgs); + if (res!=0) { + return 5; + } + + // Thread join point + (void) pthread_join(tWorker, NULL); + (void) pthread_join(tViewer, NULL); + + //Final dump of datas + address_t blockSize=0; + char *dump; + dump=slicesDump(slices.data, &blockSize, 1000, args.beginSector, args.endSector); puts(dump); free(dump); printf("blockSize==%lld\n", blockSize); - printf("c==%ld\n", c); - printf("slices->count==%d\n", slices->count); + printf("slices->count==%d\n", slices.data->count); - curr=slices->first; - i=0; + //Resources desallocation + (void) pthread_mutex_destroy(&(slices.mutex)); + + slice_t *curr, *toFree; + i=0; curr=slices.data->first; while (curr!=NULL) { i++; toFree=curr; curr=curr->next; free(toFree); } - free(slices); + free(slices.data); +} + +void * procWorker(void *a) { + struct threadArgs *tArgs = (struct threadArgs *)a; + + (void) recover( + tArgs->progArgs->src, + tArgs->progArgs->dst, + tArgs->progArgs->ddOpts, + tArgs->progArgs->beginSector, + tArgs->progArgs->endSector + ); + + return a; +} - printf("i==%d\n", i); +void *procViewer(void *a) { - return 0; + return a; } diff --git a/src/recover.c b/src/recover.c index 5e9e187..dc88877 100755 --- a/src/recover.c +++ b/src/recover.c @@ -11,7 +11,8 @@ slices_t *recover(char *src, char *dst, char *ddOpts, address_t beginSector, add int res; // Initialization : we want to try to recover the beginning of the whole zone - slices=slicesNew(); + //FIXME : already done in main + slices=slicesNewEmpty(); sliceToRead=sliceNew(beginSector, endSector, S_UNKNOWN, NULL); if (sliceToRead==NULL) { exit(1);//TODO diff --git a/src/slices.c b/src/slices.c index 68e39f9..9f40b9e 100755 --- a/src/slices.c +++ b/src/slices.c @@ -75,14 +75,29 @@ int sliceSplit(slices_t *slices, slice_t *initialSlice, address_t splitAt, slice return 1 + (splitBeforeSingularity?1:0) + (splitAfterSingularity?1:0); } -slices_t *slicesNew() { +slices_t *slicesNewEmpty() { slices_t *ss = malloc(1*sizeof(slices_t)); - if (ss!=NULL) { - ss->count=0; - ss->first=NULL; - ss->last=NULL; + if (ss==NULL) { + return NULL; } + memset(ss, 0, sizeof(slices_t)); + return ss; +} + +slices_t *slicesNewSingleton(address_t begin, address_t end, sliceStatus_t status) { + slice_t *s=NULL; + slices_t *ss = slicesNewEmpty(); + if (ss==NULL) { + return NULL; + } + s=sliceNew(begin,end,status,NULL); + if (s==NULL) { + free(ss); + return NULL; + } + slicesAppend(ss,s); + return ss; } diff --git a/src/util.c b/src/util.c deleted file mode 100755 index cb01f08..0000000 --- a/src/util.c +++ /dev/null @@ -1,24 +0,0 @@ -#include <stdio.h> -#include "util.h" - -int parseArgs(int argc, char **argv, char *src, char *dst, char *ddOpts, address_t *beginSector, address_t *endSector) { - //TODO : implement that - src="/dev/sdb"; - dst="./test.img"; - ddOpts=""; - *beginSector=0; - *endSector=21474836480ULL; //10 Tio - - return 0; -} - -void usage(char *progname) { - printf( -"Usage %s <src> <dst> [<beginSector> <endSector> [ddOpts]] \ - <src>\t\t \ - <dst>\t\t \ - <beginSector>\t \ - <endSector>\t \ - <ddOpts>\t \ -", progname); -} diff --git a/src/utils.c b/src/utils.c new file mode 100755 index 0000000..a19b021 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,24 @@ +#include <stdio.h> +#include "utils.h" + +int parseArgs(int argc, char **argv, struct progArgs *args) { + //TODO : implement that + args->src="/dev/sdb"; + args->dst="./test.img"; + args->ddOpts=""; + args->beginSector=0; + args->endSector=21474836480ULL; //10 Tio + + return 0; +} + +void usage(char *progname) { + printf( +"Usage %s <src> <dst> [<beginSector> <endSector> [ddOpts]]\n\ + <src>\t\t\n\ + <dst>\t\t\n\ + <beginSector>\t\n\ + <endSector>\t\n\ + <ddOpts>\t\n\ +", progname); +} |