forked from mruffalo/ddrescue
/
io.cc
132 lines (107 loc) · 3.21 KB
/
io.cc
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
/* GNU ddrescue - Data recovery tool
Copyright (C) 2004-2020 Antonio Diaz Diaz.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _FILE_OFFSET_BITS 64
#include <cerrno>
#include <climits>
#include <csignal>
#include <cstdio>
#include <string>
#include <vector>
#include <stdint.h>
#include <unistd.h>
#include "block.h"
#include "mapbook.h"
namespace {
int volatile signum_ = 0; // user pressed Ctrl-C or similar
extern "C" void sighandler( int signum )
{ if( signum_ == 0 && signum > 0 ) signum_ = signum; }
int set_signal( const int signum, void (*handler)( int ) )
{
struct sigaction new_action;
new_action.sa_handler = handler;
sigemptyset( &new_action.sa_mask );
new_action.sa_flags = SA_RESTART;
return sigaction( signum, &new_action, 0 );
}
} // end namespace
// Returns the number of bytes really read.
// If (returned value < size) and (errno == 0), means EOF was reached.
//
int readblock( const int fd, uint8_t * const buf, const int size )
{
int sz = 0;
errno = 0;
while( sz < size )
{
const int n = read( fd, buf + sz, size - sz );
if( n > 0 ) sz += n;
else if( n == 0 ) break; // EOF
else if( errno != EINTR ) break;
errno = 0;
}
return sz;
}
// Returns the number of bytes really read.
// If (returned value < size) and (errno == 0), means EOF was reached.
//
int readblockp( const int fd, uint8_t * const buf, const int size,
const long long pos )
{
int sz = 0;
errno = 0;
if( lseek( fd, pos, SEEK_SET ) >= 0 )
while( sz < size )
{
errno = 0;
const int n = read( fd, buf + sz, size - sz );
if( n > 0 ) sz += n;
else if( n == 0 ) break; // EOF
else if( errno != EINTR ) break;
}
return sz;
}
// Returns the number of bytes really written.
// If (returned value < size), it is always an error.
//
int writeblockp( const int fd, const uint8_t * const buf, const int size,
const long long pos )
{
int sz = 0;
errno = 0;
if( lseek( fd, pos, SEEK_SET ) >= 0 )
while( sz < size )
{
errno = 0;
const int n = write( fd, buf + sz, size - sz );
if( n > 0 ) sz += n;
else if( n < 0 && errno != EINTR ) break;
}
return sz;
}
bool interrupted() { return ( signum_ > 0 ); }
void set_signals()
{
signum_ = 0;
set_signal( SIGHUP, sighandler );
set_signal( SIGINT, sighandler );
set_signal( SIGTERM, sighandler );
set_signal( SIGUSR1, SIG_IGN );
set_signal( SIGUSR2, SIG_IGN );
}
int signaled_exit()
{
set_signal( signum_, SIG_DFL );
std::raise( signum_ );
return 128 + signum_; // in case std::raise fails to exit
}