-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy.cpp
More file actions
96 lines (73 loc) · 2.2 KB
/
Copy pathcopy.cpp
File metadata and controls
96 lines (73 loc) · 2.2 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
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iostream>
int main(int argc, char* argv[])
{
if(argc < 3)
{
std::cout << "There is some Error\n";
return -1;
}
int file = open(argv[1], O_RDONLY);
//The file can't be opened due to an internal issue
if(file == -1)
{
std::cout << "I couldn't open file \n";
exit(errno);
}
//The file can't be opened due to an internal issue
int write_in_file = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP);
if(write_in_file == -1)
{
std::cout << "I couldn't open file \n";
exit(errno);//return 0;
}
long hole = 0, data = 0, offset = 0;
long source_physical = 0, destination_physical = 0, source_logical = 0, destination_logical = 0;
char* buffer = new char[0];
long buffer_size = 0;
while(true)
{
//finding where hole starts
hole = lseek(file, offset, SEEK_HOLE);
source_physical += hole - data;
//offset = from 0 to the hole start
offset = hole;
buffer_size = hole - data;
buffer = new char[buffer_size];
//we write in buffer data that is in file and then rewrite in write_in_file
lseek(file, -buffer_size, SEEK_CUR);
read(file, buffer, buffer_size);
destination_physical += write(write_in_file, buffer, buffer_size);
data = lseek(file, offset, SEEK_DATA);
//The file can't be opened due to an internal issue
if(data == -1)
{
//if errno = ENXIO then there is no more data that we can find
if(errno == ENXIO)
{
//data = end file
data = lseek(file, 0, SEEK_END);
//in write_in_file we write holes from file
lseek(write_in_file, data - hole, SEEK_END);
destination_logical += data - hole;
break;
}
//if errno != ENXIO then there is an another problem so we stop code
else
{
std::cout << "There is some error\n";
return -1;
}
}
source_logical += data - hole;
offset = data;
lseek(write_in_file, data - hole, SEEK_END);
destination_logical += data - hole;
}
std::cout << "Source physical: " << source_physical << ", Source logical: " << source_logical;
std::cout << ", Destination physical: " << destination_physical;
std::cout << ", Destination logical: " << destination_logical;
}