///
bool MakeFileReadable (const std::string & Filename);
+
+/// FDHandle - Simple handle class to make sure a file descriptor gets closed
+/// when the object is destroyed.
+///
+class FDHandle {
+ int FD;
+ FDHandle(const FDHandle &); // DO NOT IMPLEMENT
+ void operator=(const FDHandle&); // DO NOT IMPLEMENT
+public:
+ FDHandle() : FD(-1) {}
+ FDHandle(int fd) : FD(fd) {}
+ ~FDHandle();
+
+ operator int() const { return FD; }
+
+ FDHandle &operator=(int fd);
+
+ /// 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 take();
+};
} // End llvm namespace
#endif
///
bool MakeFileReadable (const std::string & Filename);
+
+/// FDHandle - Simple handle class to make sure a file descriptor gets closed
+/// when the object is destroyed.
+///
+class FDHandle {
+ int FD;
+ FDHandle(const FDHandle &); // DO NOT IMPLEMENT
+ void operator=(const FDHandle&); // DO NOT IMPLEMENT
+public:
+ FDHandle() : FD(-1) {}
+ FDHandle(int fd) : FD(fd) {}
+ ~FDHandle();
+
+ operator int() const { return FD; }
+
+ FDHandle &operator=(int fd);
+
+ /// 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 take();
+};
} // End llvm namespace
#endif
#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;
//===----------------------------------------------------------------------===//
//
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 {
}
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 + "'");
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;
+}