Switch to unified view

a/src/internfile/internfile.cpp b/src/internfile/internfile.cpp
1
#ifndef lint
1
#ifndef lint
2
static char rcsid[] = "@(#$Id: internfile.cpp,v 1.3 2005-03-17 15:35:49 dockes Exp $ (C) 2004 J.F.Dockes";
2
static char rcsid[] = "@(#$Id: internfile.cpp,v 1.4 2005-03-25 09:40:27 dockes Exp $ (C) 2004 J.F.Dockes";
3
#endif
3
#endif
4
#include <unistd.h>
4
#include <unistd.h>
5
#include <sys/types.h>
5
#include <sys/types.h>
6
#include <sys/stat.h>
6
#include <sys/stat.h>
7
#include <errno.h>
7
#include <errno.h>
...
...
68
    if (!tfile.empty() && tfile[tfile.length() - 1] == '\n')
68
    if (!tfile.empty() && tfile[tfile.length() - 1] == '\n')
69
    tfile.erase(tfile.length() - 1, 1);
69
    tfile.erase(tfile.length() - 1, 1);
70
    return true;
70
    return true;
71
}
71
}
72
72
73
static void tmpcleanup(const string& tdir, const string& tfile)
73
void FileInterner::tmpcleanup()
74
{
74
{
75
    if (tdir.empty() || tfile.empty())
75
    if (tdir.empty() || tfile.empty())
76
    return;
76
    return;
77
    if (unlink(tfile.c_str()) < 0) {
77
    if (unlink(tfile.c_str()) < 0) {
78
    LOGERR(("tmpcleanup: unlink(%s) errno %d\n", tfile.c_str(), 
78
    LOGERR(("FileInterner::tmpcleanup: unlink(%s) errno %d\n", 
79
      errno));
79
      tfile.c_str(), errno));
80
    return;
80
    return;
81
    }
81
    }
82
}
82
}
83
83
84
bool internfile(const std::string &ifn, RclConfig *config, Rcl::Doc& doc,
84
// Handler==0 on return says we're in error
85
FileInterner::FileInterner(const std::string &f, RclConfig *cnf, 
85
        const string& tdir)
86
             const string& td)
87
    : fn(f), config(cnf), tdir(td), handler(0) 
86
{
88
{
87
    string fn = ifn;
88
    string tfile;
89
    MimeHandler *handler = 0;
90
    bool ret = false;
91
92
    string mime = mimetype(fn, config->getMimeMap());
89
    mime = mimetype(fn, config->getMimeMap());
93
    if (mime.empty()) {
90
    if (mime.empty()) {
94
    // No mime type: not listed in our map.
91
    // No mime type: not listed in our map.
95
    LOGDEB(("internfile: (no mime) [%s]\n", fn.c_str()));
92
    LOGDEB(("FileInterner::FileInterner: (no mime) [%s]\n", fn.c_str()));
96
    return false;
93
    return;
97
    }
94
    }
98
95
99
    // First check for a compressed file. If so, create a temporary
96
    // First check for a compressed file. If so, create a temporary
100
    // uncompressed file, and rerun the mime type identification, then do the
97
    // uncompressed file, and rerun the mime type identification, then do the
101
    // rest with the temp file.
98
    // rest with the temp file.
102
    list<string>ucmd;
99
    list<string>ucmd;
103
    if (getUncompressor(mime, config->getMimeConf(), ucmd)) {
100
    if (getUncompressor(mime, config->getMimeConf(), ucmd)) {
104
    if (!uncompressfile(config, fn, ucmd, tdir, tfile)) 
101
    if (!uncompressfile(config, fn, ucmd, tdir, tfile)) {
105
        return false;
102
        return;
103
  }
106
    LOGDEB(("internfile: after ucomp: tdir %s, tfile %s\n", 
104
    LOGDEB(("internfile: after ucomp: tdir %s, tfile %s\n", 
107
        tdir.c_str(), tfile.c_str()));
105
        tdir.c_str(), tfile.c_str()));
108
    fn = tfile;
106
    fn = tfile;
109
    mime = mimetype(fn, config->getMimeMap());
107
    mime = mimetype(fn, config->getMimeMap());
110
    if (mime.empty()) {
108
    if (mime.empty()) {
111
        // No mime type ?? pass on.
109
        // No mime type ?? pass on.
112
        LOGDEB(("internfile: (no mime) [%s]\n", fn.c_str()));
110
        LOGDEB(("internfile: (no mime) [%s]\n", fn.c_str()));
113
      goto out;
111
      return;
114
    }
112
    }
115
116
    }
113
    }
117
    
118
114
119
    // Look for appropriate handler
115
    // Look for appropriate handler
120
    handler = getMimeHandler(mime, config->getMimeConf());
116
    handler = getMimeHandler(mime, config->getMimeConf());
121
    if (!handler) {
117
    if (!handler) {
122
    // No handler for this type, for now :(
118
    // No handler for this type, for now :(
123
    LOGDEB(("internfile: %s : no handler\n", mime.c_str()));
119
    LOGDEB(("FileInterner::FileInterner: %s: no handler\n", mime.c_str()));
124
  goto out;
120
  return;
125
    }
121
    }
126
122
127
    LOGDEB(("internfile: %s [%s]\n", mime.c_str(), fn.c_str()));
123
    LOGDEB(("FileInterner::FileInterner: %s [%s]\n",mime.c_str(), fn.c_str()));
124
}
125
126
FileInterner::Status FileInterner::internfile(Rcl::Doc& doc, string& ipath)
127
{
128
    if (!handler)
129
  return FIError;
128
130
129
    // Turn file into a document. The document has fields for title, body 
131
    // Turn file into a document. The document has fields for title, body 
130
    // etc.,  all text converted to utf8
132
    // etc.,  all text converted to utf8
131
    if (!handler->worker(config, fn,  mime, doc)) {
133
    MimeHandler::Status mhs = handler->worker(config, fn,  mime, doc, ipath);
132
  goto out;
134
    FileInterner::Status ret = FIError;
135
    switch (mhs) {
136
    case MimeHandler::MHError: break;
137
    case MimeHandler::MHDone: ret = FIDone;break;
138
    case MimeHandler::MHAgain: ret = FIAgain;break;
133
    }
139
    }
140
134
    doc.mimetype = mime;
141
    doc.mimetype = mime;
135
136
    // Clean up. We delete the temp file and its father directory
137
    ret = true;
138
 out:
139
    delete handler;
140
    tmpcleanup(tdir, tfile);
141
    return ret;
142
    return ret;
142
}
143
}
144
145
FileInterner::~FileInterner()
146
{
147
    delete handler; 
148
    handler = 0;
149
    tmpcleanup();
150
}