aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2015-10-18 17:27:50 +0200
committerAdrian Kummerlaender2015-10-18 17:27:50 +0200
commit6a66bd864b9b95258a198e65849fa693f88032aa (patch)
tree16101853c9353b1f2146f7b842755067fe63c369
parentceb01fb33a039d4f315c9aa9217320be44cefa2d (diff)
downloadchange-6a66bd864b9b95258a198e65849fa693f88032aa.tar
change-6a66bd864b9b95258a198e65849fa693f88032aa.tar.gz
change-6a66bd864b9b95258a198e65849fa693f88032aa.tar.bz2
change-6a66bd864b9b95258a198e65849fa693f88032aa.tar.lz
change-6a66bd864b9b95258a198e65849fa693f88032aa.tar.xz
change-6a66bd864b9b95258a198e65849fa693f88032aa.tar.zst
change-6a66bd864b9b95258a198e65849fa693f88032aa.zip
Prevent external changes to `FileDescriptorGuard`'s fd member
-rw-r--r--change_log.cc2
-rw-r--r--io.h27
-rw-r--r--utility.h1
3 files changed, 19 insertions, 11 deletions
diff --git a/change_log.cc b/change_log.cc
index dc245da..5ca0e8e 100644
--- a/change_log.cc
+++ b/change_log.cc
@@ -33,7 +33,7 @@ void init() {
fd_guard = std::make_unique<io::FileDescriptorGuard>(
getenv("CHANGE_LOG_TARGET")
);
- log = std::make_unique<utility::Logger>(fd_guard->fd);
+ log = std::make_unique<utility::Logger>(*fd_guard);
} else {
log = std::make_unique<utility::Logger>(STDERR_FILENO);
}
diff --git a/io.h b/io.h
index c9ce30e..87778ec 100644
--- a/io.h
+++ b/io.h
@@ -10,20 +10,27 @@
namespace io {
-struct FileDescriptorGuard {
- FileDescriptorGuard(const std::string& path) {
- this->fd = open(path.c_str(), O_WRONLY | O_APPEND);
+class FileDescriptorGuard {
+ public:
+ FileDescriptorGuard(const std::string& path) {
+ this->fd = open(path.c_str(), O_WRONLY | O_APPEND);
+
+ if ( !this->fd ) {
+ this->fd = STDERR_FILENO;
+ }
+ }
- if ( !this->fd ) {
- this->fd = STDERR_FILENO;
+ ~FileDescriptorGuard() {
+ close(this->fd);
}
- }
- ~FileDescriptorGuard() {
- close(this->fd);
- }
+ operator int() {
+ return this->fd;
+ }
+
+ private:
+ int fd;
- int fd;
};
std::string get_file_name(int fd) {
diff --git a/utility.h b/utility.h
index ec4529e..27ce8e9 100644
--- a/utility.h
+++ b/utility.h
@@ -20,6 +20,7 @@ class Logger {
private:
__gnu_cxx::stdio_filebuf<char> buffer;
std::ostream stream;
+
};
}