aboutsummaryrefslogtreecommitdiffstats
path: root/src/dmsdosfsck.c
blob: 8e66b7fc35a71533802e63727a48cd4289248f62 (plain) (blame)
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
/*

dmsdosfsck.c

DMSDOS: filesystem check utility for CVFs.

******************************************************************************
DMSDOS (compressed MSDOS filesystem support) for Linux
written 1995-1998 by Frank Gockel and Pavel Pisa

    (C) Copyright 1995-1998 by Frank Gockel
    (C) Copyright 1996-1998 by Pavel Pisa

Some code of dmsdos has been copied from the msdos filesystem
so there are the following additional copyrights:

    (C) Copyright 1992,1993 by Werner Almesberger (msdos filesystem)
    (C) Copyright 1994,1995 by Jacques Gelinas (mmap code)
    (C) Copyright 1992-1995 by Linus Torvalds

DMSDOS was inspired by the THS filesystem (a simple doublespace
DS-0-2 compressed read-only filesystem) written 1994 by Thomas Scheuermann.

The DMSDOS code is distributed under the Gnu General Public Licence.
See file COPYING for details.
******************************************************************************

*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<unistd.h>
#include"dmsdos.h"
#include"lib_interface.h"

int check_dir(int parent,int clusternr);

/*this is not good - but currently we have only one CVF open at a time*/
struct super_block*sb;
Dblsb*dblsb;

typedef struct
{ int referrenced_from;
  int start;
} Fatdata;

Fatdata fat[65536];

#define FAT_EOF -1

int seenlist[65536];
int repair_automatically=0;
int repair_interactively=0;
int verbose=0;
int listfiles=0;

#define vprintf if(verbose)printf
#define lprintf if(listfiles)printf

int repair(char*text)
{ int c;

  if(repair_automatically)return 1;
  if(repair_interactively==0)return 0;
  fflush(stdin);
  printf("%s",text);
  fflush(stdout);
  c=fgetc(stdin);
  fflush(stdin);
  if(c=='y'||c=='Y')return 1;
  return 0;
}

int check_fat_loop(int begin)
{ int seen=0;
  int next;
  int i;
  int newval;
  
  while(begin>=2&&begin<=dblsb->s_max_cluster)
  { /* add to seenlist */
    seenlist[seen]=begin;
    ++seen;
    if(seen>65535)return -1; /* cannot happen */
    next=dbl_fat_nextcluster(sb,begin,NULL);
    
    /* check whether it was already seen */
    for(i=0;i<seen;++i)
    { if(seenlist[i]==next)
      { /* here begins a fat loop */
        printf("FAT loop at cluster %d found.\n",begin);
        if(repair("Break it?")==0)return 1;
        newval=FAT_EOF;
        dbl_fat_nextcluster(sb,begin,&newval);
        return 0;
      }
    }
    
    begin=next;
  }
  
  return 0;
}

int check_fat()
{ int i;
  int val;
  int errors=0;
  int newval=0;
      
  for(i=0;i<65536;++i)
  { fat[i].referrenced_from=0;
    fat[i].start=0;
  }
  
  for(i=2;i<=dblsb->s_max_cluster;++i)
  { vprintf("Checking cluster %d...\n",i);
  
    val=dbl_fat_nextcluster(sb,i,NULL);
    if(val)
    { if(val==1)
      { printf("cluster %d: invalid fat entry\n",i);
        if(repair("Correct it?")==0)++errors;
        else
        { newval=FAT_EOF;
          dbl_fat_nextcluster(sb,i,&newval);
        }
      }
      if(check_fat_loop(i))
      { printf("Unresolved FAT loop in filesystem. Can't continue, sorry.\n");
        close_cvf(sb);
        exit(4);
      }
    }
    if(val>=2&&val<=dblsb->s_max_cluster)
    { if(fat[val].referrenced_from)
      { /* this is a crosslink */
        printf("cluster %d is crosslinked with %d to %d\n",
               i,fat[val].referrenced_from,val);
        if(repair("Break it?")==0)++errors;
        else
        { newval=FAT_EOF;
          dbl_fat_nextcluster(sb,i,&newval);
          dbl_fat_nextcluster(sb,fat[val].referrenced_from,&newval);
          fat[val].referrenced_from=0;
        }
      }
      fat[val].referrenced_from=i;
    }
  }
  
  return errors;
}

int check_chains()
{ int i;
  int val;
  int errors=0;
  int newval=0;
  int next;

  for(i=2;i<=dblsb->s_max_cluster;++i)
  { val=dbl_fat_nextcluster(sb,i,NULL);
    if(val>=2&&val<=dblsb->s_max_cluster&&fat[i].referrenced_from==0)
    { /* this is the start of a chain */
      vprintf("checking chain beginning at cluster %d...\n",i);

     rchain:
      next=dbl_fat_nextcluster(sb,val,NULL);
      if(next==FAT_EOF)continue;
      
      if(next==0||next>dblsb->s_max_cluster)
      { printf("chain breaks unexpectedly.\n");
        if(repair("Set proper end?")==0)++errors;
        else
        { newval=FAT_EOF;
          dbl_fat_nextcluster(sb,val,&newval);
        }
      }
      else
      { val=next;
        goto rchain;
      }
      
    }
  }
  return 0;
}

struct nametest
{ unsigned char name[12];
  struct nametest*next; 
} nametest;

int add_name(struct nametest**namelist,unsigned char*text)
{ struct nametest*akt=*namelist;

  while(akt)
  { if(strncmp(akt->name,text,11)==0)return -1;
    akt=akt->next;
  }
  
  akt=malloc(sizeof(struct nametest));
  if(akt==NULL)return -2;
  
  strncpy(akt->name,text,11);
  akt->next=*namelist;
  *namelist=akt;
  
  return 0;
}

void free_namelist(struct nametest**namelist)
{ struct nametest*akt=*namelist;
  struct nametest*merk;
  
  while(akt)
  { merk=akt->next;
    free(akt);
    akt=merk;
  }
  
  *namelist=NULL;
}

int check_char(unsigned char c,int noprint)
{ if(c==0x5)c=0xE5; /* M$ hack for languages where E5 is a valid char */
  if(c<32||strchr("+\\?*<>|\"=,;",c)!=NULL)
  { if(!noprint)lprintf("?");
    return 1;
  }
  if(!noprint)lprintf("%c",c);
  return 0;
}

int check_direntry(int dirstartclust, unsigned char*data, int*need_write,
                   struct nametest**namelist)
{ int i;
  unsigned int x;
  unsigned char*pp;
  unsigned long size;
  int cluster, prevcluster, newval;
  unsigned long fatsize;
  int clustersize;
  int invchar;

  *need_write=0;
    
  if(data[0]==0||data[0]==0xE5)return 0;

  if(data[11]&8)return 0; /* ignore V entries */
  
  invchar=0;
  for(i=0;i<11;++i)
  { if(i==8)lprintf(" ");
    invchar+=check_char(data[i],0);
  }
  
  if(listfiles)
  {
  printf("  ");
  if(data[11]&1)printf("R");else printf(" ");
  if(data[11]&2)printf("H");else printf(" ");
  if(data[11]&4)printf("S");else printf(" ");
  if(data[11]&8)printf("V");else printf(" ");
  if(data[11]&16)printf("D");else printf(" ");
  if(data[11]&32)printf("A");else printf(" ");
  if(data[11]&64)printf("?");else printf(" ");
  if(data[11]&128)printf("?");else printf(" ");

  pp=&(data[22]);
  x=CHS(pp);
  printf("  %02d:%02d:%02d",x>>11,(x>>5)&63,(x&31)<<1);

  pp=&(data[24]);
  x=CHS(pp);
  printf(" %02d.%02d.%4d",x&31,(x>>5)&15,(x>>9)+1980); /* y2k compliant :) */
  }
  
  pp=&(data[26]);
  cluster=CHS(pp);
  lprintf(" %5d",cluster);

  pp=&(data[28]);
  size=CHL(pp);
  lprintf(" %7lu ",size);

  if(invchar)
  { printf("name has invalid chars, ");
    if(repair("Replace them?")!=0)
    { for(i=0;i<11;++i)
      { if(check_char(data[i],1))
        { data[i]='~';
          *need_write=1;
        }
      }
    }
  }
  
  if(add_name(namelist,data))
  { printf("duplicate filename ");
    for(i=0;i<11;++i)
    { if(i==8)printf(" ");
      printf("%c",data[i]);
    }
    printf("\n");

    if(repair("Rename?")!=0)
    { char teststr[10];
      int n=1;
      
      i=7;
      while(i>=0)
      { if(data[i]==' ')data[i]='~';
        else break;
        --i;
      }
     
      do
      { sprintf(teststr,"~%d",n++);
        for(i=0;i<strlen(teststr);++i)data[i+8-strlen(teststr)]=teststr[i];
      }
      while(add_name(namelist,data));
      *need_write=1;
    }
  }
  
  if(cluster<0||cluster==1||cluster>dblsb->s_max_cluster)
  { printf("clusternr invalid\n");
    if(repair("Truncate?")==0)return -1;
    data[26]=0;
    data[27]=0;
    cluster=0;
    *need_write=1;
  }
  else 
  { if(cluster)
    { if(fat[cluster].referrenced_from!=0||fat[cluster].start!=0)
      { printf("first cluster crosslink\n");
        if(repair("Truncate?")==0)return -1;
        data[26]=0;
        data[27]=0;
        cluster=0;
        *need_write=1; 
      }
      else fat[cluster].start=1;
    }
  }

  if(data[11]&16) /* dir */
  { if(cluster==0)
    { printf("clusternr invalid for subdir\n");
      goto irrepdir;
    }
    lprintf("OK\n");
    lprintf("descending directory...\n");
    i=check_dir(dirstartclust,cluster);
    lprintf("ascending...\n");
    if(i>=0)
    { if(size)
      { printf("directory entry has size !=0\n");
        if(repair("Correct this?")==0)++i;
        else
        { data[28]=0;
          data[29]=0;
          data[30]=0;
          data[31]=0;
          size=0;
          *need_write=1;
        }
      }
      return i;
    }
   irrepdir:
    printf("directory is irreparably damaged");
    if(repair("Convert to file?")==0)return -1;
    data[11]&=~16;
    *need_write=1;
    /* fall through */
  }
  
  if(cluster==0)
  { if(size==0)
    { lprintf("OK\n");
      return 0;
    }
    printf("wrong size\n");
    if(repair("Correct this?")==0)return -1;
    else
    { data[28]=0;
      data[29]=0;
      data[30]=0;
      data[31]=0;
      size=0;
      *need_write=1;
    }
    return 0;
  }

  clustersize=dblsb->s_sectperclust*SECTOR_SIZE;

  fatsize=0;
  prevcluster=0;
  while(cluster>1&&cluster<=dblsb->s_max_cluster)
  { prevcluster=cluster;
    cluster=dbl_fat_nextcluster(sb,cluster,NULL);
    fatsize+=clustersize;
  }
  if(cluster==0)
  { printf("fat alloc ends with zero\n");
    cluster=prevcluster;
    fatsize-=clustersize;
    if(repair("Correct this?")==0)return -1;
    newval=FAT_EOF;
    dbl_fat_nextcluster(sb,cluster,&newval);
  }
  if(cluster==1||cluster>dblsb->s_max_cluster)
  { printf("fat alloc invalid\n");
    return -1;
  }
    
  if(size==fatsize)
  { lprintf("OK\n");
    return 0;
  }
  if(size/clustersize==(fatsize-1)/clustersize)
  { lprintf("OK\n");
    return 0;
  }
  printf("file size wrong\n");
  if(repair("Recalculate file size?")==0)return -1;
  data[28]=fatsize;
  data[29]=fatsize>>8;
  data[30]=fatsize>>16;
  data[31]=fatsize>>24;
  *need_write=1;
  return 0;
}

int check_root_dir(void)
{ int i,j,errors,r;
  struct buffer_head*bh;
  int need_write=0;
  struct nametest*namelist=NULL;
  
  errors=0;
  
  for(i=0;i<dblsb->s_rootdirentries/16;++i)
  { bh=raw_bread(sb,dblsb->s_rootdir+i);
    if(bh==NULL)return -1;
    for(j=0;j<16;++j)
    { r=check_direntry(0,&(bh->b_data[j*32]),&need_write,&namelist);
      if(r)++errors;
      else if(need_write)raw_mark_buffer_dirty(sb,bh,1);
    }
    raw_brelse(sb,bh);
  }
  
  free_namelist(&namelist);
  return errors;
}

int check_dir(int parent,int clusternr)
{ unsigned char*data;
  int j,errors,r;
  int next;
  int start=2;
  int dirstartclust=clusternr;
  int need_write=0;
  int len;
  struct nametest*namelist=NULL;
  
  errors=0;
  data=malloc(dblsb->s_sectperclust*SECTOR_SIZE);
  if(data==NULL)return -1;
  
  vprintf("checking directory at start cluster %d...\n",clusternr);
  
  next=dbl_fat_nextcluster(sb,clusternr,NULL);
  if(next==0)
  { printf("warning: dir cluster %d is marked free\n",clusternr);
  }
  
  while(clusternr>0)
  { len=dmsdos_read_cluster(sb,data,clusternr);
    if(len<0){free(data);return -1;}
  
    if(start)
    { unsigned char*pp;
    
      /* check .       12345678EXT */
      if(strncmp(data,".          ",11)!=0)
      { printf("first entry is not '.'\n");
        ++errors;
      }
      pp=&(data[26]);
      if(CHS(pp)!=clusternr)
      { printf("self cluster nr is wrong in '.'\n");
        ++errors;
      }

      /* check ..      12345678EXT */
      if(strncmp(data+32,"..         ",11)!=0)
      { printf("second entry is not '..'\n");
        ++errors;
      }
      pp=&(data[26+32]);
      if(CHS(pp)!=parent)
      { printf("parent cluster nr is wrong in '..'\n");
        ++errors;
      }
      
      if(errors)
      { printf("This doesn't look like a directory, skipped\n");
        free(data);
        return -1;
      }
    }
    
    for(j=start;j<dblsb->s_sectperclust*16;++j)
    { r=check_direntry(dirstartclust,&(data[j*32]),&need_write,&namelist);
      if(r)++errors;
      else if(need_write)dmsdos_write_cluster(sb,data,len,clusternr,0,1);
    } 
    
    start=0;
    next=dbl_fat_nextcluster(sb,clusternr,NULL);
    if(next==1||next>dblsb->s_max_cluster)
    { printf("directory ends with fat alloc error\n");
      ++errors;
      break;
    }
    clusternr=next;
  }
  
  free(data);
  free_namelist(&namelist);
  return errors;
}

int check_unused_chains()
{ int i;
  int val;
  int errors=0;
  
  for(i=2;i<=dblsb->s_max_cluster;++i)
  { val=dbl_fat_nextcluster(sb,i,NULL);
    if(val>=2&&val<=dblsb->s_max_cluster&&fat[i].referrenced_from==0
       &&fat[i].start==0)
    { vprintf("chain beginning with cluster %d is unused.\n",i);
      /* if(repair("Delete it?")==0)++errors;
         else
         { free_chain(sb,i);
         }
      */
      ++errors;
    }
  }
  
  return errors;
}

int main(int argc, char*argv[])
{ int errors=0;
  int i;
  char*filename=NULL;
  
  fprintf(stderr, "dmsdosfsck 0.0.2 ALPHA TEST (be extremely careful with repairs)\n");
  
  if(argc==1)
  { usage:
    fprintf(stderr, "usage: dmsdosfsck [ -aflrtvVw ] [ -d path -d ... ] [ -u path -u ... ] device\n"
                    "-a          automatically repair the file system\n"
                    "-d path (*) drop that file\n"
                    "-f      (*) salvage unused chains to files\n"
                    "-l          list path names\n"
                    "-r          interactively repair the file system\n"
                    "-t      (*) test for bad clusters\n"
                    "-u path (*) try to undelete that (non-directory) file\n"
                    "-v          verbose mode\n"
                    "-V      (*) perform a verification pass\n"
                    "-w      (*) write changes to disk immediately\n"
                    "(*) not yet implemented but option accepted for dosfsck compatibility\n");
    exit(16);
  }
  
  for(i=1;i<argc;++i)
  { if(strcmp(argv[i],"-a")==0)repair_automatically=1;
    else if(strcmp(argv[i],"-r")==0)repair_interactively=1;
    else if(strcmp(argv[i],"-l")==0)listfiles=1;
    else if(strcmp(argv[i],"-v")==0)verbose=1;
    else if(argv[i][0]!='-')filename=argv[i];
  }
  
  if(filename==NULL)goto usage;
  
  if(repair_automatically!=0||repair_interactively!=0)
  { printf("\n\nWARNING: repair functions are incomplete. Interrupt within 5 seconds to abort.\7\n\n\n");
    sleep(5);
  }
  
  sb=open_cvf(filename,repair_automatically|repair_interactively);
  if(sb==NULL)
  { fprintf(stderr,"open_cvf %s failed - maybe this isn't a CVF.\n",filename);
    exit(16);
  }

  dblsb=MSDOS_SB(sb)->private_data; 
 
  printf("pass 1: checking FAT...\n");
 
  i=check_fat();
  if(i)
  { printf("Filesystem has fatal FAT errors\n");
    printf("Cannot continue, sorry.\n");
    printf("Don't mount this filesystem, it may crash or hang the FAT driver.\n");
    close_cvf(sb);
    exit(4);
  }

  printf("pass 2: checking cluster chains...\n");
  
  i=check_chains();
  if(i)
  { printf("filesystem has errors in cluster chains\n");
    ++errors;
  }
  
  printf("pass 3: calling dmsdos simple_check...\n");
  
  i=simple_check(sb,0);
  if(i==-1) /* there were fatal FAT errors detected */
  { printf("Filesystem still has fatal FAT errors\n");
    printf("CANNOT HAPPEN. THIS IS A BUG.\n");
    close_cvf(sb);
    abort();
  }
  if(i)
  { printf("filesystem has low-level structure errors\n");
    if(repair("Try to fix them?")==0)++errors;
    else 
    { i=simple_check(sb,1);
      if(i)
      { printf("couldn't fix all low-level structure errors\n");
        ++errors;
      }
    }
  }

  printf("pass 4: checking directories...\n");

  if(check_root_dir())
  { printf("filesystem has msdos-level structure errors\n");
    ++errors;
  }
  
  printf("pass 5: checking for unused chains...\n");
  
  i=check_unused_chains();
  if(i)
  { printf("filesystem has unused cluster chains\n");
    ++errors;
  }
  
  close_cvf(sb);
  
  if(errors==0)
  { printf("filesystem has no errors\n");
  }
  
  return (errors==0)?0:4;
}