25 #include <ibrcommon/Logger.h>
26 #include <ibrcommon/data/ConfigFile.h>
28 #ifdef HAVE_SYS_INOTIFY_H
29 #include <sys/inotify.h>
47 if (_state != SOCKET_DOWN)
48 throw ibrcommon::socket_exception(
"socket is already up");
50 #ifdef HAVE_SYS_INOTIFY_H
60 if (_state != SOCKET_UP)
61 throw ibrcommon::socket_exception(
"socket is not up");
63 #ifdef HAVE_SYS_INOTIFY_H
64 for (watch_map::iterator iter = _watch_map.begin(); iter != _watch_map.end(); ++iter)
66 const int wd = (*iter).first;
67 inotify_rm_watch(this->fd(), wd);
79 #ifdef HAVE_SYS_INOTIFY_H
80 int wd = inotify_add_watch(this->fd(), path.getPath().c_str(), opts);
81 _watch_map[wd] = path;
87 #ifdef HAVE_SYS_INOTIFY_H
88 return ::read(this->fd(), data, len);
106 IBRCOMMON_LOGGER_DEBUG_TAG(
"FileMonitor", 5) <<
"watch on: " << watch.getPath() << IBRCOMMON_LOGGER_ENDL;
108 if (!watch.isDirectory())
109 throw ibrcommon::Exception(
"can not watch files, please specify a directory");
111 ibrcommon::socketset socks = _socket.getAll();
112 if (socks.size() == 0)
return;
115 #ifdef HAVE_SYS_INOTIFY_H
116 sock.
watch(watch, IN_CREATE | IN_DELETE);
118 _watchset.insert(watch);
126 }
catch (
const ibrcommon::socket_exception &ex) {
127 IBRCOMMON_LOGGER_TAG(
"FileMonitor", error) << ex.what() << IBRCOMMON_LOGGER_ENDL;
133 #ifdef HAVE_SYS_INOTIFY_H
136 ibrcommon::socketset fds;
145 _socket.select(&fds, NULL, NULL, NULL);
148 for (ibrcommon::socketset::iterator iter = fds.begin(); iter != fds.end(); ++iter)
151 sock.
read((
char*)&buf, 1024);
154 ibrcommon::Thread::sleep(2000);
155 }
catch (
const ibrcommon::vsocket_interrupt&) {
157 }
catch (
const ibrcommon::vsocket_timeout&) {
159 }
catch (
const ibrcommon::socket_exception&) {
176 void FileMonitor::scan()
178 IBRCOMMON_LOGGER_DEBUG_TAG(
"FileMonitor", 5) <<
"scan for file changes" << IBRCOMMON_LOGGER_ENDL;
180 std::set<ibrcommon::File> watch_set;
182 for (watch_set::iterator iter = _watchset.begin(); iter != _watchset.end(); ++iter)
184 const ibrcommon::File &path = (*iter);
185 std::list<ibrcommon::File> files;
187 if (path.getFiles(files) == 0)
189 for (std::list<ibrcommon::File>::iterator iter = files.begin(); iter != files.end(); ++iter)
191 watch_set.insert(*iter);
196 IBRCOMMON_LOGGER_TAG(
"FileMonitor", error) <<
"scan of " << path.getPath() <<
" failed" << IBRCOMMON_LOGGER_ENDL;
201 for (std::set<ibrcommon::File>::iterator iter = watch_set.begin(); iter != watch_set.end(); ++iter)
203 const ibrcommon::File &path = (*iter);
204 if (path.isDirectory() && !path.isSystem())
206 if (!isActive(path)) {
213 for (std::map<ibrcommon::File, dtn::core::Node>::iterator iter = _active_paths.begin(); iter != _active_paths.end();)
215 const ibrcommon::File &path = (*iter).first;
218 if (watch_set.find(path) == watch_set.end())
221 IBRCOMMON_LOGGER_DEBUG_TAG(
"FileMonitor", 5) <<
"Node on drive gone: " << node.
getEID().
getString() << IBRCOMMON_LOGGER_ENDL;
222 _active_paths.erase(iter++);
231 void FileMonitor::adopt(
const ibrcommon::File &path)
234 const ibrcommon::File drivefile = path.get(
".dtndrive");
235 if (drivefile.exists())
237 ibrcommon::ConfigFile cf(drivefile.getPath());
243 const std::string uri =
"file://" + path.getPath() +
"/" + cf.read<std::string>(
"PATH");
248 _active_paths[path] = n;
250 IBRCOMMON_LOGGER_DEBUG_TAG(
"FileMonitor", 5) <<
"Node on drive found: " << n.getEID().getString() << IBRCOMMON_LOGGER_ENDL;
252 }
catch (
const ibrcommon::ConfigFile::key_not_found&) {};
256 bool FileMonitor::isActive(
const ibrcommon::File &path)
const
258 return (_active_paths.find(path) != _active_paths.end());
263 return "FileMonitor";
void remove(const dtn::core::Node &n)
dtn::net::ConnectionManager & getConnectionManager()
void watch(const ibrcommon::File &path, int opts)
void watch(const ibrcommon::File &watch)
void add(const dtn::core::Node &n)
ssize_t read(char *data, size_t len)
const std::string getName() const
std::string getString() const
const dtn::data::EID & getEID() const
static BundleCore & getInstance()