File: rcldocsig.py

package info (click to toggle)
recoll 1.43.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,468 kB
  • sloc: cpp: 103,827; python: 9,498; xml: 7,218; ansic: 6,447; sh: 1,212; perl: 130; makefile: 72
file content (114 lines) | stat: -rwxr-xr-x 3,380 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python3
# Copyright (C) 2020 J.F.Dockes
#
# License: GPL 2.1
#
# 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.1 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.

import sys
import hashlib
import base64
from datetime import datetime

sys.path.append("/usr/share/recoll/filters")
import xapian
import rclconfig


# The following 2 methods replicate the file UDI computation in fileudi.cpp:
# process a file path so that it is usable (short enough) for use as a Xapian
# term. We truncate and replace the truncated part by its md5 hash.
def pathHash(path, maxlen):
    HASHLEN = 22
    if len(path) <= maxlen:
        return path
    digest = hashlib.md5(path[maxlen - HASHLEN :]).digest()
    adigest = base64.b64encode(digest)
    adigest = adigest[:-2]
    return path[: maxlen - HASHLEN] + adigest


def make_udi(path, ipath=b""):
    PATHHASHLEN = 150
    path += b"|"
    path += ipath
    return pathHash(path, PATHHASHLEN)


# Return xapian posting list contents as Python list
def get_postlist(xdb, term):
    ret = list()
    for posting in xdb.postlist(term):
        ret.append(posting.docid)
    return ret


# Retrieve named value from document data record.
# The record format is a sequence of nm=value lines
def get_attributes(xdb, docid, flds, decode=True):
    doc = xdb.get_document(docid)
    data = doc.get_data()
    res = []
    for fld in flds:
        s = data.find(fld.encode("utf-8") + b"=")
        if s == -1:
            res.append(None)
        else:
            e = data.find(b"\n", s)
            if decode:
                res.append(data[s + len(fld) + 1 : e].decode("UTF-8"))
            else:
                res.append(data[s + len(fld) + 1 : e])
    return res


def msg(s):
    print("%s" % s, file=sys.stderr)


if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: rcldocdata <path>", file=sys.stderr)
        sys.exit(1)

    # Assuming utf-8 locale here...
    upath = sys.argv[1]
    path = upath.encode("utf-8")

    config = rclconfig.RclConfig()
    dbdir = config.getDbDir()
    xdb = xapian.Database(dbdir)

    # Find document by looking up its UDI unique term
    udi = make_udi(path)
    term = b"Q" + udi
    postlist = get_postlist(xdb, term)

    if len(postlist) == 0:
        msg("No entry found for %s" % upath)
        sys.exit(1)
    elif len(postlist) > 1:
        msg("Multiple document entries for %s" % upath)
        sys.exit(1)

    docid = postlist[0]
    res = get_attributes(xdb, docid, ["sig", "fbytes"])
    msg("Document signature %s file size %s" % (res[0], res[1]))
    fbstr = res[1]
    tmstr = res[0][len(fbstr) :]
    ts = int(tmstr)
    msg("Doc ref uxtime: %s" % tmstr)
    msg("Doc ref date %s" % datetime.utcfromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S"))