aboutsummaryrefslogtreecommitdiffstats
path: root/vespaclient/src/vespa/vespaclient/spoolmaster/application.cpp
blob: 7406e17430d6dfe8915b3359749c43674e9064f4 (plain) (blame)
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/defaults.h>
#include <thread>
#include <iostream>
#include <algorithm>
#include <dirent.h>
#include <unistd.h>
#include <cstring>
#include <sys/stat.h>

#include "application.h"

namespace {

std::string masterInbox() {
    return vespa::Defaults::underVespaHome("var/spool/master/inbox");
}

std::string outboxParent() {
    return vespa::Defaults::underVespaHome("var/spool/vespa");
}

}

namespace spoolmaster {

Application::Application()
    : _masterInbox(masterInbox()),
      _inboxFiles(),
      _outboxParentDir(outboxParent()),
      _outboxes()
{
    // empty
}

Application::~Application()
{
    // empty
}

bool
Application::scanInbox()
{
    std::vector<std::string> rv;
    DIR *d = opendir(_masterInbox.c_str());
    if (d == NULL) {
        perror(_masterInbox.c_str());
        mkdir(_masterInbox.c_str(), 0775);
        return false;
    }

    struct dirent *entry;
    while ((entry = readdir(d)) != NULL) {
        if (strcmp(entry->d_name, ".")  == 0) continue;
        if (strcmp(entry->d_name, "..") == 0) continue;

        std::string fn = _masterInbox;
        fn.append("/");
        fn.append(entry->d_name);

        struct stat sb;
        if (stat(fn.c_str(), &sb) == 0) {
            if (S_ISREG(sb.st_mode)) {
                rv.push_back(fn);
            }
        } else {
            perror(fn.c_str());
        }
    }
    closedir(d);

    if (access(_masterInbox.c_str(), W_OK) < 0) {
        perror(_masterInbox.c_str());
        return false;
    }

    _inboxFiles = rv;
    return (rv.size() > 0);
}

bool
Application::findOutboxes()
{
    std::vector<std::string> rv;
    DIR *d = opendir(_outboxParentDir.c_str());
    if (d == NULL) {
        perror(_outboxParentDir.c_str());
        return false;
    }
    struct dirent *entry;
    while ((entry = readdir(d)) != NULL) {
        if (strcmp(entry->d_name, ".")  == 0) continue;
        if (strcmp(entry->d_name, "..") == 0) continue;

        /* XXX: should check if d_name starts with "colo." ? */

        std::string fn = _outboxParentDir;
        fn.append("/");
        fn.append(entry->d_name);
        fn.append("/inbox");

        if (fn == _masterInbox) continue;

        struct stat sb;
        if (stat(fn.c_str(), &sb) == 0) {
            if (S_ISDIR(sb.st_mode)) {
                if (access(fn.c_str(), W_OK) < 0) {
                    std::cerr << "Cannot write to directory ";
                    perror(fn.c_str());
                    continue;
                }
                rv.push_back(fn);
            }
        } else {
            perror(fn.c_str());
        }
    }
    closedir(d);
    if (rv.size() > 0) {
        std::sort(rv.begin(), rv.end());
        sviter_t ni = rv.begin();
        sviter_t oi = _outboxes.begin();

        while (ni != rv.end()) {
            const std::string &newval = *ni;
            if (oi == _outboxes.end()) {
                std::cerr << "Found new slave inbox: " << newval << std::endl;
                ++ni;
                continue;
            }
            const std::string &oldval = *oi;
            if (newval == oldval) {
                ++ni;
                ++oi;
            } else if (newval < oldval) {
                std::cerr << "Found new slave inbox: " << newval << std::endl;
                ++ni;
            } else /* oldval < newval */ {
                std::cerr << "Slave inbox removed: " << oldval << std::endl;
                ++oi;
            }
        }
        _outboxes = rv;
        return true;
    }
    std::cerr << "Did not find any slave inboxes in: " << _outboxParentDir << std::endl;
    return false;
}

void
Application::moveLinks()
{
    for (sviter_t fni = _inboxFiles.begin(); fni != _inboxFiles.end(); ++fni) {
        const std::string& filename = *fni;
        size_t ldp = filename.rfind("/");
        std::string basename = filename.substr(ldp+1);
        for (sviter_t obi = _outboxes.begin(); obi != _outboxes.end(); ++obi) {
            std::string newFn = *obi;
            newFn.append("/");
            newFn.append(basename);

            std::cout << "linking " << filename << " -> " << newFn << std::endl;
            if (link(filename.c_str(), newFn.c_str()) < 0) {
                std::cerr << "linking " << filename << " -> " << newFn;
                perror("failed");
                return;
            }
        }
        if (unlink(filename.c_str()) < 0) {
            std::cerr << "cannot remove " << filename;
            perror(", error");
        }
    }
}


int
Application::Main()
{
    bool aborted = false;
    findOutboxes();

    try {
        while (!aborted) {
            if (scanInbox() && findOutboxes()) {
                moveLinks();
            } else {
                std::this_thread::sleep_for(std::chrono::milliseconds(200));
            }
        }
    }
    catch(std::exception &e) {
        fprintf(stderr, "ERROR: %s\n", e.what());
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

} // namespace spoolmaster