]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fsck_msdosfs/check.c
Diff reduction against NetBSD, no functional change.
[FreeBSD/FreeBSD.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
29 #include <sys/cdefs.h>
30 #ifndef lint
31 __RCSID("$NetBSD: check.c,v 1.14 2006/06/05 16:51:18 christos Exp $");
32 static const char rcsid[] =
33   "$FreeBSD$";
34 #endif /* not lint */
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41
42 #include "ext.h"
43 #include "fsutil.h"
44
45 int
46 checkfilesys(const char *fname)
47 {
48         int dosfs;
49         struct bootblock boot;
50         struct fat_descriptor *fat = NULL;
51         int finish_dosdirsection=0;
52         int mod = 0;
53         int ret = 8;
54
55         rdonly = alwaysno;
56         if (!preen)
57                 printf("** %s", fname);
58
59         dosfs = open(fname, rdonly ? O_RDONLY : O_RDWR, 0);
60         if (dosfs < 0 && !rdonly) {
61                 dosfs = open(fname, O_RDONLY, 0);
62                 if (dosfs >= 0)
63                         pwarn(" (NO WRITE)\n");
64                 else if (!preen)
65                         printf("\n");
66                 rdonly = 1;
67         } else if (!preen)
68                 printf("\n");
69
70         if (dosfs < 0) {
71                 perr("Can't open `%s'", fname);
72                 printf("\n");
73                 return 8;
74         }
75
76         if (readboot(dosfs, &boot) == FSFATAL) {
77                 close(dosfs);
78                 printf("\n");
79                 return 8;
80         }
81
82         if (skipclean && preen && checkdirty(dosfs, &boot)) {
83                 printf("%s: ", fname);
84                 printf("FILESYSTEM CLEAN; SKIPPING CHECKS\n");
85                 ret = 0;
86                 goto out;
87         }
88
89         if (!preen)  {
90                 printf("** Phase 1 - Read FAT and checking connectivity\n");
91         }
92
93         mod |= readfat(dosfs, &boot, &fat);
94         if (mod & FSFATAL) {
95                 close(dosfs);
96                 return 8;
97         }
98
99         if (!preen)
100                 printf("** Phase 2 - Checking Directories\n");
101
102         mod |= resetDosDirSection(fat);
103         finish_dosdirsection = 1;
104         if (mod & FSFATAL)
105                 goto out;
106         /* delay writing FATs */
107
108         mod |= handleDirTree(fat);
109         if (mod & FSFATAL)
110                 goto out;
111
112         if (!preen)
113                 printf("** Phase 3 - Checking for Lost Files\n");
114
115         mod |= checklost(fat);
116         if (mod & FSFATAL)
117                 goto out;
118
119         /* now write the FATs */
120         if (mod & FSFATMOD) {
121                 if (ask(1, "Update FATs")) {
122                         mod |= writefat(fat);
123                         if (mod & FSFATAL)
124                                 goto out;
125                 } else
126                         mod |= FSERROR;
127         }
128
129         if (boot.NumBad)
130                 pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n",
131                       boot.NumFiles,
132                       boot.NumFree * boot.ClusterSize / 1024, boot.NumFree,
133                       boot.NumBad * boot.ClusterSize / 1024, boot.NumBad);
134         else
135                 pwarn("%d files, %d free (%d clusters)\n",
136                       boot.NumFiles,
137                       boot.NumFree * boot.ClusterSize / 1024, boot.NumFree);
138
139         if (mod && (mod & FSERROR) == 0) {
140                 if (mod & FSDIRTY) {
141                         if (ask(1, "MARK FILE SYSTEM CLEAN") == 0)
142                                 mod &= ~FSDIRTY;
143
144                         if (mod & FSDIRTY) {
145                                 pwarn("MARKING FILE SYSTEM CLEAN\n");
146                                 mod |= writefat(fat);
147                         } else {
148                                 pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n");
149                                 mod |= FSERROR; /* file system not clean */
150                         }
151                 }
152         }
153
154         if (mod & (FSFATAL | FSERROR))
155                 goto out;
156
157         ret = 0;
158
159     out:
160         if (finish_dosdirsection)
161                 finishDosDirSection();
162         free(fat);
163         close(dosfs);
164
165         if (mod & (FSFATMOD|FSDIRMOD))
166                 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
167
168         return ret;
169 }