wwwoffle-tools.c
上传用户:seven77cht
上传日期:2007-01-04
资源大小:486k
文件大小:10k
- /***************************************
- $Header: /home/amb/wwwoffle/RCS/wwwoffle-tools.c 1.16 1999/12/18 12:27:03 amb Exp $
- WWWOFFLE - World Wide Web Offline Explorer - Version 2.5.
- Tools for use in the cache for version 2.x.
- ******************/ /******************
- Written by Andrew M. Bishop
- This file Copyright 1997,98,99 Andrew M. Bishop
- It may be distributed under the GNU Public License, version 2, or
- any higher version. See section COPYING of the GNU Public license
- for conditions under which this file may be redistributed.
- ***************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <time.h>
- #include <utime.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <errno.h>
- #include "wwwoffle.h"
- #include "misc.h"
- #include "errors.h"
- #include "config.h"
- #ifndef SPOOL_DIR
- #define SPOOL_DIR DEF_SPOOL
- #endif
- /*+ Need this for Win32 to use binary mode +*/
- #ifndef O_BINARY
- #define O_BINARY 0
- #endif
- #define LS 1
- #define LS_SPECIAL 2
- #define MV 3
- #define RM 4
- #define READ 5
- #define WRITE 6
- static void wwwoffle_ls(URL *Url);
- static void wwwoffle_ls_special(char *name);
- static void wwwoffle_mv(URL *Url1,URL *Url2);
- static void wwwoffle_rm(URL *Url);
- static void wwwoffle_read(URL *Url);
- static void wwwoffle_write(URL *Url);
- static void ls(char *file);
- /*++++++++++++++++++++++++++++++++++++++
- The main program
- ++++++++++++++++++++++++++++++++++++++*/
- int main(int argc,char **argv)
- {
- struct stat buf;
- URL *Url[2];
- int mode=0;
- int i;
- char *p;
- p=argv[0]+strlen(argv[0])-1;
- while(p>=argv[0] && *p!='/')
- p--;
- p++;
- if(!strcmp(p,"wwwoffle-ls"))
- mode=LS;
- else if(!strcmp(p,"wwwoffle-mv"))
- mode=MV;
- else if(!strcmp(p,"wwwoffle-rm"))
- mode=RM;
- else if(!strcmp(p,"wwwoffle-read"))
- mode=READ;
- else if(!strcmp(p,"wwwoffle-write"))
- mode=WRITE;
- else
- {
- fprintf(stderr,"Program must be started as: wwwoffle-ls, wwwoffle-mv, wwwoffle-rmn");
- fprintf(stderr," wwwoffle-read or wwwoffle-writen");
- exit(1);
- }
- /* Initialise */
- if(mode==LS && argc!=2)
- {fprintf(stderr,"Usage: wwwoffle-ls ( <dir>/<subdir> | <protocol>://<host> | <URL> | n"
- " outgoing | monitor | lasttime | prevtime[0-9] )n");exit(0);}
- else if(mode==MV && argc!=3)
- {fprintf(stderr,"Usage: wwwoffle-mv (<dir1>/<subdir1> | <protocol1>://<host1>)n"
- " (<dir2>/<subdir2> | <protocol2>://<host2>)n");exit(0);}
- else if(mode==RM && argc!=2)
- {fprintf(stderr,"Usage: wwwoffle-rm <URL>n");exit(0);}
- else if(mode==READ && argc!=2)
- {fprintf(stderr,"Usage: wwwoffle-read <URL>n");exit(0);}
- else if(mode==WRITE && argc!=2)
- {fprintf(stderr,"Usage: wwwoffle-write <URL>n");exit(0);}
- if(stat("outgoing",&buf) || !S_ISDIR(buf.st_mode))
- {
- chdir(SPOOL_DIR);
- if(stat("outgoing",&buf) || !S_ISDIR(buf.st_mode))
- {fprintf(stderr,"The wwwoffle-tools programs must be started from the spool directoryn"
- "There is no 'outgoing' directory here so it can't be right.n");exit(1);}
- }
- InitErrorHandler(p,0,1);
- umask(0);
- /* Get the arguments */
- for(i=1;i<argc;i++)
- {
- char *colon=strchr(argv[i],':');
- char *slash=strchr(argv[i],'/');
- if(mode==LS && (!strcmp(argv[i],"outgoing") || !strcmp(argv[i],"monitor") ||
- !strcmp(argv[i],"lasttime") || (!strncmp(argv[i],"prevtime",8) && isdigit(argv[i][8]))))
- {
- mode=LS_SPECIAL;
- }
- else if(colon && slash && colon<slash)
- {
- Url[i-1]=SplitURL(argv[i]);
- }
- else
- {
- char *slash,*url;
- slash=strchr(argv[i],'/');
- if(!slash)
- {fprintf(stderr,"Cannot parse the argument '%s'n",argv[i]);exit(1);}
- *slash=0;
- url=(char*)malloc(strlen(slash+1)+strlen(argv[i])+8);
- sprintf(url,"%s://%s",argv[i],slash+1);
- Url[i-1]=SplitURL(url);
- }
- }
- if(mode==LS)
- wwwoffle_ls(Url[0]);
- else if(mode==LS_SPECIAL)
- wwwoffle_ls_special(argv[1]);
- else if(mode==MV)
- wwwoffle_mv(Url[0],Url[1]);
- else if(mode==RM)
- wwwoffle_rm(Url[0]);
- else if(mode==READ)
- wwwoffle_read(Url[0]);
- else if(mode==WRITE)
- wwwoffle_write(Url[0]);
- exit(0);
- }
- /*++++++++++++++++++++++++++++++++++++++
- List the URLs within a directory of the cache.
- URL *Url The URL to list.
- ++++++++++++++++++++++++++++++++++++++*/
- static void wwwoffle_ls(URL *Url)
- {
- if(chdir(Url->proto))
- {PrintMessage(Warning,"Cannot change to directory '%s' [%!s].",Url->proto);return;}
- if(chdir(Url->dir))
- {PrintMessage(Warning,"Cannot change to directory '%s/%s' [%!s].",Url->proto,Url->dir);chdir("..");return;}
- if(strcmp(Url->path,"/"))
- {
- char *name=URLToFileName(Url);
- *name='D';
- ls(name);
- free(name);
- }
- else
- {
- struct dirent* ent;
- DIR *dir=opendir(".");
- if(!dir)
- {PrintMessage(Warning,"Cannot open current directory '%s/%s' [%!s].",Url->proto,Url->dir);chdir("../..");return;}
- ent=readdir(dir); /* skip . */
- if(!ent)
- {PrintMessage(Warning,"Cannot read current directory '%s/%s' [%!s].",Url->proto,Url->dir);closedir(dir);chdir("../..");return;}
- ent=readdir(dir); /* skip .. */
- while((ent=readdir(dir)))
- {
- if(*ent->d_name=='D' && ent->d_name[strlen(ent->d_name)-1]!='~')
- ls(ent->d_name);
- }
- closedir(dir);
- }
- chdir("../..");
- }
- /*++++++++++++++++++++++++++++++++++++++
- List the URLs within the outgoing, monitor or lasttime/prevtime special directory of the cache.
- char *name The name of the directory to list.
- ++++++++++++++++++++++++++++++++++++++*/
- static void wwwoffle_ls_special(char *name)
- {
- struct dirent* ent;
- DIR *dir;
- if(chdir(name))
- {PrintMessage(Warning,"Cannot change to directory '%s' [%!s].",name);return;}
- dir=opendir(".");
- if(!dir)
- {PrintMessage(Warning,"Cannot open current directory '%s' [%!s].",name);chdir("..");return;}
- ent=readdir(dir); /* skip . */
- if(!ent)
- {PrintMessage(Warning,"Cannot read current directory '%s' [%!s].",name);closedir(dir);chdir("..");return;}
- ent=readdir(dir); /* skip .. */
- while((ent=readdir(dir)))
- {
- if((*ent->d_name=='D' || *ent->d_name=='O') && ent->d_name[strlen(ent->d_name)-1]!='~')
- ls(ent->d_name);
- }
- closedir(dir);
- chdir("..");
- }
- /*++++++++++++++++++++++++++++++++++++++
- List one file.
- char *file The name of the file to ls.
- ++++++++++++++++++++++++++++++++++++++*/
- static void ls(char *file)
- {
- static char *month[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
- struct stat buf;
- time_t now=-1;
- if(now==-1)
- now=time(NULL);
- if(stat(file,&buf))
- {PrintMessage(Warning,"Cannot stat the file '%s' [%!s].",file);return;}
- else
- {
- char *url=FileNameToURL(file);
- if(url)
- {
- struct tm *tim=localtime(&buf.st_mtime);
- if(buf.st_mtime<(now-(180*24*3600)))
- printf("%s %7ld %3s %2d %d %sn",file,(long)buf.st_size,month[tim->tm_mon],tim->tm_mday,tim->tm_year+1900,url);
- else
- printf("%s %7ld %3s %2d %2d:%02d %sn",file,(long)buf.st_size,month[tim->tm_mon],tim->tm_mday,tim->tm_hour,tim->tm_min,url);
- free(url);
- }
- }
- }
- /*++++++++++++++++++++++++++++++++++++++
- Move one URL or host to another.
- URL *Url1 The source URL.
- URL *Url2 The destination URL.
- ++++++++++++++++++++++++++++++++++++++*/
- static void wwwoffle_mv(URL *Url1,URL *Url2)
- {
- struct dirent* ent;
- DIR *dir;
- if(chdir(Url1->proto))
- {PrintMessage(Warning,"Cannot change to directory '%s' [%!s].",Url1->proto);return;}
- if(chdir(Url1->dir))
- {PrintMessage(Warning,"Cannot change to directory '%s/%s' [%!s].",Url1->proto,Url1->dir);chdir("..");return;}
- dir=opendir(".");
- if(!dir)
- {PrintMessage(Warning,"Cannot open current directory '%s/%s' [%!s].",Url1->proto,Url1->dir);chdir("../..");return;}
- ent=readdir(dir); /* skip . */
- if(!ent)
- {PrintMessage(Warning,"Cannot read current directory '%s/%s' [%!s].",Url1->proto,Url1->dir);closedir(dir);chdir("../..");return;}
- ent=readdir(dir); /* skip .. */
- while((ent=readdir(dir)))
- {
- if(*ent->d_name=='D')
- {
- char *url1=FileNameToURL(ent->d_name);
- if(url1)
- {
- char *url2;
- URL *Url;
- char *path2,*name1,*name2;
- int fd2;
- Url=SplitURL(url1);
- url2=(char*)malloc(strlen(Url->pathp)+strlen(Url2->dir)+strlen(Url2->proto)+8);
- sprintf(url2,"%s://%s%s",Url2->proto,Url2->dir,Url->pathp);
- FreeURL(Url);
- name1=ent->d_name;
- Url=SplitURL(url2);
- name2=URLToFileName(Url);
- path2=(char*)malloc(strlen(Url2->proto)+strlen(Url2->dir)+strlen(name2)+16);
- sprintf(path2,"../../%s",Url2->proto);
- mkdir(path2,DEF_DIR_PERM);
- sprintf(path2,"../../%s/%s",Url2->proto,Url2->dir);
- mkdir(path2,DEF_DIR_PERM);
- *name1=*name2='D';
- sprintf(path2,"../../%s/%s/%s",Url2->proto,Url2->dir,name2);
- rename(name1,path2);
- *name1=*name2='U';
- sprintf(path2,"../../%s/%s/%s",Url2->proto,Url2->dir,name2);
- fd2=open(path2,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,DEF_FILE_PERM);
- write_string(fd2,Url->name);
- close(fd2);
- unlink(name2);
- free(url1);
- free(url2);
- free(name2);
- free(path2);
- FreeURL(Url);
- }
- }
- }
- closedir(dir);
- chdir("../..");
- }
- /*++++++++++++++++++++++++++++++++++++++
- Delete a URL.
- URL *Url The URL to delete.
- ++++++++++++++++++++++++++++++++++++++*/
- static void wwwoffle_rm(URL *Url)
- {
- DeleteWebpageSpoolFile(Url,0);
- }
- /*++++++++++++++++++++++++++++++++++++++
- Read a URL and output on stdout.
- URL *Url The URL to read.
- ++++++++++++++++++++++++++++++++++++++*/
- static void wwwoffle_read(URL *Url)
- {
- char buffer[READ_BUFFER_SIZE];
- int n,spool=OpenWebpageSpoolFile(1,Url);
- if(spool==-1)
- return;
- init_buffer(spool);
- while((n=read_data(spool,buffer,READ_BUFFER_SIZE))>0)
- write_data(1,buffer,n);
- close(spool);
- }
- /*++++++++++++++++++++++++++++++++++++++
- Write a URL from the input on stdin.
- URL *Url The URL to write.
- ++++++++++++++++++++++++++++++++++++++*/
- static void wwwoffle_write(URL *Url)
- {
- char buffer[READ_BUFFER_SIZE];
- int n,spool=OpenWebpageSpoolFile(0,Url);
- if(spool==-1)
- return;
- init_buffer(0);
- while((n=read_data(0,buffer,READ_BUFFER_SIZE))>0)
- write_data(spool,buffer,n);
- close(spool);
- }