--- smsdaemon/Spooler.cpp 2008/12/07 08:44:22 134 +++ smsdaemon/Spooler.cpp 2008/12/18 06:53:29 196 @@ -15,7 +15,7 @@ #include #include -#include "util.h" +#include "Util.h" #include "Exceptions.h" @@ -40,14 +40,17 @@ lock(); 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; @@ -62,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; } @@ -80,7 +83,8 @@ closedir(dir); - if (file != "") { + if (file != "") + { message = Util::readfile(file); ::unlink(file.c_str()); } @@ -102,7 +106,8 @@ int retrycount = 0; - while(1) { + while (1) + { ss.str(std::string()); //clear ss << std::setw(8) << std::setfill('0') << std::uppercase << std::hex << rand(); @@ -112,40 +117,48 @@ 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()