summaryrefslogtreecommitdiffstats
path: root/src/sessionp.c
blob: 7d8c39de7853fa269bc9961e784e09812829e09e (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*

    File: sessionp.c

    Copyright (C) 2006-2008 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
#include <stdio.h>
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>	/* unlink */
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <errno.h>
#include "types.h"
#include "common.h"
#include "intrf.h"
#include "filegen.h"
#include "photorec.h"
#include "sessionp.h"
#include "log.h"

#define SESSION_MAXSIZE 40960
#define SESSION_FILENAME "photorec.ses"

int session_load(char **cmd_device, char **current_cmd, alloc_data_t *list_free_space)
{
  FILE *f_session;
  char *buffer;
  char *pos;
  int taille;
  struct stat stat_rec;
  unsigned int buffer_size;
//  time_t my_time;
  char *info=NULL;
  f_session=fopen(SESSION_FILENAME,"rb");
  if(!f_session)
  {
    log_info("Can't open photorec.ses file: %s\n",strerror(errno));
    session_save(NULL, NULL, NULL);
    return -1;
  }
  if(fstat(fileno(f_session), &stat_rec)<0)
    buffer_size=SESSION_MAXSIZE;
  else
    buffer_size=stat_rec.st_size;
  buffer=(char *)MALLOC(buffer_size+1);
  taille=fread(buffer,1,buffer_size,f_session);
  buffer[taille]='\0';
  fclose(f_session);
  pos=buffer;
  if(*pos!='#')
  {
    free(buffer);
    return -1;
  }
  pos++;
  /* load time */
  strtol(pos,&pos,10); 	// my_time=strtol(pos,&pos,10);
  if(pos==NULL)
  {
    free(buffer);
    return 0;
  }
  pos=strstr(pos,"\n");
  if(pos==NULL)
  {
    free(buffer);
    return 0;
  }
  pos++;
  /* get current disk */
  info=pos;
  pos=strstr(info," ");
  if(pos==NULL)
  {
    free(buffer);
    return 0;
  }
  *pos='\0';
  pos++;
  *cmd_device=strdup(info);
  /* search part_name_option */
  info=pos;
  pos=strstr(pos,"\n");
  if(pos==NULL)
  {
    free(buffer);
    return 0;
  }
  *pos='\0';
  pos++;
  *current_cmd=strdup(info);
  while(1)
  {
    uint64_t start=0;
    uint64_t end=0;
    while(*pos>='0' && *pos<='9')
    {
      start=start*10 + (*pos - '0');
      pos++;
    }
    if(*pos++ != '-')
    {
      free(buffer);
      return 0;
    }
    while(*pos >= '0' && *pos <= '9')
    {
      end=end*10+(*pos -'0');
      pos++;
    }
    if(start <= end)
    {
      alloc_data_t *new_free_space;
      new_free_space=(alloc_data_t*)MALLOC(sizeof(*new_free_space));
      /* Temporary storage, values need to be multiplied by sector_size */
      new_free_space->start=start;
      new_free_space->end=end;
      new_free_space->file_stat=NULL;
      new_free_space->data=1;
      td_list_add_tail(&new_free_space->list, &list_free_space->list);
#ifdef DEBUG
      log_trace(">%lu-%lu<\n", start, end);
#endif
    }
    while(*pos=='\n' || *pos=='\r')
      pos++;
  }
}

int session_save(alloc_data_t *list_free_space, struct ph_param *params,  const struct ph_options *options)
{
  FILE *f_session;
  if(params!=NULL && params->status==STATUS_QUIT)
    return 0;
  f_session=fopen(SESSION_FILENAME,"wb");
  if(!f_session)
  {
    log_critical("Can't create photorec.ses file: %s\n",strerror(errno));
    return -1;
  }
  if(params!=NULL)
  {
    struct td_list_head *free_walker = NULL;
    unsigned int i;
    const file_enable_t *files_enable=options->list_file_format;
    unsigned int disable=0;
    unsigned int enable=0;
    unsigned int enable_by_default=0;
    if(options->verbose>1)
    {
      log_trace("session_save\n");
    }
    fprintf(f_session,"#%u\n%s %s,%u,",
	(unsigned int)time(NULL), params->disk->device, params->disk->arch->part_name_option, params->partition->order);
    if(params->blocksize>0)
      fprintf(f_session,"blocksize,%u,", params->blocksize);
    fprintf(f_session,"fileopt,");
    for(i=0;files_enable[i].file_hint!=NULL;i++)
    {
      if(files_enable[i].enable==0)
	disable++;
      else
	enable++;
      if(files_enable[i].enable==files_enable[i].file_hint->enable_by_default)
	enable_by_default++;
    }
    if(enable_by_default >= disable && enable_by_default >= enable)
    {
      for(i=0;files_enable[i].file_hint!=NULL;i++)
      {
	if(files_enable[i].enable!=files_enable[i].file_hint->enable_by_default &&
	      files_enable[i].file_hint->extension!=NULL &&
	      files_enable[i].file_hint->extension[0]!='\0')
	{
	  fprintf(f_session,"%s,%s,", files_enable[i].file_hint->extension,
	      (files_enable[i].enable!=0?"enable":"disable"));
	}
      }
    }
    else if(enable > disable)
    {
      fprintf(f_session,"everything,enable,");
      for(i=0;files_enable[i].file_hint!=NULL;i++)
      {
	if(files_enable[i].enable==0 &&
	      files_enable[i].file_hint->extension!=NULL &&
	      files_enable[i].file_hint->extension[0]!='\0')
	{
	  fprintf(f_session,"%s,disable,", files_enable[i].file_hint->extension);
	}
      }
    }
    else
    {
      fprintf(f_session,"everything,disable,");
      for(i=0;files_enable[i].file_hint!=NULL;i++)
      {
	if(files_enable[i].enable!=0 &&
	      files_enable[i].file_hint->extension!=NULL &&
	      files_enable[i].file_hint->extension[0]!='\0')
	{
	  fprintf(f_session,"%s,enable,", files_enable[i].file_hint->extension);
	}
      }
    }
    /* Save options */
    fprintf(f_session, "options,");
    if(options->paranoid==0)
      fprintf(f_session, "paranoid_no,");
    else if(options->paranoid==1)
      fprintf(f_session, "paranoid,");
    else
      fprintf(f_session, "paranoid_bf,");
    if(options->keep_corrupted_file>0)
      fprintf(f_session, "keep_corrupted_file,");
    else
      fprintf(f_session, "keep_corrupted_file_no,");
    if(options->mode_ext2>0)
      fprintf(f_session, "mode_ext2,");
    if(options->expert>0)
      fprintf(f_session, "expert,");
    if(options->lowmem>0)
      fprintf(f_session, "lowmem,");
    /* Save options - End */
    if(params->carve_free_space_only>0)
      fprintf(f_session,"freespace,");
    else
      fprintf(f_session,"wholespace,");
    fprintf(f_session,"search,");
    switch(params->status)
    {
      case STATUS_UNFORMAT:
        fprintf(f_session, "status=unformat,");
	break;
      case STATUS_FIND_OFFSET:
        fprintf(f_session, "status=find_offset,");
	break;
      case STATUS_EXT2_ON_BF:
	fprintf(f_session, "status=ext2_on_bf,");
	break;
      case STATUS_EXT2_ON_SAVE_EVERYTHING:
	fprintf(f_session, "status=ext2_on_save_everything,");
	break;
      case STATUS_EXT2_ON:
	fprintf(f_session, "status=ext2_on,");
	break;
      case STATUS_EXT2_OFF_SAVE_EVERYTHING:
	fprintf(f_session, "status=ext2_off_save_everything,");
	break;
      case STATUS_EXT2_OFF_BF:
	fprintf(f_session, "status=ext2_off_bf,");
	break;
      case STATUS_EXT2_OFF:
	fprintf(f_session, "status=ext2_off,");
	break;
      case STATUS_QUIT:
        break;
    }
    if(params->status!=STATUS_FIND_OFFSET && params->offset!=-1)
      fprintf(f_session, "%llu,",
	  (long long unsigned)(params->offset/params->disk->sector_size));
    fprintf(f_session,"inter\n");
    td_list_for_each(free_walker, &list_free_space->list)
    {
      alloc_data_t *current_free_space;
      current_free_space=td_list_entry(free_walker, alloc_data_t, list);
      fprintf(f_session,"%llu-%llu\n",
	  (long long unsigned)(current_free_space->start/params->disk->sector_size),
	  (long long unsigned)(current_free_space->end/params->disk->sector_size));
    }
  }
  { /* Reserve some space */
    int res;
    char *buffer;
    buffer=(char *)MALLOC(SESSION_MAXSIZE);
    memset(buffer,0,SESSION_MAXSIZE);
    res=fwrite(buffer,1,SESSION_MAXSIZE,f_session);
    free(buffer);
    if(res<SESSION_MAXSIZE)
    {
      fclose(f_session);
      return -1;
    }
  }
  fclose(f_session);
  return 0;
}

time_t regular_session_save(alloc_data_t *list_free_space, struct ph_param *params,  const struct ph_options *options, time_t current_time)
{
  time_t new_time;
  /* Save current progress */
  session_save(list_free_space, params, options);
  new_time=time(NULL);
  /* If it takes more then 30s to save the session, save every 15 minutes instead of every 5 minutes */
  return new_time+(current_time+30<new_time?15:5)*60;
}