1 /*
2 
3     File: dirpart.c
4 
5     Copyright (C) 1998-2008 Christophe GRENIER <grenier@cgsecurity.org>
6 
7     This software is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write the Free Software Foundation, Inc., 51
19     Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 
21  */
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 #include <stdio.h>
27 #ifdef HAVE_STRING_H
28 #include <string.h>
29 #endif
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
33 #include "types.h"
34 #include "common.h"
35 #include "fat.h"
36 #include "intrf.h"
37 #ifdef HAVE_NCURSES
38 #include "intrfn.h"
39 #endif
40 #include "dir.h"
41 #include "dirn.h"
42 #include "exfat_dir.h"
43 #include "ext2_dir.h"
44 #include "fat_dir.h"
45 #include "ntfs_dir.h"
46 #include "rfs_dir.h"
47 #include "dirpart.h"
48 #include "ntfs.h"
49 #include "adv.h"
50 #include "log.h"
51 #include "log_part.h"
52 
dir_partition_init(disk_t * disk,const partition_t * partition,const int verbose,const int expert,dir_data_t * dir_data)53 static dir_partition_t dir_partition_init(disk_t *disk, const partition_t *partition, const int verbose, const int expert, dir_data_t *dir_data)
54 {
55   dir_partition_t res=DIR_PART_ENOIMP;
56   if(is_part_fat(partition))
57     res=dir_partition_fat_init(disk, partition, dir_data, verbose);
58   else if(is_part_ntfs(partition))
59   {
60     res=dir_partition_ntfs_init(disk, partition, dir_data, verbose, expert);
61     if(res!=DIR_PART_OK)
62       res=dir_partition_exfat_init(disk, partition, dir_data, verbose);
63   }
64   else if(is_part_linux(partition))
65   {
66     res=dir_partition_ext2_init(disk, partition, dir_data, verbose);
67     if(res!=DIR_PART_OK)
68       res=dir_partition_reiser_init(disk, partition, dir_data, verbose);
69   }
70   if(res==DIR_PART_OK)
71     return DIR_PART_OK;
72   switch(partition->upart_type)
73   {
74     case UP_FAT12:
75     case UP_FAT16:
76     case UP_FAT32:
77       return dir_partition_fat_init(disk, partition, dir_data, verbose);
78     case UP_EXT4:
79     case UP_EXT3:
80     case UP_EXT2:
81       return dir_partition_ext2_init(disk, partition, dir_data, verbose);
82     case UP_RFS:
83     case UP_RFS2:
84     case UP_RFS3:
85       return dir_partition_reiser_init(disk, partition, dir_data, verbose);
86     case UP_NTFS:
87       return dir_partition_ntfs_init(disk, partition, dir_data, verbose, expert);
88     case UP_EXFAT:
89       return dir_partition_exfat_init(disk, partition, dir_data, verbose);
90     default:
91       return res;
92   }
93 }
94 
dir_partition(disk_t * disk,const partition_t * partition,const int verbose,const int expert,char ** current_cmd)95 dir_partition_t dir_partition(disk_t *disk, const partition_t *partition, const int verbose, const int expert, char **current_cmd)
96 {
97   dir_data_t dir_data;
98 #ifdef HAVE_NCURSES
99   WINDOW *window;
100 #endif
101   dir_partition_t res;
102   fflush(stderr);
103   dir_data.local_dir=NULL;
104   res=dir_partition_init(disk, partition, verbose, expert, &dir_data);
105 #ifdef HAVE_NCURSES
106   window=newwin(LINES, COLS, 0, 0);	/* full screen */
107   dir_data.display=window;
108   aff_copy(window);
109 #else
110   dir_data.display=NULL;
111 #endif
112   log_info("\n");
113   switch(res)
114   {
115     case DIR_PART_ENOIMP:
116       screen_buffer_reset();
117 #ifdef HAVE_NCURSES
118       aff_copy(window);
119       wmove(window,4,0);
120       aff_part(window,AFF_PART_ORDER|AFF_PART_STATUS,disk,partition);
121 #endif
122       log_partition(disk,partition);
123       screen_buffer_add("Support for this filesystem hasn't been implemented.\n");
124       screen_buffer_to_log();
125       if(current_cmd==NULL || *current_cmd==NULL)
126       {
127 #ifdef HAVE_NCURSES
128 	screen_buffer_display(window,"",NULL);
129 #endif
130       }
131       break;
132     case DIR_PART_ENOSYS:
133       screen_buffer_reset();
134 #ifdef HAVE_NCURSES
135       aff_copy(window);
136       wmove(window,4,0);
137       aff_part(window,AFF_PART_ORDER|AFF_PART_STATUS,disk,partition);
138 #endif
139       log_partition(disk,partition);
140       screen_buffer_add("Support for this filesystem wasn't enabled during compilation.\n");
141       screen_buffer_to_log();
142       if(current_cmd==NULL || *current_cmd==NULL)
143       {
144 #ifdef HAVE_NCURSES
145 	screen_buffer_display(window,"",NULL);
146 #endif
147       }
148       break;
149     case DIR_PART_EIO:
150       screen_buffer_reset();
151 #ifdef HAVE_NCURSES
152       aff_copy(window);
153       wmove(window,4,0);
154       aff_part(window,AFF_PART_ORDER|AFF_PART_STATUS,disk,partition);
155 #endif
156       log_partition(disk,partition);
157       screen_buffer_add("Can't open filesystem. Filesystem seems damaged.\n");
158       screen_buffer_to_log();
159       if(current_cmd==NULL || *current_cmd==NULL)
160       {
161 #ifdef HAVE_NCURSES
162 	screen_buffer_display(window,"",NULL);
163 #endif
164       }
165       break;
166     case DIR_PART_OK:
167       {
168 	int recursive=0;
169 	int copy_files=0;
170 	if(current_cmd!=NULL && *current_cmd!=NULL)
171 	{
172 	  int do_continue;
173 	  do
174 	  {
175 	    do_continue=0;
176 	    skip_comma_in_command(current_cmd);
177 	    if(check_command(current_cmd,"recursive",9)==0)
178 	    {
179 	      recursive=1;
180 	      do_continue=1;
181 	    }
182 	    else if(check_command(current_cmd,"fullpathname",12)==0)
183 	    {
184 	      dir_data.param|=FLAG_LIST_PATHNAME;
185 	      do_continue=1;
186 	    }
187 	    else if(check_command(current_cmd, "filecopy", 8)==0)
188 	    {
189 	      copy_files=1;
190 	      do_continue=1;
191 	    }
192 	  } while(do_continue==1);
193 	}
194 	if(recursive>0)
195 	  dir_whole_partition_log(disk,partition,&dir_data,dir_data.current_inode);
196 	else
197 	{
198 #ifdef HAVE_NCURSES
199 	  dir_partition_aff(disk, partition, &dir_data, dir_data.current_inode, current_cmd);
200 #else
201 	  if(dir_data.verbose>0)
202 	  {
203 	    log_info("\ndir_partition inode=%lu\n", dir_data.current_inode);
204 	    log_partition(disk, partition);
205 	  }
206 	  {
207 	    file_info_t dir_list;
208 	    TD_INIT_LIST_HEAD(&dir_list.list);
209 	    dir_data.get_dir(disk, partition, &dir_data, dir_data.current_inode, &dir_list);
210 	    dir_aff_log(&dir_data, &dir_list);
211 	    delete_list_file(&dir_list);
212 	  }
213 #endif
214 	}
215 	if(copy_files>0)
216 	  dir_whole_partition_copy(disk,partition,&dir_data,dir_data.current_inode);
217 	dir_data.close(&dir_data);
218       }
219       break;
220   }
221 #ifdef HAVE_NCURSES
222   delwin(window);
223   (void) clearok(stdscr, TRUE);
224 #ifdef HAVE_TOUCHWIN
225   touchwin(stdscr);
226 #endif
227   wrefresh(stdscr);
228 #endif
229   fflush(stderr);
230   free(dir_data.local_dir);
231   return res;
232 }
233