File: upgrade-cache.c

package info (click to toggle)
wwwoffle 2.1c-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 900 kB
  • ctags: 715
  • sloc: ansic: 9,987; lex: 538; makefile: 196; sh: 189
file content (356 lines) | stat: -rw-r--r-- 10,399 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/***************************************
  $Header: /home/amb/wwwoffle/RCS/upgrade-cache.c 1.2 1997/10/29 20:37:33 amb Exp $

  WWWOFFLE - World Wide Web Offline Explorer - Version 2.0.
  Upgrade the cache for version 2.
  ******************/ /******************
  Written by Andrew M. Bishop

  This file Copyright 1997 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 "misc.h"
#include "errors.h"
#include "config.h"


#define PATH_SEP '%'


char *URLToFileName(URL *Url);


int main(int argc,char** argv)
{
 DIR *dir;
 struct dirent* ent;
 struct stat buf;
 int rootp;

 /* Initialise */

 if(argc!=2)
   {fprintf(stderr,"Usage: upgrade-cache <spool-dir>\n");exit(0);}

 InitErrorHandler("upgrade-cache",0,1);

 rootp=!geteuid();

 LogLevel=Inform;

 /* Change to the spool directory. */

 if(chdir(argv[1]))
    PrintMessage(Fatal,"Cannot change to the spool directory [%!s]; upgrade failed.");

 /* Create the new http spool directory. */

 if(stat(".",&buf))
    PrintMessage(Fatal,"Cannot stat spool directory [%!s] not upgraded.");

 if(mkdir("http",buf.st_mode&07777))
    if(errno==EEXIST)
       PrintMessage(Warning,"The 'http' spool directory already exists.");
    else
       PrintMessage(Fatal,"Cannot create 'http' spool directory [%!s]; upgrade failed.");
 if(rootp && chown("http",buf.st_uid,buf.st_gid))
    PrintMessage(Important,"Cannot change owner of 'http' spool directory [%!s]; check it.");

 /* Open the spool directory. */

 dir=opendir(".");
 if(!dir)
    PrintMessage(Fatal,"Cannot open spool directory [%!s]; upgrade failed.");

 ent=readdir(dir);  /* skip .  */
 if(!ent)
    PrintMessage(Fatal,"Cannot read spool directory [%!s]; upgrade failed.");
 ent=readdir(dir);  /* skip .. */

 /* Go through each entry. */

 while((ent=readdir(dir)))
   {
    if(lstat(ent->d_name,&buf))
       PrintMessage(Warning,"Cannot stat file '%s' [%!s] not upgraded.",ent->d_name);
    else if(S_ISLNK(buf.st_mode))
      {
       char link[257];
       struct stat buf2;
       int n;

       n=readlink(ent->d_name,link,256);
       if(n==-1)
         {PrintMessage(Warning,"Cannot readlink file '%s' [%!s] not upgraded.",ent->d_name);continue;}
       link[n]=0;
       if(stat(link,&buf2))
         {PrintMessage(Warning,"Cannot stat file '%s'->'%s' [%!s] not upgraded.",ent->d_name,link);continue;}
       else if(S_ISDIR(buf2.st_mode))
         {
          char *new_name=(char*)malloc(strlen(ent->d_name)+8);

          PrintMessage(Inform,"Processing link '%s'",ent->d_name);

          strcpy(new_name,"http/");
          strcat(new_name,ent->d_name);
          if(rename(ent->d_name,new_name))
             PrintMessage(Warning,"Cannot rename '%s' to '%s' [%!s] not upgraded.",ent->d_name,new_name);
          free(new_name);
         }
      }
    else if(S_ISDIR(buf.st_mode) && !strcmp(ent->d_name,"outgoing"))
      {
       DIR *dir2;
       struct dirent* ent2;

       PrintMessage(Inform,"Processing outgoing directory");

       if(chdir(ent->d_name))
         {PrintMessage(Warning,"Cannot change to outgoing directory [%!s]; not upgraded.");continue;}

       dir2=opendir(".");
       if(!dir2)
         {PrintMessage(Warning,"Cannot open outgoing directory [%!s]; not upgraded.");chdir("..");continue;}

       ent2=readdir(dir2);  /* skip .  */
       if(!ent2)
         {PrintMessage(Warning,"Cannot read outgoing directory [%!s]; not upgraded.");closedir(dir2);chdir("..");continue;}
       ent2=readdir(dir2);  /* skip .. */

       while((ent2=readdir(dir2)))
         {
          struct stat buf2;

          if(!isdigit(ent2->d_name[0]))
            {
             unlink(ent2->d_name);
             continue;
            }

          if(stat(ent2->d_name,&buf2))
             PrintMessage(Warning,"Cannot stat file 'outgoing/%s' [%!s]; not upgraded",ent2->d_name);
          else if(S_ISREG(buf2.st_mode))
            {
             int ufd;
             char *url,*new_name2,*ptr;
             URL *Url;
             FILE *file;

             file=fopen(ent2->d_name,"r");
             ptr=fgets_realloc(NULL,file);
             url=(char*)malloc(strlen(ptr));
             sscanf(ptr,"%*s %s",url);
             free(ptr);
             fclose(file);

             Url=SplitURL(url);
             new_name2=URLToFileName(Url);

             *new_name2='O';
             if(rename(ent2->d_name,new_name2))
                PrintMessage(Warning,"Cannot rename outgoing request 'outgoing/%s' to 'outgoing/%s' [%!s] not upgraded.",ent2->d_name,new_name2);

             *new_name2='U';
             ufd=open(new_name2,O_WRONLY|O_CREAT|O_TRUNC,0644);

             if(ufd!=-1)
               {
                write_string(ufd,Url->name);
                close(ufd);
               }

             free(new_name2);
            }
         }

       closedir(dir2);
       chdir("..");
      }
    else if(S_ISDIR(buf.st_mode) && strcmp(ent->d_name,"http"))
      {
       DIR *dir2;
       struct dirent* ent2;
       struct utimbuf utbuf;
       char *new_name=(char*)malloc(strlen(ent->d_name)+8);
       char *url;
       URL *Url;

       PrintMessage(Inform,"Processing directory '%s'",ent->d_name);

       strcpy(new_name,"http/");
       strcat(new_name,ent->d_name);

       if(mkdir(new_name,buf.st_mode&07777))
         {PrintMessage(Warning,"Cannot create '%s' spool directory [%!s]; not upgraded.",new_name);continue;}
       if(rootp && chown(new_name,buf.st_uid,buf.st_gid))
          PrintMessage(Important,"Cannot change owner of '%s' spool directory [%!s]; check it.",new_name);

       if(chdir(ent->d_name))
         {PrintMessage(Warning,"Cannot change to directory '%s' [%!s]; not upgraded.",ent->d_name);continue;}

       dir2=opendir(".");
       if(!dir2)
         {PrintMessage(Warning,"Cannot open directory '%s' [%!s]; not upgraded.",ent->d_name);chdir("..");continue;}

       ent2=readdir(dir2);  /* skip .  */
       if(!ent2)
         {PrintMessage(Warning,"Cannot read directory '%s' [%!s]; not upgraded.",ent->d_name);closedir(dir2);chdir("..");continue;}
       ent2=readdir(dir2);  /* skip .. */

       while((ent2=readdir(dir2)))
         {
          struct stat buf2;

          if(ent2->d_name[0] && ent2->d_name[0]==PATH_SEP &&
             ent2->d_name[1] && ent2->d_name[1]==PATH_SEP)
             continue;

          if(stat(ent2->d_name,&buf2))
             PrintMessage(Warning,"Cannot stat file '%s/%s' [%!s]; not upgraded",ent->d_name,ent2->d_name);
          else if(S_ISREG(buf2.st_mode))
            {
             char *path=(char*)malloc(strlen(ent2->d_name)+2),*ext,*args=NULL;
             char *new_name2,*new_file;

             if(*ent2->d_name==PATH_SEP)
                strcpy(path,ent2->d_name);
             else
               {
                *path='/';
                strcpy(path+1,ent2->d_name);
               }

             for(ext=path;*ext;ext++)
                if(*ext==PATH_SEP)
                   if(*(ext+1)==PATH_SEP && *(ext+2)!=PATH_SEP)
                      break;
                   else
                      *ext='/';

             if(*ext)
               {
                int r=0,rr;
                int afd=open(ext,O_RDONLY);

                if(afd==-1)
                  {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;}

                args=(char*)malloc(256+1);

                while((rr=read(afd,&args[r],256)))
                  {
                   r+=rr;
                   args=(char*)realloc(args,r+256+1);
                  }

                args[r]=0;
                close(afd);

                if(unlink(ext))
                  {PrintMessage(Warning,"Cannot delete '%s/%s' [%!s] not upgraded.",ent->d_name,ent2->d_name);continue;}

                *ext=0;
               }

             url=(char*)malloc(strlen(ent->d_name)+strlen(path)+(args?strlen(args):0)+16);
             if(args)
                sprintf(url,"http://%s/%s?%s",ent->d_name,*path=='/'?path+1:path,args);
             else
                sprintf(url,"http://%s/%s",ent->d_name,*path=='/'?path+1:path);
             Url=SplitURL(url);

             new_file=URLToFileName(Url);
             *new_file='D';

             new_name2=(char*)malloc(strlen(ent->d_name)+strlen(new_file)+16);
             strcpy(new_name2,"../http/");
             strcat(new_name2,ent->d_name);
             strcat(new_name2,"/");
             strcat(new_name2,new_file);
             free(new_file);

             if(rename(ent2->d_name,new_name2))
               {PrintMessage(Warning,"Cannot rename '%s/%s' to '%s' [%!s] not upgraded.",ent->d_name,ent2->d_name,new_name2+3);continue;}
             else
               {
                int ufd;

                *(new_name2+9+strlen(ent->d_name))='U';
                ufd=open(new_name2,O_WRONLY|O_CREAT|O_TRUNC,0644);

                if(ufd!=-1)
                  {
                   write_string(ufd,Url->name);
                   close(ufd);
                  }
               }

             if(args)
                free(args);
             free(new_name2);

             FreeURL(Url);
             free(url);
            }
         }

       closedir(dir2);
       chdir("..");

       utbuf.actime=buf.st_atime;
       utbuf.modtime=buf.st_mtime;
       utime(new_name,&utbuf);

       if(rmdir(ent->d_name))
          PrintMessage(Warning,"Cannot unlink directory '%s' [%!s]; check it is empty.",ent->d_name);

       free(new_name);
      }
   }

 closedir(dir);

 return(0);
}


/*++++++++++++++++++++++++++++++++++++++
  Convert a URL to a filename

  char *URLToFileName Returns the filename.

  URL *Url The URL to convert to a filename.
  ++++++++++++++++++++++++++++++++++++++*/

char *URLToFileName(URL *Url)
{
 char *hash;
 char *file;

 hash=MakeHash(Url->name);

 file=(char*)malloc(strlen(hash)+2);

 sprintf(file,"X%s",hash);

 return(file);
}