salvager: Fix conversion from stdio calls
[openafs.git] / src / vol / namei_ops.c
index 41c07c7..5601980 100644 (file)
@@ -26,6 +26,8 @@
 #include <winnt.h>
 #include <winbase.h>
 #include <winsock2.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 #endif
 #include <errno.h>
 #include <fcntl.h>
 #include <afs/errmap_nt.h>
 #endif
 
-/*@+fcnmacros +macrofcndecl@*/
-#ifdef O_LARGEFILE
-#ifdef S_SPLINT_S
-#endif /*S_SPLINT_S */
-#define afs_stat               stat64
-#define afs_fstat              fstat64
-#ifdef AFS_NT40_ENV
-#define afs_open                nt_open
-#else
-#define afs_open               open64
-#endif
-#define afs_fopen              fopen64
-#else /* !O_LARGEFILE */
-#ifdef S_SPLINT_S
-#endif /*S_SPLINT_S */
-#define afs_stat               stat
-#define afs_fstat              fstat
-#ifdef AFS_NT40_ENV
-#define afs_open                nt_open
-#else
-#define afs_open               open
-#endif
-#define afs_fopen              fopen
-#endif /* !O_LARGEFILE */
-/*@=fcnmacros =macrofcndecl@*/
-
 #ifndef LOCK_SH
 #define   LOCK_SH   1    /* shared lock */
 #define   LOCK_EX   2    /* exclusive lock */
@@ -383,10 +359,10 @@ namei_ViceREADME(char *partition)
 
     (void)afs_snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s" OS_DIRSEP "README",
                        partition, INODEDIR);
-    fd = afs_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
-    if (fd >= 0) {
-       (void)write(fd, VICE_README, strlen(VICE_README));
-       close(fd);
+    fd = OS_OPEN(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
+    if (fd != INVALID_FD) {
+       (void)OS_WRITE(fd, VICE_README, strlen(VICE_README));
+       OS_CLOSE(fd);
     }
     return (errno);
 }
@@ -717,7 +693,7 @@ SetOGM(FD_t fd, int parm, int tag)
 
 /* GetOGM - get parm and tag from owner, group and mode bits. */
 static void
-GetOGMFromStat(struct afs_stat *status, int *parm, int *tag)
+GetOGMFromStat(struct afs_stat_st *status, int *parm, int *tag)
 {
     *parm = status->st_uid | (status->st_gid << 15);
     *parm |= (status->st_mode & 0x18) << 27;
@@ -727,7 +703,7 @@ GetOGMFromStat(struct afs_stat *status, int *parm, int *tag)
 static int
 CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
 {
-    struct afs_stat status;
+    struct afs_stat_st status;
     int parm, tag;
     if (afs_fstat(fdP->fd_fd, &status) < 0)
        return -1;
@@ -808,11 +784,11 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
     p++;
     for (tag = 0; tag < NAMEI_MAXVOLS; tag++) {
         *p = *int_to_base32(str1, tag);
-        fd = afs_open((char *)&name.n_path, O_CREAT | O_RDWR | O_TRUNC | O_EXCL, 0666);
+        fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_TRUNC | O_EXCL, 0666);
         if (fd == INVALID_FD) {
             if (errno == ENOTDIR || errno == ENOENT) {
                 if (namei_CreateDataDirectories(&name, &created_dir) == 0)
-                    fd = afs_open((char *)&name.n_path, O_CREAT | O_RDWR | O_TRUNC | O_EXCL, 0666);
+                    fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_TRUNC | O_EXCL, 0666);
             }
         }
 
@@ -861,7 +837,7 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
 
 bad:
     if (fd != INVALID_FD)
-        nt_close(fd);
+        OS_CLOSE(fd);
 
     if (code || (fd == INVALID_FD)) {
        if (p2 != INODESPECIAL) {
@@ -944,21 +920,21 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
     }
 
     namei_HandleToName(&name, &tmp);
-    fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
-    if (fd < 0) {
+    fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
+    if (fd == INVALID_FD) {
        if (errno == ENOTDIR || errno == ENOENT) {
            if (namei_CreateDataDirectories(&name, &created_dir) < 0)
                goto bad;
-           fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR,
+           fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR,
                          0);
-           if (fd < 0)
+           if (fd == INVALID_FD)
                goto bad;
        } else {
            goto bad;
        }
     }
     if (SetOGM(fd, ogm_parm, tag) < 0) {
-       close(fd);
+       OS_CLOSE(fd);
        fd = INVALID_FD;
        goto bad;
     }
@@ -972,11 +948,11 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
     }
 
   bad:
-    if (fd >= 0)
-       close(fd);
+    if (fd != INVALID_FD)
+       OS_CLOSE(fd);
 
 
-    if (code || (fd < 0)) {
+    if (code || (fd == INVALID_FD)) {
        if (p2 != -1) {
            fdP = IH_OPEN(lh);
            if (fdP) {
@@ -985,7 +961,7 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
            }
        }
     }
-    return (code || (fd < 0)) ? (Inode) - 1 : tmp.ih_ino;
+    return (code || (fd == INVALID_FD)) ? (Inode) - 1 : tmp.ih_ino;
 }
 #endif
 
@@ -998,7 +974,7 @@ namei_iopen(IHandle_t * h)
 
     /* Convert handle to file name. */
     namei_HandleToName(&name, h);
-    fd = afs_open((char *)&name.n_path, O_RDWR, 0666);
+    fd = OS_OPEN((char *)&name.n_path, O_RDWR, 0666);
     return fd;
 }
 
@@ -1176,10 +1152,11 @@ namei_replace_file_by_hardlink(IHandle_t *hLink, IHandle_t *hTarget)
 int
 namei_copy_on_write(IHandle_t *h)
 {
-    afs_int32 fd, code = 0;
+    afs_int32 code = 0;
+    FD_t fd;
     namei_t name;
     FdHandle_t *fdP;
-    struct afs_stat tstat;
+    struct afs_stat_st tstat;
     afs_foff_t offset;
 
     namei_HandleToName(&name, h);
@@ -1195,14 +1172,14 @@ namei_copy_on_write(IHandle_t *h)
        if (!fdP)
            return EIO;
        afs_snprintf(path, sizeof(path), "%s-tmp", name.n_path);
-       fd = afs_open(path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
-       if (fd < 0) {
+       fd = OS_OPEN(path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
+       if (fd == INVALID_FD) {
            FDH_CLOSE(fdP);
            return EIO;
        }
        buf = malloc(8192);
        if (!buf) {
-           close(fd);
+           OS_CLOSE(fd);
            OS_UNLINK(path);
            FDH_CLOSE(fdP);
            return ENOMEM;
@@ -1213,12 +1190,12 @@ namei_copy_on_write(IHandle_t *h)
            tlen = size > 8192 ? 8192 : size;
            if (FDH_PREAD(fdP, buf, tlen, offset) != tlen)
                break;
-           if (write(fd, buf, tlen) != tlen)
+           if (OS_WRITE(fd, buf, tlen) != tlen)
                break;
            size -= tlen;
            offset += tlen;
        }
-       close(fd);
+       OS_CLOSE(fd);
        FDH_REALLYCLOSE(fdP);
        free(buf);
        if (size)
@@ -1597,9 +1574,9 @@ static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
                       unsigned int volid);
 static int DecodeVolumeName(char *name, unsigned int *vid);
 static int namei_ListAFSSubDirs(IHandle_t * dirIH,
-                               int (*write_fun) (FILE *,
+                               int (*write_fun) (FD_t,
                                                  struct ViceInodeInfo *,
-                                                 char *, char *), FILE * fp,
+                                                 char *, char *), FD_t fp,
                                int (*judgeFun) (struct ViceInodeInfo *,
                                                 afs_uint32 vid, void *),
                                afs_uint32 singleVolumeNumber, void *rock);
@@ -1615,11 +1592,11 @@ static int namei_ListAFSSubDirs(IHandle_t * dirIH,
  * can use the same inode reading code.
  */
 static int
-WriteInodeInfo(FILE * fp, struct ViceInodeInfo *info, char *dir, char *name)
+WriteInodeInfo(FD_t fp, struct ViceInodeInfo *info, char *dir, char *name)
 {
     size_t n;
-    n = fwrite(info, sizeof(*info), 1, fp);
-    return (n == 1) ? 0 : -2;
+    n = OS_WRITE(fp, info, sizeof(*info));
+    return (n == sizeof(*info)) ? 0 : -2;
 }
 
 
@@ -1627,7 +1604,7 @@ int mode_errors;          /* Number of errors found in mode bits on directories. */
 void
 VerifyDirPerms(char *path)
 {
-    struct afs_stat status;
+    struct afs_stat_st status;
 
     if (afs_stat(path, &status) < 0) {
        Log("Unable to stat %s. Please manually verify mode bits for this"
@@ -1665,13 +1642,12 @@ VerifyDirPerms(char *path)
  *                for this.
  */
 int
-ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
+ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
               int (*judgeInode) (struct ViceInodeInfo * info, afs_uint32 vid, void *rock),
               afs_uint32 singleVolumeNumber, int *forcep, int forceR, char *wpath,
               void *rock)
 {
     int ninodes;
-    struct afs_stat status;
 
     *forcep = 0; /* no need to salvage until further notice */
 
@@ -1683,18 +1659,14 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
        namei_ListAFSFiles(mountedOn, WriteInodeInfo, inodeFile, judgeInode,
                           singleVolumeNumber, rock);
 
-    if (!inodeFile)
+    if (inodeFile == INVALID_FD)
        return ninodes;
 
     if (ninodes < 0) {
        return ninodes;
     }
 
-    if (fflush(inodeFile) == EOF) {
-       Log("Unable to successfully flush inode file for %s\n", mountedOn);
-       return -2;
-    }
-    if (fsync(fileno(inodeFile)) == -1) {
+    if (OS_SYNC(inodeFile) == -1) {
        Log("Unable to successfully fsync inode file for %s\n", mountedOn);
        return -2;
     }
@@ -1702,13 +1674,9 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
     /*
      * Paranoia:  check that the file is really the right size
      */
-    if (afs_fstat(fileno(inodeFile), &status) == -1) {
-       Log("Unable to successfully stat inode file for %s\n", mountedOn);
-       return -2;
-    }
-    if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
+    if (OS_SIZE(inodeFile) != ninodes * sizeof(struct ViceInodeInfo)) {
        Log("Wrong size (%d instead of %lu) in inode file for %s\n",
-           (int) status.st_size,
+           (int) OS_SIZE(inodeFile),
            (long unsigned int) ninodes * sizeof(struct ViceInodeInfo),
            mountedOn);
        return -2;
@@ -1739,9 +1707,9 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
  */
 int
 namei_ListAFSFiles(char *dev,
-                  int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
+                  int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
                                    char *),
-                  FILE * fp,
+                  FD_t fp,
                   int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
                   afs_uint32 singleVolumeNumber, void *rock)
 {
@@ -1856,9 +1824,9 @@ _namei_examine_special(char * path1,
                       char * dname,
                       IHandle_t * myIH,
                       FdHandle_t * linkHandle,
-                      int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
+                      int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
                                        char *),
-                      FILE * fp,
+                      FD_t fp,
                       int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
                       int singleVolumeNumber,
                       void *rock)
@@ -1895,7 +1863,7 @@ _namei_examine_special(char * path1,
        /* Open this handle */
        (void)afs_snprintf(path2, sizeof(path2),
                           "%s" OS_DIRSEP "%s", path1, dname);
-       linkHandle->fd_fd = afs_open(path2, Testing ? O_RDONLY : O_RDWR, 0666);
+       linkHandle->fd_fd = OS_OPEN(path2, Testing ? O_RDONLY : O_RDWR, 0666);
        info.linkCount =
            namei_GetLinkCount(linkHandle, (Inode) 0, 1, 1, Testing);
     }
@@ -1945,9 +1913,9 @@ _namei_examine_reg(char * path3,
                   char * dname,
                   IHandle_t * myIH,
                   FdHandle_t * linkHandle,
-                  int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
+                  int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
                                    char *),
-                  FILE * fp,
+                  FD_t fp,
                   int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
                   int singleVolumeNumber,
                   void *rock)
@@ -2010,10 +1978,10 @@ struct listsubdirs_work_node {
                                          *   inode, this will be pointed at the
                                          *   link table
                                          */
-    FILE * fp;                          /**< file pointer for writeFun */
+    FD_t fp;                            /**< file pointer for writeFun */
 
     /** function which will write inode metadata to fp */
-    int (*writeFun) (FILE *, struct ViceInodeInfo *, char *, char *);
+    int (*writeFun) (FD_t, struct ViceInodeInfo *, char *, char *);
 
     /** inode filter function */
     int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *);
@@ -2340,9 +2308,9 @@ _namei_examine_file_spawn(const struct listsubdirs_work_node *work,
  */
 static int
 namei_ListAFSSubDirs(IHandle_t * dirIH,
-                    int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
+                    int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
                                      char *),
-                    FILE * fp,
+                    FD_t fp,
                     int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
                     afs_uint32 singleVolumeNumber, void *rock)
 {
@@ -2661,7 +2629,7 @@ DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
            /* Open this handle */
            char lpath[1024];
            (void)sprintf(lpath, "%s" OS_DIRSEP "%s", fpath, data.cFileName);
-           linkHandle.fd_fd = afs_open(lpath, O_RDONLY, 0666);
+           linkHandle.fd_fd = OS_OPEN(lpath, O_RDONLY, 0666);
            info->linkCount =
                namei_GetLinkCount(&linkHandle, (Inode) 0, 0, 0, 0);
        }
@@ -2692,7 +2660,7 @@ DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
            unsigned int volid)
 {
     char fpath[512];
-    struct afs_stat status;
+    struct afs_stat_st status;
     int parm, tag;
     lb64_string_t check;
 
@@ -2941,7 +2909,7 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
 
     (void)afs_snprintf(oldpath, sizeof oldpath, "%s" OS_DIRSEP "%s", dir_name,
                       infoName);
-    fd = afs_open(oldpath, O_RDWR, 0);
+    fd = OS_OPEN(oldpath, O_RDWR, 0);
     if (fd == INVALID_FD) {
        Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
            oldpath);
@@ -2950,7 +2918,7 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
     }
     t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_VOLINFO);
     namei_HandleToName(&n, &t_ih);
-    fd2 = afs_open(n.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
+    fd2 = OS_OPEN(n.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
     if (fd2 == INVALID_FD) {
        Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n", n.n_path);
        OS_CLOSE(fd);
@@ -2972,7 +2940,7 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
     namei_HandleToName(&n, &t_ih);
     (void)afs_snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
                       smallName);
-    fd = afs_open(newpath, O_RDWR, 0);
+    fd = OS_OPEN(newpath, O_RDWR, 0);
     if (fd == INVALID_FD) {
        Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
        code = -1;
@@ -2991,7 +2959,7 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
     namei_HandleToName(&n, &t_ih);
     (void)afs_snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
                       largeName);
-    fd = afs_open(newpath, O_RDWR, 0);
+    fd = OS_OPEN(newpath, O_RDWR, 0);
     if (fd == INVALID_FD) {
        Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n", newpath);
        code = -1;