summaryrefslogtreecommitdiffstats
path: root/src/dimage.c
blob: 5292738a3516fa4bf3444b12b8c2e2f436761791 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*

    File: dimage.c

    Copyright (C) 2007 Christophe GRENIER <grenier@cgsecurity.org>
  
    This software 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, write the Free Software Foundation, Inc., 51
    Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_LOCALE_H
#include <locale.h>	/* setlocale */
#endif
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "types.h"
#include "common.h"
#include "testdisk.h"
#include "intrf.h"
#ifdef HAVE_NCURSES
#include "intrfn.h"
#else
#include <stdio.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "intrface.h"
#include "fnctdsk.h"
#include "log.h"
#include "dimage.h"

#define READ_SIZE 256*512

#ifndef O_LARGEFILE
#define O_LARGEFILE 0
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif

int disk_image(disk_t *disk_car, const partition_t *partition, const char *image_dd)
{
  int ind_stop=0;
  uint64_t nbr_read_error=0;
  uint64_t offset=partition->part_offset;
  const uint64_t offset_end=partition->part_offset+partition->part_size;
  const uint64_t offset_inc=(offset_end-offset)/100;
  uint64_t offset_next=offset;
  unsigned char *buffer_disk=MALLOC(READ_SIZE);
  int disk_dst;
#ifdef HAVE_NCURSES
  WINDOW *window;
#endif
  if((disk_dst=open(image_dd,O_LARGEFILE|O_RDWR|O_BINARY|O_CREAT,0644))<0)
  {
    log_error("Can't create file %s.\n",image_dd);
    display_message("Can't create file!\n");
    return -1;
  }
#ifdef HAVE_NCURSES
  window=newwin(0,0,0,0);	/* full screen */
  aff_copy(window);
  wmove(window,5,0);
  wprintw(window,"%s\n",disk_car->description_short(disk_car));
  wmove(window,6,0);
  aff_part(window,AFF_PART_ORDER|AFF_PART_STATUS,disk_car,partition);
  wmove(window,22,0);
  wattrset(window, A_REVERSE);
  waddstr(window,"  Stop  ");
  wattroff(window, A_REVERSE);
#endif
  while(ind_stop==0 && offset < offset_end)
  {
    unsigned int readsize=READ_SIZE;
    int update=0;
    if(offset_end-offset < READ_SIZE)
      readsize=offset_end-offset;
    if(disk_car->read(disk_car, readsize, buffer_disk, offset)<0)
    {
      update=1;
      nbr_read_error++;
    }
    offset+=READ_SIZE;
    if(offset>offset_next)
    {
      update=1;
      offset_next+=offset_inc;
    }
    if(update)
    {
#ifdef HAVE_NCURSES
      unsigned int i;
      unsigned int percent=(offset-partition->part_offset)*100/partition->part_size;
      wmove(window,7,0);
      wprintw(window,"%3u %% ", percent);
      for(i=0;i<percent*3/5;i++)
	wprintw(window,"=");
      wprintw(window,">");
      wrefresh(window);
      ind_stop=check_enter_key_or_s(window);
#endif
    }
    if(write(disk_dst,buffer_disk,readsize)<0)
    {
      ind_stop=2;
    }
  }
  close(disk_dst);
  delwin(window);
  (void) clearok(window, TRUE);
#ifdef HAVE_TOUCHWIN
  touchwin(window);
#endif
  if(ind_stop==2)
  {
    display_message("No space left for the file image.\n");
    return -2;
  }
  if(nbr_read_error==0)
    display_message("Image created successfully.\n");
  else
    display_message("Image created successfully but read errors have occured.\n");
  return 0;
}