summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorChristophe Grenier <grenier@cgsecurity.org>2014-02-01 10:05:52 +0100
committerChristophe Grenier <grenier@cgsecurity.org>2014-02-01 10:05:52 +0100
commit6a4dc80c3c330acb6f65ddfbf05f8a342d7af4cd (patch)
treee88f85cf950aaa5f9648c6ee33d6ed986fb7f199 /src
parent990c9a93918675323855ee61a95a03ddd36c4f2e (diff)
TestDisk: add missing log_close() in main()
Diffstat (limited to 'src')
-rw-r--r--src/testdisk.c309
1 files changed, 169 insertions, 140 deletions
diff --git a/src/testdisk.c b/src/testdisk.c
index dde8a31..d988803 100644
--- a/src/testdisk.c
+++ b/src/testdisk.c
@@ -85,11 +85,143 @@ static void sighup_hdlr(int sig)
}
#endif
+static void display_help(void)
+{
+ printf("\n" \
+ "Usage: testdisk [/log] [/debug] [file.dd|file.e01|device]\n"\
+ " testdisk /list [/log] [file.dd|file.e01|device]\n" \
+ " testdisk /version\n" \
+ "\n" \
+ "/log : create a testdisk.log file\n" \
+ "/debug : add debug information\n" \
+ "/list : display current partitions\n" \
+ "\n" \
+ "TestDisk checks and recovers lost partitions\n" \
+ "It works with :\n" \
+ "- BeFS (BeOS) - BSD disklabel (Free/Open/Net BSD)\n" \
+ "- CramFS, Compressed File System - DOS/Windows FAT12, FAT16 and FAT32\n" \
+ "- XBox FATX - Windows exFAT\n" \
+ "- HFS, HFS+, Hierarchical File System - JFS, IBM's Journaled File System\n" \
+ "- Linux btrfs - Linux ext2, ext3 and ext4\n" \
+ "- Linux GFS2 - Linux LUKS\n" \
+ "- Linux Raid - Linux Swap\n" \
+ "- LVM, LVM2, Logical Volume Manager - Netware NSS\n" \
+ "- Windows NTFS - ReiserFS 3.5, 3.6 and 4\n" \
+ "- Sun Solaris i386 disklabel - UFS and UFS2 (Sun/BSD/...)\n" \
+ "- XFS, SGI's Journaled File System - Wii WBFS\n" \
+ "- Sun ZFS\n");
+}
+
+static void display_version(void)
+{
+ printf("\n");
+ printf("Version: %s\n", VERSION);
+ printf("Compiler: %s\n", get_compiler());
+ printf("Compilation date: %s\n", get_compilation_date());
+ printf("ext2fs lib: %s, ntfs lib: %s, reiserfs lib: %s, ewf lib: %s\n",
+ td_ext2fs_version(), td_ntfs_version(), td_reiserfs_version(), td_ewf_version());
+ printf("OS: %s\n" , get_os());
+}
+
+static int display_disk_list(list_disk_t *list_disk, const int testdisk_mode,
+ const int create_backup, const int safe, const int saveheader, const int unit, const int verbose)
+{
+ list_disk_t *element_disk;
+ printf("Please wait...\n");
+ /* Scan for available device only if no device or image has been supplied in parameter */
+ if(list_disk==NULL)
+ list_disk=hd_parse(list_disk, verbose, testdisk_mode);
+ if(list_disk==NULL)
+ {
+ printf("No disk detected.\n");
+#if defined(__CYGWIN__) || defined(__MINGW32__) || defined(DJGPP)
+#else
+#ifdef HAVE_GETEUID
+ if(geteuid()!=0)
+ {
+ printf("You need to be root to use TestDisk.\n");
+ }
+#endif
+#endif
+ return 1;
+ }
+
+ /* Activate the cache */
+ for(element_disk=list_disk;element_disk!=NULL;element_disk=element_disk->next)
+ element_disk->disk=new_diskcache(element_disk->disk,testdisk_mode);
+ if(safe==0)
+ hd_update_all_geometry(list_disk, verbose);
+ for(element_disk=list_disk;element_disk!=NULL;element_disk=element_disk->next)
+ {
+ disk_t *disk=element_disk->disk;
+ const int hpa_dco=is_hpa_or_dco(disk);
+ printf("%s\n", disk->description(disk));
+ printf("Sector size:%u\n", disk->sector_size);
+ if(disk->model!=NULL)
+ printf("Model: %s", disk->model);
+ if(disk->serial_no!=NULL)
+ printf(", S/N:%s", disk->serial_no);
+ if(disk->fw_rev!=NULL)
+ printf(", FW:%s", disk->fw_rev);
+ printf("\n");
+ if(hpa_dco!=0)
+ {
+ if(disk->sector_size!=0)
+ printf("size %llu sectors\n", (long long unsigned)(disk->disk_real_size/disk->sector_size));
+ if(disk->user_max!=0)
+ printf("user_max %llu sectors\n", (long long unsigned)disk->user_max);
+ if(disk->native_max!=0)
+ printf("native_max %llu sectors\n", (long long unsigned)(disk->native_max+1));
+ if(disk->dco!=0)
+ printf("dco %llu sectors\n", (long long unsigned)(disk->dco+1));
+ if(hpa_dco&1)
+ printf("Host Protected Area (HPA) present.\n");
+ if(hpa_dco&2)
+ printf("Device Configuration Overlay (DCO) present.\n");
+ }
+ printf("\n");
+ }
+
+ for(element_disk=list_disk;element_disk!=NULL;element_disk=element_disk->next)
+ {
+ disk_t *disk=element_disk->disk;
+ autodetect_arch(disk, NULL);
+ if(unit==UNIT_DEFAULT)
+ autoset_unit(disk);
+ else
+ disk->unit=unit;
+ interface_list(disk, verbose, saveheader, create_backup);
+ printf("\n");
+ }
+ delete_list_disk(list_disk);
+ return 0;
+}
+
+static void log_disk_list(list_disk_t *list_disk)
+{
+ list_disk_t *element_disk;
+ /* save disk parameters to rapport */
+ log_info("Hard disk list\n");
+ for(element_disk=list_disk;element_disk!=NULL;element_disk=element_disk->next)
+ {
+ disk_t *disk=element_disk->disk;
+ log_info("%s, sector size=%u", disk->description(disk), disk->sector_size);
+ if(disk->model!=NULL)
+ log_info(" - %s", disk->model);
+ if(disk->serial_no!=NULL)
+ log_info(", S/N:%s", disk->serial_no);
+ if(disk->fw_rev!=NULL)
+ log_info(", FW:%s", disk->fw_rev);
+ log_info("\n");
+ }
+ log_info("\n");
+}
+
int main( int argc, char **argv )
{
int i;
int use_sudo=0;
- int help=0, version=0, verbose=0, dump_ind=0;
+ int verbose=0, dump_ind=0;
int create_log=TD_LOG_NONE;
int do_list=0;
int unit=UNIT_DEFAULT;
@@ -135,9 +267,11 @@ int main( int argc, char **argv )
else if((strcmp(argv[i],"/logname")==0) ||(strcmp(argv[i],"-logname")==0))
{
if(i+2>=argc)
- help=1;
- else
- logfile=argv[++i];
+ {
+ display_help();
+ return 1;
+ }
+ logfile=argv[++i];
}
else if((strcmp(argv[i],"/log")==0) ||(strcmp(argv[i],"-log")==0))
{
@@ -163,10 +297,18 @@ int main( int argc, char **argv )
else if((strcmp(argv[i],"/help")==0) || (strcmp(argv[i],"-help")==0) || (strcmp(argv[i],"--help")==0) ||
(strcmp(argv[i],"/h")==0) || (strcmp(argv[i],"-h")==0) ||
(strcmp(argv[i],"/?")==0) || (strcmp(argv[i],"-?")==0))
- help=1;
+ {
+ display_help();
+ log_close();
+ return 0;
+ }
else if((strcmp(argv[i],"/version")==0) || (strcmp(argv[i],"-version")==0) || (strcmp(argv[i],"--version")==0) ||
(strcmp(argv[i],"/v")==0) || (strcmp(argv[i],"-v")==0))
- version=1;
+ {
+ display_version();
+ log_close();
+ return 0;
+ }
else if(strcmp(argv[i],"/list")==0 || strcmp(argv[i],"-list")==0 || strcmp(argv[i],"-l")==0)
{
do_list=1;
@@ -185,8 +327,11 @@ int main( int argc, char **argv )
else if(strcmp(argv[i],"/cmd")==0)
{
if(i+2>=argc)
- help=1;
- else
+ {
+ display_help();
+ log_close();
+ return 1;
+ }
{
disk_t *disk_car;
cmd_device=argv[++i];
@@ -194,11 +339,11 @@ int main( int argc, char **argv )
disk_car=file_test_availability(cmd_device, verbose, testdisk_mode);
if(disk_car==NULL)
{
- printf("\nUnable to open file or device %s\n",cmd_device);
- help=1;
+ printf("\nUnable to open file or device %s: %s\n", cmd_device, strerror(errno));
+ log_close();
+ return 1;
}
- else
- list_disk=insert_new_disk(list_disk,disk_car);
+ list_disk=insert_new_disk(list_disk,disk_car);
}
}
else
@@ -206,124 +351,19 @@ int main( int argc, char **argv )
disk_t *disk_car=file_test_availability(argv[i], verbose, testdisk_mode);
if(disk_car==NULL)
{
- printf("\nUnable to open file or device %s\n",argv[i]);
- help=1;
+ printf("\nUnable to open file or device %s: %s\n", argv[i], strerror(errno));
+ log_close();
+ return 1;
}
- else
- list_disk=insert_new_disk(list_disk,disk_car);
+ list_disk=insert_new_disk(list_disk,disk_car);
}
}
- if(version!=0)
- {
- printf("\n");
- printf("Version: %s\n", VERSION);
- printf("Compiler: %s\n", get_compiler());
- printf("Compilation date: %s\n", get_compilation_date());
- printf("ext2fs lib: %s, ntfs lib: %s, reiserfs lib: %s, ewf lib: %s\n",
- td_ext2fs_version(), td_ntfs_version(), td_reiserfs_version(), td_ewf_version());
- printf("OS: %s\n" , get_os());
- return 0;
- }
- if(help!=0)
- {
- printf("\n" \
- "Usage: testdisk [/log] [/debug] [file.dd|file.e01|device]\n"\
- " testdisk /list [/log] [file.dd|file.e01|device]\n" \
- " testdisk /version\n" \
- "\n" \
- "/log : create a testdisk.log file\n" \
- "/debug : add debug information\n" \
- "/list : display current partitions\n" \
- "\n" \
- "TestDisk checks and recovers lost partitions\n" \
- "It works with :\n" \
- "- BeFS (BeOS) - BSD disklabel (Free/Open/Net BSD)\n" \
- "- CramFS, Compressed File System - DOS/Windows FAT12, FAT16 and FAT32\n" \
- "- XBox FATX - Windows exFAT\n" \
- "- HFS, HFS+, Hierarchical File System - JFS, IBM's Journaled File System\n" \
- "- Linux btrfs - Linux ext2, ext3 and ext4\n" \
- "- Linux GFS2 - Linux LUKS\n" \
- "- Linux Raid - Linux Swap\n" \
- "- LVM, LVM2, Logical Volume Manager - Netware NSS\n" \
- "- Windows NTFS - ReiserFS 3.5, 3.6 and 4\n" \
- "- Sun Solaris i386 disklabel - UFS and UFS2 (Sun/BSD/...)\n" \
- "- XFS, SGI's Journaled File System - Wii WBFS\n" \
- "- Sun ZFS\n" \
- "\n" \
- "If you have problems with TestDisk or bug reports, please contact me.\n");
- return 0;
- }
screen_buffer_reset();
if(do_list!=0)
{
- printf("Please wait...\n");
- /* Scan for available device only if no device or image has been supplied in parameter */
- if(list_disk==NULL)
- list_disk=hd_parse(list_disk, verbose, testdisk_mode);
- if(list_disk==NULL)
- {
- printf("No disk detected.\n");
-#if defined(__CYGWIN__) || defined(__MINGW32__) || defined(DJGPP)
-#else
-#ifdef HAVE_GETEUID
- if(geteuid()!=0)
- {
- printf("You need to be root to use TestDisk.\n");
- }
-#endif
-#endif
- return 1;
- }
-
- /* Activate the cache */
- for(element_disk=list_disk;element_disk!=NULL;element_disk=element_disk->next)
- element_disk->disk=new_diskcache(element_disk->disk,testdisk_mode);
- if(safe==0)
- hd_update_all_geometry(list_disk, verbose);
- for(element_disk=list_disk;element_disk!=NULL;element_disk=element_disk->next)
- {
- disk_t *disk=element_disk->disk;
- const int hpa_dco=is_hpa_or_dco(disk);
- printf("%s\n", disk->description(disk));
- printf("Sector size:%u\n", disk->sector_size);
- if(disk->model!=NULL)
- printf("Model: %s", disk->model);
- if(disk->serial_no!=NULL)
- printf(", S/N:%s", disk->serial_no);
- if(disk->fw_rev!=NULL)
- printf(", FW:%s", disk->fw_rev);
- printf("\n");
- if(hpa_dco!=0)
- {
- if(disk->sector_size!=0)
- printf("size %llu sectors\n", (long long unsigned)(disk->disk_real_size/disk->sector_size));
- if(disk->user_max!=0)
- printf("user_max %llu sectors\n", (long long unsigned)disk->user_max);
- if(disk->native_max!=0)
- printf("native_max %llu sectors\n", (long long unsigned)(disk->native_max+1));
- if(disk->dco!=0)
- printf("dco %llu sectors\n", (long long unsigned)(disk->dco+1));
- if(hpa_dco&1)
- printf("Host Protected Area (HPA) present.\n");
- if(hpa_dco&2)
- printf("Device Configuration Overlay (DCO) present.\n");
- }
- printf("\n");
- }
-
- for(element_disk=list_disk;element_disk!=NULL;element_disk=element_disk->next)
- {
- disk_t *disk=element_disk->disk;
- autodetect_arch(disk, NULL);
- if(unit==UNIT_DEFAULT)
- autoset_unit(disk);
- else
- disk->unit=unit;
- interface_list(disk, verbose, saveheader, create_backup);
- printf("\n");
- }
- delete_list_disk(list_disk);
- return 0;
+ const int res=display_disk_list(list_disk, testdisk_mode, create_backup, safe, saveheader, unit, verbose);
+ log_close();
+ return res;
}
#ifdef HAVE_SETLOCALE
if(run_setlocale>0)
@@ -343,7 +383,10 @@ int main( int argc, char **argv )
#ifdef HAVE_NCURSES
/* ncurses need locale for correct unicode support */
if(start_ncurses("TestDisk",argv[0]))
+ {
+ log_close();
return 1;
+ }
if(argc==1 && create_log==TD_LOG_NONE)
{
verbose=1;
@@ -375,7 +418,6 @@ int main( int argc, char **argv )
for(i=1;i<argc;i++)
log_info(" %s", argv[i]);
log_info("\n\n");
- log_flush();
}
log_info("TestDisk %s, Data Recovery Utility, %s\nChristophe GRENIER <grenier@cgsecurity.org>\nhttp://www.cgsecurity.org\n", VERSION, TESTDISKDATE);
log_info("OS: %s\n" , get_os());
@@ -392,6 +434,7 @@ int main( int argc, char **argv )
}
#endif
#endif
+ log_flush();
#ifdef HAVE_NCURSES
aff_copy(stdscr);
wmove(stdscr,5,0);
@@ -414,21 +457,7 @@ int main( int argc, char **argv )
#endif
if(safe==0)
hd_update_all_geometry(list_disk, verbose);
- /* save disk parameters to rapport */
- log_info("Hard disk list\n");
- for(element_disk=list_disk;element_disk!=NULL;element_disk=element_disk->next)
- {
- disk_t *disk=element_disk->disk;
- log_info("%s, sector size=%u", disk->description(disk), disk->sector_size);
- if(disk->model!=NULL)
- log_info(" - %s", disk->model);
- if(disk->serial_no!=NULL)
- log_info(", S/N:%s", disk->serial_no);
- if(disk->fw_rev!=NULL)
- log_info(", FW:%s", disk->fw_rev);
- log_info("\n");
- }
- log_info("\n");
+ log_disk_list(list_disk);
#ifdef SUDO_BIN
if(list_disk==NULL)
{