File: mh_text.cpp

package info (click to toggle)
recoll 1.43.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,952 kB
  • sloc: cpp: 104,785; python: 9,933; xml: 7,314; ansic: 6,447; sh: 1,252; perl: 166; makefile: 72
file content (219 lines) | stat: -rw-r--r-- 6,530 bytes parent folder | download
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
/* Copyright (C) 2005 J.F.Dockes 
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#include "autoconfig.h"

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include "safeunistd.h"

#include <iostream>
#include <string>

#include "cstr.h"
#include "mh_text.h"
#include "log.h"
#include "readfile.h"
#include "md5ut.h"
#include "rclconfig.h"
#include "pxattr.h"
#include "pathut.h"

using namespace std;

void MimeHandlerText::getparams()
{
    m_config->getConfParam("textfilemaxmbs", &m_maxmbs);

    // Text file page size: if set, we split text files into
    // multiple documents
    int ps = 1000;
    m_config->getConfParam("textfilepagekbs", &ps);
    if (ps != -1) {
        ps *= 1024;
        m_paging = true;
    } else {
        m_paging = false;
    }
    m_pagesz = size_t(ps);
    m_offs = 0;
}

// Process a plain text file
bool MimeHandlerText::set_document_file_impl(const string&, const string &fn)
{
    LOGDEB("MimeHandlerText::set_document_file: [" << fn << "] offs " <<
           m_offs << "\n");

    m_fn = fn;
    // file size for oversize check
    m_totlen = path_filesize(m_fn);
    if (m_totlen < 0) {
        LOGERR("MimeHandlerText::set_document_file: stat " << m_fn <<
               " errno " << errno << "\n");
        return false;
    }

    // Check for charset defined in extended attribute as per:
    // http://freedesktop.org/wiki/CommonExtendedAttributes
    pxattr::get(m_fn, "charset", &m_charsetfromxattr);

    getparams();
    if (m_maxmbs != -1 && m_totlen / (1024*1024) > m_maxmbs) {
        LOGINF("MimeHandlerText: file too big (textfilemaxmbs=" << m_maxmbs <<
               "), contents will not be indexed: " << fn << endl);
    } else {
        if (!readnext()) {
            return false;
        } 
    }
    m_havedoc = true;
    return true;
}

bool MimeHandlerText::set_document_string_impl(const string&, const string& otext)
{
    m_fn.clear();
    m_totlen = otext.size();

    getparams();
    if (m_maxmbs != -1 && m_totlen / (1024*1024) > m_maxmbs) {
        LOGINF("MimeHandlerText: text too big (textfilemaxmbs=" << m_maxmbs <<
               "), contents will not be indexed\n");
    } else {
        if (!m_paging || (m_totlen <= (int64_t)m_pagesz)) {
            // Avoid copy for texts smaller than page size
            m_paging = false;
            m_text = otext;
            m_offs = m_totlen;
        } else {
            m_alltext = otext;
            readnext();
        }
    }
        
    m_havedoc = true;
    return true;
}

bool MimeHandlerText::skip_to_document(const string& ipath)
{
    char *endptr;
    int64_t t = strtoll(ipath.c_str(), &endptr, 10);
    if (endptr == ipath.c_str()) {
        LOGERR("MimeHandlerText::skip_to_document: bad ipath offs ["  <<
               ipath << "]\n");
        return false;
    }
    m_offs = t;
    readnext();
    return true;
}

bool MimeHandlerText::next_document()
{
    LOGDEB("MimeHandlerText::next_document: m_havedoc "  << m_havedoc << "\n");

    if (m_havedoc == false)
        return false;

    if (m_charsetfromxattr.empty())
        m_metaData[cstr_dj_keyorigcharset] = m_dfltInputCharset;
    else 
        m_metaData[cstr_dj_keyorigcharset] = m_charsetfromxattr;

    m_metaData[cstr_dj_keymt] = cstr_textplain;

    size_t srclen = m_text.length();
    if (!m_forPreview) {
        string md5, xmd5;
        MD5String(m_text, md5);
        m_metaData[cstr_dj_keymd5] = MD5HexPrint(md5, xmd5);
    }
    m_metaData[cstr_dj_keycontent].swap(m_text);

    // We transcode even if defcharset is supposedly already utf-8:
    // this validates the encoding.
    // txtdcode() truncates the text if transcoding fails
    (void)txtdcode("mh_text");

    // If the text length is 0 (the file is empty or oversize), or we are 
    // not paging, we're done
    if (srclen == 0 || !m_paging) {
        m_havedoc = false;
        return true;
    } else {
        // Paging: set ipath then read next chunk. 

        int64_t start_offset = m_offs - srclen;
        string buf = std::to_string(start_offset);

        // Don't set ipath for the first chunk to avoid having 2
        // records for small files (one for the file, one for the
        // first chunk). This is a hack. The right thing to do would
        // be to use a different mtype for files over the page size,
        // and keep text/plain only for smaller files.
        if (start_offset != 0)
            m_metaData[cstr_dj_keyipath] = buf;

        readnext();

        // This ensures that the first chunk (offs==srclen) of a
        // multi-chunk file does have an ipath. Else it stands for the
        // whole file (see just above), which used to be the case but
        // does not seem right
        if (m_havedoc)
            m_metaData[cstr_dj_keyipath] = buf;

        return true;
    }
}

bool MimeHandlerText::readnext()
{
    string reason;
    m_text.clear();
    if (!m_fn.empty()) {
        if (!file_to_string(m_fn, m_text, m_offs, m_pagesz, &reason)) {
            LOGERR("MimeHandlerText: can't read file: "  << reason << "\n" );
            m_havedoc = false;
            return false;
        }
    } else {
        m_text = m_alltext.substr((size_t)m_offs, m_pagesz);
    }

    if (m_text.length() == 0) {
        // EOF
        m_havedoc = false;
        return true;
    }

    // If possible try to adjust the chunk to end right after a line
    // Don't do this for the last chunk. Last chunk of exactly the
    // page size might be unduly split, no big deal
    if (m_text.length() == m_pagesz && m_text.back() != '\n' &&
        m_text.back() != '\r') {
        string::size_type pos = m_text.find_last_of("\n\r");
        if (pos != string::npos && pos != 0) {
            m_text.erase(pos);
        }
    }
    m_offs += m_text.length();
    return true;
}