X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=temp_file.cpp;fp=temp_file.cpp;h=6b8165c1541ec97bd6b635fbde9886c31abd406c;hb=fed762f0b70eeb556c4b1bd660beb129099e8068;hp=0000000000000000000000000000000000000000;hpb=90012acc26c4a8210c4bce3dac69a09309cce9f8;p=videolink.git diff --git a/temp_file.cpp b/temp_file.cpp new file mode 100644 index 0000000..6b8165c --- /dev/null +++ b/temp_file.cpp @@ -0,0 +1,53 @@ +// Copyright 2005 Ben Hutchings . +// See the file "COPYING" for licence details. + +#include "temp_file.hpp" + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +temp_file::temp_file(const std::string & base_name) +{ + fd_ = Glib::file_open_tmp(name_, base_name); + assert(fd_ >= 0); + + // Workaround bug in glibc <2.2 that may cause the file to be + // created with lax permissions. +# ifdef __GLIBC__ +# if !__GLIBC_PREREQ(2, 2) + if (fchmod(fd_, S_IREAD|S_IWRITE) != 0 || ftruncate(fd_, 0) != 0) + { + close(fd_); + throw std::runtime_error(std::strerror(errno)); + } +# endif +# endif +} + +temp_file::~temp_file() +{ + close(); + + // Don't assert that this is successful. The file could have + // been removed by another process. + unlink(name_.c_str()); +} + +void temp_file::close() +{ + if (fd_ >= 0) + { + int result = ::close(fd_); + assert(result == 0); + fd_ = -1; + } +}