Finish removing sunos 4.x references and build cruft
[openafs.git] / src / vol / listinodes.c
index fd608aa..0bf3428 100644 (file)
 #include <afsconfig.h>
 #include <afs/param.h>
 
-#include <string.h>
+#include <roken.h>
 
+#include <ctype.h>
 
 #ifndef AFS_NAMEI_ENV
-#if defined(AFS_LINUX20_ENV) || defined(AFS_SUN4_ENV)
+#if defined(AFS_LINUX20_ENV)
 /* ListViceInodes
  *
  * Return codes:
@@ -33,7 +34,7 @@
  * -2 - Unable to completely write temp file. Produces warning message in log.
  */
 int
-ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
+ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
               afs_uint32 (*judgeInode) (), afs_uint32 judgeParam, int *forcep, int forceR,
               char *wpath, void *rock)
 {
@@ -41,10 +42,7 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
     return -1;
 }
 #else
-#include <ctype.h>
-#include <sys/param.h>
-#if defined(AFS_SGI_ENV)
-#else
+#if !defined(AFS_SGI_ENV)
 #ifdef AFS_OSF_ENV
 #include <ufs/fs.h>
 #else /* AFS_OSF_ENV */
@@ -69,7 +67,6 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
 #endif
 #endif /* AFS_VFSINCL_ENV */
 #endif /* AFS_OSF_ENV */
-#include <sys/time.h>
 #ifdef AFS_VFSINCL_ENV
 #include <sys/vnode.h>
 #ifdef   AFS_SUN5_ENV
@@ -89,20 +86,15 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
 #endif /* AFS_SGI_ENV */
 #include <afs/osi_inode.h>
 #include <sys/file.h>
-#include <stdio.h>
 #include <rx/xdr.h>
 #include <afs/afsint.h>
 #include "nfs.h"
 #include <afs/afssyscalls.h>
 #include "viceinode.h"
-#include <sys/stat.h>
 #if defined (AFS_AIX_ENV) || defined (AFS_HPUX_ENV)
 #include <sys/ino.h>
 #endif
-#include <afs/afs_assert.h>
-#if defined(AFS_HPUX101_ENV)
-#include <unistd.h>
-#endif
+
 #include "lock.h"
 #include "ihandle.h"
 #include "vnode.h"
@@ -112,34 +104,12 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
 #include "fssync.h"
 #include "volume_inline.h"
 
-/*@+fcnmacros +macrofcndecl@*/
-#ifdef O_LARGEFILE
-#ifdef S_SPLINT_S
-extern off64_t afs_lseek(int FD, off64_t O, int F);
-#endif /*S_SPLINT_S */
-#define afs_lseek(FD, O, F)   lseek64(FD, (off64_t) (O), F)
-#define afs_stat      stat64
-#define afs_fstat     fstat64
-#define afs_open      open64
-#define afs_fopen     fopen64
-#else /* !O_LARGEFILE */
-#ifdef S_SPLINT_S
-extern off_t afs_lseek(int FD, off_t O, int F);
-#endif /*S_SPLINT_S */
-#define afs_lseek(FD, O, F)   lseek(FD, (off_t) (O), F)
-#define afs_stat      stat
-#define afs_fstat     fstat
-#define afs_open      open
-#define afs_fopen     fopen
-#endif /* !O_LARGEFILE */
-/*@=fcnmacros =macrofcndecl@*/
-
 /* Notice:  parts of this module have been cribbed from vfsck.c */
 
 #define        ROOTINODE       2
 static char *partition;
 int Testing=0;
-int pfd;
+FD_t pfd;
 
 #ifdef AFS_AIX32_ENV
 #include <jfs/filsys.h>
@@ -187,7 +157,7 @@ struct dinode *ginode();
 
 
 int
-ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
+ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
               int (*judgeInode) (), afs_uint32 judgeParam, int *forcep, int forceR,
               char *wpath, void *rock)
 {
@@ -198,7 +168,7 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
     struct stat root_inode;
     int ninodes = 0, err = 0;
 
-    pfd = -1;                  /* initialize so we don't close on error output below. */
+    pfd = INVALID_FD;                  /* initialize so we don't close on error output below. */
     *forcep = 0;
     sync();
     sleep(1);                  /* simulate operator    */
@@ -272,8 +242,8 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
 
     fmax = fs.s_fsize / (FSBSIZE / 512);       /* first invalid blk num */
 
-    pfd = afs_open(rdev, O_RDONLY);
-    if (pfd < 0) {
+    pfd = OS_OPEN(rdev, O_RDONLY, 0666);
+    if (pfd == INVALID_FD) {
        Log("Unable to open `%s' inode for reading\n", rdev);
        return -1;
     }
@@ -319,8 +289,8 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
        if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
            continue;
 
-       if (inodeFile) {
-           if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
+       if (inodeFile != INVALID_FD) {
+           if (OS_WRITE(inodeFile, &info, sizeof(info)) != sizeof(info)) {
                Log("Error writing inode file for partition %s\n", partition);
                goto out;
            }
@@ -328,13 +298,8 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
        ++ninodes;
     }
 
-    if (inodeFile) {
-       if (fflush(inodeFile) == EOF) {
-           Log("Unable to successfully flush inode file for %s\n", partition);
-           err = -2;
-           goto out1;
-       }
-       if (fsync(fileno(inodeFile)) == -1) {
+    if (inodeFile != INVALID_FD) {
+       if (OS_SYNC(inodeFile) == -1) {
            Log("Unable to successfully fsync inode file for %s\n", partition);
            err = -2;
            goto out1;
@@ -343,12 +308,7 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
        /*
         * Paranoia:  check that the file is really the right size
         */
-       if (fstat(fileno(inodeFile), &status) == -1) {
-           Log("Unable to successfully stat inode file for %s\n", partition);
-           err = -2;
-           goto out1;
-       }
-       if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
+       if (OS_SIZE(inodeFile) != ninodes * sizeof(struct ViceInodeInfo)) {
            Log("Wrong size (%d instead of %d) in inode file for %s\n",
                status.st_size, ninodes * sizeof(struct ViceInodeInfo),
                partition);
@@ -356,14 +316,14 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
            goto out1;
        }
     }
-    close(pfd);
+    OS_CLOSE(pfd);
     return 0;
 
   out:
     err = -1;
   out1:
-    if (pfd >= 0)
-       close(pfd);
+    if (pfd != INVALID_FD)
+       OS_CLOSE(pfd);
 
     return err;
 }
@@ -372,10 +332,10 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
 int
 ReadSuper(struct superblock *fs, char *devName)
 {
-    int pfd;
+    FD_t pfd;
 
-    pfd = afs_open(devName, O_RDONLY);
-    if (pfd < 0) {
+    pfd = OS_OPEN(devName, O_RDONLY, 0666);
+    if (pfd == INVALID_FD) {
        Log("Unable to open inode on %s for reading superblock.\n", devName);
        return -1;
     }
@@ -384,7 +344,7 @@ ReadSuper(struct superblock *fs, char *devName)
        Log("Unable to read superblock on %s.\n", devName);
        return -1;
     }
-    close(pfd);
+    OS_CLOSE(pfd);
     return (0);
 }
 
@@ -639,7 +599,7 @@ xfs_RenameFiles(char *dir, xfs_Rename_t * renames, int n_renames)
 
 
 int
-xfs_ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
+xfs_ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
                   int (*judgeInode) (), afs_uint32 judgeParam, int *forcep,
                   int forceR, char *wpath, void *rock)
 {
@@ -664,7 +624,6 @@ xfs_ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
     int n_renames = 0;
     int n_avail = 0;
     uint64_t pino;
-    struct stat status;
     int errors = 0;
 
     *forcep = 0;
@@ -775,10 +734,10 @@ xfs_ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
                n_renames++;
            }
 
-           if (inodeFile) {
-               if (fwrite
-                   (&info.ili_info, sizeof(vice_inode_info_t), 1, inodeFile)
-                   != 1) {
+           if (inodeFile != INVALID_FD) {
+               if (OS_WRITE
+                   (inodeFile, &info.ili_info, sizeof(vice_inode_info_t))
+                   != sizeof(vice_inode_info_t)) {
                    Log("Error writing inode file for partition %s\n", mountedOn);
                    goto err1_exit;
                }
@@ -800,25 +759,17 @@ xfs_ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
     closedir(top_dirp);
     if (renames)
        free((char *)renames);
-    if (inodeFile) {
-       if (fflush(inodeFile) == EOF) {
-           ("Unable to successfully flush inode file for %s\n", mountedOn);
-           return errors ? -1 : -2;
-       }
-       if (fsync(fileno(inodeFile)) == -1) {
+    if (inodeFile != INVALID_FD) {
+       if (OS_SYNC(inodeFile) == -1) {
            Log("Unable to successfully fsync inode file for %s\n", mountedOn);
            return errors ? -1 : -2;
        }
        /*
         * Paranoia:  check that the file is really the right size
         */
-       if (fstat(fileno(inodeFile), &status) == -1) {
-           Log("Unable to successfully stat inode file for %s\n", partition);
-           return errors ? -1 : -2;
-       }
-       if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
+       if (OS_SIZE(inodeFile) != ninodes * sizeof(struct ViceInodeInfo)) {
            Log("Wrong size (%d instead of %d) in inode file for %s\n",
-               status.st_size, ninodes * sizeof(struct ViceInodeInfo),
+               OS_SIZE(inodeFile), ninodes * sizeof(struct ViceInodeInfo),
                partition);
            return errors ? -1 : -2;
        }
@@ -844,7 +795,7 @@ xfs_ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
 #endif
 
 int
-ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
+ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
               int (*judgeInode) (), afs_uint32 judgeParam, int *forcep, int forceR,
               char *wpath, void *rock)
 {
@@ -909,7 +860,7 @@ BUFAREA sblk;
 
 extern char *afs_rawname();
 int
-ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
+ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
               int (*judgeInode) (), afs_uint32 judgeParam, int *forcep, int forceR,
               char *wpath, void *rock)
 {
@@ -925,7 +876,6 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
     int i, c, e, bufsize, code, err = 0;
     char dev[50], rdev[100], err1[512], *ptr1;
     struct dinode *inodes = NULL, *einodes, *dptr;
-    struct stat status;
     int ninodes = 0;
     struct dinode *p;
     struct ViceInodeInfo info;
@@ -944,8 +894,8 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
     sleep(10);
 #endif
 
-    pfd = afs_open(rdev, O_RDONLY);
-    if (pfd <= 0) {
+    pfd = OS_OPEN(rdev, O_RDONLY, 0666);
+    if (pfd == INVALID_FD) {
        sprintf(err1, "Could not open device %s to get inode list\n", rdev);
        perror(err1);
        return -1;
@@ -1005,8 +955,8 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
        info.u.param[3] = auxp->aux_param4;
        if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
            continue;
-       if (inodeFile) {
-           if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
+       if (inodeFile != INVALID_FD) {
+           if (OS_WRITE(inodeFile, &info, sizeof(info)) != sizeof(info)) {
                Log("Error writing inode file for partition %s\n", partition);
                goto out;
            }
@@ -1082,10 +1032,10 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
        i = c * sblock.fs_ipg;
        e = i + sblock.fs_ipg;
 #if    defined(AFS_HPUX102_ENV)
-       if (afs_lseek(pfd, dbtoo(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
+       if (OS_SEEK(pfd, dbtoo(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
            -1) {
 #else
-       if (afs_lseek(pfd, dbtob(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
+       if (OS_SEEK(pfd, dbtob(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
            -1) {
 #endif
 #else
@@ -1103,14 +1053,14 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
        e = i + super.fs.fs_ipg;
 #ifdef AFS_OSF_ENV
        dblk1 = fsbtodb(&super.fs, itod(&super.fs, i));
-       if (afs_lseek(pfd, (off_t) ((off_t) dblk1 * DEV_BSIZE), L_SET) == -1) {
+       if (OS_SEEK(pfd, (off_t) ((off_t) dblk1 * DEV_BSIZE), L_SET) == -1) {
 #else
 #if defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
        f1 = fsbtodb(&super.fs, itod(&super.fs, i));
        off = (offset_t) f1 << DEV_BSHIFT;
-       if (llseek(pfd, off, L_SET) == -1) {
+       if (OS_SEEK(pfd, off, L_SET) == -1) {
 #else
-       if (afs_lseek(pfd, dbtob(fsbtodb(&super.fs, itod(&super.fs, i))), L_SET)
+       if (OS_SEEK(pfd, dbtob(fsbtodb(&super.fs, itod(&super.fs, i))), L_SET)
            == -1) {
 #endif /* AFS_SUN5_ENV */
 #endif /* AFS_OSF_ENV */
@@ -1121,7 +1071,7 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
        }
        while (i < e) {
            if (!forceR) {
-               if (read(pfd, inodes, bufsize) != bufsize) {
+               if (OS_READ(pfd, inodes, bufsize) != bufsize) {
                    Log("Error reading inodes for partition %s; run vfsck\n",
                        partition);
                    goto out;
@@ -1130,10 +1080,10 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
                int bj, bk;
                dptr = inodes;
                for (bj = bk = 0; bj < bufsize; bj = bj + 512, bk++) {
-                   if ((code = read(pfd, dptr, 512)) != 512) {
+                   if ((code = OS_READ(pfd, dptr, 512)) != 512) {
                        Log("Error reading inode %d? for partition %s (errno = %d); run vfsck\n", bk + i, partition, errno);
-                       if (afs_lseek(pfd, 512, L_SET) == -1) {
-                           Log("Lseek failed\n");
+                       if (OS_SEEK(pfd, 512, L_SET) == -1) {
+                           Log("OS_SEEK failed\n");
                            goto out;
                        }
                        dptr->di_mode = 0;
@@ -1170,7 +1120,7 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
                osi_Panic("Tru64 needs AFS_3DISPARES\n");
 #endif
 #endif
-#if    defined(AFS_SUN56_ENV)
+#if    defined(AFS_SUN5_ENV)
                /* if this is a pre-sol2.6 unconverted inode, bail out */
                {
                    afs_uint32 p1, p2, p3, p4;
@@ -1216,8 +1166,8 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
                    info.linkCount = p->di_nlink;
                    if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
                        continue;
-                   if (inodeFile) {
-                       if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
+                   if (inodeFile != INVALID_FD) {
+                       if (OS_WRITE(inodeFile, &info, sizeof(info)) != sizeof(info)) {
                            Log("Error writing inode file for partition %s\n",
                                partition);
                            goto out;
@@ -1231,13 +1181,8 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
     if (inodes)
        free(inodes);
 #endif
-    if (inodeFile) {
-       if (fflush(inodeFile) == EOF) {
-           Log("Unable to successfully flush inode file for %s\n", partition);
-           err = -2;
-           goto out1;
-       }
-       if (fsync(fileno(inodeFile)) == -1) {
+    if (inodeFile != INVALID_FD) {
+       if (OS_SYNC(inodeFile) == -1) {
            Log("Unable to successfully fsync inode file for %s\n", partition);
            err = -2;
            goto out1;
@@ -1246,26 +1191,21 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
        /*
         * Paranoia:  check that the file is really the right size
         */
-       if (fstat(fileno(inodeFile), &status) == -1) {
-           Log("Unable to successfully stat inode file for %s\n", partition);
-           err = -2;
-           goto out1;
-       }
-       if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
+       if (OS_SIZE(inodeFile) != ninodes * sizeof(struct ViceInodeInfo)) {
            Log("Wrong size (%d instead of %d) in inode file for %s\n",
-               status.st_size, ninodes * sizeof(struct ViceInodeInfo),
+               OS_SIZE(inodeFile), ninodes * sizeof(struct ViceInodeInfo),
                partition);
            err = -2;
            goto out1;
        }
     }
-    close(pfd);
+    OS_CLOSE(pfd);
     return 0;
 
   out:
     err = -1;
   out1:
-    close(pfd);
+    OS_CLOSE(pfd);
     if (inodes)
        free(inodes);
     return err;
@@ -1279,26 +1219,26 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
 #endif
 
 int
-bread(int fd, char *buf, daddr_t blk, afs_int32 size)
+bread(FD_t fd, char *buf, daddr_t blk, afs_int32 size)
 {
 #ifdef AFS_AIX_ENV
 #ifdef  AFS_AIX41_ENV
     offset_t off = (offset_t) blk << FSBSHIFT;
-    if (llseek(fd, off, 0) < 0) {
+    if (OS_SEEK(fd, off, 0) < 0) {
        Log("Unable to seek to offset %llu for block %u\n", off, blk);
        return -1;
     }
 #else /* AFS_AIX41_ENV */
-    if (afs_lseek(fd, blk * Bsize, 0) < 0) {
+    if (OS_SEEK(fd, blk * Bsize, 0) < 0) {
        Log("Unable to seek to offset %u for block %u\n", blk * Bsize, blk);
     }
 #endif /* AFS_AIX41_ENV */
 #else
-    if (afs_lseek(fd, (off_t) dbtob(blk), L_SET) < 0) {
+    if (OS_SEEK(fd, (off_t) dbtob(blk), L_SET) < 0) {
        Log("Unable to seek to offset %u for block %u\n", dbtob(blk), blk);
     }
 #endif
-    if (read(fd, buf, size) != size) {
+    if (OS_READ(fd, buf, size) != size) {
        Log("Unable to read block %d, partition %s\n", blk, partition);
        return -1;
     }