summaryrefslogtreecommitdiffstats
path: root/src/photorec.c
blob: 5b316f49c92ff921cc23798214e66ce4a696e374 (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
/*

    File: photorec.c

    Copyright (C) 1998-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_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>	/* unlink, ftruncate */
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <errno.h>
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include "types.h"
#include "common.h"
#include "fnctdsk.h"
#include "dir.h"
#include "filegen.h"
#include "photorec.h"
#include "exfatp.h"
#include "ext2p.h"
#include "fatp.h"
#include "ntfsp.h"
#include "log.h"
#include "setdate.h"
#include "dfxml.h"

/* #define DEBUG_FILE_FINISH */
/* #define DEBUG_UPDATE_SEARCH_SPACE */
/* #define DEBUG_FREE */

static void update_search_space_aux(alloc_data_t *list_search_space, uint64_t start, uint64_t end, alloc_data_t **new_current_search_space, uint64_t *offset);
static void file_block_truncate_zero(const file_recovery_t *file_recovery, alloc_data_t *list_search_space);
static int file_block_truncate(const file_recovery_t *file_recovery, alloc_data_t *list_search_space, const unsigned int blocksize);

void file_block_log(const file_recovery_t *file_recovery, const unsigned int sector_size)
{
  struct td_list_head *tmp;
  if(file_recovery->filename[0]=='\0')
    return;
  log_info("%s\t",file_recovery->filename);
  td_list_for_each(tmp, &file_recovery->location.list)
  {
    const alloc_list_t *element=td_list_entry_const(tmp, const alloc_list_t, list);
    if(element->data>0)
      log_info(" %lu-%lu", (unsigned long)(element->start/sector_size), (unsigned long)(element->end/sector_size));
    else
      log_info(" (%lu-%lu)", (unsigned long)(element->start/sector_size), (unsigned long)(element->end/sector_size));
  }
  log_info("\n");
}

void del_search_space(alloc_data_t *list_search_space, const uint64_t start, const uint64_t end)
{
  update_search_space_aux(list_search_space, start, end, NULL, NULL);
}

static void update_search_space_aux(alloc_data_t *list_search_space, const uint64_t start, const uint64_t end, alloc_data_t **new_current_search_space, uint64_t *offset)
{
  struct td_list_head *search_walker = NULL;
#ifdef DEBUG_UPDATE_SEARCH_SPACE
  log_trace("update_search_space_aux offset=%llu remove [%llu-%llu]\n",
      (long long unsigned)(offset==NULL?0:((*offset)/512)),
      (unsigned long long)(start/512),
      (unsigned long long)(end/512));
#endif
  if(start > end)
    return ;
  td_list_for_each_prev(search_walker, &list_search_space->list)
  {
    alloc_data_t *current_search_space;
    current_search_space=td_list_entry(search_walker, alloc_data_t, list);
#ifdef DEBUG_UPDATE_SEARCH_SPACE
    log_trace("update_search_space_aux offset=%llu remove [%llu-%llu] in [%llu-%llu]\n",
	(long long unsigned)(offset==NULL?0:((*offset)/512)),
        (unsigned long long)(start/512),
        (unsigned long long)(end/512),
        (unsigned long long)(current_search_space->start/512),
        (unsigned long long)(current_search_space->end/512));
#endif
    if(current_search_space->start==start)
    {
      const uint64_t pivot=current_search_space->end+1;
      if(end < current_search_space->end)
      { /* current_search_space->start==start end<current_search_space->end */
        if(offset!=NULL && new_current_search_space!=NULL &&
            current_search_space->start<=*offset && *offset<=end)
        {
          *new_current_search_space=current_search_space;
          *offset=end+1;
        }
        current_search_space->start=end+1;
        current_search_space->file_stat=NULL;
        return ;
      }
      /* current_search_space->start==start current_search_space->end<=end */
      if(offset!=NULL && new_current_search_space!=NULL &&
          current_search_space->start<=*offset && *offset<=current_search_space->end)
      {
        *new_current_search_space=td_list_next_entry(current_search_space, list);
        *offset=(*new_current_search_space)->start;
      }
      td_list_del(search_walker);
      free(current_search_space);
      update_search_space_aux(list_search_space, pivot, end, new_current_search_space, offset);
      return ;
    }
    if(current_search_space->end==end)
    {
      const uint64_t pivot=current_search_space->start-1;
#ifdef DEBUG_UPDATE_SEARCH_SPACE
      log_trace("current_search_space->end==end\n");
#endif
      if(current_search_space->start < start)
      { /* current_search_space->start<start current_search_space->end==end */
        if(offset!=NULL && new_current_search_space!=NULL &&
            start<=*offset && *offset<=current_search_space->end)
        {
	  *new_current_search_space=td_list_next_entry(current_search_space, list);
          *offset=(*new_current_search_space)->start;
        }
        current_search_space->end=start-1;
        return ;
      }
      /* start<=current_search_space->start current_search_space->end==end */
      if(offset!=NULL && new_current_search_space!=NULL &&
          current_search_space->start<=*offset && *offset<=current_search_space->end)
      {
	*new_current_search_space=td_list_next_entry(current_search_space, list);
        *offset=(*new_current_search_space)->start;
      }
      td_list_del(search_walker);
      free(current_search_space);
      update_search_space_aux(list_search_space, start, pivot, new_current_search_space, offset);
      return ;
    }
    if(start < current_search_space->start && current_search_space->start <= end)
    {
      const uint64_t pivot=current_search_space->start;
      update_search_space_aux(list_search_space, start, pivot-1,  new_current_search_space, offset);
      update_search_space_aux(list_search_space, pivot, end,      new_current_search_space, offset);
      return ;
    }
    if(start <= current_search_space->end && current_search_space->end < end)
    {
      const uint64_t pivot=current_search_space->end;
      update_search_space_aux(list_search_space, start, pivot, new_current_search_space, offset);
      update_search_space_aux(list_search_space, pivot+1, end, new_current_search_space, offset);
      return ;
    }
    if(current_search_space->start < start && end < current_search_space->end)
    {
      alloc_data_t *new_free_space;
      new_free_space=(alloc_data_t*)MALLOC(sizeof(*new_free_space));
      new_free_space->start=start;
      new_free_space->end=current_search_space->end;
      new_free_space->file_stat=NULL;
      new_free_space->data=1;
      current_search_space->end=start-1;
      td_list_add(&new_free_space->list,search_walker);
      if(offset!=NULL && new_current_search_space!=NULL &&
          new_free_space->start<=*offset && *offset<=new_free_space->end)
      {
        *new_current_search_space=new_free_space;
      }
      update_search_space_aux(list_search_space, start, end, new_current_search_space, offset);
      return ;
    }
  }
}

void init_search_space(alloc_data_t *list_search_space, const disk_t *disk_car, const partition_t *partition)
{
  alloc_data_t *new_sp;
  new_sp=(alloc_data_t*)MALLOC(sizeof(*new_sp));
  new_sp->start=partition->part_offset;
  new_sp->end=partition->part_offset+partition->part_size-1;
  if(new_sp->end > disk_car->disk_size-1)
    new_sp->end = disk_car->disk_size-1;
  if(new_sp->end > disk_car->disk_real_size-1)
    new_sp->end = disk_car->disk_real_size-1;
  new_sp->file_stat=NULL;
  new_sp->data=1;
  new_sp->list.prev=&new_sp->list;
  new_sp->list.next=&new_sp->list;
  td_list_add_tail(&new_sp->list, &list_search_space->list);
}

void free_list_search_space(alloc_data_t *list_search_space)
{
  struct td_list_head *search_walker = NULL;
  struct td_list_head *search_walker_next = NULL;
  td_list_for_each_safe(search_walker,search_walker_next,&list_search_space->list)
  {
    alloc_data_t *current_search_space;
    current_search_space=td_list_entry(search_walker, alloc_data_t, list);
    td_list_del(search_walker);
    free(current_search_space);
  }
}

/** 
 * @param recup_dir - base name of output directory
 * @param initial_dir_num - first number to try appending.
 * @return the number that was appended.
 */

unsigned int photorec_mkdir(const char *recup_dir, const unsigned int initial_dir_num)
{
  char working_recup_dir[2048];
  int dir_ok=0;
  int dir_num=initial_dir_num;
#ifdef DJGPP
  int i=0;
#endif
  do
  {
    snprintf(working_recup_dir,sizeof(working_recup_dir)-1,"%s.%d",recup_dir,dir_num);
#ifdef HAVE_MKDIR
#ifdef __MINGW32__
    if(mkdir(working_recup_dir)!=0 && errno==EEXIST)
#else
      if(mkdir(working_recup_dir, 0775)!=0 && errno==EEXIST)
#endif
#else
#warning "You need a mkdir function!"
#endif
      {
	dir_num++;
      }
      else
      {
	dir_ok=1;
      }
#ifdef DJGPP
  /* Avoid endless loop in Dos version of Photorec after 999 directories if working with short name */
    i++;
    if(dir_ok==0 && i==1000)
    {
      dir_num=initial_dir_num;
      dir_ok=1;
    }
#endif
  } while(dir_ok==0);
  return dir_num;
}

int get_prev_file_header(alloc_data_t *list_search_space, alloc_data_t **current_search_space, uint64_t *offset)
{
  int nbr;
  alloc_data_t *file_space=*current_search_space;
  uint64_t size=0;
  /* Search backward the first fragment of a file not successfully recovered
   * Limit the search to 10 fragments or 1GB */
  for(nbr=0; nbr<3 && size < (uint64_t)200*1024*1024; nbr++)
  {
    file_space=td_list_prev_entry(file_space, list);
    if(file_space==list_search_space)
      return -1;
    size+=file_space->end - file_space->start + 1;
    if(file_space->file_stat!=NULL)
    {
      *current_search_space=file_space;
      *offset=file_space->start;
      return 0;
    }
  }
  return -1;
}

void forget(alloc_data_t *list_search_space, alloc_data_t *current_search_space)
{
  struct td_list_head *search_walker = NULL;
  struct td_list_head *prev= NULL;
  int nbr=0;
  if(current_search_space==list_search_space)
    return ;
  for(search_walker=&current_search_space->list;
      search_walker!=&list_search_space->list;
      search_walker=prev)
  {
    prev=search_walker->prev;
    if(nbr>10000)
    {
      alloc_data_t *tmp;
      tmp=td_list_entry(search_walker, alloc_data_t, list);
      td_list_del(&tmp->list);
      free(tmp);
    }
    else
      nbr++;
  }
}

unsigned int remove_used_space(disk_t *disk_car, const partition_t *partition, alloc_data_t *list_search_space)
{
  if( partition->upart_type==UP_FAT12 ||
      partition->upart_type==UP_FAT16 || 
      partition->upart_type==UP_FAT32)
    return fat_remove_used_space(disk_car, partition, list_search_space);
  else if(partition->upart_type==UP_EXFAT)
    return exfat_remove_used_space(disk_car, partition, list_search_space);
#if defined(HAVE_LIBNTFS) || defined(HAVE_LIBNTFS3G)
  else if(partition->upart_type==UP_NTFS)
    return ntfs_remove_used_space(disk_car, partition, list_search_space);
#endif
#ifdef HAVE_LIBEXT2FS
  else if(partition->upart_type==UP_EXT2 || partition->upart_type==UP_EXT3 || partition->upart_type==UP_EXT4)
    return ext2_remove_used_space(disk_car, partition, list_search_space);
#endif
  return 0;
}

void update_stats(file_stat_t *file_stats, alloc_data_t *list_search_space)
{
  struct td_list_head *search_walker = NULL;
  int i;
  /* Reset */
  for(i=0;file_stats[i].file_hint!=NULL;i++)
    file_stats[i].not_recovered=0;
  /* Update */
  td_list_for_each(search_walker, &list_search_space->list)
  {
    alloc_data_t *current_search_space;
    current_search_space=td_list_entry(search_walker, alloc_data_t, list);
    if(current_search_space->file_stat!=NULL)
    {
      current_search_space->file_stat->not_recovered++;
    }
  }
}

void write_stats_log(const file_stat_t *file_stats)
{
  unsigned int file_nbr=0;
  unsigned int i;
  unsigned int nbr;
  file_stat_t *new_file_stats;
  for(i=0;file_stats[i].file_hint!=NULL;i++);
  if(i==0)
    return ;
  nbr=i;
  new_file_stats=(file_stat_t*)MALLOC(nbr*sizeof(file_stat_t));
  memcpy(new_file_stats, file_stats, nbr*sizeof(file_stat_t));
  qsort(new_file_stats, nbr, sizeof(file_stat_t), sorfile_stat_ts);
  for(i=0;i<nbr;i++)
  {
    if(new_file_stats[i].recovered+new_file_stats[i].not_recovered>0)
    {
      file_nbr+=new_file_stats[i].recovered;
      log_info("%s: %u/%u recovered\n",
          (new_file_stats[i].file_hint->extension!=NULL?
           new_file_stats[i].file_hint->extension:""),
          new_file_stats[i].recovered, new_file_stats[i].recovered+new_file_stats[i].not_recovered);
    }
  }
  free(new_file_stats);
  if(file_nbr!=1)
  {
    log_info("Total: %u files found\n\n",file_nbr);
  }
  else
  {
    log_info("Total: %u file found\n\n",file_nbr);
  }
}

int sorfile_stat_ts(const void *p1, const void *p2)
{
  const file_stat_t *f1=(const file_stat_t *)p1;
  const file_stat_t *f2=(const file_stat_t *)p2;
  /* bigest to lowest */
  if(f1->recovered < f2->recovered)
    return 1;
  if(f1->recovered > f2->recovered)
    return -1;
  return 0;
}

partition_t *new_whole_disk(const disk_t *disk_car)
{
  partition_t *fake_partition;
  fake_partition=partition_new(disk_car->arch);
  fake_partition->part_offset=0;
  fake_partition->part_size=disk_car->disk_size;
  strncpy(fake_partition->fsname,"Whole disk",sizeof(fake_partition->fsname)-1);
  return fake_partition;
}

unsigned int find_blocksize(alloc_data_t *list_search_space, const unsigned int default_blocksize, uint64_t *offset)
{
  unsigned int blocksize=128*512;
  struct td_list_head *search_walker = NULL;
  int run_again;
  *offset=0;
  if(td_list_empty(&list_search_space->list))
    return default_blocksize;
  *offset=(td_list_first_entry(&list_search_space->list, alloc_data_t, list))->start % blocksize;
  do
  {
    run_again=0;
    td_list_for_each(search_walker, &list_search_space->list)
    {
      const alloc_data_t *tmp=td_list_entry_const(search_walker, const alloc_data_t, list);
      if(tmp->file_stat!=NULL)
      {
	if(tmp->start%blocksize!=*offset && blocksize>default_blocksize)
	{
	  blocksize=blocksize>>1;
	  *offset=tmp->start%blocksize;
	  run_again=1;
	}
      }
    }
  } while(run_again>0);
  return blocksize;
}

void update_blocksize(const unsigned int blocksize, alloc_data_t *list_search_space, const uint64_t offset)
{
  struct td_list_head *search_walker = NULL;
  struct td_list_head *search_walker_prev = NULL;
  log_info("blocksize=%u, offset=%u\n", blocksize, (unsigned int)(offset%blocksize));
  /* Align end of last range (round up) */
  search_walker=list_search_space->list.prev;
  {
    alloc_data_t *current_search_space;
    current_search_space=td_list_entry(search_walker, alloc_data_t, list);
    current_search_space->end=(current_search_space->end+1-offset%blocksize+blocksize-1)/blocksize*blocksize+offset%blocksize-1;
  }
  /* Align start of each range */
  td_list_for_each_prev_safe(search_walker,search_walker_prev,&list_search_space->list)
  {
    alloc_data_t *current_search_space=td_list_entry(search_walker, alloc_data_t, list);
    const uint64_t aligned_start=(current_search_space->start-offset%blocksize+blocksize-1)/blocksize*blocksize+offset%blocksize;
    if(current_search_space->start!=aligned_start)
    {
      alloc_data_t *prev_search_space=td_list_entry(search_walker_prev, alloc_data_t, list);
      if(prev_search_space->end + 1 == current_search_space->start)
      {
	/* merge with previous block */
	prev_search_space->end = current_search_space->end;
	td_list_del(search_walker);
	free(current_search_space);
      }
      else
      {
	current_search_space->start=aligned_start;
	current_search_space->file_stat=NULL;
	if(current_search_space->start>=current_search_space->end)
	{
	  /* block too small - delete it */
	  td_list_del(search_walker);
	  free(current_search_space);
	}
      }
    }
  }
  /* Align end of each range (truncate) */
  td_list_for_each_prev_safe(search_walker, search_walker_prev, &list_search_space->list)
  {
    alloc_data_t *current_search_space=td_list_entry(search_walker, alloc_data_t, list);
    current_search_space->end=(current_search_space->end+1-offset%blocksize)/blocksize*blocksize+offset%blocksize-1;
    if(current_search_space->start>=current_search_space->end)
    {
      /* block too small - delete it */
      td_list_del(search_walker);
      free(current_search_space);
    }
  }
}

uint64_t free_list_allocation_end=0;

void file_block_free(alloc_list_t *list_allocation)
{
  struct td_list_head *tmp = NULL;
  struct td_list_head *tmp_next = NULL;
  td_list_for_each_safe(tmp,tmp_next,&list_allocation->list)
  {
    alloc_list_t *allocated_space;
    allocated_space=td_list_entry(tmp, alloc_list_t, list);
    free_list_allocation_end=allocated_space->end;
    td_list_del(tmp);
    free(allocated_space);
  }
}
/* file_finish_aux()
    @param file_recovery - handle!=NULL
    @param struct ph_param *params
*/

static void file_finish_aux(file_recovery_t *file_recovery, struct ph_param *params, const int paranoid)
{
  if(params->status!=STATUS_EXT2_ON_SAVE_EVERYTHING &&
      params->status!=STATUS_EXT2_OFF_SAVE_EVERYTHING &&
      file_recovery->file_stat!=NULL && file_recovery->file_check!=NULL && 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("%s File too small ( %llu < %llu), reject it\n",
	file_recovery->filename,
	(long long unsigned) file_recovery->file_size,
	(long long unsigned) file_recovery->min_filesize);
    file_recovery->file_size=0;
  }
  if(file_recovery->file_size==0)
  {
    if(paranoid==2)
      return ;
    fclose(file_recovery->handle);
    file_recovery->handle=NULL;
    /* File is zero-length; erase it */
    unlink(file_recovery->filename);
    return;
  }
#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->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);
  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!=NULL)
    file_recovery->file_stat->recovered++;
}

/** file_finish_bf()
    @param file_recovery - 
    @param struct ph_param *params
    @param alloc_data_t *list_search_space

    @returns:
   -1: file not recovered, file_size=0 offset_error!=0
    0: file not recovered
    1: file recovered
 */

int file_finish_bf(file_recovery_t *file_recovery, struct ph_param *params,
    alloc_data_t *list_search_space)
{
  if(file_recovery->file_stat==NULL)
    return 0;
  if(file_recovery->handle)
    file_finish_aux(file_recovery, params, 2);
  if(file_recovery->file_size==0)
  {
    if(file_recovery->offset_error!=0)
      return -1;
    file_block_truncate_zero(file_recovery, list_search_space);
    if(file_recovery->handle!=NULL)
    {
      fclose(file_recovery->handle);
      unlink(file_recovery->filename);
    }
    reset_file_recovery(file_recovery);
    return 0;
  }
  file_block_truncate(file_recovery, list_search_space, params->blocksize);
  file_block_log(file_recovery, params->disk->sector_size);
#ifdef ENABLE_DFXML
  xml_log_file_recovered(file_recovery);
#endif
  file_block_free(&file_recovery->location);
  return 1;
}

void file_recovery_aborted(file_recovery_t *file_recovery, struct ph_param *params, alloc_data_t *list_search_space)
{
  if(file_recovery->file_stat==NULL)
    return ;
  params->offset=file_recovery->location.start;
  if(file_recovery->handle)
  {
    fclose(file_recovery->handle);
    file_recovery->handle=NULL;
    /* File is zero-length; erase it */
    unlink(file_recovery->filename);
  }
  file_block_truncate_zero(file_recovery, list_search_space);
  reset_file_recovery(file_recovery);
}

pfstatus_t file_finish2(file_recovery_t *file_recovery, struct ph_param *params, const int paranoid, alloc_data_t *list_search_space)
{
  int file_truncated;
  if(file_recovery->file_stat==NULL)
    return PFSTATUS_BAD;
  if(file_recovery->handle)
    file_finish_aux(file_recovery, params, (paranoid==0?0:1));
  if(file_recovery->file_size==0)
  {
    file_block_truncate_zero(file_recovery, list_search_space);
    reset_file_recovery(file_recovery);
    return PFSTATUS_BAD;
  }
  file_truncated=file_block_truncate(file_recovery, list_search_space, params->blocksize);
  file_block_log(file_recovery, params->disk->sector_size);
#ifdef ENABLE_DFXML
  xml_log_file_recovered(file_recovery);
#endif
  file_block_free(&file_recovery->location);
  reset_file_recovery(file_recovery);
  return (file_truncated>0?PFSTATUS_OK_TRUNCATED:PFSTATUS_OK);
}

void info_list_search_space(const alloc_data_t *list_search_space, const alloc_data_t *current_search_space, const unsigned int sector_size, const int keep_corrupted_file, const int verbose)
{
  struct td_list_head *search_walker = NULL;
  unsigned long int nbr_headers=0;
  uint64_t sectors_with_unknown_data=0;
  td_list_for_each(search_walker,&list_search_space->list)
  {
    alloc_data_t *tmp;
    tmp=td_list_entry(search_walker, alloc_data_t, list);
    if(tmp->file_stat!=NULL)
    {
      nbr_headers++;
      tmp->file_stat->not_recovered++;
    }
    sectors_with_unknown_data+=(tmp->end-tmp->start+sector_size-1)/sector_size;
    if(verbose>0)
    {
      if(tmp==current_search_space)
        log_info("* ");
      log_info("%lu-%lu: %s\n",(long unsigned)(tmp->start/sector_size),
          (long unsigned)(tmp->end/sector_size),
          (tmp->file_stat!=NULL && tmp->file_stat->file_hint!=NULL?
           (tmp->file_stat->file_hint->extension?
            tmp->file_stat->file_hint->extension:""):
           "(null)"));
    }
  }
  log_info("%llu sectors contain unknown data, %lu invalid files found %s.\n",
      (long long unsigned)sectors_with_unknown_data, (long unsigned)nbr_headers,
      (keep_corrupted_file>0?"but saved":"and rejected"));
}

void free_search_space(alloc_data_t *list_search_space)
{
  struct td_list_head *search_walker = NULL;
  struct td_list_head *search_walker_next = NULL;
  td_list_for_each_safe(search_walker,search_walker_next,&list_search_space->list)
  {
    alloc_data_t *current_search_space;
    current_search_space=td_list_entry(search_walker, alloc_data_t, list);
    td_list_del(search_walker);
    free(current_search_space);
  }
}

void set_filename(file_recovery_t *file_recovery, struct ph_param *params)
{
  const int broken=(params->status==STATUS_EXT2_ON_SAVE_EVERYTHING ||
      params->status==STATUS_EXT2_OFF_SAVE_EVERYTHING);
  if(file_recovery->extension==NULL || file_recovery->extension[0]=='\0')
  {
    snprintf(file_recovery->filename,sizeof(file_recovery->filename)-1,
	"%s.%u/%c%07u", params->recup_dir,
	params->dir_num, (broken?'b':'f'),
	(unsigned int)((file_recovery->location.start - params->partition->part_offset)/ params->disk->sector_size));
  }
  else
  {
    snprintf(file_recovery->filename,sizeof(file_recovery->filename)-1,
	"%s.%u/%c%07u.%s", params->recup_dir,
	params->dir_num, (broken?'b':'f'),
	(unsigned int)((file_recovery->location.start - params->partition->part_offset) / params->disk->sector_size), file_recovery->extension);
  }
}

static void set_search_start_aux(alloc_data_t **new_current_search_space, alloc_data_t *list_search_space, const uint64_t offset)
{
  struct td_list_head *search_walker = NULL;
  td_list_for_each(search_walker, &list_search_space->list)
  {
    alloc_data_t *current_search_space;
    current_search_space=td_list_entry(search_walker, alloc_data_t, list);
    if(current_search_space->start<=offset && offset<= current_search_space->end)
    {
      *new_current_search_space=current_search_space;
      return;
    }
  }
  /* not found */
  search_walker=list_search_space->list.next;
  *new_current_search_space=td_list_entry(search_walker, alloc_data_t, list);
}

uint64_t set_search_start(struct ph_param *params, alloc_data_t **new_current_search_space, alloc_data_t *list_search_space)
{
  uint64_t offset=(*new_current_search_space)->start;
  if(params->offset!=-1)
  {
    offset=params->offset;
    set_search_start_aux(new_current_search_space, list_search_space, offset);
  }
  else if(params->cmd_run!=NULL && params->cmd_run[0]!='\0')
  {
    offset=0;
    skip_comma_in_command(&params->cmd_run);
    while(*params->cmd_run >= '0' && *params->cmd_run <= '9')
    {
      offset=offset * 10 + (*params->cmd_run - '0');
      params->cmd_run++;
    }
    offset*=params->disk->sector_size;
    set_search_start_aux(new_current_search_space, list_search_space, offset);
  }
  return offset;
}

void params_reset(struct ph_param *params, const struct ph_options *options)
{
  params->file_nbr=0;
  params->status=STATUS_FIND_OFFSET;
  params->real_start_time=time(NULL);
  params->dir_num=1;
  params->file_stats=init_file_stats(options->list_file_format);
  params->offset=-1;
  if(params->blocksize==0)
    params->blocksize=params->disk->sector_size;
}

const char *status_to_name(const photorec_status_t status)
{
  switch(status)
  {
    case STATUS_UNFORMAT:
      return "STATUS_UNFORMAT";
    case STATUS_FIND_OFFSET:
      return "STATUS_FIND_OFFSET";
    case STATUS_EXT2_ON:
      return "STATUS_EXT2_ON";
    case STATUS_EXT2_ON_BF:			
      return "STATUS_EXT2_ON_BF";
    case STATUS_EXT2_OFF:
      return "STATUS_EXT2_OFF";
    case STATUS_EXT2_OFF_BF:
      return "STATUS_EXT2_OFF_BF";
    case STATUS_EXT2_ON_SAVE_EVERYTHING:
      return "STATUS_EXT2_ON_SAVE_EVERYTHING";
    case STATUS_EXT2_OFF_SAVE_EVERYTHING:
      return "STATUS_EXT2_OFF_SAVE_EVERYTHING";
    case STATUS_QUIT :
    default:
      return "STATUS_QUIT";
  }
}

void status_inc(struct ph_param *params, const struct ph_options *options)
{
  params->offset=-1;
  switch(params->status)
  {
    case STATUS_UNFORMAT:
      params->status=STATUS_FIND_OFFSET;
      break;
    case STATUS_FIND_OFFSET:
      params->status=(options->mode_ext2>0?STATUS_EXT2_ON:STATUS_EXT2_OFF);
      params->file_nbr=0;
      break;
    case STATUS_EXT2_ON:
      if(options->paranoid>1)
	params->status=STATUS_EXT2_ON_BF;
      else if(options->paranoid==1 && options->keep_corrupted_file>0)
	params->status=STATUS_EXT2_ON_SAVE_EVERYTHING;
      else
	params->status=STATUS_QUIT;
      break;
    case STATUS_EXT2_ON_BF:
      if(options->keep_corrupted_file>0)
	params->status=STATUS_EXT2_ON_SAVE_EVERYTHING;
      else
	params->status=STATUS_QUIT;
      break;
    case STATUS_EXT2_OFF:
      if(options->paranoid>1)
	params->status=STATUS_EXT2_OFF_BF;
      else if(options->paranoid==1 && options->keep_corrupted_file>0)
	params->status=STATUS_EXT2_OFF_SAVE_EVERYTHING;
      else
	params->status=STATUS_QUIT;
      break;
    case STATUS_EXT2_OFF_BF:
      if(options->keep_corrupted_file>0)
	params->status=STATUS_EXT2_OFF_SAVE_EVERYTHING;
      else
	params->status=STATUS_QUIT;
      break;
    default:
      params->status=STATUS_QUIT;
      break;
  }
}

list_part_t *init_list_part(disk_t *disk, const struct ph_options *options)
{
  int insert_error=0;
  list_part_t *list_part;
  partition_t *partition_wd;
  list_part=disk->arch->read_part(disk, (options!=NULL?options->verbose:0), 0);
  partition_wd=new_whole_disk(disk);
  list_part=insert_new_partition(list_part, partition_wd, 0, &insert_error);
  if(insert_error>0)
  {
    free(partition_wd);
  }
  return list_part;
}

/* file_block_remove_from_sp: remove block from list_search_space, update offset and new_current_search_space in consequence */
static inline void file_block_remove_from_sp_aux(alloc_data_t *tmp, alloc_data_t **new_current_search_space, uint64_t *offset, const unsigned int blocksize)
{
  if(tmp->start == *offset)
  {
    tmp->start+=blocksize;
    *offset += blocksize;
    tmp->file_stat=NULL;
    if(tmp->start <= tmp->end)
      return ;
    *new_current_search_space=td_list_next_entry(tmp, list);
    *offset=(*new_current_search_space)->start;
    td_list_del(&tmp->list);
    free(tmp);
    return ;
  }
  if(*offset + blocksize == tmp->end + 1)
  {
    tmp->end-=blocksize;
    *new_current_search_space=td_list_next_entry(tmp, list);
    *offset=(*new_current_search_space)->start;
    return ;
  }
  {
    alloc_data_t *new_sp;
    new_sp=(alloc_data_t*)MALLOC(sizeof(*new_sp));
    new_sp->start=*offset + blocksize;
    new_sp->end=tmp->end;
    new_sp->file_stat=NULL;
    new_sp->data=tmp->data;
    new_sp->list.prev=&new_sp->list;
    new_sp->list.next=&new_sp->list;
    tmp->end=*offset - 1;
    td_list_add(&new_sp->list, &tmp->list);
    *new_current_search_space=new_sp;
    *offset += blocksize;
  }
}

static inline void file_block_remove_from_sp(alloc_data_t *list_search_space, alloc_data_t **new_current_search_space, uint64_t *offset, const unsigned int blocksize)
{
  struct td_list_head *search_walker = &(*new_current_search_space)->list;
  if(search_walker!=NULL)
  {
    alloc_data_t *tmp;
    tmp=td_list_entry(search_walker, alloc_data_t, list);
    if(tmp->start <= *offset && *offset + blocksize <= tmp->end + 1)
    {
      file_block_remove_from_sp_aux(tmp, new_current_search_space, offset, blocksize);
      return;
    }
  }
  td_list_for_each(search_walker, &list_search_space->list)
  {
    alloc_data_t *tmp;
    tmp=td_list_entry(search_walker, alloc_data_t, list);
    if(tmp->start <= *offset && *offset + blocksize <= tmp->end + 1)
    {
      file_block_remove_from_sp_aux(tmp, new_current_search_space, offset, blocksize);
      return ;
    }
  }
  log_critical("file_block_remove_from_sp(list_search_space, alloc_data_t **new_current_search_space, uint64_t *offset, const unsigned int blocksize) failed\n");
}

static inline void file_block_add_to_file(alloc_list_t *list, const uint64_t offset, const uint64_t blocksize, const unsigned int data)
{
  if(!td_list_empty(&list->list))
  {
    alloc_list_t *prev=td_list_last_entry(&list->list, alloc_list_t, list);
    if(prev->end+1==offset && prev->data==data)
    {
      prev->end=offset+blocksize-1;
      return ;
    }
  }
  {
    alloc_list_t *new_list=(alloc_list_t *)MALLOC(sizeof(*new_list));
    new_list->start=offset;
    new_list->end=offset+blocksize-1;
    new_list->data=data;
    td_list_add_tail(&new_list->list, &list->list);
  }
}

void file_block_append(file_recovery_t *file_recovery, alloc_data_t *list_search_space, alloc_data_t **new_current_search_space, uint64_t *offset, const unsigned int blocksize, const unsigned int data)
{
  file_block_add_to_file(&file_recovery->location, *offset, blocksize, data);
  file_block_remove_from_sp(list_search_space, new_current_search_space, offset, blocksize);
}

static void file_block_truncate_aux(const uint64_t start, const uint64_t end, alloc_data_t *list_search_space)
{
  struct td_list_head *search_walker = NULL;
  if(start >= end)
    return ;
  td_list_for_each(search_walker, &list_search_space->list)
  {
    alloc_data_t *tmp;
    tmp=td_list_entry(search_walker, alloc_data_t, list);
    if(tmp->start == end + 1 && tmp->file_stat==NULL)
    {
      tmp->start=start;
      return;
    }
    if(tmp->end + 1 == start)
    {
      tmp->end=end;
      return;
    }
    if(end < tmp->start)
    {
      alloc_data_t *new_sp;
      new_sp=(alloc_data_t*)MALLOC(sizeof(*new_sp));
      new_sp->start=start;
      new_sp->end=end;
      new_sp->file_stat=NULL;
      new_sp->data=1;
      new_sp->list.prev=&new_sp->list;
      new_sp->list.next=&new_sp->list;
      td_list_add(&new_sp->list, tmp->list.prev);
      return;
    }
  }
  {
    alloc_data_t *new_sp;
    new_sp=(alloc_data_t*)MALLOC(sizeof(*new_sp));
    new_sp->start=start;
    new_sp->end=end;
    new_sp->file_stat=NULL;
    new_sp->data=1;
    new_sp->list.prev=&new_sp->list;
    new_sp->list.next=&new_sp->list;
    td_list_add_tail(&new_sp->list, &list_search_space->list);
  }
}

static void file_block_truncate_zero_aux(const uint64_t start, const uint64_t end, alloc_data_t *list_search_space, file_stat_t *file_stat)
{
  struct td_list_head *search_walker = NULL;
  if(start >= end)
    return ;
  td_list_for_each(search_walker, &list_search_space->list)
  {
    alloc_data_t *tmp;
    tmp=td_list_entry(search_walker, alloc_data_t, list);
    if(tmp->start == end + 1 && tmp->file_stat==NULL)
    {
      tmp->start=start;
      tmp->file_stat=file_stat;
      return;
    }
    if(end < tmp->start)
    {
      alloc_data_t *new_sp;
      new_sp=(alloc_data_t*)MALLOC(sizeof(*new_sp));
      new_sp->start=start;
      new_sp->end=end;
      new_sp->file_stat=file_stat;
      new_sp->data=1;
      new_sp->list.prev=&new_sp->list;
      new_sp->list.next=&new_sp->list;
      td_list_add(&new_sp->list, tmp->list.prev);
      return;
    }
  }
  {
    alloc_data_t *new_sp;
    new_sp=(alloc_data_t*)MALLOC(sizeof(*new_sp));
    new_sp->start=start;
    new_sp->end=end;
    new_sp->file_stat=file_stat;
    new_sp->data=1;
    new_sp->list.prev=&new_sp->list;
    new_sp->list.next=&new_sp->list;
    td_list_add_tail(&new_sp->list, &list_search_space->list);
  }
}

static void file_block_truncate_zero(const file_recovery_t *file_recovery, alloc_data_t *list_search_space)
{
  struct td_list_head *tmp;
  struct td_list_head *next;
  int first=1;
  td_list_for_each_safe(tmp, next, &file_recovery->location.list)
  {
    alloc_list_t *element=td_list_entry(tmp, alloc_list_t, list);
    if(first)
    {
      file_block_truncate_zero_aux(element->start, element->end, list_search_space, file_recovery->file_stat);
      first=0;
    }
    else
      file_block_truncate_aux(element->start, element->end, list_search_space);
    td_list_del(tmp);
    free(element);
  }
}

static int file_block_truncate(const file_recovery_t *file_recovery, alloc_data_t *list_search_space, const unsigned int blocksize)
{
  struct td_list_head *tmp;
  struct td_list_head *next;
  uint64_t size=0;
  int file_truncated=0;
  td_list_for_each_safe(tmp, next, &file_recovery->location.list)
  {
    alloc_list_t *element=td_list_entry(tmp, alloc_list_t, list);
    if(size >= file_recovery->file_size)
    {
      file_block_truncate_aux(element->start, element->end, list_search_space);
      td_list_del(tmp);
      free(element);
      file_truncated=1;
    }
    else if(element->data>0)
    {
      if(size + element->end - element->start + 1 > file_recovery->file_size)
      {
	const uint64_t diff=(file_recovery->file_size - size + blocksize - 1) / blocksize * blocksize;
	file_block_truncate_aux(element->start + diff, element->end, list_search_space);
	element->end-=element->end - element->start + 1 - diff;
	size=file_recovery->file_size;
      }
      else
	size+=(element->end-element->start+1);
    }
  }
  return file_truncated;
}

static uint64_t file_offset_end(const file_recovery_t *file_recovery)
{
  const struct td_list_head *tmp=file_recovery->location.list.prev;
  const alloc_list_t *element=td_list_entry_const(tmp, const alloc_list_t, list);
  return element->end;
}

static void file_block_move(const file_recovery_t *file_recovery, alloc_data_t *list_search_space, alloc_data_t **new_current_search_space, uint64_t *offset)
{
  const uint64_t end=file_offset_end(file_recovery);
  struct td_list_head *tmp;
  td_list_for_each(tmp, &list_search_space->list)
  {
    alloc_data_t *element=td_list_entry(tmp, alloc_data_t, list);
    if(element->start > end)
    {
      *new_current_search_space=element;
      *offset=element->start;
      return;
    }
  }
  *new_current_search_space=list_search_space;
}

void file_block_truncate_and_move(file_recovery_t *file_recovery, alloc_data_t *list_search_space, const unsigned int blocksize,  alloc_data_t **new_current_search_space, uint64_t *offset, unsigned char *buffer)
{
  file_block_truncate(file_recovery, list_search_space, blocksize);
  file_block_move(file_recovery, list_search_space, new_current_search_space, offset);
  if(file_recovery->offset_ok > file_recovery->file_size)
    file_recovery->offset_ok=file_recovery->file_size;
  if(file_recovery->offset_error > file_recovery->file_size)
    file_recovery->offset_error=0;
  file_recovery->calculated_file_size=0;
  if(file_recovery->data_check!=NULL)
  {
    uint64_t i;
    unsigned char *block_buffer;
    block_buffer=&buffer[blocksize];
    if(my_fseek(file_recovery->handle, 0, SEEK_SET) < 0)
      return ;
    for(i=0; i< file_recovery->file_size; i+= blocksize)
    {
      if(fread(block_buffer, blocksize, 1, file_recovery->handle) != 1)
	return ;
      file_recovery->data_check(buffer, 2*blocksize, file_recovery);
      memcpy(buffer, block_buffer, blocksize);
    }
  }
  else
  {
    if(my_fseek(file_recovery->handle, file_recovery->file_size, SEEK_SET) < 0)
      return ;
  }
}