/strigi-0.7.7/strigidaemon/bin/daemon/eventlistener/famlistener.cpp
C++ | 494 lines | 384 code | 81 blank | 29 comment | 75 complexity | 4cee5f067baa9794d524ac9f6da12946 MD5 | raw file
Possible License(s): LGPL-2.0
- /* This file is part of Strigi Desktop Search
- *
- * Copyright (C) 2007 Flavio Castelli <flavio.castelli@gmail.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library 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
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public License
- * along with this library; see the file COPYING.LIB. If not, write to
- * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
- */
- #include "famlistener.h"
- #include <strigi/analyzerconfiguration.h>
- #include "combinedindexmanager.h"
- #include "event.h"
- #include "eventlistenerqueue.h"
- #include <strigi/filelister.h>
- #include <strigi/indexreader.h>
- #include "../strigilogging.h"
- #include <cerrno>
- #include <cstring>
- #include <sys/resource.h>
- #include <sys/select.h>
- #include <sys/types.h>
- #include <vector>
- #include <algorithm>
- using namespace std;
- using namespace Strigi;
- class MatchString {
- string m_fixed_val;
- public:
- MatchString (string fixed_val) {m_fixed_val = fixed_val;}
- bool operator() (pair<FAMRequest,string> val) {
- return (m_fixed_val.compare(val.second) == 0 ? true : false);
- }
- };
- bool operator< (const FAMRequest& f1, const FAMRequest& f2)
- {
- return f1.reqnum < f2.reqnum;
- }
- FamEvent::FamEvent (const string& watchName, FAMEvent event)
- : FsEvent (watchName, event.filename),
- m_event(event),
- m_watchName (watchName)
- {
- bool doStat = true;
-
- switch (event.code)
- {
- case FAMChanged:
- m_type = UPDATE;
- break;
- case FAMMoved:
- case FAMDeleted:
- m_type = DELETE;
- break;
- case FAMCreated:
- m_type = CREATE;
- break;
- default:
- STRIGI_LOG_DEBUG ("strigi.FamEvent.FamEvent",
- "unuseful event");
- doStat = false;
- break;
- }
- if (!doStat)
- return;
-
- struct stat status;
- string path = m_watchName;
- if (path[path.length() - 1] != '/')
- path += '/';
- path += event.filename;
- if ( stat( path.c_str(), &status) == 0) {
- if (S_ISDIR(status.st_mode))
- m_regardsDir = true;
- else
- m_regardsDir = false;
- }
- else {
- string msg;
- msg = "unable to execute stat() on FAMEvent.filename because of: ";
- msg += strerror (errno);
- STRIGI_LOG_ERROR ("strigi.FamEvent", msg)
- }
- }
- char* FamEvent::name()
- {
- return m_event.filename;
- }
- const string FamEvent::description()
- {
- string message;
- switch (m_event.code)
- {
- case FAMChanged:
- message += "FAMChanged";
- break;
- case FAMMoved:
- message += "FAMMoved";
- break;
- case FAMDeleted:
- message += "FAMDeleted";
- break;
- case FAMCreated:
- message += "FAMCreated";
- break;
- case FAMExists:
- message += "FAMExists";
- break;
- case FAMEndExist:
- message += "FAMEndExist";
- break;
- case FAMAcknowledge:
- message += "FAMAcknowledge";
- break;
- case FAMStartExecuting:
- message += "FAMStartExecuting";
- break;
- case FAMStopExecuting:
- message += "FAMStopExecuting";
- break;
- }
-
- message += " event regarding ";
- message += (m_regardsDir) ? "dir " : "file ";
- message += m_event.filename;
- message += " ; associated watch description: " + m_watchName;
-
- return message;
- }
- bool FamEvent::regardsDir()
- {
- return m_regardsDir;
- }
- class FamListener::Private
- {
- public:
- Private();
- virtual ~Private();
- bool init();
- void stopMonitoring();
- bool isEventInteresting (FsEvent * event);
- bool isEventValid(FsEvent* event);
- // event methods
- void pendingEvent(vector<FsEvent*>& events, unsigned int& counter);
- // dir methods
- void dirRemoved (string dir);
-
- // watches methods
- bool addWatch (const std::string& path);
- void addWatches (const std::set<std::string>& watches);
- void rmWatch(FAMRequest& famRequest, std::string path);
- void rmWatches(std::map<FAMRequest, std::string>& watchesToRemove);
- void rmWatches(std::set<std::string>& watchesToRemove);
- void clearWatches();
- private:
- FAMConnection m_famConnection;
- std::map<FAMRequest, std::string> m_watches; //!< map containing all inotify watches added by FamListener. Key is watch descriptor, value is dir path
- };
- bool FamListener::Private::init()
- {
- if (FAMOpen(&m_famConnection) == -1)
- return false;
-
- return true;
- }
- FamListener::Private::Private()
- {
- }
- FamListener::Private::~Private()
- {
- clearWatches();
-
- if (FAMClose (&m_famConnection) == -1)
- STRIGI_LOG_ERROR ("strigi.FamListener",
- "Error during FAM close procedure");
- }
- void FamListener::Private::pendingEvent(vector<FsEvent*>& events,
- unsigned int& counter)
- {
- sleep (1);
-
- if (FAMPending(&m_famConnection)) {
- FAMEvent event;
- if (FAMNextEvent (&m_famConnection, &event) == -1) {
- STRIGI_LOG_ERROR ("strigi.FamListener.pendingEvent",
- "Fam event retrieval failed");
- return;
- }
- if ((event.code == FAMChanged) || (event.code == FAMMoved) ||
- (event.code == FAMDeleted) || (event.code == FAMCreated))
- {
- map<FAMRequest, string>::iterator match;
- match = m_watches.find (event.fr);
- if (match != m_watches.end()) {
- events.push_back (new FamEvent (match->second, event));
- counter++;
- }
- }
- }
- }
- bool FamListener::Private::isEventValid(FsEvent* event)
- {
- FamEvent* famEvent = dynamic_cast<FamEvent*> (event);
- if (famEvent == 0)
- return false;
- map<FAMRequest, string>::iterator match = m_watches.find(famEvent->fr());
- return (match != m_watches.end());