File: make_test.cc

package info (click to toggle)
gddrescue 1.30-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,280 kB
  • sloc: cpp: 5,853; sh: 799; makefile: 159
file content (133 lines) | stat: -rw-r--r-- 4,730 bytes parent folder | download
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
/* GNU ddrescuelog - Tool for ddrescue mapfiles
   Copyright (C) 2011-2026 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/>.
*/
/*
This option is a work in progress. The geometries implemented so far are
some of those described in the following article:

H. Wong, "Discovering Hard Disk Physical Geometry through Microbenchmarking",
Sept., 2019. Available online at
<http://blog.stuffedcow.net/2019/09/hard-disk-geometry-microbenchmarking/>
*/

#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <unistd.h>

#include "mapfile.h"
#include "ddrescuelog.h"


namespace {

struct CHS
  {
  enum Track_order{ F, FF };
  int cylinders;
  int heads;
  int first_track_sectors;
  int last_track_sectors;
  int tracks_zone;
  Track_order track_order;
  int bad_head;
  };

CHS * chsp = 0;

} // end namespace


// cylinders:heads:sectors1[-sectors2[,tracks_zone]]:track_order:bad_head
void parse_chs( const char * const arg, const char * const pn )
  {
  const char * tail;
  if( chsp == 0 ) chsp = new CHS;
  CHS & chs = *chsp;
  chs.cylinders = getnum( arg, pn, 0, 40, 1000000, &tail );
  if( *tail != ':' )
err: { show_option_error( arg, "Invalid 'C:H:S:order:head' description in", pn );
       std::exit( 1 ); }
  chs.heads = getnum( tail + 1, pn, 0, 1, 256, &tail );
  if( *tail != ':' ) goto err;
  chs.first_track_sectors = getnum( tail + 1, pn, 0, 8, 10000, &tail );
  if( *tail == '-' )
    {
    chs.last_track_sectors = getnum( tail + 1, pn, 0, 8, 10000, &tail );
    if( chs.last_track_sectors == chs.first_track_sectors ) goto err;
    if( *tail == ',' )
      chs.tracks_zone = getnum( tail + 1, pn, 0, 8, chs.cylinders, &tail );
    else chs.tracks_zone = chs.cylinders / std::min( 24,
         std::abs( chs.last_track_sectors - chs.first_track_sectors ) );
    }
  else { chs.last_track_sectors = chs.first_track_sectors;
         chs.tracks_zone = chs.cylinders; }
  if( *tail++ != ':' ) goto err;
  if( *tail == 'F' )
    { if( tail[1] == 'F' ) { chs.track_order = chs.FF; tail += 2; }
      else { chs.track_order = chs.F; ++tail; } }
  else goto err;
  if( *tail != ':' ) goto err;
  chs.bad_head = getnum( tail + 1, pn, 0, 0, chs.heads - 1, &tail );
  if( *tail != 0 ) goto err;
  if( ( chs.track_order == chs.F ) !=
      ( chs.last_track_sectors == chs.first_track_sectors ) ) goto err;
  }


int make_test( const char * const mapname, const int hardbs,
               const bool force )
  {
  Mapfile mapfile( mapname );
  const bool to_stdout = std::strcmp( mapname, "-" ) == 0;
  if( !to_stdout && !force && mapfile.read_mapfile( 0, false ) )
    {
    show_file_error( mapname, "Mapfile exists. Use '--force' to overwrite it." );
    return 1;
    }
  mapfile.current_status( mapfile.finished );
  const CHS & chs = *chsp;
  const int zones =
    chs.cylinders / chs.tracks_zone + ( chs.cylinders % chs.tracks_zone != 0 );
  switch( chs.track_order )
    {
    case CHS::F:
      for( int c = 0; c < chs.cylinders; ++c )
        for( int h = 0; h < chs.heads; ++h )
          for( int s = 0; s < chs.first_track_sectors; ++s )
            mapfile.append_sblock( hardbs, ( chs.bad_head == h ) ?
                                   Sblock::bad_sector : Sblock::finished );
      break;
    case CHS::FF:
      for( int z = 0; z * chs.tracks_zone < chs.cylinders; ++z )
        {
        const int climit = std::min( chs.cylinders, (z+1) * chs.tracks_zone );
        const int sectors = ( chs.first_track_sectors * ( zones - 1 - z ) +
                              chs.last_track_sectors * z ) / ( zones - 1 );
        for( int h = 0; h < chs.heads; ++h )
          for( int c = z * chs.tracks_zone; c < climit; ++c )
            for( int s = 0; s < sectors; ++s )
              mapfile.append_sblock( hardbs, ( chs.bad_head == h ) ?
                                     Sblock::bad_sector : Sblock::finished );
        }
    }
  mapfile.compact_sblock_vector();
  if( !mapfile.write_mapfile( to_stdout ? stdout : 0 ) ||
      ( to_stdout && std::fclose( stdout ) != 0 ) )
    { show_file_error( mapfile.pname( false ), "Error writing mapfile", errno );
      return 1; }
  return 0;
  }