summaryrefslogtreecommitdiffstats
path: root/src/photorec.c
diff options
context:
space:
mode:
authorChristophe Grenier <grenier@cgsecurity.org>2012-05-01 18:38:13 +0200
committerChristophe Grenier <grenier@cgsecurity.org>2012-05-01 18:38:13 +0200
commit4fcebd8d05fc1ca4492588185b706e1a7a4224d8 (patch)
treee00daab2e04275f204b9ce2564c1ca99d3978e12 /src/photorec.c
parent63f3e201a53cb80f2debeddc8da33ddbb0a6f9c0 (diff)
Avoid to truncate files that will get deleted.
It seems to give better performance on journalized filesystem.
Diffstat (limited to 'src/photorec.c')
-rw-r--r--src/photorec.c147
1 files changed, 55 insertions, 92 deletions
diff --git a/src/photorec.c b/src/photorec.c
index 7af903c..955cc0d 100644
--- a/src/photorec.c
+++ b/src/photorec.c
@@ -574,35 +574,17 @@ static void free_list_allocation(alloc_list_t *list_allocation)
free(allocated_space);
}
}
-
-/** file_finish()
- @param file_recovery -
+/* file_finish_aux()
+ @param file_recovery - handle!=NULL
@param struct ph_param *params
- @param alloc_data_t *list_search_space
- @param alloc_data_t **current_search_space
- @param *offset
-
- @returns:
- -1: file not recovered, file_size=0 offset_error!=0
- 0: file not recovered
- 1: file recovered
- */
+*/
-int file_finish(file_recovery_t *file_recovery, struct ph_param *params,
- alloc_data_t *list_search_space, alloc_data_t **current_search_space, uint64_t *offset)
+static void file_finish_aux(file_recovery_t *file_recovery, struct ph_param *params, const int paranoid)
{
- int file_recovered=0;
-#ifdef DEBUG_FILE_FINISH
- log_debug("file_finish start %lu (%lu-%lu)\n", (long unsigned int)((*offset)/params->blocksize),
- (unsigned long int)((*current_search_space)->start/params->blocksize),
- (unsigned long int)((*current_search_space)->end/params->blocksize));
- log_debug("file_recovery->offset_error=%llu\n", (long long unsigned)file_recovery->offset_error);
- log_debug("file_recovery->handle %s NULL\n", (file_recovery->handle!=NULL?"!=":"=="));
- info_list_search_space(list_search_space, NULL, DEFAULT_SECTOR_SIZE, 0, 1);
-#endif
- if(file_recovery->handle)
+ if(params->status!=STATUS_EXT2_ON_SAVE_EVERYTHING &&
+ params->status!=STATUS_EXT2_OFF_SAVE_EVERYTHING)
{
- if(file_recovery->file_stat!=NULL && file_recovery->file_check!=NULL)
+ if(file_recovery->file_stat!=NULL && file_recovery->file_check!=NULL && paranoid>0)
{ /* Check if recovered file is valid */
file_recovery->file_check(file_recovery);
}
@@ -620,6 +602,16 @@ int file_finish(file_recovery_t *file_recovery, struct ph_param *params,
file_recovery->file_size=0;
file_recovery->file_size_on_disk=0;
}
+ }
+ if(file_recovery->file_size==0)
+ {
+ fclose(file_recovery->handle);
+ file_recovery->handle=NULL;
+ /* File is zero-length; erase it */
+ unlink(file_recovery->filename);
+ }
+ else
+ {
#ifdef HAVE_FTRUNCATE
fflush(file_recovery->handle);
if(ftruncate(fileno(file_recovery->handle), file_recovery->file_size)<0)
@@ -629,23 +621,47 @@ int file_finish(file_recovery_t *file_recovery, struct ph_param *params,
#endif
fclose(file_recovery->handle);
file_recovery->handle=NULL;
- // log_debug("%s %llu\n",file_recovery->filename,(long long unsigned)file_recovery->file_size);
- if(file_recovery->file_size==0)
+ if(file_recovery->time!=0 && file_recovery->time!=(time_t)-1)
+ set_date(file_recovery->filename, file_recovery->time, file_recovery->time);
+ if(file_recovery->file_rename!=NULL)
+ file_recovery->file_rename(file_recovery->filename);
+ if((++params->file_nbr)%MAX_FILES_PER_DIR==0)
{
- /* File is zero-length; erase it */
- unlink(file_recovery->filename);
+ params->dir_num=photorec_mkdir(params->recup_dir, params->dir_num+1);
}
- else
- {
- if(file_recovery->time!=0 && file_recovery->time!=(time_t)-1)
- set_date(file_recovery->filename, file_recovery->time, file_recovery->time);
- if((++params->file_nbr)%MAX_FILES_PER_DIR==0)
- {
- params->dir_num=photorec_mkdir(params->recup_dir, params->dir_num+1);
- }
+ if(params->status!=STATUS_EXT2_ON_SAVE_EVERYTHING &&
+ params->status!=STATUS_EXT2_OFF_SAVE_EVERYTHING)
file_recovery->file_stat->recovered++;
- }
}
+}
+
+/** file_finish()
+ @param file_recovery -
+ @param struct ph_param *params
+ @param alloc_data_t *list_search_space
+ @param alloc_data_t **current_search_space
+ @param *offset
+
+ @returns:
+ -1: file not recovered, file_size=0 offset_error!=0
+ 0: file not recovered
+ 1: file recovered
+ */
+
+int file_finish(file_recovery_t *file_recovery, struct ph_param *params,
+ alloc_data_t *list_search_space, alloc_data_t **current_search_space, uint64_t *offset)
+{
+ int file_recovered=0;
+#ifdef DEBUG_FILE_FINISH
+ log_debug("file_finish start %lu (%lu-%lu)\n", (long unsigned int)((*offset)/params->blocksize),
+ (unsigned long int)((*current_search_space)->start/params->blocksize),
+ (unsigned long int)((*current_search_space)->end/params->blocksize));
+ log_debug("file_recovery->offset_error=%llu\n", (long long unsigned)file_recovery->offset_error);
+ log_debug("file_recovery->handle %s NULL\n", (file_recovery->handle!=NULL?"!=":"=="));
+ info_list_search_space(list_search_space, NULL, DEFAULT_SECTOR_SIZE, 0, 1);
+#endif
+ if(file_recovery->handle)
+ file_finish_aux(file_recovery, params, 1);
if(file_recovery->file_stat!=NULL)
{
list_truncate(&file_recovery->location,file_recovery->file_size);
@@ -703,60 +719,7 @@ int file_finish2(file_recovery_t *file_recovery, struct ph_param *params, const
info_list_search_space(list_search_space, NULL, DEFAULT_SECTOR_SIZE, 0, 1);
#endif
if(file_recovery->handle)
- {
- if(params->status!=STATUS_EXT2_ON_SAVE_EVERYTHING &&
- params->status!=STATUS_EXT2_OFF_SAVE_EVERYTHING)
- {
- if(file_recovery->file_stat!=NULL && file_recovery->file_check!=NULL && options->paranoid>0)
- { /* Check if recovered file is valid */
- file_recovery->file_check(file_recovery);
- }
- /* FIXME: need to adapt read_size to volume size to avoid this */
- if(file_recovery->file_size > params->disk->disk_size)
- file_recovery->file_size = params->disk->disk_size;
- if(file_recovery->file_size > params->disk->disk_real_size)
- file_recovery->file_size = params->disk->disk_real_size;
-
- if(file_recovery->file_stat!=NULL && file_recovery->file_size> 0 &&
- file_recovery->file_size < file_recovery->min_filesize)
- {
- log_info("File too small ( %llu < %llu), reject it\n",
- (long long unsigned) file_recovery->file_size,
- (long long unsigned) file_recovery->min_filesize);
- file_recovery->file_size=0;
- file_recovery->file_size_on_disk=0;
- }
-#ifdef HAVE_FTRUNCATE
- fflush(file_recovery->handle);
- if(ftruncate(fileno(file_recovery->handle), file_recovery->file_size)<0)
- {
- log_critical("ftruncate failed.\n");
- }
-#endif
- }
- fclose(file_recovery->handle);
- file_recovery->handle=NULL;
- if(file_recovery->file_size==0)
- {
- unlink(file_recovery->filename);
- }
- else
- {
- if(file_recovery->time!=0 && file_recovery->time!=(time_t)-1)
- set_date(file_recovery->filename, file_recovery->time, file_recovery->time);
- if(file_recovery->file_rename!=NULL)
- file_recovery->file_rename(file_recovery->filename);
- if((++params->file_nbr)%MAX_FILES_PER_DIR==0)
- {
- params->dir_num=photorec_mkdir(params->recup_dir, params->dir_num+1);
- }
- if(params->status!=STATUS_EXT2_ON_SAVE_EVERYTHING &&
- params->status!=STATUS_EXT2_OFF_SAVE_EVERYTHING)
- {
- file_recovery->file_stat->recovered++;
- }
- }
- }
+ file_finish_aux(file_recovery, params, options->paranoid);
if(file_recovery->file_stat!=NULL)
{
if(file_recovery->file_size==0)