FileMonitor.cpp
branchv_0
changeset 7 e6065118326f
child 23 42341f66de52
equal deleted inserted replaced
6:fd93a46db15b 7:e6065118326f
       
     1 /**
       
     2  * ShaderShark
       
     3  * Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 
       
    18 #include <string>
       
    19 #include <stdexcept>
       
    20 #include <map>
       
    21 #include <sys/inotify.h>
       
    22 #include <string.h>
       
    23 #include <unistd.h>
       
    24 
       
    25 #include "FileMonitor.h"
       
    26 
       
    27 void checkError(bool hasError, const std::string& message) {
       
    28 	if (hasError) throw std::logic_error(message + strerror(errno));
       
    29 }
       
    30 
       
    31 class WatchedFile::Impl {
       
    32 public:
       
    33 	int fd;
       
    34 	int wd;
       
    35 	std::string fileName;
       
    36 	uint32_t mask;
       
    37 	std::shared_ptr<FileMonitor::Impl> fileMonitorImpl;
       
    38 
       
    39 	virtual ~Impl();
       
    40 };
       
    41 
       
    42 WatchedFile::WatchedFile() {
       
    43 	impl = std::make_shared<Impl>();
       
    44 }
       
    45 
       
    46 WatchedFile::~WatchedFile() {
       
    47 }
       
    48 
       
    49 class FileMonitor::Impl {
       
    50 public:
       
    51 	int fd;
       
    52 	std::map<int, std::string> fileNames;
       
    53 
       
    54 	void unwatch(int wd) {
       
    55 		int result = inotify_rm_watch(fd, wd);
       
    56 		checkError(result < 0, "unable to stop monitoring a file: ");
       
    57 		fileNames.erase(wd);
       
    58 	}
       
    59 
       
    60 	virtual ~Impl() {
       
    61 		close(fd);
       
    62 	}
       
    63 
       
    64 };
       
    65 
       
    66 FileMonitor::FileMonitor() : FileMonitor(std::make_shared<Impl>()) {
       
    67 }
       
    68 
       
    69 FileMonitor::FileMonitor(std::shared_ptr<Impl> impl) {
       
    70 	this->impl = impl;
       
    71 	impl->fd = inotify_init1(IN_NONBLOCK);
       
    72 	checkError(impl->fd < 0, "unable to initialize FileMonitor: ");
       
    73 }
       
    74 
       
    75 FileMonitor::~FileMonitor() {
       
    76 }
       
    77 
       
    78 int FileMonitor::getFD() const {
       
    79 	return impl->fd;
       
    80 }
       
    81 
       
    82 WatchedFile FileMonitor::watch(const std::string& fileName) {
       
    83 	return watch(fileName, IN_CLOSE_WRITE);
       
    84 }
       
    85 
       
    86 WatchedFile FileMonitor::watch(const std::string& fileName, uint32_t mask) {
       
    87 	int wd = inotify_add_watch(impl->fd, fileName.c_str(), mask);
       
    88 	checkError(wd < 0, "unable to monitor a file: ");
       
    89 	impl->fileNames[wd] = fileName;
       
    90 	WatchedFile wf;
       
    91 	wf.impl->fd = impl->fd;
       
    92 	wf.impl->wd = wd;
       
    93 	wf.impl->fileMonitorImpl = impl;
       
    94 	wf.impl->fileName = fileName;
       
    95 	wf.impl->mask = mask;
       
    96 	return wf;
       
    97 }
       
    98 
       
    99 bool FileMonitor::readEvent(FileEvent& event) {
       
   100 	struct inotify_event rawEvent;
       
   101 	int result = ::read(impl->fd, &rawEvent, sizeof (rawEvent));
       
   102 	if (result == sizeof (rawEvent)) {
       
   103 		event.fileName = impl->fileNames[rawEvent.wd];
       
   104 		event.mask = rawEvent.mask;
       
   105 		return true;
       
   106 	} else {
       
   107 		return false;
       
   108 	}
       
   109 }
       
   110 
       
   111 WatchedFile::Impl::~Impl() {
       
   112 	fileMonitorImpl->unwatch(wd);
       
   113 }
       
   114 
       
   115 FileMonitor WatchedFile::getFileMonitor() const {
       
   116 	return FileMonitor(impl->fileMonitorImpl);
       
   117 }
       
   118 
       
   119 std::string WatchedFile::getFileName() const {
       
   120 	return impl->fileName;
       
   121 }
       
   122 
       
   123 uint32_t WatchedFile::getMask() const {
       
   124 	return impl->mask;
       
   125 }
       
   126 
       
   127