aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2003-12-29 21:35:05 +0000
committerChris Lattner <sabre@nondot.org>2003-12-29 21:35:05 +0000
commit2d6481cc2a168c4a867f8046119607f72552ef31 (patch)
tree36f57f499b30326aab5c8aef51b8275d167b3011 /lib
parent872ccce0a801930aa370f8d161ceb9d6b1a3bdae (diff)
downloadexternal_llvm-2d6481cc2a168c4a867f8046119607f72552ef31.zip
external_llvm-2d6481cc2a168c4a867f8046119607f72552ef31.tar.gz
external_llvm-2d6481cc2a168c4a867f8046119607f72552ef31.tar.bz2
Factor FDHandle out of the bytecode reader into the FileUtilities.h support
routines. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@10642 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r--lib/Bytecode/Reader/ReaderWrappers.cpp20
-rw-r--r--lib/Support/FileUtilities.cpp23
2 files changed, 27 insertions, 16 deletions
diff --git a/lib/Bytecode/Reader/ReaderWrappers.cpp b/lib/Bytecode/Reader/ReaderWrappers.cpp
index 291ad87..bc3cbbd 100644
--- a/lib/Bytecode/Reader/ReaderWrappers.cpp
+++ b/lib/Bytecode/Reader/ReaderWrappers.cpp
@@ -16,12 +16,13 @@
#include "ReaderInternals.h"
#include "llvm/Module.h"
#include "llvm/Instructions.h"
+#include "Support/FileUtilities.h"
#include "Support/StringExtras.h"
#include "Config/fcntl.h"
-#include <sys/stat.h>
-#include <cerrno>
#include "Config/unistd.h"
#include "Config/sys/mman.h"
+#include <sys/stat.h>
+#include <cerrno>
using namespace llvm;
//===----------------------------------------------------------------------===//
@@ -29,19 +30,6 @@ using namespace llvm;
//
namespace {
- /// FDHandle - Simple handle class to make sure a file descriptor gets closed
- /// when the object is destroyed.
- ///
- class FDHandle {
- int FD;
- public:
- FDHandle(int fd) : FD(fd) {}
- operator int() const { return FD; }
- ~FDHandle() {
- if (FD != -1) close(FD);
- }
- };
-
/// BytecodeFileReader - parses a bytecode file from a file
///
class BytecodeFileReader : public BytecodeParser {
@@ -63,7 +51,7 @@ static std::string ErrnoMessage (int savedErrNum, std::string descr) {
}
BytecodeFileReader::BytecodeFileReader(const std::string &Filename) {
- FDHandle FD = open(Filename.c_str(), O_RDONLY);
+ FDHandle FD(open(Filename.c_str(), O_RDONLY));
if (FD == -1)
throw ErrnoMessage(errno, "open '" + Filename + "'");
diff --git a/lib/Support/FileUtilities.cpp b/lib/Support/FileUtilities.cpp
index e6abc8f..02b4edd 100644
--- a/lib/Support/FileUtilities.cpp
+++ b/lib/Support/FileUtilities.cpp
@@ -194,3 +194,26 @@ bool llvm::MakeFileExecutable(const std::string &Filename) {
bool llvm::MakeFileReadable(const std::string &Filename) {
return AddPermissionsBits(Filename, 0444);
}
+
+//===----------------------------------------------------------------------===//
+// FDHandle class implementation
+//
+
+FDHandle::~FDHandle() {
+ if (FD != -1) close(FD);
+}
+
+FDHandle &FDHandle::operator=(int fd) {
+ if (FD != -1) close(FD);
+ FD = fd;
+ return *this;
+}
+
+
+/// take - Take ownership of the file descriptor away from the FDHandle
+/// object, so that the file is not closed when the FDHandle is destroyed.
+int FDHandle::take() {
+ int Ret = FD;
+ FD = -1;
+ return Ret;
+}