aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

main.c (8562B)


      1 /*	$NetBSD: main.c,v 1.41 2020/04/05 15:25:39 joerg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1986, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      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  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1997 Manuel Bouyer.
     34  *
     35  * Redistribution and use in source and binary forms, with or without
     36  * modification, are permitted provided that the following conditions
     37  * are met:
     38  * 1. Redistributions of source code must retain the above copyright
     39  *    notice, this list of conditions and the following disclaimer.
     40  * 2. Redistributions in binary form must reproduce the above copyright
     41  *    notice, this list of conditions and the following disclaimer in the
     42  *    documentation and/or other materials provided with the distribution.
     43  *
     44  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     45  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     47  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     48  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     49  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     50  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     51  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     52  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     53  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     54  */
     55 
     56 #ifndef lint
     57 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\
     58  The Regents of the University of California.  All rights reserved.");
     59 #endif /* not lint */
     60 
     61 #ifndef lint
     62 #if 0
     63 static char sccsid[] = "@(#)main.c	8.2 (Berkeley) 1/23/94";
     64 #else
     65 __RCSID("$NetBSD: main.c,v 1.41 2020/04/05 15:25:39 joerg Exp $");
     66 #endif
     67 #endif /* not lint */
     68 
     69 #include <sys/param.h>
     70 #include <sys/time.h>
     71 #include <ufs/ext2fs/ext2fs_dinode.h>
     72 #include <ufs/ext2fs/ext2fs.h>
     73 #include <stdlib.h>
     74 #include <string.h>
     75 #include <ctype.h>
     76 #include <stdio.h>
     77 #include <time.h>
     78 #include <unistd.h>
     79 #include <signal.h>
     80 
     81 #include "fsck.h"
     82 #include "extern.h"
     83 #include "fsutil.h"
     84 #include "exitvalues.h"
     85 
     86 struct bufarea       bufhead;
     87 struct bufarea       sblk;
     88 struct bufarea       asblk;
     89 struct bufarea      *pdirbp;
     90 struct bufarea      *pbp;
     91 struct bufarea      *getdatablk(daddr_t, long);
     92 struct m_ext2fs      sblock;
     93 struct dups         *duplist;
     94 struct dups         *muldup;
     95 struct zlncnt       *zlnhead;
     96 struct inoinfo     **inphead, **inpsort;
     97 long                 numdirs, listmax, inplast;
     98 long                 dev_bsize;
     99 long                 secsize;
    100 char                 nflag;
    101 char                 yflag;
    102 int                  bflag;
    103 int                  Uflag;
    104 int                  debug;
    105 int                  preen;
    106 char                 havesb;
    107 char                 skipclean;
    108 int                  fsmodified;
    109 int                  fsreadfd;
    110 int                  fswritefd;
    111 int                  rerun;
    112 daddr_t              maxfsblock;
    113 char                *blockmap;
    114 ino_t                maxino;
    115 ino_t                lastino;
    116 char                *statemap;
    117 u_char              *typemap;
    118 int16_t             *lncntp;
    119 ino_t                lfdir;
    120 daddr_t              n_blks;
    121 daddr_t              n_files;
    122 struct ext2fs_dinode zino;
    123 
    124 static int  checkfilesys(const char *, char *, long, int);
    125 static void usage(void) __dead;
    126 int         unresolved;
    127 
    128 int
    129 main(int argc, char *argv[])
    130 {
    131   int result;
    132 
    133   ARGBEGIN
    134   {
    135     case 'n':
    136     case 'f':
    137       break;
    138     default:
    139       usage();
    140   }
    141   ARGEND
    142   if (argc != 1)
    143     usage();
    144   nflag  = 1;
    145   result = checkfilesys(argv[0], NULL, 0, 0);
    146   return fshut(stdout, "<stdout>") ? FSCK_EXIT_CHECK_FAILED : result;
    147 }
    148 
    149 /*
    150  * Check the specified filesystem.
    151  */
    152 /* ARGSUSED */
    153 static int
    154 checkfilesys(const char *filesys, char *mntpt, long auxdata, int child)
    155 {
    156   daddr_t        n_bfree;
    157   struct dups   *dp;
    158   struct zlncnt *zlnp;
    159   int            i;
    160 
    161   if (preen && child)
    162     (void)signal(SIGQUIT, voidquit);
    163   setcdevname(filesys, preen);
    164   if (debug && preen)
    165     pwarn("starting\n");
    166   switch (setup(filesys)) {
    167     case 0:
    168       return FSCK_EXIT_CHECK_FAILED;
    169     case -1:
    170       return FSCK_EXIT_OK;
    171   }
    172   /*
    173    * 1: scan inodes tallying blocks used
    174    */
    175   if (preen == 0) {
    176     if (sblock.e2fs.e2fs_rev > E2FS_REV0) {
    177       printf("** Last Mounted on %.64s\n", sblock.e2fs.e2fs_fsmnt);
    178     }
    179     printf("** Phase 1 - Check Blocks and Sizes\n");
    180   }
    181   pass1();
    182 
    183   /*
    184    * 1b: locate first references to duplicates, if any
    185    */
    186   if (duplist) {
    187     if (preen)
    188       pfatal("INTERNAL ERROR: dups with -p");
    189     printf("** Phase 1b - Rescan For More DUPS\n");
    190     pass1b();
    191   }
    192 
    193   /*
    194    * 2: traverse directories from root to mark all connected directories
    195    */
    196   if (preen == 0)
    197     printf("** Phase 2 - Check Pathnames\n");
    198   pass2();
    199 
    200   /*
    201    * 3: scan inodes looking for disconnected directories
    202    */
    203   if (preen == 0)
    204     printf("** Phase 3 - Check Connectivity\n");
    205   pass3();
    206 
    207   /*
    208    * 4: scan inodes looking for disconnected files; check reference counts
    209    */
    210   if (preen == 0)
    211     printf("** Phase 4 - Check Reference Counts\n");
    212   pass4();
    213 
    214   /*
    215    * 5: check and repair resource counts in cylinder groups
    216    */
    217   if (preen == 0)
    218     printf("** Phase 5 - Check Cyl groups\n");
    219   pass5();
    220 
    221   /*
    222    * print out summary statistics
    223    */
    224   n_bfree = sblock.e2fs.e2fs_fbcount;
    225 
    226   pwarn(
    227       "%lld files, %lld used, %lld free\n",
    228       (long long)n_files,
    229       (long long)n_blks,
    230       (long long)n_bfree
    231   );
    232   if (debug &&
    233       /* 9 reserved and unused inodes in FS */
    234       (n_files -= maxino - 9 - sblock.e2fs.e2fs_ficount))
    235     printf("%lld files missing\n", (long long)n_files);
    236   if (debug) {
    237     for (i = 0; i < sblock.e2fs_ncg; i++)
    238       n_blks += cgoverhead(i);
    239     n_blks += sblock.e2fs.e2fs_first_dblock;
    240     if (n_blks -= maxfsblock - n_bfree)
    241       printf("%lld blocks missing\n", (long long)n_blks);
    242     if (duplist != NULL) {
    243       printf("The following duplicate blocks remain:");
    244       for (dp = duplist; dp; dp = dp->next)
    245         printf(" %lld,", (long long)dp->dup);
    246       printf("\n");
    247     }
    248     if (zlnhead != NULL) {
    249       printf("The following zero link count inodes remain:");
    250       for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
    251         printf(" %llu,", (unsigned long long)zlnp->zlncnt);
    252       printf("\n");
    253     }
    254   }
    255   zlnhead = (struct zlncnt *)0;
    256   duplist = (struct dups *)0;
    257   muldup  = (struct dups *)0;
    258   inocleanup();
    259   if (fsmodified) {
    260     time_t t;
    261     (void)time(&t);
    262     sblock.e2fs.e2fs_wtime    = t;
    263     sblock.e2fs.e2fs_lastfsck = t;
    264     sbdirty();
    265   }
    266   ckfini(1);
    267   free(blockmap);
    268   free(statemap);
    269   free((char *)lncntp);
    270   if (!fsmodified)
    271     return unresolved ? FSCK_EXIT_UNRESOLVED : FSCK_EXIT_OK;
    272   if (!preen)
    273     printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
    274   if (rerun)
    275     printf("\n***** PLEASE RERUN FSCK *****\n");
    276   return unresolved ? FSCK_EXIT_UNRESOLVED : FSCK_EXIT_OK;
    277 }
    278 
    279 static void
    280 usage(void)
    281 {
    282   (void)fprintf(stderr, "usage: %s [-fn] filesystem\n", getprogname());
    283   exit(FSCK_EXIT_USAGE);
    284 }