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

浏览器

开发平台:

Unix_Linux

  1. /***************************************
  2.   $Header: /home/amb/wwwoffle/RCS/upgrade-cache.c 1.5 1999/07/08 19:28:48 amb Exp $
  3.   WWWOFFLE - World Wide Web Offline Explorer - Version 2.5.
  4.   Upgrade the cache for version 2.
  5.   ******************/ /******************
  6.   Written by Andrew M. Bishop
  7.   This file Copyright 1997,98,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. #define PATH_SEP '%'
  30. char *URLToFileName(URL *Url);
  31. int main(int argc,char** argv)
  32. {
  33.  DIR *dir;
  34.  struct dirent* ent;
  35.  struct stat buf;
  36.  int rootp;
  37.  /* Initialise */
  38.  if(argc!=2)
  39.    {fprintf(stderr,"Usage: upgrade-cache <spool-dir>n");exit(0);}
  40.  InitErrorHandler("upgrade-cache",0,1);
  41.  rootp=!geteuid();
  42.  umask(0);
  43.  /* Change to the spool directory. */
  44.  if(chdir(argv[1]))
  45.     PrintMessage(Fatal,"Cannot change to the spool directory [%!s]; upgrade failed.");
  46.  /* Create the new http spool directory. */
  47.  if(stat(".",&buf))
  48.     PrintMessage(Fatal,"Cannot stat spool directory [%!s] not upgraded.");
  49.  if(mkdir("http",buf.st_mode&07777))
  50.     if(errno==EEXIST)
  51.        PrintMessage(Warning,"The 'http' spool directory already exists.");
  52.     else
  53.        PrintMessage(Fatal,"Cannot create 'http' spool directory [%!s]; upgrade failed.");
  54.  if(rootp && chown("http",buf.st_uid,buf.st_gid))
  55.     PrintMessage(Important,"Cannot change owner of 'http' spool directory [%!s]; check it.");
  56.  /* Open the spool directory. */
  57.  dir=opendir(".");
  58.  if(!dir)
  59.     PrintMessage(Fatal,"Cannot open spool directory [%!s]; upgrade failed.");
  60.  ent=readdir(dir);  /* skip .  */
  61.  if(!ent)
  62.     PrintMessage(Fatal,"Cannot read spool directory [%!s]; upgrade failed.");
  63.  ent=readdir(dir);  /* skip .. */
  64.  /* Go through each entry. */
  65.  while((ent=readdir(dir)))
  66.    {
  67.     if(lstat(ent->d_name,&buf))
  68.        PrintMessage(Warning,"Cannot stat file '%s' [%!s] not upgraded.",ent->d_name);
  69.     else if(S_ISLNK(buf.st_mode))
  70.       {
  71.        char link[257];
  72.        struct stat buf2;
  73.        int n;
  74.        n=readlink(ent->d_name,link,256);
  75.        if(n==-1)
  76.          {PrintMessage(Warning,"Cannot readlink file '%s' [%!s] not upgraded.",ent->d_name);continue;}
  77.        link[n]=0;
  78.        if(stat(link,&buf2))
  79.          {PrintMessage(Warning,"Cannot stat file '%s'->'%s' [%!s] not upgraded.",ent->d_name,link);continue;}
  80.        else if(S_ISDIR(buf2.st_mode))
  81.          {
  82.           char *new_name=(char*)malloc(strlen(ent->d_name)+8);
  83.           PrintMessage(Inform,"Processing link '%s'",ent->d_name);
  84.           strcpy(new_name,"http/");
  85.           strcat(new_name,ent->d_name);
  86.           if(rename(ent->d_name,new_name))
  87.              PrintMessage(Warning,"Cannot rename '%s' to '%s' [%!s] not upgraded.",ent->d_name,new_name);
  88.           free(new_name);
  89.          }
  90.       }
  91.     else if(S_ISDIR(buf.st_mode) && !strcmp(ent->d_name,"outgoing"))
  92.       {
  93.        DIR *dir2;
  94.        struct dirent* ent2;
  95.        PrintMessage(Inform,"Processing outgoing directory.");
  96.        if(chdir(ent->d_name))
  97.          {PrintMessage(Warning,"Cannot change to outgoing directory [%!s]; not upgraded.");continue;}
  98.        dir2=opendir(".");
  99.        if(!dir2)
  100.          {PrintMessage(Warning,"Cannot open outgoing directory [%!s]; not upgraded.");chdir("..");continue;}
  101.        ent2=readdir(dir2);  /* skip .  */
  102.        if(!ent2)
  103.          {PrintMessage(Warning,"Cannot read outgoing directory [%!s]; not upgraded.");closedir(dir2);chdir("..");continue;}
  104.        ent2=readdir(dir2);  /* skip .. */
  105.        while((ent2=readdir(dir2)))
  106.          {
  107.           struct stat buf2;
  108.           if(!isdigit(ent2->d_name[0]))
  109.             {
  110.              unlink(ent2->d_name);
  111.              continue;
  112.             }
  113.           if(stat(ent2->d_name,&buf2))
  114.              PrintMessage(Warning,"Cannot stat file 'outgoing/%s' [%!s]; not upgraded.",ent2->d_name);
  115.           else if(S_ISREG(buf2.st_mode))
  116.             {
  117.              int ufd;
  118.              char *url,*new_name2,*ptr;
  119.              URL *Url;
  120.              int file;
  121.              file=open(ent2->d_name,O_RDONLY);
  122.              init_buffer(file);
  123.              ptr=read_line(file,NULL);
  124.              url=(char*)malloc(strlen(ptr));
  125.              sscanf(ptr,"%*s %s",url);
  126.              free(ptr);
  127.              close(file);
  128.              Url=SplitURL(url);
  129.              new_name2=URLToFileName(Url);
  130.              *new_name2='O';
  131.              if(rename(ent2->d_name,new_name2))
  132.                 PrintMessage(Warning,"Cannot rename outgoing request 'outgoing/%s' to 'outgoing/%s' [%!s] not upgraded.",ent2->d_name,new_name2);
  133.              *new_name2='U';
  134.              ufd=open(new_name2,O_WRONLY|O_CREAT|O_TRUNC,DEF_FILE_PERM);
  135.              if(ufd!=-1)
  136.                {
  137.                 write_string(ufd,Url->name);
  138.                 close(ufd);
  139.                }
  140.              free(new_name2);
  141.             }
  142.          }
  143.        closedir(dir2);
  144.        chdir("..");
  145.       }
  146.     else if(S_ISDIR(buf.st_mode) && strcmp(ent->d_name,"http"))
  147.       {
  148.        DIR *dir2;
  149.        struct dirent* ent2;
  150.        struct utimbuf utbuf;
  151.        char *new_name=(char*)malloc(strlen(ent->d_name)+8);
  152.        char *url;
  153.        URL *Url;
  154.        PrintMessage(Inform,"Processing directory '%s'",ent->d_name);
  155.        strcpy(new_name,"http/");
  156.        strcat(new_name,ent->d_name);
  157.        if(mkdir(new_name,buf.st_mode&07777))
  158.          {PrintMessage(Warning,"Cannot create '%s' spool directory [%!s]; not upgraded.",new_name);continue;}
  159.        if(rootp && chown(new_name,buf.st_uid,buf.st_gid))
  160.           PrintMessage(Important,"Cannot change owner of '%s' spool directory [%!s]; check it.",new_name);
  161.        if(chdir(ent->d_name))
  162.          {PrintMessage(Warning,"Cannot change to directory '%s' [%!s]; not upgraded.",ent->d_name);continue;}
  163.        dir2=opendir(".");
  164.        if(!dir2)
  165.          {PrintMessage(Warning,"Cannot open directory '%s' [%!s]; not upgraded.",ent->d_name);chdir("..");continue;}
  166.        ent2=readdir(dir2);  /* skip .  */
  167.        if(!ent2)
  168.          {PrintMessage(Warning,"Cannot read directory '%s' [%!s]; not upgraded.",ent->d_name);closedir(dir2);chdir("..");continue;}
  169.        ent2=readdir(dir2);  /* skip .. */
  170.        while((ent2=readdir(dir2)))
  171.          {
  172.           struct stat buf2;
  173.           if(ent2->d_name[0] && ent2->d_name[0]==PATH_SEP &&
  174.              ent2->d_name[1] && ent2->d_name[1]==PATH_SEP)
  175.              continue;
  176.           if(stat(ent2->d_name,&buf2))
  177.              PrintMessage(Warning,"Cannot stat file '%s/%s' [%!s]; not upgraded.",ent->d_name,ent2->d_name);
  178.           else if(S_ISREG(buf2.st_mode))
  179.             {
  180.              char *path=(char*)malloc(strlen(ent2->d_name)+2),*ext,*args=NULL;
  181.              char *new_name2,*new_file;
  182.              if(*ent2->d_name==PATH_SEP)
  183.                 strcpy(path,ent2->d_name);
  184.              else
  185.                {
  186.                 *path='/';
  187.                 strcpy(path+1,ent2->d_name);
  188.                }
  189.              for(ext=path;*ext;ext++)
  190.                 if(*ext==PATH_SEP)
  191.                    if(*(ext+1)==PATH_SEP && *(ext+2)!=PATH_SEP)
  192.                       break;
  193.                    else
  194.                       *ext='/';
  195.              if(*ext)
  196.                {
  197.                 int r=0,rr;
  198.                 int afd=open(ext,O_RDONLY);
  199.                 if(afd==-1)
  200.                   {PrintMessage(Warning,"Cannot open '%s/%s' to read arguments for '%s/%s' [%!s] not upgraded.",ent->d_name,ext,ent->d_name,ent2->d_name);continue;}
  201.                 args=(char*)malloc(256+1);
  202.                 while((rr=read(afd,&args[r],256)))
  203.                   {
  204.                    r+=rr;
  205.                    args=(char*)realloc(args,r+256+1);
  206.                   }
  207.                 args[r]=0;
  208.                 close(afd);
  209.                 if(unlink(ext))
  210.                   {PrintMessage(Warning,"Cannot delete '%s/%s' [%!s] not upgraded.",ent->d_name,ent2->d_name);continue;}
  211.                 *ext=0;
  212.                }
  213.              url=(char*)malloc(strlen(ent->d_name)+strlen(path)+(args?strlen(args):0)+16);
  214.              if(args)
  215.                 sprintf(url,"http://%s/%s?%s",ent->d_name,*path=='/'?path+1:path,args);
  216.              else
  217.                 sprintf(url,"http://%s/%s",ent->d_name,*path=='/'?path+1:path);
  218.              Url=SplitURL(url);
  219.              new_file=URLToFileName(Url);
  220.              *new_file='D';
  221.              new_name2=(char*)malloc(strlen(ent->d_name)+strlen(new_file)+16);
  222.              strcpy(new_name2,"../http/");
  223.              strcat(new_name2,ent->d_name);
  224.              strcat(new_name2,"/");
  225.              strcat(new_name2,new_file);
  226.              free(new_file);
  227.              if(rename(ent2->d_name,new_name2))
  228.                {PrintMessage(Warning,"Cannot rename '%s/%s' to '%s' [%!s] not upgraded.",ent->d_name,ent2->d_name,new_name2+3);continue;}
  229.              else
  230.                {
  231.                 int ufd;
  232.                 *(new_name2+9+strlen(ent->d_name))='U';
  233.                 ufd=open(new_name2,O_WRONLY|O_CREAT|O_TRUNC,DEF_FILE_PERM);
  234.                 if(ufd!=-1)
  235.                   {
  236.                    write_string(ufd,Url->name);
  237.                    close(ufd);
  238.                   }
  239.                }
  240.              if(args)
  241.                 free(args);
  242.              free(new_name2);
  243.              FreeURL(Url);
  244.              free(url);
  245.             }
  246.          }
  247.        closedir(dir2);
  248.        chdir("..");
  249.        utbuf.actime=buf.st_atime;
  250.        utbuf.modtime=buf.st_mtime;
  251.        utime(new_name,&utbuf);
  252.        if(rmdir(ent->d_name))
  253.           PrintMessage(Warning,"Cannot unlink directory '%s' [%!s]; check it is empty.",ent->d_name);
  254.        free(new_name);
  255.       }
  256.    }
  257.  closedir(dir);
  258.  return(0);
  259. }
  260. /*++++++++++++++++++++++++++++++++++++++
  261.   Convert a URL to a filename
  262.   char *URLToFileName Returns the filename.
  263.   URL *Url The URL to convert to a filename.
  264.   ++++++++++++++++++++++++++++++++++++++*/
  265. char *URLToFileName(URL *Url)
  266. {
  267.  char *hash;
  268.  char *file;
  269.  hash=MakeHash(Url->name);
  270.  file=(char*)malloc(strlen(hash)+2);
  271.  sprintf(file,"X%s",hash);
  272.  return(file);
  273. }