1 // Larbin
2 // Sebastien Ailleret
3 // 07-12-01 -> 10-12-01
4
5 #include <iostream>
6 #include <cstring>
7 #include <unistd.h>
8
9 #include "options.h"
10
11 #include "types.h"
12 #include "global.h"
13 #include "fetch/file.h"
14 #include "utils/text.h"
15 #include "utils/debug.h"
16 #include "interf/output.h"
17
18 static char *fileName;
19 static uint endFileName;
20
21 /** A page has been loaded successfully
22 * @param page the page that has been fetched
23 */
loaded(html * page)24 void loaded (html *page) {
25 // get file name and create needed directories
26 url *u = page->getUrl();
27 uint p = u->getPort();
28 char *h = u->getHost();
29 char *f = u->getFile();
30 // update dir name
31 uint d = u->hostHashCode() % nbDir;
32 for (int i=2; i<7; i++) {
33 fileName[endFileName-i] = d % 10 + '0';
34 d /= 10;
35 }
36 // set file name
37 uint len = endFileName;
38 if (p == 80)
39 len += sprintf(fileName+endFileName, "%s%s", h, f);
40 else
41 len += sprintf(fileName+endFileName, "%s:%u%s", h, p, f);
42 // make sure the path of the file exists
43 bool cont = true;
44 struct stat st;
45 while (cont) {
46 len--;
47 while (fileName[len] != '/') len--;
48 fileName[len] = 0;
49 cont = stat(fileName, &st); // this becomes true at least for saveDir
50 fileName[len] = '/';
51 }
52 cont = true;
53 while (cont) {
54 len++;
55 while (fileName[len] != '/' && fileName[len] != 0) len++;
56 if (fileName[len] == '/') {
57 fileName[len] = 0;
58 if (mkdir(fileName, S_IRWXU) != 0) perror("trouble while creating dir");
59 fileName[len] = '/';
60 } else { // fileName[len] == 0
61 cont = false;
62 }
63 }
64 if (fileName[len-1] == '/') {
65 strcpy(fileName+len, indexFile);
66 }
67 // open fds and write file
68 int fd = creat(fileName, S_IRWXU);
69 if (fd >= 0) {
70 // some url mysteries might prevent this from being possible
71 // ex: if http://bbs.computoredge.com/ceo
72 // and http://bbs.computoredge.com/ceo/ both exist
73 ecrireBuff(fd, page->getPage(), page->getLength());
74 close(fd);
75 }
76 }
77
78 /** The fetch failed
79 * @param u the URL of the doc
80 * @param reason reason of the fail
81 */
failure(url * u,FetchError reason)82 void failure (url *u, FetchError reason) {
83 }
84
85 /** initialisation function
86 */
initUserOutput()87 void initUserOutput () {
88 mkdir(saveDir, S_IRWXU);
89 endFileName = strlen(saveDir);
90 fileName = new char[endFileName+maxUrlSize+50];
91 strcpy(fileName, saveDir);
92 if (fileName[endFileName-1] != '/') fileName[endFileName++] = '/';
93 strcpy(fileName+endFileName, "d00000/");
94 endFileName += 7; // indique le premier char a ecrire
95 }
96
97 /** stats, called in particular by the webserver
98 * the webserver is in another thread, so be careful
99 * However, if it only reads things, it is probably not useful
100 * to use mutex, because incoherence in the webserver is not as critical
101 * as efficiency
102 */
outputStats(int fds)103 void outputStats(int fds) {
104 ecrire(fds, "Nothing to declare");
105 }
106