//
//===----------------------------------------------------------------------===//
+#include "llvm/Support/Errc.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/ErrorHandling.h"
if (std::error_code EC =
sys::fs::openFileForWrite(Twine(ResultPath.begin()), ResultFD,
sys::fs::F_RW | sys::fs::F_Excl, Mode)) {
- if (EC == std::errc::file_exists)
+ if (EC == errc::file_exists)
goto retry_random_path;
return EC;
}
case FS_Dir: {
if (std::error_code EC =
sys::fs::create_directory(ResultPath.begin(), false)) {
- if (EC == std::errc::file_exists)
+ if (EC == errc::file_exists)
goto retry_random_path;
return EC;
}
std::error_code EC = create_directory(P, IgnoreExisting);
// If we succeeded, or had any error other than the parent not existing, just
// return it.
- if (EC != std::errc::no_such_file_or_directory)
+ if (EC != errc::no_such_file_or_directory)
return EC;
// We failed because of a no_such_file_or_directory, try to create the
return create_directory(P, IgnoreExisting);
}
+std::error_code copy_file(const Twine &From, const Twine &To) {
+ int ReadFD, WriteFD;
+ if (std::error_code EC = openFileForRead(From, ReadFD))
+ return EC;
+ if (std::error_code EC = openFileForWrite(To, WriteFD, F_None)) {
+ close(ReadFD);
+ return EC;
+ }
+
+ const size_t BufSize = 4096;
+ char *Buf = new char[BufSize];
+ int BytesRead = 0, BytesWritten = 0;
+ for (;;) {
+ BytesRead = read(ReadFD, Buf, BufSize);
+ if (BytesRead <= 0)
+ break;
+ while (BytesRead) {
+ BytesWritten = write(WriteFD, Buf, BytesRead);
+ if (BytesWritten < 0)
+ break;
+ BytesRead -= BytesWritten;
+ }
+ if (BytesWritten < 0)
+ break;
+ }
+ close(ReadFD);
+ close(WriteFD);
+ delete[] Buf;
+
+ if (BytesRead < 0 || BytesWritten < 0)
+ return std::error_code(errno, std::generic_category());
+ return std::error_code();
+}
+
bool exists(file_status status) {
return status_known(status) && status.type() != file_type::file_not_found;
}
}
/// @brief Identify the magic in magic.
- file_magic identify_magic(StringRef Magic) {
+file_magic identify_magic(StringRef Magic) {
if (Magic.size() < 4)
return file_magic::unknown;
switch ((unsigned char)Magic[0]) {
char Buffer[32];
int Length = read(FD, Buffer, sizeof(Buffer));
- if (Length < 0)
+ if (close(FD) != 0 || Length < 0)
return std::error_code(errno, std::generic_category());
Result = identify_magic(StringRef(Buffer, Length));