Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/libexec/lfs_cleanerd clean this up a bit. avoid annoying cod...



details:   https://anonhg.NetBSD.org/src/rev/d966abac70cb
branches:  trunk
changeset: 539924:d966abac70cb
user:      christos <christos%NetBSD.org@localhost>
date:      Fri Nov 29 17:15:46 2002 +0000

description:
clean this up a bit. avoid annoying code duplication on opening files,
and make error messages consistent.

diffstat:

 libexec/lfs_cleanerd/library.c |  224 ++++++++++++++++++----------------------
 1 files changed, 102 insertions(+), 122 deletions(-)

diffs (truncated from 485 to 300 lines):

diff -r 54ae0848b74d -r d966abac70cb libexec/lfs_cleanerd/library.c
--- a/libexec/lfs_cleanerd/library.c    Fri Nov 29 17:11:21 2002 +0000
+++ b/libexec/lfs_cleanerd/library.c    Fri Nov 29 17:15:46 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: library.c,v 1.29 2002/06/14 05:21:21 perseant Exp $    */
+/*     $NetBSD: library.c,v 1.30 2002/11/29 17:15:46 christos Exp $    */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -38,7 +38,7 @@
 #if 0
 static char sccsid[] = "@(#)library.c  8.3 (Berkeley) 5/24/95";
 #else
-__RCSID("$NetBSD: library.c,v 1.29 2002/06/14 05:21:21 perseant Exp $");
+__RCSID("$NetBSD: library.c,v 1.30 2002/11/29 17:15:46 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -63,9 +63,9 @@
 #include "clean.h"
 
 void    add_blocks(FS_INFO *, BLOCK_INFO_15 *, int *, SEGSUM *, caddr_t,
-            daddr_t, daddr_t);
+    daddr_t, daddr_t);
 void    add_inodes(FS_INFO *, BLOCK_INFO_15 *, int *, SEGSUM *, caddr_t,
-            daddr_t);
+    daddr_t);
 int     bi_compare(const void *, const void *);
 int     bi_toss(const void *, const void *, const void *);
 void    get_ifile(FS_INFO *, int);
@@ -76,8 +76,8 @@
 extern int debug;
 extern u_long cksum(void *, size_t);   /* XXX */
 
-static int ifile_fd;
-static int dev_fd;
+static int ifile_fd = -1;
+static int dev_fd = -1;
 
 /*
  * This function will get information on a a filesystem which matches
@@ -89,30 +89,25 @@
 fs_getmntinfo(struct statfs **buf, char *name, const char *type)
 {
        /* allocate space for the filesystem info */
-       *buf = (struct statfs *)malloc(sizeof(struct statfs));
-       if (*buf == NULL)
+       if ((*buf = malloc(sizeof(struct statfs))) == NULL)
                return 0;
 
        /* grab the filesystem info */
-        if (ifile_fd <= 0) {
-               if (statfs(name, *buf) < 0) {
-                       free(*buf);
-                       return 0;
-               }
-        } else if(fstatfs(ifile_fd, *buf) < 0) {
-               free(*buf);
-               return 0;
-        }
+        if (ifile_fd == -1) {
+               if (statfs(name, *buf) == -1)
+                       goto bad;
+        } else if(fstatfs(ifile_fd, *buf) == -1)
+               goto bad;
 
        /* check to see if it's the one we want */
        if (strncmp(type, (*buf)->f_fstypename, MFSNAMELEN) ||
-           strncmp(name, (*buf)->f_mntonname, MNAMELEN)) {
-               /* "this is not the filesystem you're looking for" */
-               free(*buf);
-               return 0;
-       }
+           strncmp(name, (*buf)->f_mntonname, MNAMELEN))
+               goto bad;
 
        return 1;
+bad:
+       free(*buf);
+       return 0;
 }
 
 /*
@@ -120,21 +115,20 @@
  * Returns an pointer to an FS_INFO structure, NULL on error.
  */
 FS_INFO *
-get_fs_info (struct statfs *lstatfsp, int use_mmap)
+get_fs_info(struct statfs *lstatfsp, int use_mmap)
 {
        FS_INFO *fsp;
 
-       fsp = (FS_INFO *)malloc(sizeof(FS_INFO));
-       if (fsp == NULL)
+       if ((fsp = malloc(sizeof(FS_INFO))) == NULL)
                return NULL;
        memset(fsp, 0, sizeof(FS_INFO));
 
        fsp->fi_statfsp = lstatfsp;
-       if (get_superblock (fsp, &fsp->fi_lfs)) {
-               syslog(LOG_ERR, "Exiting: get_fs_info: get_superblock failed: %m");
+       if (get_superblock(fsp, &fsp->fi_lfs)) {
+               syslog(LOG_ERR, "get_fs_info: get_superblock failed (%m)");
                 exit(1);
         }
-       get_ifile (fsp, use_mmap);
+       get_ifile(fsp, use_mmap);
        return (fsp);
 }
 
@@ -146,48 +140,56 @@
 void
 reread_fs_info(FS_INFO *fsp, int use_mmap)
 {
-       if (ifile_fd > 0) {
-               if (fstatfs(ifile_fd, fsp->fi_statfsp)) {
-                       syslog(LOG_ERR, "Exiting: reread_fs_info: fstatfs failed: %m");
+       if (ifile_fd != -1) {
+               if (fstatfs(ifile_fd, fsp->fi_statfsp) == -1) {
+                       syslog(LOG_ERR, "reread_fs_info: fstatfs failed (%m)");
                        exit(1);
                }
        } else if (statfs(fsp->fi_statfsp->f_mntonname, fsp->fi_statfsp)) {
-               syslog(LOG_ERR, "Exiting: reread_fs_info: statfs failed: %m");
+               syslog(LOG_ERR, "reread_fs_info: statfs `%s' failed (%m)",
+                   fsp->fi_statfsp->f_mntonname);
                 exit(1);
         }
-       get_ifile (fsp, use_mmap);
+       get_ifile(fsp, use_mmap);
 }
 
+static int
+getdevfd(FS_INFO *fsp)
+{
+       char rdev[MNAMELEN];
+
+       if (dev_fd != -1)
+               return dev_fd;
+
+       (void)snprintf(rdev, sizeof(rdev), "/dev/r/%s",
+           fsp->fi_statfsp->f_mntfromname + 5);
+       if ((dev_fd = open(rdev, O_RDONLY)) == -1) {
+               syslog(LOG_ERR, "Cannot open `%s' (%m)", rdev);
+               exit(1);
+       }
+       return dev_fd;
+}
+
+
 /*
  * Read a block from disk.
  */
 int
 get_rawblock(FS_INFO *fsp, char *buf, size_t size, ufs_daddr_t daddr)
 {
-       char rdev[MNAMELEN];
-
-       if (dev_fd <= 0) {
-               strcpy(rdev, "/dev/r"); /* XXX */
-               strcat(rdev + 6, fsp->fi_statfsp->f_mntfromname + 5);
-               dev_fd = open(rdev, O_RDONLY);
-       }
-       if (dev_fd < 0) {
-               syslog(LOG_ERR, "%s", rdev);
-               exit(1);
-       }
-       return pread(dev_fd, buf, size, fsbtob(&fsp->fi_lfs, (off_t)daddr));
+       return pread(getdevfd(fsp), buf, size, fsbtob(&fsp->fi_lfs,
+           (off_t)daddr));
 }
 
 /*
  * Read an inode from disk.
  */
 struct dinode *
-get_dinode (FS_INFO *fsp, ino_t ino)
+get_dinode(FS_INFO *fsp, ino_t ino)
 {
         static struct dinode dino;
         struct dinode *dip, *dib;
         struct lfs *lfsp;
-       char rdev[MNAMELEN];
        BLOCK_INFO_15 bi;
 
         lfsp = &fsp->fi_lfs;
@@ -206,23 +208,20 @@
        if (bi.bi_daddr <= 0)
                return NULL;
 
-       if (dev_fd <= 0) {
-               strcpy(rdev, "/dev/r"); /* XXX */
-               strcat(rdev + 6, fsp->fi_statfsp->f_mntfromname + 5);
-               dev_fd = open(rdev, O_RDONLY);
+       lseek(getdevfd(fsp), (off_t)0, SEEK_SET);
+       if ((dib = malloc(lfsp->lfs_ibsize)) == NULL) {
+               syslog(LOG_WARNING, "lfs_bmapv: %m");
+               return NULL;
        }
-       if (dev_fd < 0) {
-               syslog(LOG_ERR, "%s", rdev);
-               exit(1);
-       }
-       dib = (struct dinode *)malloc(lfsp->lfs_ibsize);
+
        pread(dev_fd, dib, lfsp->lfs_ibsize, fsbtob(lfsp, (off_t)bi.bi_daddr));
        for (dip = dib; dip != dib + lfsp->lfs_inopb; ++dip)
                if (dip->di_u.inumber == ino)
                        break;
        if (dip == dib + lfsp->lfs_inopb) {
                free(dib);
-               syslog(LOG_WARNING, "dinode %d not found at fsb 0x%x", ino, bi.bi_daddr);
+               syslog(LOG_WARNING, "dinode %d not found at fsb 0x%x",
+                   ino, bi.bi_daddr);
                return NULL;
        }
         dino = *dip; /* structure copy */
@@ -235,34 +234,22 @@
  * Gets the superblock from disk (possibly in face of errors)
  */
 int
-get_superblock (FS_INFO *fsp, struct lfs *sbp)
+get_superblock(FS_INFO *fsp, struct lfs *sbp)
 {
-       char mntfromname[MNAMELEN+1];
        char buf[LFS_SBPAD];
        static off_t sboff = LFS_LABELPAD;
 
-       strcpy(mntfromname, "/dev/r");
-       strcat(mntfromname, fsp->fi_statfsp->f_mntfromname+5);
-
-       if(dev_fd <= 0) {
-               if ((dev_fd = open(mntfromname, O_RDONLY, (mode_t)0)) < 0) {
-                       syslog(LOG_WARNING,"get_superblock: bad open: %m");
-                       return (-1);
-               }
-       } else
-               lseek(dev_fd, 0, SEEK_SET);
+       lseek(getdevfd(fsp), 0, SEEK_SET);
                
-       do {
+       for (;;) {
                get(dev_fd, sboff, buf, LFS_SBPAD);
                memcpy(&(sbp->lfs_dlfs), buf, sizeof(struct dlfs));
                if (sboff == LFS_LABELPAD && fsbtob(sbp, 1) > LFS_LABELPAD)
                        sboff = fsbtob(sbp, (off_t)sbp->lfs_sboffs[0]);
                else
                        break;
-       } while (1);
+       }
        
-       /* close (fid); */
-
        /* Compatibility */
        if (sbp->lfs_version < 2) {
                sbp->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
@@ -280,7 +267,7 @@
  * fatal error on failure.
  */
 void
-get_ifile (FS_INFO *fsp, int use_mmap)
+get_ifile(FS_INFO *fsp, int use_mmap)
 {
        struct stat file_stat;
        struct statfs statfsbuf;
@@ -294,24 +281,27 @@
        strcat(strcat(strcpy(ifile_name, fsp->fi_statfsp->f_mntonname), "/"),
            IFILE_NAME);
 
-       if(ifile_fd <= 0) {
+       if(ifile_fd == -1) {
                /* XXX KS - Do we ever *write* to the ifile? */
-               if ((ifile_fd = open(ifile_name, O_RDONLY, (mode_t)0)) < 0) {
-                       syslog(LOG_ERR, "Exiting: get_ifile: bad open: %m");
+               if ((ifile_fd = open(ifile_name, O_RDONLY)) == -1) {
+                       syslog(LOG_ERR, "get_ifile: cannot open `%s': %m",
+                           ifile_name);
                        exit(1);
                }
        } else
-               lseek(ifile_fd, 0, SEEK_SET);
+               lseek(ifile_fd, (off_t)0, SEEK_SET);
 
-       if (fstat (ifile_fd, &file_stat)) {
+       if (fstat(ifile_fd, &file_stat) == -1) {
                /* If the fs was unmounted, don't complain */
-               statfs(fsp->fi_statfsp->f_mntonname, &statfsbuf);
-               if(memcmp(&statfsbuf.f_fsid,&fsp->fi_statfsp->f_fsid,
-                         sizeof(statfsbuf.f_fsid))!=0)
-               {
-                       /* Filesystem still mounted, this error is real */
-                       syslog(LOG_ERR, "Exiting: get_ifile: fstat failed: %m");
-                       exit(1);
+               if (statfs(fsp->fi_statfsp->f_mntonname, &statfsbuf) != -1) {
+                       if(memcmp(&statfsbuf.f_fsid, &fsp->fi_statfsp->f_fsid,
+                           sizeof(statfsbuf.f_fsid)) != 0) {
+                               /* Filesystem still mounted, 
+                                * this error is real
+                                */
+                               syslog(LOG_ERR, "get_ifile: fstat failed: %m");



Home | Main Index | Thread Index | Old Index