aboutsummaryrefslogtreecommitdiff
path: root/paludis/util/safe_ofstream.cc
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2009-02-09 21:35:39 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2009-02-09 21:35:39 +0000
commit8772b7a5c74c3254a53e70a922e9e0a2bc1c09c3 (patch)
treedb429b81a81135f285ed2ff16c873d65f09a813f /paludis/util/safe_ofstream.cc
parentd946b89871e6dad288440d5fd1ad29bfcd1dff1d (diff)
downloadpaludis-8772b7a5c74c3254a53e70a922e9e0a2bc1c09c3.tar.gz
paludis-8772b7a5c74c3254a53e70a922e9e0a2bc1c09c3.tar.xz
Avoid std::ifstream and std::ofstream.
Unfortunately std::ifstream and std::ofstream are allowed to fail in unobvious ways when given a duff file. In particular, gcc won't error out when a std::ifstream is created for a directory until the first read occurs. So we write our own stream buffer classes that do error checking and throw useful exceptions on error, and use those instead.
Diffstat (limited to 'paludis/util/safe_ofstream.cc')
-rw-r--r--paludis/util/safe_ofstream.cc103
1 files changed, 103 insertions, 0 deletions
diff --git a/paludis/util/safe_ofstream.cc b/paludis/util/safe_ofstream.cc
new file mode 100644
index 000000000..a71592405
--- /dev/null
+++ b/paludis/util/safe_ofstream.cc
@@ -0,0 +1,103 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2009 Ciaran McCreesh
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <paludis/util/safe_ofstream.hh>
+#include <paludis/util/stringify.hh>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+
+using namespace paludis;
+
+SafeOFStreamBuf::SafeOFStreamBuf(const int f) :
+ fd(f)
+{
+}
+
+SafeOFStreamBuf::int_type
+SafeOFStreamBuf::overflow(int_type c)
+{
+ if (c != traits_type::eof())
+ {
+ char z = c;
+ if (1 != write(fd, &z, 1))
+ return traits_type::eof();
+ }
+ return c;
+}
+
+std::streamsize
+SafeOFStreamBuf::xsputn(const char * s, std::streamsize num)
+{
+ return write(fd, s, num);
+}
+
+SafeOFStreamBase::SafeOFStreamBase(const int f) :
+ buf(f)
+{
+}
+
+namespace
+{
+ int check_open_fsentry(const FSEntry & e, int open_flags)
+ {
+ Context context("When opening '" + stringify(e) + "' for write:");
+
+ if (-1 == open_flags)
+ open_flags = O_CREAT | O_TRUNC | O_WRONLY;
+
+ int result(open(stringify(e).c_str(), open_flags, 0644));
+ if (-1 == result)
+ throw SafeOFStreamError("Could not open '" + stringify(e) + "': " + strerror(errno));
+
+ return result;
+ }
+}
+
+SafeOFStream::SafeOFStream(const int f) :
+ SafeOFStreamBase(f),
+ std::ostream(&buf),
+ _close(false)
+{
+}
+
+SafeOFStream::SafeOFStream(const FSEntry & e, const int open_flags) :
+ SafeOFStreamBase(check_open_fsentry(e, open_flags)),
+ std::ostream(&buf),
+ _close(true)
+{
+}
+
+SafeOFStream::~SafeOFStream()
+{
+ if (_close)
+ ::close(buf.fd);
+
+ if (! *this)
+ throw SafeOFStreamError("Write to fd " + stringify(buf.fd) + " failed");
+}
+
+SafeOFStreamError::SafeOFStreamError(const std::string & s) throw () :
+ Exception(s)
+{
+}
+