"Fossies" - the Fresh Open Source Software Archive

Member "coda-6.9.5/coda-src/egasr/removeinc.c" (29 Aug 2005, 3738 Bytes) of package /linux/misc/old/coda-6.9.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "removeinc.c" see the Fossies "Dox" file reference documentation.

    1 /* BLURB gpl
    2 
    3                            Coda File System
    4                               Release 6
    5 
    6           Copyright (c) 1987-2003 Carnegie Mellon University
    7                   Additional copyrights listed below
    8 
    9 This  code  is  distributed "AS IS" without warranty of any kind under
   10 the terms of the GNU General Public Licence Version 2, as shown in the
   11 file  LICENSE.  The  technical and financial  contributors to Coda are
   12 listed in the file CREDITS.
   13 
   14                         Additional copyrights
   15                            none currently
   16 
   17 #*/
   18 
   19 
   20 
   21 /*
   22  * removeinc - removes an inconsistent file (after repairing with an empty file) 
   23  */ 
   24  
   25 #ifdef __cplusplus
   26 extern "C" {
   27 #endif
   28 
   29 #ifdef HAVE_CONFIG_H
   30 #include <config.h>
   31 #endif
   32 
   33 #include <stdlib.h>
   34 #include <stdio.h>
   35 #include <sys/param.h>
   36 #include <sys/types.h>
   37 #include <sys/stat.h>
   38 #include "coda_string.h"
   39 #include "coda_assert.h"
   40 #include <errno.h>
   41 #include <venusioctl.h>
   42 #include <vcrcommon.h>
   43 #include <coda_assert.h>
   44 #include <codadir.h>
   45 
   46 extern int path(char *, char *, char *);
   47 extern int wildmat(char *text, char *pattern);
   48 
   49 #ifdef __cplusplus
   50 }
   51 #endif
   52 
   53 
   54 int IsObjInc(char *name, ViceFid *fid, char *realm) 
   55 {
   56     struct GetFid {
   57     ViceFid fid;
   58     ViceVersionVector vv;
   59     char realm[MAXHOSTNAMELEN+1];
   60     } out;
   61     struct ViceIoctl vio;
   62     struct stat statbuf;
   63     char symval[MAXPATHLEN];
   64     int rc, n;
   65 
   66     memset(fid, 0, sizeof(ViceFid));
   67     realm[0] = '\0';
   68 
   69     /* what if the begin repair has been done already */
   70     rc = stat(name, &statbuf);
   71     if (rc) {
   72     /* is it a sym link */
   73     symval[0] = 0;
   74     rc = readlink(name, symval, MAXPATHLEN);
   75     if (rc < 0) return(0);
   76 
   77     /* it's a sym link, alright */
   78     n = sscanf(symval, "@%x.%x.%x@%s",
   79            &fid->Volume, &fid->Vnode, &fid->Unique, realm);
   80 
   81     return(n == 4);
   82     }
   83 
   84     memset(&out, 0, sizeof(out));
   85     vio.in = NULL;
   86     vio.in_size = 0;
   87     vio.out = (char *)&out;
   88     vio.out_size = sizeof(out);
   89 
   90     rc = pioctl(name, _VICEIOCTL(_VIOC_GETFID), &vio, 0);
   91     if (rc < 0 && errno != ETOOMANYREFS) {
   92     fprintf(stderr, "Error %d for Getfid\n", errno);
   93     return(0);
   94     }
   95     memcpy(fid, &out.fid, sizeof(ViceFid));
   96     strcpy(realm, out.realm);
   97 
   98     if (!ISDIR(out.fid) && (statbuf.st_mode & S_IFDIR))
   99     return(1);
  100 
  101     if (out.vv.StoreId.Host == -1) 
  102     return(1);
  103 
  104     return(0);
  105 }
  106 
  107 int main(int argc, char **argv) 
  108 {
  109     ViceFid fid;
  110     char realm[MAXHOSTNAMELEN+1];
  111     char tmpfname[80];
  112     int fd;
  113     struct ViceIoctl vioc;
  114     char space[2048];
  115     int rc;
  116 
  117     if (argc != 2) {
  118     fprintf(stderr, "Usage: %s <inc-file-name>\n", argv[0]);
  119     exit(-1);
  120     }
  121     
  122     /* make sure object is inconsistent */
  123     if (!IsObjInc(argv[1], &fid, realm)) {
  124     fprintf(stderr, "%s isn't inconsistent\n", argv[1]);
  125     exit(0);
  126     }
  127     
  128     /* get fid and make sure it is a file */
  129     if (ISDIR(fid) && !FID_IsLocalDir(&fid)) {
  130     fprintf(stderr, "%s is a directory - must be removed manually\n", argv[1]);
  131     exit(-1);
  132     }
  133 
  134     /* create an empty file /tmp/REPAIR.XXXXXX */
  135     strcpy(tmpfname, "/tmp/RMINC.XXXXXX");
  136     if ((fd = mkstemp(tmpfname)) < 0) {
  137     fprintf(stderr, "Couldn't create /tmp file\n");
  138     exit(-1);
  139     }
  140     close(fd);
  141 
  142     /* dorepair on the fid with an empty file */
  143     vioc.in_size = (short)(1+strlen(tmpfname)); 
  144     vioc.in = tmpfname;
  145     vioc.out_size = (short) sizeof(space);
  146     vioc.out = space;
  147     memset(space, 0, (int) sizeof(space));
  148     rc = pioctl(argv[1], _VICEIOCTL(_VIOC_REPAIR), &vioc, 0);
  149     if (rc < 0 && errno != ETOOMANYREFS) {
  150     fprintf(stderr, "Error %d for repair\n", errno);
  151     unlink(tmpfname);
  152     exit(-1);
  153     }
  154     unlink(tmpfname);
  155 
  156     /* remove the repaired file */
  157     if (unlink(argv[1])) {
  158     fprintf(stderr, "Couldn't remove %s\n", argv[1]);
  159     exit(-1);
  160     }
  161     exit(0);
  162 }