"Fossies" - the Fresh Open Source Software Archive 
Member "giis_4.6.2/src/dir.c" (6 Nov 2012, 15851 Bytes) of package /linux/misc/old/giis_4.6.2.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*
2 * /giis/dir.c This files deals with gathering vital infomations need for recovery.
3 *
4 * Copyright (C) 2005,2006,2007,2008,2009,2010,2011,2012 Lakshmipathi.G <lakshmipathi.g@giis.co.in>.
5 * This file may be redistributed under the terms of the GNU Public License.
6 *
7 */
8
9 #include "giis.h"
10
11 /*
12 * set_content_offset() will first set offset of directory and then read it into buffer
13 */
14
15 int set_content_offset ()
16 {
17 int i;
18 fs.block_number = iin.in.i_block[0];
19 fs.content_offset = (unsigned long long) fs.block_number * fs.block_size;
20 lseek64 (fd, fs.content_offset, 0);
21 //giis4.4
22 dir_seq=1;large_dir=0;
23 if(iin.in.i_size > fs.block_size){
24
25 large_dir=1;
26 large_dir_count=iin.in.i_size/fs.block_size;
27 for(i=0;i<(large_dir_count-1);i++){
28 if((iin.in.i_block[i+1]-iin.in.i_block[i]) == 1){
29 dir_seq++;
30 }
31 else
32 break;
33 }
34 }else{
35 large_dir=0;
36 }
37 return 1;
38
39 }
40
41 /*
42 * read_dir() Loads directory into buffer
43 */
44
45 int read_dir ()
46 {
47 int i;
48 i = read (fd, ide.buffer, DIR_SIZE);
49 if (i == -1)
50 {
51 perror ("");
52 printf ("Error Number:%d", errno);
53 return -1;
54 }
55
56 return 1;
57 }
58
59 /*
60 * record_dir() enters directory informations into a file
61 *
62 * Modification for giis2.1:
63 *
64 * Makes a call to check4dir_dups().
65 *
66 * Modification for giis4.2:
67 * Provides user an option to choose directory .
68 *
69 */
70
71 int record_dir (unsigned long parent)
72 {
73 int fp, i;
74 int add_dir_option=-1;
75 int config_file=0;
76
77 fp = open (DIR_INFO_FILE, 2);
78 if (fp == -1)
79 {
80 perror ("open:");
81 printf ("\nError No:%d", errno);
82 close (fp);
83 return -1;
84 }
85
86 lseek (fp, 0, 2); /* Set file pointer to end */
87
88 /* Don't Call during installation. */
89
90 if (install == 0)
91 {
92 i = check4dir_dups (parent);
93 if (i == -1)
94 {
95 perror ("");
96 printf ("Error Number:%d", errno);close (fp);
97 return -1;
98 }
99 if (i == 0)
100 {
101 /* Already we have it */
102
103 close (fp);
104 return 1;
105 }
106
107 }
108
109 /* date n time */
110 result = time (NULL);
111 strcpy (date_time, asctime (localtime (&result)));
112 strcpy (giis_d.info.date_time, date_time);
113
114 if (entry == 0)
115 { /* entry and found are Global */
116 giis_d.info.inode_number = ide.de.inode; /* current inode */
117 for (i = 0; i < ide.de.name_len; i++)
118 giis_d.info.name[i] = ide.de.name[i];
119 giis_d.info.name[i] = '\0';
120 close (fp);
121 return 1;
122 }
123 if (entry == 1)
124 {
125 giis_d.info.parent_inode_number = ide.de.inode;
126 giis_d.info.search_flag = 0;
127 i = write (fp, giis_d.buffer, GIIS_DSIZE);
128 if (i != GIIS_DSIZE)
129 {
130 perror ("write():");
131 printf ("\nError No:%d", errno);
132 close (fp);
133 return -1;
134 }
135 close (fp);
136 return 1;
137 }
138 if (entry >= 2 && !found)
139 giis_d.info.parent_inode_number = giis_d.info.inode_number;
140 found = 1;
141 for (i = 0; i < ide.de.name_len; i++)
142 giis_d.info.name[i] = ide.de.name[i];
143 giis_d.info.name[i] = '\0';
144
145 giis_d.info.inode_number = ide.de.inode;
146 if (giis_d.info.name[0] == '.')
147 { /* don't save system files except . and .. */
148 close (fp);
149 return 1;
150 }
151 giis_d.info.search_flag = 0;
152
153 if ((strcmp (giis_d.info.name, "root") == 0))
154 {
155 giis_d.info.search_flag = 1;
156 root_inode = giis_d.info.inode_number;
157 }
158
159 if ((strcmp (giis_d.info.name, "home") == 0))
160 {
161 giis_d.info.search_flag = 1;
162 home_inode = giis_d.info.inode_number;
163 }
164
165 // giis4.2 : Newly added directories
166
167 //Record everything under / on users choice
168 //Fix: use update_dir giis 4.3 to avaoid user being prompted during giis updation.
169 //giis 4.4 install mode supports config file.
170 if((install_mode == 0) && (update_dir == 0)&&(giis_d.info.parent_inode_number == ROOT_INODE)&&(strcmp (giis_d.info.name, "giis") != 0)&&(strcmp (giis_d.info.name, "home") != 0) && (strcmp (giis_d.info.name, "root") != 0)){
171 printf("\n\n\t\t ==================");
172 printf("\n\t\t directory : /%s ",giis_d.info.name);
173 printf("\n\t\t ====================");
174 printf("\n\t To Add this directory press : 1 ");
175 printf("\n\t To skip this directory press : 0\n");
176 printf("\n\t Please Enter you choice :");
177 scanf("%d",&add_dir_option);
178
179 if(add_dir_option == 1){
180 printf("\n\t\t /%s => Added",giis_d.info.name);
181 giis_d.info.search_flag = 1;
182 }
183 else{
184 printf("\n\t\t /%s => Skipped",giis_d.info.name);
185 giis_d.info.search_flag = 0;
186 }
187 }
188 /// giis-4.2
189 if (update_dir)
190 { /* Record all dir found during updates */
191 giis_d.info.search_flag = 1;
192 giis_d.info.parent_inode_number = parent;
193
194 }
195 //Check directory List to added new
196 config_file=read_config_file(giis_d.info.name);
197 if((config_file==444)&&(giis_d.info.parent_inode_number==2)){
198 printf("\n %s available in /etc/giis.conf ",giis_d.info.name);
199 giis_d.info.search_flag=1;config_file=0;
200 }
201
202 i = write (fp, giis_d.buffer, GIIS_DSIZE);
203 if (i != GIIS_DSIZE)
204 {
205 perror (" write()");
206 printf ("\nError No:%d \t fp=%d", errno, fp);
207 close (fp);
208 return -1;
209 }
210 close (fp);
211 return 1;
212 }
213
214 /*
215 * record_file() records file informations necessary for recovery.
216 * file is system file which often begins with "." then don't record it for obvious reason.
217 * Since that's not created by any users.
218 *
219 * Modification for giis2.1:
220 *
221 * Makes a call to check4file_dups().
222 */
223
224 int record_file (unsigned long parent)
225 {
226 int fp, i;
227 int len=0;
228 static unsigned long parent_original=0;
229 /* giis 4.3 : Store system/hidden files too */
230 /*
231 if (ide.de.name[0] == '.')
232 return 1;
233 */
234 if(ide.de.file_type==2){
235 parent_original=ide.de.inode;
236 }
237 fp = open (FILE_INFO_FILE, 1);
238 if (fp == -1)
239 {
240 perror ("open:");
241 printf ("\nError No:%d", errno);
242 close (fp);
243 return -1;
244 }
245 lseek (fp, 0, 2); /* Set file pointer to end */
246 /* date n time */
247 result = time (NULL);
248 strcpy (date_time, asctime (localtime (&result)));
249 strcpy (giis_f.info.date_time, date_time);
250
251
252 /* Get inode details */
253
254 fs.inode_number = ide.de.inode;
255 i = find_inode_offset ();
256 if (i == -1)
257 {
258 perror ("");
259 printf ("dir.c:record_file()-find_inode_offset:Error Number:%d", errno);close (fp);
260 return -1;
261 }
262
263 i = read_inode ();
264 if (i == -1)
265 {
266 perror ("");
267 printf ("dir.c:record_file()-read_inode:Error Number:%d", errno);close (fp);
268 return -1;
269 }
270
271 /* Store files iff it's a new one or it's old one with some contents modified and
272 Don't Called during installation. */
273 if (install == 0)
274 {
275 /* if mtime not within last 20 mintues then ingore -no need to call avoid dups..
276 auto_time2 compute from TIME value in giis.conf*/
277
278 if(iin.in.i_mtime < (time(0)-auto_time2))
279 {
280 close (fp);
281 return 1;
282 }
283
284 i = check4file_dups (parent_original);
285 if (i == -1)
286 {
287 perror ("");
288 printf ("dir.c:record_file()-check4file_dups:Error Number:%d", errno);close (fp);
289 return -1;
290 }
291 if (i == 0)
292 { /* Already we have it */
293 close (fp);
294 return 1;
295 }
296 }
297
298 for (i = 0; i < ide.de.name_len; i++)
299 giis_f.info.name[i] = ide.de.name[i];
300 giis_f.info.name[i] = '\0';
301
302 len=i;
303 if(len > MAX_FILE_LENGTH ){
304 close (fp);
305 return 1; //File name is too large-Just ignore these files.
306 }
307
308
309 giis_f.info.inode_number = ide.de.inode;
310 giis_f.info.parent_inode_number = parent_original; /*Same parent 4 all files in dir--but set it correctly :) */
311 giis_f.info.mode = iin.in.i_mode;
312 giis_f.info.owner = iin.in.i_uid;
313 giis_f.info.file_blocks_count = iin.in.i_blocks;
314 giis_f.info.file_size = iin.in.i_size;
315 giis_f.info.file_flags = iin.in.i_flags;
316 giis_f.info.search_flag = 0;
317 //reset recorded time correctly
318 strcpy (giis_f.info.date_time, date_time);
319
320
321 giis_f.info.last_data_block = 0;
322 for (i = 0; i < 15; i++)
323 {
324 giis_f.info.data_block[i] = iin.in.i_block[i];
325 /* Store block number of files<48KB size */
326 if (iin.in.i_block[i] == 0 && i <= 12 && !giis_f.info.last_data_block)
327 giis_f.info.last_data_block = iin.in.i_block[i - 1];
328 }
329
330 /* Error check 4 some raw error check details */
331
332 if (iin.in.i_block[0] == iin.in.i_block[1]){
333 close(fp);
334 return 1;
335 }
336 if (iin.in.i_size > 49152 && (iin.in.i_block[12] == 0)){
337 close(fp);
338 return 1;
339 }
340 if (iin.in.i_size < 49152 && (iin.in.i_block[12] != 0)){
341 close(fp);
342 return 1;
343 }
344
345 if (iin.in.i_block[14] != 0) /* Not supported :-) */
346 {
347 close(fp);
348 return 1;
349 }
350
351
352
353 /* Set flags */
354
355 giis_f.info.sfragment_flag = 0; /* Fragment level for single indirect */
356 giis_f.info.dfragment_flag = 0; /* Do the Same for double */
357 giis_f.info.is_offset = 0;
358 giis_f.info.id_offset = 0;
359
360
361 if (iin.in.i_size >= 49152)
362 {
363
364 if (giis_f.info.data_block[12] != 0)
365 {
366 i = search4sequence12 ();
367 if (i == -1)
368 {
369 perror ("");
370 printf ("dir.c:record_file()-search4sequence12:Error Number:%d", errno);close (fp);
371 return -1;
372 }
373 }
374
375 if (giis_f.info.data_block[13] != 0)
376 {
377
378 gi_round = 0;
379 hole = 0; /* used in search4sequence13 */
380 giis_f.info.last_data_block = 0;
381 i = search4sequence13 ();
382 if (i == -1)
383 {
384 perror ("");
385 printf ("dir.c:record_file()-search4sequence13:Error Number:%d", errno);close (fp);
386 return -1;
387 }
388 }
389
390 }
391
392 i = write (fp, giis_f.buffer, GIIS_FSIZE);
393 if (i != GIIS_FSIZE)
394 {
395 perror (" write():");
396 printf ("\nError No:%d", errno);
397 close (fp);
398 return -1;
399 }
400
401
402
403 close (fp);
404 return 1;
405 }
406
407 /*
408 * show_dir() displays the content of the requested directory inode.
409 * And during installation stores file and directory entries.
410 *
411 * Modification for giis2:
412 *
413 * Introduce new variable install_file with it's help we first store all directory
414 * informations first. Once all directories are recorded then we start to record files
415 * from these directories.
416 *
417 * Modification for giis2.1:
418 *
419 * update_dir variable to update directories.
420 */
421
422 int show_dir ()
423 {
424 int i;
425 unsigned long long temp_content_offset = 0, parent = 0; /* So that we don't mess up original offset */
426 temp_content_offset = fs.content_offset;
427 int total_rec_len=0;//giis 4.3
428 int process_block1=0,process_block2=0,process_block3=0,process_block4=0;
429 int process_block5=0,process_block6=0,process_block7=0;
430 int process_block8=0,process_block9=0,process_block10=0;
431 int process_block11=0;
432
433
434 if(large_dir==1 && dir_seq==1){
435
436 process_block1=iin.in.i_block[1]-iin.in.i_block[0];
437 if(iin.in.i_block[2])
438 process_block2=iin.in.i_block[2]-iin.in.i_block[1];
439 if(iin.in.i_block[3])
440 process_block3=iin.in.i_block[3]-iin.in.i_block[2];
441 if(iin.in.i_block[4])
442 process_block4=iin.in.i_block[4]-iin.in.i_block[3];
443
444 if(iin.in.i_block[5])
445 process_block5=iin.in.i_block[5]-iin.in.i_block[4];
446 if(iin.in.i_block[6])
447 process_block6=iin.in.i_block[6]-iin.in.i_block[5];
448 if(iin.in.i_block[7])
449 process_block7=iin.in.i_block[7]-iin.in.i_block[6];
450
451 if(iin.in.i_block[8])
452 process_block8=iin.in.i_block[8]-iin.in.i_block[7];
453 if(iin.in.i_block[9])
454 process_block9=iin.in.i_block[9]-iin.in.i_block[8];
455 if(iin.in.i_block[10])
456 process_block10=iin.in.i_block[10]-iin.in.i_block[9];
457 if(iin.in.i_block[11])
458 process_block11=iin.in.i_block[11]-iin.in.i_block[10];
459
460 }
461
462
463 top:
464 while ((ide.de.rec_len != 0) && (ide.de.rec_len % 4 == 0)&&total_rec_len<(fs.block_size*dir_seq))
465 {
466 total_rec_len+=ide.de.rec_len;
467
468 /*First entry is current directory inode */
469
470 if ((entry == 0) && (ide.de.file_type == 2))
471 {
472 parent = ide.de.inode;
473 }
474
475 if (update_dir && (ide.de.file_type == 2))
476 {
477 record_dir (parent);
478 entry++;
479 }
480 if (update_dir == 0)
481 {
482
483 if ((update_file == 0) && (install_file == 0) && (ide.de.file_type == 2))
484 {
485 record_dir (parent); /*Install:Record data in DIR_INFO_FILE */
486 entry++;
487 }
488 //giis 4.3 : Igonore sockets,symbolic links etc..except regular file and directory.
489
490 if ((update_file || install_file) && (ide.de.file_type == 1 ||( ide.de.name_len==1 && ide.de.file_type == 2 && ide.de.name[0]=='.')))
491 {
492 /*Record data in FILE_INFO_FILE */
493 record_file (parent);
494 }
495 }
496 temp_content_offset += ide.de.rec_len; /* Next entry */
497 i = lseek64 (fd, temp_content_offset, 0);
498 i = read (fd, ide.buffer, DIR_SIZE);
499 }
500
501 //if large_dir=1 and dir_seq=1 then it refers it's large directory but doesn't have data block in sequence
502 //only first data block is processed.to process others use process_block1,process_block2..
503 if((large_dir==1) && (dir_seq==1) &&( (process_block1!=0)||(process_block2!=0)||(process_block3!=0)||(process_block4!=0)||(process_block5!=0)
504 ||(process_block6!=0)||(process_block7!=0)||(process_block8!=0)||(process_block9!=0)||(process_block10!=0)
505 ||(process_block11!=0))){
506 total_rec_len=0;//reset
507
508 if(process_block1!=0){
509 temp_content_offset=temp_content_offset+(process_block1*fs.block_size);
510 i = lseek64 (fd, temp_content_offset, 0);
511 i = read (fd, ide.buffer, DIR_SIZE);process_block1=0;
512 goto top;
513 }
514 if(process_block2!=0){
515 temp_content_offset=temp_content_offset+(process_block2*fs.block_size);
516 i = lseek64 (fd, temp_content_offset, 0);
517 i = read (fd, ide.buffer, DIR_SIZE);process_block2=0;
518 goto top;
519 }
520 if(process_block3!=0){
521 temp_content_offset=temp_content_offset+(process_block3*fs.block_size);
522 i = lseek64 (fd, temp_content_offset, 0);
523 i = read (fd, ide.buffer, DIR_SIZE);process_block3=0;
524 goto top;
525 }
526 if(process_block4!=0){
527 temp_content_offset=temp_content_offset+(process_block4*fs.block_size);
528 i = lseek64 (fd, temp_content_offset, 0);
529 i = read (fd, ide.buffer, DIR_SIZE);process_block4=0;
530 goto top;
531 }
532
533
534 if(process_block5!=0){
535 temp_content_offset=temp_content_offset+(process_block5*fs.block_size);
536 i = lseek64 (fd, temp_content_offset, 0);
537 i = read (fd, ide.buffer, DIR_SIZE);process_block5=0;
538 goto top;
539 }
540 if(process_block6!=0){
541 temp_content_offset=temp_content_offset+(process_block6*fs.block_size);
542 i = lseek64 (fd, temp_content_offset, 0);
543 i = read (fd, ide.buffer, DIR_SIZE);process_block6=0;
544 goto top;
545 }
546 if(process_block7!=0){
547 temp_content_offset=temp_content_offset+(process_block7*fs.block_size);
548 i = lseek64 (fd, temp_content_offset, 0);
549 i = read (fd, ide.buffer, DIR_SIZE);process_block7=0;
550 goto top;
551 }
552 if(process_block8!=0){
553 temp_content_offset=temp_content_offset+(process_block8*fs.block_size);
554 i = lseek64 (fd, temp_content_offset, 0);
555 i = read (fd, ide.buffer, DIR_SIZE);process_block8=0;
556 goto top;
557 }
558 if(process_block9!=0){
559 temp_content_offset=temp_content_offset+(process_block9*fs.block_size);
560 i = lseek64 (fd, temp_content_offset, 0);
561 i = read (fd, ide.buffer, DIR_SIZE);process_block9=0;
562 goto top;
563 }
564 if(process_block10!=0){
565 temp_content_offset=temp_content_offset+(process_block10*fs.block_size);
566 i = lseek64 (fd, temp_content_offset, 0);
567 i = read (fd, ide.buffer, DIR_SIZE);process_block10=0;
568 goto top;
569 }
570 if(process_block11!=0){
571 temp_content_offset=temp_content_offset+(process_block11*fs.block_size);
572 i = lseek64 (fd, temp_content_offset, 0);
573 i = read (fd, ide.buffer, DIR_SIZE);process_block11=0;
574 goto top;
575 }
576
577
578
579 }//ends
580
581 /* reset for next directories */
582 entry = 0;
583 found = 0;
584 return 1;
585 }