endian-cache.c
上传用户:seven77cht
上传日期:2007-01-04
资源大小:486k
文件大小:9k
源码类别:

浏览器

开发平台:

Unix_Linux

  1. /***************************************
  2.   $Header: /home/amb/wwwoffle/RCS/endian-cache.c 1.5 1999/07/08 19:28:47 amb Exp $
  3.   WWWOFFLE - World Wide Web Offline Explorer - Version 2.5.
  4.   Correct the the cache for the endian problem.
  5.   ******************/ /******************
  6.   Written by Andrew M. Bishop
  7.   This file Copyright 1998,99 Andrew M. Bishop
  8.   It may be distributed under the GNU Public License, version 2, or
  9.   any higher version.  See section COPYING of the GNU Public license
  10.   for conditions under which this file may be redistributed.
  11.   ***************************************/
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include <time.h>
  17. #include <utime.h>
  18. #include <sys/time.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <dirent.h>
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24. #include <errno.h>
  25. #include "wwwoffle.h"
  26. #include "misc.h"
  27. #include "errors.h"
  28. #include "config.h"
  29. static void UpgradeProto(char *proto);
  30. static void UpgradeHost(char *proto,char *host);
  31. static void UpgradeSpecial(char *dirname);
  32. static int rootp=0;
  33. int main(int argc,char** argv)
  34. {
  35.  long endiancheck=1;
  36.  int i;
  37.  struct stat buf;
  38.  /* Initialise */
  39.  if(*(char*)&endiancheck)
  40.    {fprintf(stderr,"This is a little-endian machine no change neededn");exit(0);}
  41.  if(argc!=2)
  42.    {fprintf(stderr,"Usage: endian-cache <spool-dir>n");exit(1);}
  43.  InitErrorHandler("endian-cache",0,1);
  44.  rootp=!geteuid();
  45.  umask(0);
  46.  /* Change to the spool directory. */
  47.  if(chdir(argv[1]))
  48.     PrintMessage(Fatal,"Cannot change to the spool directory [%!s]; upgrade failed.");
  49.  /* Create the new spool directory. */
  50.  for(i=0;i<3;i++)
  51.    {
  52.     char *proto,*old;
  53.     if(i==0)
  54.        proto="http",old="http.old";
  55.     else if(i==1)
  56.        proto="ftp",old="ftp.old";
  57.     else
  58.        proto="finger",old="finger.old";
  59.     if(stat(proto,&buf))
  60.        PrintMessage(Inform,"Cannot stat the '%s' directory [%!s] not upgraded.",proto);
  61.     else
  62.       {
  63.        printf("Converting '%s' directoryn",proto);
  64.        if(rename(proto,old))
  65.           PrintMessage(Warning,"Cannot rename the '%s' directory to '%s' [%!s]; upgrade failed.",proto,old);
  66.        else
  67.           if(mkdir(proto,buf.st_mode&07777))
  68.             {
  69.              if(errno==EEXIST)
  70.                 PrintMessage(Warning,"The '%s' directory already exists.",proto);
  71.              else
  72.                 PrintMessage(Warning,"Cannot create '%s' directory [%!s]; upgrade failed.",proto);
  73.             }
  74.           else
  75.             {
  76.              if(rootp && chown(proto,buf.st_uid,buf.st_gid))
  77.                 PrintMessage(Important,"Cannot change owner of '%s' spool directory [%!s]; check it.",proto);
  78.              if(chdir(old))
  79.                 PrintMessage(Warning,"Cannot change to the '%s' directory [%!s]; upgrade failed.",old);
  80.              else
  81.                {
  82.                 UpgradeProto(proto);
  83.                 chdir("..");
  84.                 if(rmdir(old))
  85.                    PrintMessage(Warning,"Cannot delete the '%s' directory [%!s]; upgrade failed.",old);
  86.                }
  87.             }
  88.       }
  89.    }
  90.  for(i=0;i<3;i++)
  91.    {
  92.     char *special,*old;
  93.     if(i==0)
  94.        special="outgoing",old="outgoing.old";
  95.     else if(i==1)
  96.        special="lasttime",old="lasttime.old";
  97.     else
  98.        special="monitor",old="monitor.old";
  99.     if(stat(special,&buf))
  100.        PrintMessage(Inform,"Cannot stat the '%s' directory [%!s] not upgraded.",special);
  101.     else
  102.       {
  103.        printf("Converting '%s' directoryn",special);
  104.        if(rename(special,old))
  105.           PrintMessage(Warning,"Cannot rename the '%s' directory to '%s' [%!s]; upgrade failed.",special,old);
  106.        else
  107.           if(mkdir(special,buf.st_mode&07777))
  108.             {
  109.              if(errno==EEXIST)
  110.                 PrintMessage(Warning,"The '%s' directory already exists.",special);
  111.              else
  112.                 PrintMessage(Warning,"Cannot create '%s' directory [%!s]; upgrade failed.",special);
  113.             }
  114.           else
  115.             {
  116.              if(rootp && chown(special,buf.st_uid,buf.st_gid))
  117.                 PrintMessage(Important,"Cannot change owner of '%s' spool directory [%!s]; check it.",special);
  118.              if(chdir(old))
  119.                 PrintMessage(Warning,"Cannot change to the '%s' directory [%!s]; upgrade failed.",old);
  120.              else
  121.                {
  122.                 UpgradeSpecial(special);
  123.                 chdir("..");
  124.                 if(rmdir(old))
  125.                    PrintMessage(Warning,"Cannot delete the '%s' directory [%!s]; upgrade failed.",old);
  126.                }
  127.             }
  128.       }
  129.    }
  130.  return(0);
  131. }
  132. /*++++++++++++++++++++++++++++++++++++++
  133.   Upgrade a complete protocol directory.
  134.   char *proto The protocol of the spool directory we are in.
  135.   ++++++++++++++++++++++++++++++++++++++*/
  136. static void UpgradeProto(char *proto)
  137. {
  138.  DIR *dir;
  139.  struct dirent* ent;
  140.  struct stat buf;
  141.  /* Open the spool directory. */
  142.  dir=opendir(".");
  143.  if(!dir)
  144.    {PrintMessage(Warning,"Cannot open spool directory '%s.old' [%!s]; upgrade failed.",proto);return;}
  145.  ent=readdir(dir);  /* skip .  */
  146.  if(!ent)
  147.    {PrintMessage(Warning,"Cannot read spool directory '%s.old' [%!s]; upgrade failed.",proto);closedir(dir);return;}
  148.  ent=readdir(dir);  /* skip .. */
  149.  /* Go through each entry. */
  150.  while((ent=readdir(dir)))
  151.    {
  152.     char *newname=(char*)malloc(strlen(proto)+strlen(ent->d_name)+16);
  153.     sprintf(newname,"../%s/%s",proto,ent->d_name);
  154.     if(lstat(ent->d_name,&buf))
  155.        PrintMessage(Warning,"Cannot stat file '%s.old/%s' [%!s] not upgraded.",proto,ent->d_name);
  156.     else if(S_ISDIR(buf.st_mode))
  157.       {
  158.        if(mkdir(newname,buf.st_mode&07777))
  159.          {
  160.           if(errno==EEXIST)
  161.              PrintMessage(Warning,"The '%s/%s' directory already exists.",proto,ent->d_name);
  162.           else
  163.              PrintMessage(Warning,"Cannot create '%s/%s' directory [%!s]; upgrade failed.",proto,ent->d_name);
  164.          }
  165.        else
  166.          {
  167.           if(rootp && chown(newname,buf.st_uid,buf.st_gid))
  168.              PrintMessage(Important,"Cannot change owner of '%s/%s' spool directory [%!s]; check it.",proto,ent->d_name);
  169.           if(chdir(ent->d_name))
  170.              PrintMessage(Warning,"Cannot change to the '%s.old/%s' directory [%!s]; upgrade failed.",proto,ent->d_name);
  171.           else
  172.             {
  173.              UpgradeHost(proto,ent->d_name);
  174.              chdir("..");
  175.              if(rmdir(ent->d_name))
  176.                 PrintMessage(Warning,"Cannot delete the '%s.old/%s' directory [%!s]; upgrade failed.",proto,ent->d_name);
  177.             }
  178.          }
  179.       }
  180.     else
  181.        if(rename(ent->d_name,newname))
  182.           PrintMessage(Warning,"Cannot move file '%s.old/%s' [%!s] not upgraded.",proto,ent->d_name);
  183.     free(newname);
  184.    }
  185.  closedir(dir);
  186. }
  187. /*++++++++++++++++++++++++++++++++++++++
  188.   Upgrade a complete host directory.
  189.   char *proto The protocol of the spool directory we are in.
  190.   char *host The hostname of the spool directory we are in.
  191.   ++++++++++++++++++++++++++++++++++++++*/
  192. static void UpgradeHost(char *proto,char *host)
  193. {
  194.  DIR *dir;
  195.  struct dirent* ent;
  196.  struct stat buf;
  197.  char *newname=(char*)malloc(42+strlen(proto)+strlen(host)),*p;
  198.  /* Open the spool directory. */
  199.  dir=opendir(".");
  200.  if(!dir)
  201.    {PrintMessage(Warning,"Cannot open spool directory '%s.old/%s' [%!s]; upgrade failed.",proto,host);return;}
  202.  ent=readdir(dir);  /* skip .  */
  203.  if(!ent)
  204.    {PrintMessage(Warning,"Cannot read spool directory '%s.old/%s' [%!s]; upgrade failed.",proto,host);closedir(dir);return;}
  205.  ent=readdir(dir);  /* skip .. */
  206.  /* Go through each entry. */
  207.  sprintf(newname,"../../%s/%s/",proto,host);
  208.  p=newname+strlen(newname);
  209.  while((ent=readdir(dir)))
  210.    {
  211.     if(!lstat(ent->d_name,&buf) && *ent->d_name=='D' && S_ISREG(buf.st_mode))
  212.       {
  213.        char *url=FileNameToURL(ent->d_name);
  214.        if(url)
  215.          {
  216.           URL *Url=SplitURL(url);
  217.           char *name=URLToFileName(Url);
  218.           char *oldname=ent->d_name;
  219.           strcpy(p,name);
  220.           *oldname=*p='U';
  221.           rename(oldname,newname);
  222.           *oldname=*p='D';
  223.           rename(oldname,newname);
  224.           free(name);
  225.           FreeURL(Url);
  226.           free(url);
  227.          }
  228.       }
  229.    }
  230.  closedir(dir);
  231.  free(newname);
  232. }
  233. /*++++++++++++++++++++++++++++++++++++++
  234.   Upgrade a complete special directory.
  235.   char *special The name of the special directory.
  236.   ++++++++++++++++++++++++++++++++++++++*/
  237. static void UpgradeSpecial(char *special)
  238. {
  239.  DIR *dir;
  240.  struct dirent* ent;
  241.  struct stat buf;
  242.  char *newname=(char*)malloc(32+strlen(special)),*p;
  243.  /* Open the spool directory. */
  244.  dir=opendir(".");
  245.  if(!dir)
  246.    {PrintMessage(Warning,"Cannot open spool directory '%s.old' [%!s]; upgrade failed.",special);return;}
  247.  ent=readdir(dir);  /* skip .  */
  248.  if(!ent)
  249.    {PrintMessage(Warning,"Cannot read spool directory '%s.old' [%!s]; upgrade failed.",special);closedir(dir);return;}
  250.  ent=readdir(dir);  /* skip .. */
  251.  /* Go through each entry. */
  252.  sprintf(newname,"../%s/",special);
  253.  p=newname+strlen(newname);
  254.  while((ent=readdir(dir)))
  255.    {
  256.     if(!lstat(ent->d_name,&buf) && (*ent->d_name=='O' || *ent->d_name=='D')  && S_ISREG(buf.st_mode))
  257.       {
  258.        char *url=FileNameToURL(ent->d_name);
  259.        if(url)
  260.          {
  261.           URL *Url=SplitURL(url);
  262.           char *name=URLToFileName(Url);
  263.           char *oldname=ent->d_name;
  264.           strcpy(p,name);
  265.           *oldname=*p='U';
  266.           rename(oldname,newname);
  267.           *oldname=*p='D';
  268.           rename(oldname,newname);
  269.           *oldname=*p='O';
  270.           rename(oldname,newname);
  271.           free(name);
  272.           FreeURL(Url);
  273.           free(url);
  274.          }
  275.       }
  276.    }
  277.  closedir(dir);
  278.  free(newname);
  279. }