home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Magazine / wwwoffle-2.1.tar.gz / wwwoffle-2.1 / upgrade-cache.c < prev    next >
C/C++ Source or Header  |  1998-01-17  |  10KB  |  357 lines

  1. /***************************************
  2.   $Header: /home/amb/wwwoffle/RCS/upgrade-cache.c 1.2 1997/10/29 20:37:33 amb Exp $
  3.  
  4.   WWWOFFLE - World Wide Web Offline Explorer - Version 2.0.
  5.   Upgrade the cache for version 2.
  6.   ******************/ /******************
  7.   Written by Andrew M. Bishop
  8.  
  9.   This file Copyright 1997 Andrew M. Bishop
  10.   It may be distributed under the GNU Public License, version 2, or
  11.   any higher version.  See section COPYING of the GNU Public license
  12.   for conditions under which this file may be redistributed.
  13.   ***************************************/
  14.  
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20.  
  21. #include <time.h>
  22. #include <utime.h>
  23. #include <sys/time.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <dirent.h>
  27. #include <fcntl.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30.  
  31. #include "misc.h"
  32. #include "errors.h"
  33. #include "config.h"
  34.  
  35.  
  36. #define PATH_SEP '%'
  37.  
  38.  
  39. char *URLToFileName(URL *Url);
  40.  
  41.  
  42. int main(int argc,char** argv)
  43. {
  44.  DIR *dir;
  45.  struct dirent* ent;
  46.  struct stat buf;
  47.  int rootp;
  48.  
  49.  /* Initialise */
  50.  
  51.  if(argc!=2)
  52.    {fprintf(stderr,"Usage: upgrade-cache <spool-dir>\n");exit(0);}
  53.  
  54.  InitErrorHandler("upgrade-cache",0,1);
  55.  
  56.  rootp=!geteuid();
  57.  
  58.  LogLevel=Inform;
  59.  
  60.  /* Change to the spool directory. */
  61.  
  62.  if(chdir(argv[1]))
  63.     PrintMessage(Fatal,"Cannot change to the spool directory [%!s]; upgrade failed.");
  64.  
  65.  /* Create the new http spool directory. */
  66.  
  67.  if(stat(".",&buf))
  68.     PrintMessage(Fatal,"Cannot stat spool directory [%!s] not upgraded.");
  69.  
  70.  if(mkdir("http",buf.st_mode&07777))
  71.     if(errno==EEXIST)
  72.        PrintMessage(Warning,"The 'http' spool directory already exists.");
  73.     else
  74.        PrintMessage(Fatal,"Cannot create 'http' spool directory [%!s]; upgrade failed.");
  75.  if(rootp && chown("http",buf.st_uid,buf.st_gid))
  76.     PrintMessage(Important,"Cannot change owner of 'http' spool directory [%!s]; check it.");
  77.  
  78.  /* Open the spool directory. */
  79.  
  80.  dir=opendir(".");
  81.  if(!dir)
  82.     PrintMessage(Fatal,"Cannot open spool directory [%!s]; upgrade failed.");
  83.  
  84.  ent=readdir(dir);  /* skip .  */
  85.  if(!ent)
  86.     PrintMessage(Fatal,"Cannot read spool directory [%!s]; upgrade failed.");
  87.  ent=readdir(dir);  /* skip .. */
  88.  
  89.  /* Go through each entry. */
  90.  
  91.  while((ent=readdir(dir)))
  92.    {
  93.     if(lstat(ent->d_name,&buf))
  94.        PrintMessage(Warning,"Cannot stat file '%s' [%!s] not upgraded.",ent->d_name);
  95.     else if(S_ISLNK(buf.st_mode))
  96.       {
  97.        char link[257];
  98.        struct stat buf2;
  99.        int n;
  100.  
  101.        n=readlink(ent->d_name,link,256);
  102.        if(n==-1)
  103.          {PrintMessage(Warning,"Cannot readlink file '%s' [%!s] not upgraded.",ent->d_name);continue;}
  104.        link[n]=0;
  105.        if(stat(link,&buf2))
  106.          {PrintMessage(Warning,"Cannot stat file '%s'->'%s' [%!s] not upgraded.",ent->d_name,link);continue;}
  107.        else if(S_ISDIR(buf2.st_mode))
  108.          {
  109.           char *new_name=(char*)malloc(strlen(ent->d_name)+8);
  110.  
  111.           PrintMessage(Inform,"Processing link '%s'",ent->d_name);
  112.  
  113.           strcpy(new_name,"http/");
  114.           strcat(new_name,ent->d_name);
  115.           if(rename(ent->d_name,new_name))
  116.              PrintMessage(Warning,"Cannot rename '%s' to '%s' [%!s] not upgraded.",ent->d_name,new_name);
  117.           free(new_name);
  118.          }
  119.       }
  120.     else if(S_ISDIR(buf.st_mode) && !strcmp(ent->d_name,"outgoing"))
  121.       {
  122.        DIR *dir2;
  123.        struct dirent* ent2;
  124.  
  125.        PrintMessage(Inform,"Processing outgoing directory");
  126.  
  127.        if(chdir(ent->d_name))
  128.          {PrintMessage(Warning,"Cannot change to outgoing directory [%!s]; not upgraded.");continue;}
  129.  
  130.        dir2=opendir(".");
  131.        if(!dir2)
  132.          {PrintMessage(Warning,"Cannot open outgoing directory [%!s]; not upgraded.");chdir("..");continue;}
  133.  
  134.        ent2=readdir(dir2);  /* skip .  */
  135.        if(!ent2)
  136.          {PrintMessage(Warning,"Cannot read outgoing directory [%!s]; not upgraded.");closedir(dir2);chdir("..");continue;}
  137.        ent2=readdir(dir2);  /* skip .. */
  138.  
  139.        while((ent2=readdir(dir2)))
  140.          {
  141.           struct stat buf2;
  142.  
  143.           if(!isdigit(ent2->d_name[0]))
  144.             {
  145.              unlink(ent2->d_name);
  146.              continue;
  147.             }
  148.  
  149.           if(stat(ent2->d_name,&buf2))
  150.              PrintMessage(Warning,"Cannot stat file 'outgoing/%s' [%!s]; not upgraded",ent2->d_name);
  151.           else if(S_ISREG(buf2.st_mode))
  152.             {
  153.              int ufd;
  154.              char *url,*new_name2,*ptr;
  155.              URL *Url;
  156.              FILE *file;
  157.  
  158.              file=fopen(ent2->d_name,"r");
  159.              ptr=fgets_realloc(NULL,file);
  160.              url=(char*)malloc(strlen(ptr));
  161.              sscanf(ptr,"%*s %s",url);
  162.              free(ptr);
  163.              fclose(file);
  164.  
  165.              Url=SplitURL(url);
  166.              new_name2=URLToFileName(Url);
  167.  
  168.              *new_name2='O';
  169.              if(rename(ent2->d_name,new_name2))
  170.                 PrintMessage(Warning,"Cannot rename outgoing request 'outgoing/%s' to 'outgoing/%s' [%!s] not upgraded.",ent2->d_name,new_name2);
  171.  
  172.              *new_name2='U';
  173.              ufd=open(new_name2,O_WRONLY|O_CREAT|O_TRUNC,0644);
  174.  
  175.              if(ufd!=-1)
  176.                {
  177.                 write_string(ufd,Url->name);
  178.                 close(ufd);
  179.                }
  180.  
  181.              free(new_name2);
  182.             }
  183.          }
  184.  
  185.        closedir(dir2);
  186.        chdir("..");
  187.       }
  188.     else if(S_ISDIR(buf.st_mode) && strcmp(ent->d_name,"http"))
  189.       {
  190.        DIR *dir2;
  191.        struct dirent* ent2;
  192.        struct utimbuf utbuf;
  193.        char *new_name=(char*)malloc(strlen(ent->d_name)+8);
  194.        char *url;
  195.        URL *Url;
  196.  
  197.        PrintMessage(Inform,"Processing directory '%s'",ent->d_name);
  198.  
  199.        strcpy(new_name,"http/");
  200.        strcat(new_name,ent->d_name);
  201.  
  202.        if(mkdir(new_name,buf.st_mode&07777))
  203.          {PrintMessage(Warning,"Cannot create '%s' spool directory [%!s]; not upgraded.",new_name);continue;}
  204.        if(rootp && chown(new_name,buf.st_uid,buf.st_gid))
  205.           PrintMessage(Important,"Cannot change owner of '%s' spool directory [%!s]; check it.",new_name);
  206.  
  207.        if(chdir(ent->d_name))
  208.          {PrintMessage(Warning,"Cannot change to directory '%s' [%!s]; not upgraded.",ent->d_name);continue;}
  209.  
  210.        dir2=opendir(".");
  211.        if(!dir2)
  212.          {PrintMessage(Warning,"Cannot open directory '%s' [%!s]; not upgraded.",ent->d_name);chdir("..");continue;}
  213.  
  214.        ent2=readdir(dir2);  /* skip .  */
  215.        if(!ent2)
  216.          {PrintMessage(Warning,"Cannot read directory '%s' [%!s]; not upgraded.",ent->d_name);closedir(dir2);chdir("..");continue;}
  217.        ent2=readdir(dir2);  /* skip .. */
  218.  
  219.        while((ent2=readdir(dir2)))
  220.          {
  221.           struct stat buf2;
  222.  
  223.           if(ent2->d_name[0] && ent2->d_name[0]==PATH_SEP &&
  224.              ent2->d_name[1] && ent2->d_name[1]==PATH_SEP)
  225.              continue;
  226.  
  227.           if(stat(ent2->d_name,&buf2))
  228.              PrintMessage(Warning,"Cannot stat file '%s/%s' [%!s]; not upgraded",ent->d_name,ent2->d_name);
  229.           else if(S_ISREG(buf2.st_mode))
  230.             {
  231.              char *path=(char*)malloc(strlen(ent2->d_name)+2),*ext,*args=NULL;
  232.              char *new_name2,*new_file;
  233.  
  234.              if(*ent2->d_name==PATH_SEP)
  235.                 strcpy(path,ent2->d_name);
  236.              else
  237.                {
  238.                 *path='/';
  239.                 strcpy(path+1,ent2->d_name);
  240.                }
  241.  
  242.              for(ext=path;*ext;ext++)
  243.                 if(*ext==PATH_SEP)
  244.                    if(*(ext+1)==PATH_SEP && *(ext+2)!=PATH_SEP)
  245.                       break;
  246.                    else
  247.                       *ext='/';
  248.  
  249.              if(*ext)
  250.                {
  251.                 int r=0,rr;
  252.                 int afd=open(ext,O_RDONLY);
  253.  
  254.                 if(afd==-1)
  255.                   {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;}
  256.  
  257.                 args=(char*)malloc(256+1);
  258.  
  259.                 while((rr=read(afd,&args[r],256)))
  260.                   {
  261.                    r+=rr;
  262.                    args=(char*)realloc(args,r+256+1);
  263.                   }
  264.  
  265.                 args[r]=0;
  266.                 close(afd);
  267.  
  268.                 if(unlink(ext))
  269.                   {PrintMessage(Warning,"Cannot delete '%s/%s' [%!s] not upgraded.",ent->d_name,ent2->d_name);continue;}
  270.  
  271.                 *ext=0;
  272.                }
  273.  
  274.              url=(char*)malloc(strlen(ent->d_name)+strlen(path)+(args?strlen(args):0)+16);
  275.              if(args)
  276.                 sprintf(url,"http://%s/%s?%s",ent->d_name,*path=='/'?path+1:path,args);
  277.              else
  278.                 sprintf(url,"http://%s/%s",ent->d_name,*path=='/'?path+1:path);
  279.              Url=SplitURL(url);
  280.  
  281.              new_file=URLToFileName(Url);
  282.              *new_file='D';
  283.  
  284.              new_name2=(char*)malloc(strlen(ent->d_name)+strlen(new_file)+16);
  285.              strcpy(new_name2,"../http/");
  286.              strcat(new_name2,ent->d_name);
  287.              strcat(new_name2,"/");
  288.              strcat(new_name2,new_file);
  289.              free(new_file);
  290.  
  291.              if(rename(ent2->d_name,new_name2))
  292.                {PrintMessage(Warning,"Cannot rename '%s/%s' to '%s' [%!s] not upgraded.",ent->d_name,ent2->d_name,new_name2+3);continue;}
  293.              else
  294.                {
  295.                 int ufd;
  296.  
  297.                 *(new_name2+9+strlen(ent->d_name))='U';
  298.                 ufd=open(new_name2,O_WRONLY|O_CREAT|O_TRUNC,0644);
  299.  
  300.                 if(ufd!=-1)
  301.                   {
  302.                    write_string(ufd,Url->name);
  303.                    close(ufd);
  304.                   }
  305.                }
  306.  
  307.              if(args)
  308.                 free(args);
  309.              free(new_name2);
  310.  
  311.              FreeURL(Url);
  312.              free(url);
  313.             }
  314.          }
  315.  
  316.        closedir(dir2);
  317.        chdir("..");
  318.  
  319.        utbuf.actime=buf.st_atime;
  320.        utbuf.modtime=buf.st_mtime;
  321.        utime(new_name,&utbuf);
  322.  
  323.        if(rmdir(ent->d_name))
  324.           PrintMessage(Warning,"Cannot unlink directory '%s' [%!s]; check it is empty.",ent->d_name);
  325.  
  326.        free(new_name);
  327.       }
  328.    }
  329.  
  330.  closedir(dir);
  331.  
  332.  return(0);
  333. }
  334.  
  335.  
  336. /*++++++++++++++++++++++++++++++++++++++
  337.   Convert a URL to a filename
  338.  
  339.   char *URLToFileName Returns the filename.
  340.  
  341.   URL *Url The URL to convert to a filename.
  342.   ++++++++++++++++++++++++++++++++++++++*/
  343.  
  344. char *URLToFileName(URL *Url)
  345. {
  346.  char *hash;
  347.  char *file;
  348.  
  349.  hash=MakeHash(Url->name);
  350.  
  351.  file=(char*)malloc(strlen(hash)+2);
  352.  
  353.  sprintf(file,"X%s",hash);
  354.  
  355.  return(file);
  356. }
  357.