bf8c593629c452aaf840b6c6f707ca477b8c8663
[dragonfly.git] / sbin / fsck_msdosfs / check.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
5  * Copyright (c) 1995 Martin Husemann
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34
35 #include "ext.h"
36 #include "fsutil.h"
37
38 int
39 checkfilesys(const char *fname)
40 {
41         int dosfs;
42         struct bootblock boot;
43         struct fatEntry *fat = NULL;
44         int i, finish_dosdirsection=0;
45         int mod = 0;
46         int ret = 8;
47
48         rdonly = alwaysno;
49         if (!preen)
50                 printf("** %s", fname);
51
52         dosfs = open(fname, rdonly ? O_RDONLY : O_RDWR, 0);
53         if (dosfs < 0 && !rdonly) {
54                 dosfs = open(fname, O_RDONLY, 0);
55                 if (dosfs >= 0)
56                         pwarn(" (NO WRITE)\n");
57                 else if (!preen)
58                         printf("\n");
59                 rdonly = 1;
60         } else if (!preen)
61                 printf("\n");
62
63         if (dosfs < 0) {
64                 perr("Can't open `%s'", fname);
65                 return 8;
66         }
67
68         if (readboot(dosfs, &boot) != FSOK) {
69                 close(dosfs);
70                 printf("\n");
71                 return 8;
72         }
73
74         if (!preen)  {
75                 if (boot.ValidFat < 0)
76                         printf("** Phase 1 - Read and Compare FATs\n");
77                 else
78                         printf("** Phase 1 - Read FAT\n");
79         }
80
81         mod |= readfat(dosfs, &boot, boot.ValidFat >= 0 ? boot.ValidFat : 0, &fat);
82         if (mod & FSFATAL) {
83                 close(dosfs);
84                 return 8;
85         }
86
87         if (boot.ValidFat < 0)
88                 for (i = 1; i < boot.bpbFATs; i++) {
89                         struct fatEntry *currentFat;
90
91                         mod |= readfat(dosfs, &boot, i, &currentFat);
92
93                         if (mod & FSFATAL)
94                                 goto out;
95
96                         mod |= comparefat(&boot, fat, currentFat, i);
97                         free(currentFat);
98                         if (mod & FSFATAL)
99                                 goto out;
100                 }
101
102         if (!preen)
103                 printf("** Phase 2 - Check Cluster Chains\n");
104
105         mod |= checkfat(&boot, fat);
106         if (mod & FSFATAL)
107                 goto out;
108         /* delay writing FATs */
109
110         if (!preen)
111                 printf("** Phase 3 - Checking Directories\n");
112
113         mod |= resetDosDirSection(&boot, fat);
114         finish_dosdirsection = 1;
115         if (mod & FSFATAL)
116                 goto out;
117         /* delay writing FATs */
118
119         mod |= handleDirTree(dosfs, &boot, fat);
120         if (mod & FSFATAL)
121                 goto out;
122
123         if (!preen)
124                 printf("** Phase 4 - Checking for Lost Files\n");
125
126         mod |= checklost(dosfs, &boot, fat);
127         if (mod & FSFATAL)
128                 goto out;
129
130         /* now write the FATs */
131         if (mod & FSFATMOD) {
132                 if (ask(1, "Update FATs")) {
133                         mod |= writefat(dosfs, &boot, fat, mod & FSFIXFAT);
134                         if (mod & FSFATAL)
135                                 goto out;
136                 } else
137                         mod |= FSERROR;
138         }
139
140         if (boot.NumBad)
141                 pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n",
142                       boot.NumFiles,
143                       boot.NumFree * boot.ClusterSize / 1024, boot.NumFree,
144                       boot.NumBad * boot.ClusterSize / 1024, boot.NumBad);
145         else
146                 pwarn("%d files, %d free (%d clusters)\n",
147                       boot.NumFiles,
148                       boot.NumFree * boot.ClusterSize / 1024, boot.NumFree);
149
150         if (mod && (mod & FSERROR) == 0) {
151                 if (mod & FSDIRTY) {
152                         if (ask(1, "MARK FILE SYSTEM CLEAN") == 0)
153                                 mod &= ~FSDIRTY;
154
155                         if (mod & FSDIRTY) {
156                                 pwarn("MARKING FILE SYSTEM CLEAN\n");
157                                 mod |= writefat(dosfs, &boot, fat, 1);
158                         } else {
159                                 pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n");
160                                 mod |= FSERROR; /* file system not clean */
161                         }
162                 }
163         }
164
165         if (mod & (FSFATAL | FSERROR))
166                 goto out;
167
168         ret = 0;
169
170 out:
171         if (finish_dosdirsection)
172                 finishDosDirSection();
173         free(fat);
174         close(dosfs);
175
176         if (mod & (FSFATMOD|FSDIRMOD))
177                 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
178
179         return ret;
180 }