aruu

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

utilities.c (11560B)


      1 /*	$NetBSD: utilities.c,v 1.23 2013/06/23 02:06:04 dholland 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 #if 0
     58 static char sccsid[] = "@(#)utilities.c	8.1 (Berkeley) 6/5/93";
     59 #else
     60 __RCSID("$NetBSD: utilities.c,v 1.23 2013/06/23 02:06:04 dholland Exp $");
     61 #endif
     62 #endif /* not lint */
     63 
     64 #include <sys/param.h>
     65 #include <sys/time.h>
     66 #include <ufs/ext2fs/ext2fs_dinode.h>
     67 #include <ufs/ext2fs/ext2fs_dir.h>
     68 #include <ufs/ext2fs/ext2fs.h>
     69 #include <stdio.h>
     70 #include <stdlib.h>
     71 #include <errno.h>
     72 #include <string.h>
     73 #include <ctype.h>
     74 #include <unistd.h>
     75 #include <signal.h>
     76 
     77 #include "fsutil.h"
     78 #include "fsck.h"
     79 #include "extern.h"
     80 #include "exitvalues.h"
     81 
     82 extern int unresolved;
     83 
     84 long diskreads, totalreads; /* Disk cache statistics */
     85 
     86 int
     87 ftypeok(struct ext2fs_dinode *dp)
     88 {
     89   switch (fs2h16(dp->e2di_mode) & IFMT) {
     90     case IFDIR:
     91     case IFREG:
     92     case IFBLK:
     93     case IFCHR:
     94     case IFLNK:
     95     case IFSOCK:
     96     case IFIFO:
     97       return (1);
     98 
     99     default:
    100       if (debug)
    101         printf("bad file type 0%o\n", fs2h16(dp->e2di_mode));
    102       return (0);
    103   }
    104 }
    105 
    106 int
    107 reply(const char *question)
    108 {
    109   int  persevere;
    110   char c;
    111 
    112   if (preen)
    113     pfatal("INTERNAL ERROR: GOT TO reply()");
    114   persevere = !strcmp(question, "CONTINUE");
    115   printf("\n");
    116   if (!persevere && (nflag || fswritefd < 0)) {
    117     unresolved = 1;
    118     printf("%s? no\n\n", question);
    119     return (0);
    120   }
    121   if (yflag || (persevere && nflag)) {
    122     printf("%s? yes\n\n", question);
    123     return (1);
    124   }
    125   do {
    126     printf("%s? [yn] ", question);
    127     (void)fflush(stdout);
    128     c = getc(stdin);
    129     while (c != '\n' && getc(stdin) != '\n')
    130       if (feof(stdin))
    131         return (0);
    132   } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
    133   printf("\n");
    134   if (c == 'y' || c == 'Y')
    135     return (1);
    136   return (0);
    137 }
    138 
    139 /*
    140  * Malloc buffers and set up cache.
    141  */
    142 void
    143 bufinit(void)
    144 {
    145   struct bufarea *bp;
    146   long            bufcnt, i;
    147   char           *bufp;
    148 
    149   diskreads = totalreads = 0;
    150   pbp = pdirbp   = (struct bufarea *)0;
    151   bufhead.b_next = bufhead.b_prev = &bufhead;
    152   bufcnt                          = MAXBUFSPACE / sblock.e2fs_bsize;
    153   if (bufcnt < MINBUFS)
    154     bufcnt = MINBUFS;
    155   for (i = 0; i < bufcnt; i++) {
    156     bp   = malloc(sizeof(struct bufarea));
    157     bufp = malloc((size_t)sblock.e2fs_bsize);
    158     if (bp == NULL || bufp == NULL) {
    159       free(bp);
    160       free(bufp);
    161       if (i >= MINBUFS)
    162         break;
    163       errexit("cannot allocate buffer pool");
    164     }
    165     bp->b_un.b_buf         = bufp;
    166     bp->b_prev             = &bufhead;
    167     bp->b_next             = bufhead.b_next;
    168     bufhead.b_next->b_prev = bp;
    169     bufhead.b_next         = bp;
    170     initbarea(bp);
    171   }
    172   bufhead.b_size = i; /* save number of buffers */
    173 }
    174 
    175 /*
    176  * Manage a cache of directory blocks.
    177  */
    178 struct bufarea *
    179 getdatablk(daddr_t blkno, long size)
    180 {
    181   struct bufarea *bp;
    182 
    183   for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
    184     if (bp->b_bno == EXT2_FSBTODB(&sblock, blkno))
    185       goto foundit;
    186   for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
    187     if ((bp->b_flags & B_INUSE) == 0)
    188       break;
    189   if (bp == &bufhead)
    190     errexit("deadlocked buffer pool");
    191   getblk(bp, blkno, size);
    192   diskreads++;
    193   /* fall through */
    194 foundit:
    195   totalreads++;
    196   bp->b_prev->b_next     = bp->b_next;
    197   bp->b_next->b_prev     = bp->b_prev;
    198   bp->b_prev             = &bufhead;
    199   bp->b_next             = bufhead.b_next;
    200   bufhead.b_next->b_prev = bp;
    201   bufhead.b_next         = bp;
    202   bp->b_flags |= B_INUSE;
    203   return (bp);
    204 }
    205 
    206 void
    207 getblk(struct bufarea *bp, daddr_t blk, long size)
    208 {
    209   daddr_t dblk;
    210 
    211   dblk = EXT2_FSBTODB(&sblock, blk);
    212   if (bp->b_bno != dblk) {
    213     flush(fswritefd, bp);
    214     bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
    215     bp->b_bno  = dblk;
    216     bp->b_size = size;
    217   }
    218 }
    219 
    220 void
    221 flush(int fd, struct bufarea *bp)
    222 {
    223   int i;
    224 
    225   if (!bp->b_dirty)
    226     return;
    227   if (bp->b_errs != 0)
    228     pfatal(
    229         "WRITING %sZERO'ED BLOCK %lld TO DISK\n",
    230         (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
    231         (long long)bp->b_bno
    232     );
    233   bp->b_dirty = 0;
    234   bp->b_errs  = 0;
    235   bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
    236   if (bp != &sblk)
    237     return;
    238   for (i = 0; i < sblock.e2fs_ngdb; i++) {
    239     bwrite(
    240         fswritefd,
    241         (char *)&sblock.e2fs_gd[i * sblock.e2fs_bsize / sizeof(struct ext2_gd)],
    242         EXT2_FSBTODB(&sblock, ((sblock.e2fs_bsize > 1024) ? 0 : 1) + i + 1),
    243         sblock.e2fs_bsize
    244     );
    245   }
    246 }
    247 
    248 void
    249 ckfini(int markclean)
    250 {
    251   struct bufarea *bp, *nbp;
    252   int             cnt = 0;
    253 
    254   if (fswritefd < 0) {
    255     (void)close(fsreadfd);
    256     return;
    257   }
    258   flush(fswritefd, &sblk);
    259   if (havesb && sblk.b_bno != SBOFF / dev_bsize && !preen && reply("UPDATE STANDARD SUPERBLOCKS")) {
    260     sblk.b_bno = SBOFF / dev_bsize;
    261     sbdirty();
    262     flush(fswritefd, &sblk);
    263     copyback_sb(&asblk);
    264     asblk.b_dirty = 1;
    265     flush(fswritefd, &asblk);
    266   }
    267   for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
    268     cnt++;
    269     flush(fswritefd, bp);
    270     nbp = bp->b_prev;
    271     free(bp->b_un.b_buf);
    272     free(bp);
    273   }
    274   if (bufhead.b_size != cnt)
    275     errexit("Panic: lost %d buffers", bufhead.b_size - cnt);
    276   pbp = pdirbp = (struct bufarea *)0;
    277   if (markclean && (sblock.e2fs.e2fs_state & E2FS_ISCLEAN) == 0) {
    278     /*
    279      * Mark the file system as clean, and sync the superblock.
    280      */
    281     if (preen)
    282       pwarn("MARKING FILE SYSTEM CLEAN\n");
    283     else if (!reply("MARK FILE SYSTEM CLEAN"))
    284       markclean = 0;
    285     if (markclean) {
    286       sblock.e2fs.e2fs_state = E2FS_ISCLEAN;
    287       sbdirty();
    288       flush(fswritefd, &sblk);
    289     }
    290   }
    291   if (debug)
    292     printf(
    293         "cache missed %ld of %ld (%d%%)\n",
    294         diskreads,
    295         totalreads,
    296         (int)(diskreads * 100 / totalreads)
    297     );
    298   (void)close(fsreadfd);
    299   (void)close(fswritefd);
    300 }
    301 
    302 int
    303 bread(int fd, char *buf, daddr_t blk, long size)
    304 {
    305   off_t offset;
    306 
    307   offset = blk;
    308   offset *= dev_bsize;
    309   if (blk < 0 || pread(fd, buf, size, offset) != size)
    310     errexit("cannot read block %lld", (long long)blk);
    311   return 0;
    312 }
    313 
    314 void
    315 bwrite(int fd, char *buf, daddr_t blk, long size)
    316 {
    317   (void)fd;
    318   (void)buf;
    319   (void)blk;
    320   (void)size;
    321   errexit("write requested in read-only checker");
    322 }
    323 
    324 /*
    325  * allocate a data block
    326  */
    327 int
    328 allocblk(void)
    329 {
    330   int i;
    331 
    332   for (i = 0; i < maxfsblock - 1; i++) {
    333     if (testbmap(i))
    334       continue;
    335     setbmap(i);
    336     n_blks++;
    337     return (i);
    338   }
    339   return (0);
    340 }
    341 
    342 /*
    343  * Free a previously allocated block
    344  */
    345 void
    346 freeblk(daddr_t blkno)
    347 {
    348   struct inodesc idesc;
    349 
    350   idesc.id_blkno    = blkno;
    351   idesc.id_numfrags = 1;
    352   (void)pass4check(&idesc);
    353 }
    354 
    355 /*
    356  * Find a pathname
    357  */
    358 void
    359 getpathname(char *namebuf, size_t namebuflen, ino_t curdir, ino_t ino)
    360 {
    361   int            len;
    362   char          *cp;
    363   struct inodesc idesc;
    364   static int     busy = 0;
    365 
    366   if (curdir == ino && ino == EXT2_ROOTINO) {
    367     (void)strlcpy(namebuf, "/", namebuflen);
    368     return;
    369   }
    370   if (busy || (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
    371     (void)strlcpy(namebuf, "?", namebuflen);
    372     return;
    373   }
    374   busy = 1;
    375   memset(&idesc, 0, sizeof(struct inodesc));
    376   idesc.id_type = DATA;
    377   idesc.id_fix  = IGNORE;
    378   cp            = &namebuf[MAXPATHLEN - 1];
    379   *cp           = '\0';
    380   if (curdir != ino) {
    381     idesc.id_parent = curdir;
    382     goto namelookup;
    383   }
    384   while (ino != EXT2_ROOTINO) {
    385     idesc.id_number = ino;
    386     idesc.id_func   = findino;
    387     idesc.id_name   = "..";
    388     if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
    389       break;
    390   namelookup:
    391     idesc.id_number = idesc.id_parent;
    392     idesc.id_parent = ino;
    393     idesc.id_func   = findname;
    394     idesc.id_name   = namebuf;
    395     if ((ckinode(ginode(idesc.id_number), &idesc) & FOUND) == 0)
    396       break;
    397     len = strlen(namebuf);
    398     cp -= len;
    399     memcpy(cp, namebuf, (size_t)len);
    400     *--cp = '/';
    401     if (cp < &namebuf[EXT2FS_MAXNAMLEN])
    402       break;
    403     ino = idesc.id_number;
    404   }
    405   busy = 0;
    406   if (ino != EXT2_ROOTINO)
    407     *--cp = '?';
    408   memcpy(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
    409 }
    410 
    411 /*
    412  * determine whether an inode should be fixed.
    413  */
    414 int
    415 dofix(struct inodesc *idesc, const char *msg)
    416 {
    417   switch (idesc->id_fix) {
    418     case DONTKNOW:
    419       if (idesc->id_type == DATA)
    420         direrror(idesc->id_number, msg);
    421       else
    422         pwarn("%s", msg);
    423       if (preen) {
    424         printf(" (SALVAGED)\n");
    425         idesc->id_fix = FIX;
    426         return (ALTERED);
    427       }
    428       if (reply("SALVAGE") == 0) {
    429         idesc->id_fix = NOFIX;
    430         return (0);
    431       }
    432       idesc->id_fix = FIX;
    433       return (ALTERED);
    434 
    435     case FIX:
    436       return (ALTERED);
    437 
    438     case NOFIX:
    439     case IGNORE:
    440       return (0);
    441 
    442     default:
    443       errexit("UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
    444   }
    445   /* NOTREACHED */
    446 }