2007-10-29 22:38:52 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
File: sessionp.c
|
|
|
|
|
2008-06-11 13:09:44 +02:00
|
|
|
Copyright (C) 2006-2008 Christophe GRENIER <grenier@cgsecurity.org>
|
2007-10-29 22:38:52 +01:00
|
|
|
|
|
|
|
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"
|
2011-07-21 08:06:49 +02:00
|
|
|
#include "photorec.h"
|
2007-10-29 22:38:52 +01:00
|
|
|
#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;
|
2009-03-01 20:00:07 +01:00
|
|
|
time_t my_time;
|
|
|
|
char *info=NULL;
|
2007-10-29 22:38:52 +01:00
|
|
|
f_session=fopen(SESSION_FILENAME,"rb");
|
|
|
|
if(!f_session)
|
|
|
|
{
|
2008-01-11 00:03:03 +01:00
|
|
|
log_info("Can't open photorec.ses file: %s\n",strerror(errno));
|
2011-07-21 08:06:49 +02:00
|
|
|
session_save(NULL, NULL, NULL, NULL, 0, NULL, 0);
|
2007-10-29 22:38:52 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(fstat(fileno(f_session), &stat_rec)<0)
|
|
|
|
buffer_size=SESSION_MAXSIZE;
|
|
|
|
else
|
|
|
|
buffer_size=stat_rec.st_size;
|
2008-06-23 19:28:58 +02:00
|
|
|
buffer=(char *)MALLOC(buffer_size+1);
|
2007-10-29 22:38:52 +01:00
|
|
|
taille=fread(buffer,1,buffer_size,f_session);
|
|
|
|
buffer[taille]='\0';
|
|
|
|
fclose(f_session);
|
|
|
|
pos=buffer;
|
|
|
|
if(*pos!='#')
|
|
|
|
{
|
|
|
|
free(buffer);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-03-01 20:00:07 +01:00
|
|
|
pos++;
|
|
|
|
/* load time */
|
|
|
|
my_time=strtol(pos,&pos,10);
|
|
|
|
if(pos==NULL)
|
2007-10-29 22:38:52 +01:00
|
|
|
{
|
2009-03-01 20:00:07 +01:00
|
|
|
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)
|
|
|
|
{
|
2011-03-25 14:34:35 +01:00
|
|
|
uint64_t start=0;
|
|
|
|
uint64_t end=0;
|
2009-03-01 20:00:07 +01:00
|
|
|
while(*pos>='0' && *pos<='9')
|
2007-10-29 22:38:52 +01:00
|
|
|
{
|
2009-03-01 20:00:07 +01:00
|
|
|
start=start*10 + (*pos - '0');
|
|
|
|
pos++;
|
2007-10-29 22:38:52 +01:00
|
|
|
}
|
2009-03-01 20:00:07 +01:00
|
|
|
if(*pos++ != '-')
|
2007-10-29 22:38:52 +01:00
|
|
|
{
|
|
|
|
free(buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-03-01 20:00:07 +01:00
|
|
|
while(*pos >= '0' && *pos <= '9')
|
2007-10-29 22:38:52 +01:00
|
|
|
{
|
2009-03-01 20:00:07 +01:00
|
|
|
end=end*10+(*pos -'0');
|
|
|
|
pos++;
|
2007-10-29 22:38:52 +01:00
|
|
|
}
|
2009-03-01 20:00:07 +01:00
|
|
|
if(start <= end)
|
2007-10-29 22:38:52 +01:00
|
|
|
{
|
2009-03-01 20:00:07 +01:00
|
|
|
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;
|
|
|
|
td_list_add_tail(&new_free_space->list, &list_free_space->list);
|
2007-10-29 22:38:52 +01:00
|
|
|
#ifdef DEBUG
|
2009-03-01 20:00:07 +01:00
|
|
|
log_trace(">%lu-%lu<\n", start, end);
|
2007-10-29 22:38:52 +01:00
|
|
|
#endif
|
2009-03-01 20:00:07 +01:00
|
|
|
}
|
|
|
|
while(*pos=='\n' || *pos=='\r')
|
|
|
|
pos++;
|
2007-10-29 22:38:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-21 08:06:49 +02:00
|
|
|
int session_save(alloc_data_t *list_free_space, disk_t *disk, const partition_t *partition, const file_enable_t *files_enable, const unsigned int blocksize, const struct ph_options *options, const unsigned int carve_free_space_only)
|
2007-10-29 22:38:52 +01:00
|
|
|
{
|
|
|
|
FILE *f_session;
|
|
|
|
f_session=fopen(SESSION_FILENAME,"wb");
|
|
|
|
if(!f_session)
|
|
|
|
{
|
|
|
|
log_critical("Can't create photorec.ses file: %s\n",strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2011-07-21 08:06:49 +02:00
|
|
|
if(disk!=NULL)
|
2007-10-29 22:38:52 +01:00
|
|
|
{
|
|
|
|
struct td_list_head *free_walker = NULL;
|
|
|
|
unsigned int i;
|
2011-07-21 20:43:01 +02:00
|
|
|
if(options->verbose>1)
|
|
|
|
{
|
|
|
|
log_trace("session_save\n");
|
|
|
|
}
|
2008-06-11 13:09:44 +02:00
|
|
|
fprintf(f_session,"#%u\n%s %s,%u,blocksize,%u,fileopt,",
|
2011-07-21 08:06:49 +02:00
|
|
|
(unsigned int)time(NULL), disk->device, disk->arch->part_name_option, partition->order, blocksize);
|
2007-10-29 22:38:52 +01:00
|
|
|
for(i=0;files_enable[i].file_hint!=NULL;i++)
|
|
|
|
{
|
|
|
|
if(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"));
|
|
|
|
}
|
|
|
|
}
|
2008-06-11 13:09:44 +02:00
|
|
|
/* Save options */
|
|
|
|
fprintf(f_session, "options,");
|
2011-07-21 08:06:49 +02:00
|
|
|
if(options->paranoid==0)
|
2008-06-11 13:09:44 +02:00
|
|
|
fprintf(f_session, "paranoid_no,");
|
2011-07-21 08:06:49 +02:00
|
|
|
else if(options->paranoid==1)
|
2008-06-11 13:09:44 +02:00
|
|
|
fprintf(f_session, "paranoid,");
|
|
|
|
else
|
|
|
|
fprintf(f_session, "paranoid_bf,");
|
|
|
|
/* TODO: allow_partial_last_cylinder ? */
|
2011-07-21 08:06:49 +02:00
|
|
|
if(options->keep_corrupted_file>0)
|
2008-06-11 13:09:44 +02:00
|
|
|
fprintf(f_session, "keep_corrupted_file,");
|
|
|
|
else
|
|
|
|
fprintf(f_session, "keep_corrupted_file_no,");
|
2011-07-21 08:06:49 +02:00
|
|
|
if(options->mode_ext2>0)
|
2008-06-11 13:09:44 +02:00
|
|
|
fprintf(f_session, "mode_ext2,");
|
2011-07-21 08:06:49 +02:00
|
|
|
if(options->expert>0)
|
2008-06-11 13:09:44 +02:00
|
|
|
fprintf(f_session, "expert,");
|
2011-07-21 08:06:49 +02:00
|
|
|
if(options->lowmem>0)
|
2008-06-11 13:09:44 +02:00
|
|
|
fprintf(f_session, "lowmem,");
|
|
|
|
/* Save options - End */
|
|
|
|
if(carve_free_space_only>0)
|
|
|
|
fprintf(f_session,"freespace,");
|
|
|
|
else
|
|
|
|
fprintf(f_session,"wholespace,");
|
2007-10-29 22:38:52 +01:00
|
|
|
fprintf(f_session,"search,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);
|
2011-03-25 14:34:35 +01:00
|
|
|
fprintf(f_session,"%llu-%llu\n",
|
2011-07-21 08:06:49 +02:00
|
|
|
(long long unsigned)(current_free_space->start/disk->sector_size),
|
|
|
|
(long long unsigned)(current_free_space->end/disk->sector_size));
|
2007-10-29 22:38:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{ /* Reserve some space */
|
|
|
|
int res;
|
|
|
|
char *buffer;
|
2008-06-23 19:28:58 +02:00
|
|
|
buffer=(char *)MALLOC(SESSION_MAXSIZE);
|
2007-10-29 22:38:52 +01:00
|
|
|
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;
|
|
|
|
}
|