1 // Larbin
2 // Sebastien Ailleret
3 // 07-12-01 -> 07-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 
19 static int nbdir = -1;
20 static int nbfile = filesPerDir;
21 static char buf[maxUrlSize+30];
22 static char *fileName;
23 static int indexFds = -1;
24 static uint endFileName;
25 
26 /** A page has been loaded successfully, save it to disk
27  * @param page the page that has been fetched
28  */
loaded(html * page)29 void loaded (html *page) {
30   nbfile++;
31   if (nbfile < filesPerDir) {
32     int tmp = nbfile;
33     int pos = endFileName;
34     while (tmp != 0) {
35       fileName[pos--] = '0' + (tmp % 10);
36       tmp /= 10;
37     }
38   } else { // new dir
39     // create the directory
40     nbdir++; nbfile = 0;
41     int pos = endFileName-7;
42     int tmp = nbdir;
43     while (tmp != 0) {
44       fileName[pos--] = '0' + (tmp % 10);
45       tmp /= 10;
46     }
47     fileName[endFileName-6] = 0;
48     if (mkdir(fileName, S_IRWXU) != 0) perror("trouble while creating dir");
49     fileName[endFileName-6] = '/';
50     // open new index
51     close(indexFds);
52     strcpy(fileName+endFileName-5, "index");
53     indexFds = creat(fileName, S_IRWXU);
54     if (indexFds < 0) {
55       std::cerr << "cannot open file " << fileName << "\n";
56       exit(1);
57     }
58     // new filename
59     fileName[endFileName-5] = 'f';
60     for (uint i=endFileName; i>endFileName-5; i--) fileName[i]='0';
61   }
62   int fd = creat(fileName, S_IRWXU);
63   if (fd < 0) {
64     std::cerr << "cannot open file " << fileName << "\n";
65     exit(1);
66   }
67   int s=0;
68   s = sprintf(buf, "%4u ", nbfile);
69 #ifdef URL_TAGS
70   s += sprintf(buf+s, "(%u) ", page->getUrl()->tag);
71 #endif // URL_TAGS
72   s += page->getUrl()->writeUrl(buf+s);
73   buf[s++] = '\n';
74   ecrireBuff(indexFds, buf, s);
75   ecrireBuff(fd, page->getPage(), page->getLength());
76   close(fd);
77 }
78 
79 /** The fetch failed
80  * @param u the URL of the doc
81  * @param reason reason of the fail
82  */
failure(url * u,FetchError reason)83 void failure (url *u, FetchError reason) {
84   // do nothing
85 }
86 
87 /** initialisation function
88  */
initUserOutput()89 void initUserOutput () {
90   mkdir(saveDir, S_IRWXU);
91   endFileName = strlen(saveDir);
92   fileName = new char[endFileName+maxUrlSize+50];
93   strcpy(fileName, saveDir);
94   if (fileName[endFileName-1] != '/') fileName[endFileName++] = '/';
95   strcpy(fileName+endFileName, "d00000/f00000");
96   endFileName += 12; // indique le dernier char ecrit
97 }
98 
99 /** stats, called in particular by the webserver
100  * the webserver is in another thread, so be careful
101  * However, if it only reads things, it is probably not useful
102  * to use mutex, because incoherence in the webserver is not as critical
103  * as efficiency
104  */
outputStats(int fds)105 void outputStats(int fds) {
106   ecrire(fds, "Nothing to declare");
107 }
108