forked from rohit-94/raid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecover.cpp
More file actions
124 lines (91 loc) · 2.4 KB
/
recover.cpp
File metadata and controls
124 lines (91 loc) · 2.4 KB
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
#include "recover.h"
#include <stdlib.h>
#include <unistd.h>
/***************************************************************************
Name : recoverDisk
Input Param : char *
Output Param : void
Description : This functions recovers the data present in disk that has crashed, with the help of all other disk's data.
***************************************************************************/
void recoverDisk(int disk_num)
{
FILE *f[5];
string destination_filepath[5];
int n = 5;
destination_filepath[0]="./raid_files/f1";
destination_filepath[1]="./raid_files/f2";
destination_filepath[2]="./raid_files/f3";
destination_filepath[3]="./raid_files/f4";
destination_filepath[4]="./raid_files/f5";
for(int i = 0 ; i < n ; ++i)
{
if(i != disk_num)
{
f[i] = fopen(destination_filepath[i].c_str(),"rb");
if(NULL == f[i])
{
printf("Disk access for disk %d denied", i);
}
}
}
f[disk_num] = fopen(destination_filepath[disk_num].c_str(),"wb");
unsigned char buff[4][256];
unsigned char parity[256];
while(1)
{
int j = 0;
int fno;
char proclnk[512];
char filename[512];
int nread[4] = {0,0,0,0};
for(int i=0 ; i < n ; ++i)
{
if(i != disk_num)
{
/***************************************************************************/
cout << j;
fno = fileno(f[i]);
sprintf(proclnk, "/proc/self/fd/%d", fno);
ssize_t r = readlink(proclnk, filename, 512);
if (r < 0)
{
printf("failed to readlink\n");
exit(1);
}
filename[r] = '\0';
printf("fp -> fno -> filename: %p -> %d -> %s\n",
f[i], fno, filename);
/**************************************************************************/
nread[j] = fread(buff[j],1,255,f[i]);
++j;
/******************************************************************************/
buff[j-1][nread[j-1]] =(unsigned char) '\0';
cout << buff[j-1] << endl << endl;
/******************************************************************************/
}
}
printf("nread :- %d %d %d %d\n",nread[0],nread[1],nread[2],nread[3]);
if(nread[0])
{
for(int i = 0 ; i < nread[0] ; ++i)
{
parity[i] = buff[0][i]^buff[1][i]^buff[2][i]^buff[3][i];
}
fwrite(parity, 1 , nread[0] , f[disk_num]);
}
else
{
break;
}
}
for(int i = 0 ; i < n ; ++i)
{
fclose(f[i]);
}
printf("Over\n");
}
int main()
{
recoverDisk(2);
return 0;
}