LibreELEC/Kodi add-on for offline file search using 'plocate' as backend

  • Hello Community, my name is Aliet Expósito, and I am a programmer from Cuba.

    As the title suggests, I am developing a simple add-on for LibreELEC/Kodi that allows instant search across your devices using plocate (https://plocate.sesse.net/) as the backend. It is currently in the early stages of development, and I would appreciate your feedback and answers to some questions.

    plocate is an excellent project, faster than other locate implementations and with a smaller index. I am developing this add-on on a Raspberry Pi 4, running LibreELEC 12.0 and Kodi 21.0. plocate scans my 5TB disk full of multimedia, containing 17,502 files, in about 12 seconds on the first run:
    LibreELEC:~ # time /storage/plinkr/plocate/obj-64bits-static/updatedb --database-root /var/media/easystore/ --output /storage/plinkr/plocate/plocate.db
    real    0m 12.10s
    user    0m 0.64s
    sys     0m 0.23s

    Subsequent runs are faster, taking less than 1 second, depending on file changes:
    LibreELEC:~ # time /storage/plinkr/plocate/obj-64bits-static/updatedb --database-root /var/media/easystore/ --output /storage/plinkr/plocate/plocate.db
    real    0m 0.77s
    user    0m 0.54s
    sys     0m 0.23s

    The add-on uses a modified version of 95-udevil-mount.rules, copied to /storage/.config/udev.rules.d/95-udevil-mount.rules to run updatedb and create the index for the connected device. After the device is mounted by udevil, the script runs with /usr/bin/systemd-run --on-active=1, allowing it to execute in the background without affecting normal operation. The script (for BusyBox's sh) uses the UUID of the partitions to create an index for each scanned partition.

    However, I face a challenge in appropriately copying the modified rules to /storage/.config/udev.rules.d/95-udevil-mount.rules post-installation and removing these rules upon add-on uninstallation. Similarly, making the shell script executable has posed difficulties. One workaround could be to check if the file exists each time the add-on is executed, but this does not resolve the issue of removal upon uninstallation.

    The add-on returns search results in Kodi and allows you to open directories or play media files directly. Configuration options enable various plocate behaviors, such as case-insensitive searches, regular expressions, basename searches, and searching only currently connected devices or including disconnected ones. The results dialog also allows sorting by size in descending order, and color-codes directories differently from files.

    Since plocate needs to be compiled, I am uncertain how to handle this requirement. Should the add-on be separated into two, with one containing plocate and the other serving as the Kodi search frontend?

    I use this plugin frequently because I have a lot of multimedia content spread across multiple disks, and it is very useful for directly locating specific files. Being mostly offline, the global search requires internet access, which is often unavailable. I believe this add-on could be valuable to others and would like to share it with the community. I use LibreELEC a lot, and not only me, but my family as well, I am very grateful to this project.

    I would greatly appreciate any recommendations or guidance on best practices for developing add-ons for LibreELEC or Kodi. Finding up-to-date documentation has been challenging.

    The code is hosted at: https://github.com/plinkr/script.plocate.search and it's on a beta stage.

    Thank you for your time and assistance.

  • Compiling the plocate binary from sources and then storing it in a 'bin' directory for use should be simple (including precompiled blobs would be rejected if you ever wanted to include this in the LE binary add-on repo). The sources use the meson build system and important things like specifying a non-standard DB location at compile-time appear to be supported already. If the add-contains compiled binaries I don't think script.plocate.search is correct naming (script.* is really reserved for things that are only scripts; normally Python scripts). Something like tools.plocate would be more appropriate. There is no need to have the binary built separately from the add-on; we only do that for e.g. add-ons like "System Tools" and "Multimedia Tools" where multiple separate binaries are being packaged.

    LE supports udev rules being overlaid from /storage/.config/udev.rules.d at boot time, so the add-on can run a setup (b)ash script that detects presence of a known-name .rules file and create it if missing, but as you noticed there is no "uninstall" function in add-ons so if the add-on is removed the rules file becomes orphaned. You can probably add some udev logic to check for presence of the add-on bin directory first, and if not found skip the rule so orphaned rules files do no harm. It's messy though, so:

    Or you can use a systemd service to run a script that creates or updates the DB before Kodi starts. The negative: using systemd not udev means you lose the ability to trigger an update to the database when a USB drive containing media is connected. The positive: you have simple systemd packaging that aligns with how most binary add-ons are already being started/stopped and packaged for LE use (so lots of prior art in our buildsystem). IMHO most users with portable USB drives full of media will have them connected to their HTPC device before boot so the loss of the connect drive feature isn't such a big deal. NB: plocate also ships with an example systemd service so you can use that with a minor tweak (the buildsystem can patch the sources) to add a kodi.target dependency.

  • Hello,

    You are absolutely right; it makes more sense to use 'tools.plocate.search' as the add-on name. I used 'script.*' because all the add-ons I reviewed in the "Program Add-ons" section of the Kodi site started with 'script.*', so I assumed it was conventional. I will change the name to 'tools.plocate.search' as you suggested.

    Regarding including the add-on in the LE repository, I understand that it cannot contain any binaries. The add-on should include the necessary build scripts to compile 'plocate' to ensure it meets repository standards.

    As for the 'udev' rules, I will need to perform several tests with the advice you provided to find an elegant solution. I would prefer not to lose the ability to update the index when a new drive is connected, as those of us close to me who use LE frequently connect and disconnect drives/USBs while the device is running. However, if no better option is available, I will use 'systemd' to update the index as you recommended. I will also implement a feature in the add-on settings to manually update the index for newly connected drives.

    I will thoroughly review the advice you gave and study LE add-ons that compile binaries to determine the best approach.

    Thank you very much for your time and assistance.