vol-lock-fd-type-20090323
[openafs.git] / src / vol / vol-salvage.c
index 88fa5c0..91b3d33 100644 (file)
@@ -336,13 +336,13 @@ childJob_t myjob = { SALVAGER_MAGIC, NOT_CHILD, "" };
 void
 ObtainSalvageLock(void)
 {
-    int salvageLock;
+    FD_t salvageLock;
 
 #ifdef AFS_NT40_ENV
     salvageLock =
-       (int)CreateFile(AFSDIR_SERVER_SLVGLOCK_FILEPATH, 0, 0, NULL,
+       (FD_t)CreateFile(AFSDIR_SERVER_SLVGLOCK_FILEPATH, 0, 0, NULL,
                        OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
-    if (salvageLock == (int)INVALID_HANDLE_VALUE) {
+    if (salvageLock == INVALID_FD) {
        fprintf(stderr,
                "salvager:  There appears to be another salvager running!  Aborted.\n");
        Exit(1);
@@ -790,7 +790,7 @@ SalvageFileSys1(struct DiskPartition64 *partP, VolumeId singleVolumeNumber)
      * modified to actually do that so that the NT crt can be used there.
      */
     inodeFd =
-       _open_osfhandle((long)nt_open(inodeListPath, O_RDWR, 0), O_RDWR);
+       _open_osfhandle((intptr_t)nt_open(inodeListPath, O_RDWR, 0), O_RDWR);
     nt_unlink(inodeListPath);  /* NT's crt unlink won't if file is open. */
 #else
     inodeFd = afs_open(inodeListPath, O_RDONLY);
@@ -880,6 +880,7 @@ DeleteExtraVolumeHeaderFile(register struct VolumeSummary *vsp)
     vsp->fileName = 0;
 }
 
+int
 CompareInodes(const void *_p1, const void *_p2)
 {
     register const struct ViceInodeInfo *p1 = _p1;
@@ -980,9 +981,9 @@ void
 CountVolumeInodes(register struct ViceInodeInfo *ip, int maxInodes,
                  register struct InodeSummary *summary)
 {
-    int volume = ip->u.vnode.volumeId;
-    int rwvolume = volume;
-    register n, nSpecial;
+    VolumeId volume = ip->u.vnode.volumeId;
+    VolumeId rwvolume = volume;
+    register int n, nSpecial;
     register Unique maxunique;
     n = nSpecial = 0;
     maxunique = 0;
@@ -1007,7 +1008,7 @@ CountVolumeInodes(register struct ViceInodeInfo *ip, int maxInodes,
 }
 
 int
-OnlyOneVolume(struct ViceInodeInfo *inodeinfo, int singleVolumeNumber, void *rock)
+OnlyOneVolume(struct ViceInodeInfo *inodeinfo, afs_uint32 singleVolumeNumber, void *rock)
 {
     if (inodeinfo->u.vnode.vnodeNumber == INODESPECIAL)
        return (inodeinfo->u.special.parentId == singleVolumeNumber);
@@ -1197,14 +1198,15 @@ CompareVolumes(const void *_p1, const void *_p2)
 void
 GetVolumeSummary(VolumeId singleVolumeNumber)
 {
-    DIR *dirp;
+    DIR *dirp = NULL;
     afs_int32 nvols = 0;
     struct VolumeSummary *vsp, vs;
     struct VolumeDiskHeader diskHeader;
     struct dirent *dp;
 
     /* Get headers from volume directory */
-    if (chdir(fileSysPath) == -1 || (dirp = opendir(".")) == NULL)
+    dirp = opendir(fileSysPath);
+    if (dirp  == NULL)
        Abort("Can't read directory %s; not salvaged\n", fileSysPath);
     if (!singleVolumeNumber) {
        while ((dp = readdir(dirp))) {
@@ -1212,7 +1214,9 @@ GetVolumeSummary(VolumeId singleVolumeNumber)
            p = strrchr(dp->d_name, '.');
            if (p != NULL && strcmp(p, VHDREXT) == 0) {
                int fd;
-               if ((fd = afs_open(dp->d_name, O_RDONLY)) != -1
+               char name[64];
+               sprintf(name, "%s/%s", fileSysPath, dp->d_name);
+               if ((fd = afs_open(name, O_RDONLY)) != -1
                    && read(fd, (char *)&diskHeader, sizeof(diskHeader))
                    == sizeof(diskHeader)
                    && diskHeader.stamp.magic == VOLUMEHEADERMAGIC) {
@@ -1246,7 +1250,9 @@ GetVolumeSummary(VolumeId singleVolumeNumber)
        if (p != NULL && strcmp(p, VHDREXT) == 0) {
            int error = 0;
            int fd;
-           if ((fd = afs_open(dp->d_name, O_RDONLY)) == -1
+           char name[64];
+           sprintf(name, "%s/%s", fileSysPath, dp->d_name);
+           if ((fd = afs_open(name, O_RDONLY)) == -1
                || read(fd, &diskHeader, sizeof(diskHeader))
                != sizeof(diskHeader)
                || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
@@ -1287,7 +1293,7 @@ GetVolumeSummary(VolumeId singleVolumeNumber)
                    || (vsp->header.id == singleVolumeNumber
                        || vsp->header.parent == singleVolumeNumber)) {
                    (void)afs_snprintf(nameShouldBe, sizeof nameShouldBe,
-                                      VFORMAT, vsp->header.id);
+                                      VFORMAT, afs_cast_uint32(vsp->header.id));
                    if (singleVolumeNumber 
                        && vsp->header.id != singleVolumeNumber)
                        AskOffline(vsp->header.id, fileSysPartition->name);
@@ -1429,7 +1435,7 @@ DoSalvageVolumeGroup(register struct InodeSummary *isp, int nVols)
     int check;
     Inode ino;
     int dec_VGLinkH = 0;
-    int VGLinkH_p1;
+    int VGLinkH_p1 =0;
     FdHandle_t *fdP = NULL;
 
     VGLinkH_cnt = 0;
@@ -1736,23 +1742,26 @@ SalvageVolumeHeaderFile(register struct InodeSummary *isp,
     }
 
     if (isp->volSummary == NULL) {
-       char name[64];
-       (void)afs_snprintf(name, sizeof name, VFORMAT, isp->volumeId);
+       char path[64];
+       char headerName[64];
+       (void)afs_snprintf(headerName, sizeof headerName, VFORMAT, afs_cast_uint32(isp->volumeId));
+       (void)afs_snprintf(path, sizeof path, "%s/%s", fileSysPath, headerName);
        if (check) {
            Log("No header file for volume %u\n", isp->volumeId);
            return -1;
        }
        if (!Showmode)
-           Log("No header file for volume %u; %screating %s/%s\n",
+           Log("No header file for volume %u; %screating %s\n",
                isp->volumeId, (Testing ? "it would have been " : ""),
-               fileSysPathName, name);
-       headerFd = afs_open(name, O_RDWR | O_CREAT | O_TRUNC, 0644);
+               path);
+       headerFd = afs_open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
        assert(headerFd != -1);
        isp->volSummary = (struct VolumeSummary *)
            malloc(sizeof(struct VolumeSummary));
-       isp->volSummary->fileName = ToString(name);
+       isp->volSummary->fileName = ToString(headerName);
     } else {
-       char name[64];
+       char path[64];
+       char headerName[64];
        /* hack: these two fields are obsolete... */
        isp->volSummary->header.volumeAcl = 0;
        isp->volSummary->header.volumeMountTable = 0;
@@ -1762,18 +1771,19 @@ SalvageVolumeHeaderFile(register struct InodeSummary *isp,
             sizeof(struct VolumeHeader))) {
            /* We often remove the name before calling us, so we make a fake one up */
            if (isp->volSummary->fileName) {
-               strcpy(name, isp->volSummary->fileName);
+               strcpy(headerName, isp->volSummary->fileName);
            } else {
-               (void)afs_snprintf(name, sizeof name, VFORMAT, isp->volumeId);
-               isp->volSummary->fileName = ToString(name);
+               (void)afs_snprintf(headerName, sizeof headerName, VFORMAT, afs_cast_uint32(isp->volumeId));
+               isp->volSummary->fileName = ToString(headerName);
            }
+           (void)afs_snprintf(path, sizeof path, "%s/%s", fileSysPath, headerName);
 
-           Log("Header file %s is damaged or no longer valid%s\n", name,
+           Log("Header file %s is damaged or no longer valid%s\n", path,
                (check ? "" : "; repairing"));
            if (check)
                return -1;
 
-           headerFd = afs_open(name, O_RDWR | O_TRUNC, 0644);
+           headerFd = afs_open(path, O_RDWR | O_TRUNC, 0644);
            assert(headerFd != -1);
        }
     }
@@ -3290,7 +3300,7 @@ PrintInodeList(void)
     register struct ViceInodeInfo *ip;
     struct ViceInodeInfo *buf;
     struct afs_stat status;
-    register nInodes;
+    register int nInodes;
 
     assert(afs_fstat(inodeFd, &status) == 0);
     buf = (struct ViceInodeInfo *)malloc(status.st_size);
@@ -3355,8 +3365,7 @@ Fork(void)
 }
 
 void
-Exit(code)
-     int code;
+Exit(int code)
 {
     if (ShowLog)
        showlog();
@@ -3543,16 +3552,18 @@ ToString(char *s)
     assert(p != NULL);
     strcpy(p, s);
     return p;
-
 }
 
 /* Remove the FORCESALVAGE file */
 void
 RemoveTheForce(char *path)
 {
+    char target[1024];
+    struct afs_stat force; /* so we can use afs_stat to find it */
+    strcpy(target,path);
+    strcat(target,"/FORCESALVAGE");
     if (!Testing && ForceSalvage) {
-       if (chdir(path) == 0)
-           unlink("FORCESALVAGE");
+        if (afs_stat(target,&force) == 0)  unlink(target);
     }
 }
 
@@ -3564,10 +3575,11 @@ int
 UseTheForceLuke(char *path)
 {
     struct afs_stat force;
+    char target[1024];
+    strcpy(target,path);
+    strcat(target,"/FORCESALVAGE");
 
-    assert(chdir(path) != -1);
-
-    return (afs_stat("FORCESALVAGE", &force) == 0);
+    return (afs_stat(target, &force) == 0);
 }
 #else
 /*