File: update_links.c

package info (click to toggle)
pavuk 0.9.34-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,744 kB
  • ctags: 3,826
  • sloc: ansic: 51,740; sh: 3,405; makefile: 363
file content (158 lines) | stat: -rw-r--r-- 3,293 bytes parent folder | download | duplicates (5)
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
/***************************************************************************/
/*    This code is part of WWW grabber called pavuk                        */
/*    Copyright (c) 1997 - 2001 Stefan Ondrejicka                          */
/*    Distributed under GPL 2 or later                                     */
/***************************************************************************/

#include "config.h"

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <dirent.h>
#include <limits.h>
#include <utime.h>

#include "tools.h"
#include "url.h"
#include "doc.h"
#include "html.h"
#include "gui_api.h"
#include "update_links.h"

static void rewrite_links(char *);

void update_links(char *dirname)
{
  DIR *dir;
  struct dirent *dent;
  char next_dir[PATH_MAX];
  struct stat estat;

  xprintf(1, gettext("Entering directory %s\n"), dirname);
  if(!(dir = opendir(dirname)))
  {
    xperror(dirname);
    return;
  }

  while((dent = readdir(dir)))
  {
    _Xt_Serve;

    sprintf(next_dir, "%s/%s", dirname, dent->d_name);
    if(!strcmp(dent->d_name, "."))
      continue;
    if(!strcmp(dent->d_name, ".."))
      continue;
    if(stat(next_dir, &estat))
    {
      xperror(next_dir);
      continue;
    }

    if(S_ISDIR(estat.st_mode))
    {
      if(!strcmp(dent->d_name, ".pavuk_info") && cfg.enable_info)
        continue;

      update_links(next_dir);
    }
    else if(file_is_html(next_dir))
    {
      xprintf(1, gettext("Relocating %s\n"), next_dir);
      rewrite_links(next_dir);
    }
    else
      xprintf(1, gettext("Omitting %s\n"), next_dir);
#ifdef I_FACE
    if(cfg.xi_face)
    {
      if(cfg.rbreak || cfg.stop)
      {
        closedir(dir);
        return;
      }
    }
#endif
  }

  closedir(dir);
  xprintf(1, gettext("Leaving directory %s\n"), dirname);
}

static void rewrite_links(char *fn)
{
  char pom[2048];
  char *savetmp, *p;
  int sock;
  doc pdoc;
  struct stat estat;
  struct utimbuf ut;
  url dum;

  if(stat(fn, &estat) == 0)
  {
    if(S_ISDIR(estat.st_mode))
    {
      xprintf(1, gettext("Can't open directory %s\n"), fn);
      return;
    }
  }

  ut.actime = estat.st_atime;
  ut.modtime = estat.st_mtime;

  memset(&dum, '\0', sizeof(url));
  dum.type = URLT_FILE;
  dum.p.file.filename = fn;
  dum.local_name = fn;
  if(!strcasecmp(tl_get_extension(fn), "css"))
    dum.status = URL_STYLE;
  else
    dum.status = 0;

  doc_init(&pdoc, &dum);

  doc_download(&pdoc, 1, TRUE);

  _free(pdoc.mime);

  html_process_parent_document(&pdoc, NULL, NULL);

  strcpy(pom, fn);
  p = strrchr(pom, '/');
  sprintf(p + 1, "._lnkupd%d", (int) getpid());
  savetmp = tl_strdup(pom);
  rename(fn, savetmp);

  if((sock = open(fn, O_BINARY | O_TRUNC | O_CREAT | O_WRONLY, 0644)) < 0)
  {
    xperror(fn);
    rename(savetmp, fn);
    free(savetmp);
    free(pdoc.contents);
    doc_remove_lock(&pdoc);
    return;
  }
  if(write(sock, pdoc.contents, pdoc.size) != pdoc.size)
  {
    xperror(fn);
    close(sock);
    rename(savetmp, fn);
    free(savetmp);
    free(pdoc.contents);
    doc_remove_lock(&pdoc);
    return;
  }
  close(sock);
  utime(fn, &ut);
  unlink(savetmp);
  free(savetmp);
  free(pdoc.contents);
  doc_remove_lock(&pdoc);
}