--- smsdaemon/Spooler.cpp 2008/12/07 00:59:05 132 +++ smsdaemon/Spooler.cpp 2010/04/28 09:38:09 688 @@ -15,7 +15,7 @@ #include #include -#include "util.h" +#include "Util.h" #include "Exceptions.h" @@ -38,18 +38,22 @@ { bool error = false; lock(); - std::string file = getSpoolFilename(); + std::string file = findSpoolFilename(); std::ofstream out(file.c_str()); - if (out) { + if (out) + { out << recipient << "\n" << message; out.close(); - } else { + } + else + { error = true; } unlock(); - + if (error) throw std::runtime_error("Could not create spoolfile"); + this->filename = file; } std::string Spooler::dequeue() @@ -61,11 +65,11 @@ errnoException(); string file; - string message; + string message; dirent* entry; - while ( (entry = readdir(dir)) != 0) + while ( (entry = readdir(dir)) != 0) { - if (entry->d_name[0] == '.' ) + if (entry->d_name[0] == '.' ) { continue; } @@ -79,27 +83,31 @@ closedir(dir); - if (file != "") { + if (file != "") + { message = Util::readfile(file); ::unlink(file.c_str()); } unlock(); + this->filename = file; + if (file != "") return message; else throw filenotfoundexception(); } -std::string Spooler::getSpoolFilename() +std::string Spooler::findSpoolFilename() { std::string file; std::stringstream ss; int retrycount = 0; - while(1) { + while (1) + { ss.str(std::string()); //clear ss << std::setw(8) << std::setfill('0') << std::uppercase << std::hex << rand(); @@ -109,38 +117,51 @@ int fd = ::open(file.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH); retrycount ++; - if (fd != -1) { + if (fd != -1) + { ::close(fd); return file; } + int err = errno; + ::close(fd); - if (errno == EEXIST) + if (err == EEXIST) + { continue; + } if (retrycount > 20) throw std::runtime_error("to many retry attempt at creating spool file"); - throw std::runtime_error("no access to spool directory"); + throw std::runtime_error("no access to spool directory"); } } void Spooler::lock() { - fd = open(spooldir.c_str() , O_RDONLY); + lockfd = open(spooldir.c_str() , O_RDONLY); - if (fd == -1) { + if (lockfd == -1) + { errnoException("Couldn open lockfile"); } - int status = flock(fd, LOCK_EX); + int status = flock(lockfd, LOCK_EX); - if (status == -1) { + if (status == -1) + { errnoException(); } } void Spooler::unlock() { - flock(fd, LOCK_UN); + flock(lockfd, LOCK_UN); + close(lockfd); +} + +string Spooler::getFilename() +{ + return filename; }