File: NotifyInotify.cc

package info (click to toggle)
j4-dmenu-desktop 3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 1,180 kB
  • sloc: cpp: 6,282; python: 473; sh: 81; makefile: 8
file content (91 lines) | stat: -rw-r--r-- 3,034 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
//
// This file is part of j4-dmenu-desktop.
//
// j4-dmenu-desktop 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 3 of the License, or
// (at your option) any later version.
//
// j4-dmenu-desktop 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 j4-dmenu-desktop.  If not, see <http://www.gnu.org/licenses/>.
//

#include "NotifyInotify.hh"

#include <errno.h>
#include <stdio.h>
#include <sys/inotify.h>
#include <sys/types.h>
#include <unistd.h>
#include <utility>

#include "FileFinder.hh"

NotifyInotify::directory_entry::directory_entry(int r, std::string p)
    : rank(r), path(std::move(p)) {}

NotifyInotify::NotifyInotify(const stringlist_t &search_path) {
    inotifyfd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
    if (inotifyfd == -1)
        PFATALE("inotify_init");

    for (int i = 0; i < (int)search_path.size();
         i++) // size() is converted to int to silent warnings about
              // narrowing when adding i to directories
    {
        int wd = inotify_add_watch(inotifyfd, search_path[i].c_str(),
                                   IN_DELETE | IN_MODIFY | IN_MOVE);
        if (wd == -1)
            PFATALE("inotify_add_watch");
        directories.insert({
            wd, {i, {}}
        });
        FileFinder find(search_path[i]);
        while (++find) {
            if (!find.isdir())
                continue;
            wd = inotify_add_watch(inotifyfd, find.path().c_str(),
                                   IN_DELETE | IN_MODIFY);
            directories.insert({
                wd, {i, find.path().substr(search_path[i].length()) + '/'}
            });
        }
    }
}

int NotifyInotify::getfd() const {
    return inotifyfd;
}

std::vector<NotifyInotify::FileChange> NotifyInotify::getchanges() {
    char buffer alignas(inotify_event)[4096];
    ssize_t len;
    std::vector<FileChange> result;

    while ((len = read(inotifyfd, buffer, sizeof buffer)) > 0) {
        const inotify_event *event;
        for (const char *ptr = buffer; ptr < buffer + len;
             ptr += sizeof(inotify_event) + event->len) {
            event = reinterpret_cast<const inotify_event *>(ptr);

            const auto &dir = directories.at(event->wd);

            if (event->mask & IN_MODIFY || event->mask & IN_MOVED_TO)
                result.push_back(
                    {dir.rank, dir.path + event->name, changetype::modified});
            else // IN_DELETE or IN_MOVED_FROM
                result.push_back(
                    {dir.rank, dir.path + event->name, changetype::deleted});
        }
    }

    if (len == -1 && errno != EAGAIN)
        perror("read");

    return result;
}