llvm.org GIT mirror llvm / c1b49b5
Add a wrapper for open. This centralizes the handling of O_BINARY and opens the way for hiding more differences (like how open behaves with directories). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@186447 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 6 years ago
27 changed file(s) with 132 addition(s) and 121 deletion(s). Raw diff Collapse all Expand all
610610 error_code createUniqueDirectory(const Twine &Prefix,
611611 SmallVectorImpl &ResultPath);
612612
613 enum OpenFlags {
614 F_None = 0,
615
616 /// F_Excl - When opening a file, this flag makes raw_fd_ostream
617 /// report an error if the file already exists.
618 F_Excl = 1,
619
620 /// F_Append - When opening a file, if it already exists append to the
621 /// existing file instead of returning an error. This may not be specified
622 /// with F_Excl.
623 F_Append = 2,
624
625 /// F_Binary - The file should be opened in binary mode on platforms that
626 /// make this distinction.
627 F_Binary = 4
628 };
629
630 inline OpenFlags operator|(OpenFlags A, OpenFlags B) {
631 return OpenFlags(unsigned(A) | unsigned(B));
632 }
633
634 inline OpenFlags &operator|=(OpenFlags &A, OpenFlags B) {
635 A = A | B;
636 return A;
637 }
638
639 error_code openFileForWrite(const Twine &Name, int &ResultFD, OpenFlags Flags,
640 unsigned Mode = 0666);
641
642 error_code openFileForRead(const Twine &Name, int &ResultFD);
643
613644 /// @brief Canonicalize path.
614645 ///
615646 /// Sets result to the file system's idea of what path is. The result is always
4646 /// tool_output_file - This constructor's arguments are passed to
4747 /// to raw_fd_ostream's constructor.
4848 tool_output_file(const char *filename, std::string &ErrorInfo,
49 unsigned Flags = 0);
49 sys::fs::OpenFlags Flags = sys::fs::F_None);
5050
5151 tool_output_file(const char *Filename, int FD);
5252
1616 #include "llvm/ADT/StringRef.h"
1717 #include "llvm/Support/Compiler.h"
1818 #include "llvm/Support/DataTypes.h"
19 #include "llvm/Support/FileSystem.h"
1920
2021 namespace llvm {
2122 class format_object_base;
334335 void error_detected() { Error = true; }
335336
336337 public:
337
338 enum {
339 /// F_Excl - When opening a file, this flag makes raw_fd_ostream
340 /// report an error if the file already exists.
341 F_Excl = 1,
342
343 /// F_Append - When opening a file, if it already exists append to the
344 /// existing file instead of returning an error. This may not be specified
345 /// with F_Excl.
346 F_Append = 2,
347
348 /// F_Binary - The file should be opened in binary mode on platforms that
349 /// make this distinction.
350 F_Binary = 4
351 };
352
353338 /// raw_fd_ostream - Open the specified file for writing. If an error occurs,
354339 /// information about the error is put into ErrorInfo, and the stream should
355340 /// be immediately destroyed; the string will be empty if no error occurred.
361346 /// file descriptor when it is done (this is necessary to detect
362347 /// output errors).
363348 raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
364 unsigned Flags = 0);
349 sys::fs::OpenFlags Flags = sys::fs::F_None);
365350
366351 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
367352 /// ShouldClose is true, this closes the file when the stream is destroyed.
1717
1818 int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
1919 std::string ErrorInfo;
20 raw_fd_ostream OS(Path, ErrorInfo, raw_fd_ostream::F_Binary);
20 raw_fd_ostream OS(Path, ErrorInfo, sys::fs::F_Binary);
2121
2222 if (!ErrorInfo.empty())
2323 return -1;
270270 raw_ostream *OutFile = 0;
271271 if (OutFileName) {
272272 std::string ErrorInfo;
273 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
274 raw_fd_ostream::F_Append);
273 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo, sys::fs::F_Append);
275274 if (!ErrorInfo.empty()) {
276275 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
277276 exit(1);
592592 raw_ostream *OS = getContext().getSecureLog();
593593 if (OS == NULL) {
594594 std::string Err;
595 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
595 OS = new raw_fd_ostream(SecureLogFile, Err, sys::fs::F_Append);
596596 if (!Err.empty()) {
597597 delete OS;
598598 return Error(IDLoc, Twine("can't open secure log file: ") +
1616 #define DEBUG_TYPE "Data-stream"
1717 #include "llvm/Support/DataStream.h"
1818 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Support/FileSystem.h"
1920 #include "llvm/Support/Program.h"
2021 #include "llvm/Support/system_error.h"
2122 #include
2627 #else
2728 #include
2829 #endif
29 #include
3030 using namespace llvm;
3131
3232 // Interface goals:
6868 sys::ChangeStdinToBinary();
6969 return error_code::success();
7070 }
71
72 int OpenFlags = O_RDONLY;
73 #ifdef O_BINARY
74 OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
75 #endif
76 Fd = ::open(Filename.c_str(), OpenFlags);
77 if (Fd == -1)
78 return error_code(errno, posix_category());
79 return error_code::success();
71
72 return sys::fs::openFileForRead(Filename, Fd);
8073 }
8174 };
8275
4040 #define S_ISBLK(x) (0)
4141 #endif
4242 #endif
43 #include
4443 using namespace llvm;
4544
4645 //===----------------------------------------------------------------------===//
252251 OwningPtr &result,
253252 int64_t FileSize,
254253 bool RequiresNullTerminator) {
255 int OpenFlags = O_RDONLY;
256 #ifdef O_BINARY
257 OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
258 #endif
259 int FD = ::open(Filename, OpenFlags);
260 if (FD == -1)
261 return error_code(errno, posix_category());
254 int FD;
255 error_code EC = sys::fs::openFileForRead(Filename, FD);
256 if (EC)
257 return EC;
262258
263259 error_code ret = getOpenFile(FD, Filename, result, FileSize, FileSize,
264260 0, RequiresNullTerminator);
1717 #include
1818 #include
1919 #include
20 #include
2021
2122 #if !defined(_MSC_VER) && !defined(__MINGW32__)
2223 #include
688689 int Dummy;
689690 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
690691 true, 0, FS_Dir);
692 }
693
694 error_code openFileForWrite(const Twine &Name, int &ResultFD,
695 sys::fs::OpenFlags Flags, unsigned Mode) {
696 // Verify that we don't have both "append" and "excl".
697 assert((!(Flags & sys::fs::F_Excl) || !(Flags & sys::fs::F_Append)) &&
698 "Cannot specify both 'excl' and 'append' file creation flags!");
699
700 int OpenFlags = O_WRONLY | O_CREAT;
701
702 #ifdef O_BINARY
703 if (Flags & F_Binary)
704 OpenFlags |= O_BINARY;
705 #endif
706
707 if (Flags & F_Append)
708 OpenFlags |= O_APPEND;
709 else
710 OpenFlags |= O_TRUNC;
711
712 if (Flags & F_Excl)
713 OpenFlags |= O_EXCL;
714
715 SmallString<128> Storage;
716 StringRef P = Name.toNullTerminatedStringRef(Storage);
717 while ((ResultFD = open(P.begin(), OpenFlags, Mode)) < 0) {
718 if (errno != EINTR)
719 return error_code(errno, system_category());
720 }
721 return error_code::success();
722 }
723
724 error_code openFileForRead(const Twine &Name, int &ResultFD) {
725 int OpenFlags = O_RDONLY;
726 #ifdef O_BINARY
727 OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
728 #endif
729
730 SmallString<128> Storage;
731 StringRef P = Name.toNullTerminatedStringRef(Storage);
732 while ((ResultFD = open(P.begin(), OpenFlags)) < 0) {
733 if (errno != EINTR)
734 return error_code(errno, system_category());
735 }
736 return error_code::success();
691737 }
692738
693739 error_code make_absolute(SmallVectorImpl &path) {
6565 // compensate for this, the test-suite Makefiles have code to delete the
6666 // info output file before running commands which write to it.
6767 std::string Error;
68 raw_ostream *Result = new raw_fd_ostream(OutputFilename.c_str(),
69 Error, raw_fd_ostream::F_Append);
68 raw_ostream *Result =
69 new raw_fd_ostream(OutputFilename.c_str(), Error, sys::fs::F_Append);
7070 if (Error.empty())
7171 return Result;
7272
3636 }
3737
3838 tool_output_file::tool_output_file(const char *filename, std::string &ErrorInfo,
39 unsigned Flags)
40 : Installer(filename),
41 OS(filename, ErrorInfo, Flags) {
39 sys::fs::OpenFlags Flags)
40 : Installer(filename), OS(filename, ErrorInfo, Flags) {
4241 // If open fails, no cleanup is needed.
4342 if (!ErrorInfo.empty())
4443 Installer.Keep = true;
1717 #include "llvm/Config/config.h"
1818 #include "llvm/Support/Compiler.h"
1919 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/FileSystem.h"
2021 #include "llvm/Support/Format.h"
2122 #include "llvm/Support/Process.h"
2223 #include "llvm/Support/Program.h"
2425 #include
2526 #include
2627 #include
27 #include
2828
2929 #if defined(HAVE_UNISTD_H)
3030 # include
3131 #endif
32 #if defined(HAVE_FCNTL_H)
33 # include
34 #endif
3532 #if defined(HAVE_SYS_UIO_H) && defined(HAVE_WRITEV)
3633 # include
3734 #endif
4239
4340 #if defined(_MSC_VER)
4441 #include
45 #include
4642 #ifndef STDIN_FILENO
4743 # define STDIN_FILENO 0
4844 #endif
423419 /// stream should be immediately destroyed; the string will be empty
424420 /// if no error occurred.
425421 raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
426 unsigned Flags)
427 : Error(false), UseAtomicWrites(false), pos(0)
428 {
422 sys::fs::OpenFlags Flags)
423 : Error(false), UseAtomicWrites(false), pos(0) {
429424 assert(Filename != 0 && "Filename is null");
430 // Verify that we don't have both "append" and "excl".
431 assert((!(Flags & F_Excl) || !(Flags & F_Append)) &&
432 "Cannot specify both 'excl' and 'append' file creation flags!");
433
434425 ErrorInfo.clear();
435426
436427 // Handle "-" as stdout. Note that when we do this, we consider ourself
440431 FD = STDOUT_FILENO;
441432 // If user requested binary then put stdout into binary mode if
442433 // possible.
443 if (Flags & F_Binary)
434 if (Flags & sys::fs::F_Binary)
444435 sys::ChangeStdoutToBinary();
445436 // Close stdout when we're done, to detect any output errors.
446437 ShouldClose = true;
447438 return;
448439 }
449440
450 int OpenFlags = O_WRONLY|O_CREAT;
451 #ifdef O_BINARY
452 if (Flags & F_Binary)
453 OpenFlags |= O_BINARY;
454 #endif
455
456 if (Flags & F_Append)
457 OpenFlags |= O_APPEND;
458 else
459 OpenFlags |= O_TRUNC;
460 if (Flags & F_Excl)
461 OpenFlags |= O_EXCL;
462
463 while ((FD = open(Filename, OpenFlags, 0666)) < 0) {
464 if (errno != EINTR) {
465 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
466 ShouldClose = false;
467 return;
468 }
441 error_code EC = sys::fs::openFileForWrite(Filename, FD, Flags);
442
443 if (EC) {
444 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
445 ShouldClose = false;
446 return;
469447 }
470448
471449 // Ok, we successfully opened the file, so it'll need to be closed.
199199 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
200200 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
201201 std::string error;
202 raw_fd_ostream dest(Filename, error, raw_fd_ostream::F_Binary);
202 raw_fd_ostream dest(Filename, error, sys::fs::F_Binary);
203203 formatted_raw_ostream destf(dest);
204204 if (!error.empty()) {
205205 *ErrorMessage = strdup(error.c_str());
425425 DICompileUnit CU(CU_Nodes->getOperand(i));
426426 std::string ErrorInfo;
427427 raw_fd_ostream out(mangleName(CU, "gcno").c_str(), ErrorInfo,
428 raw_fd_ostream::F_Binary);
428 sys::fs::F_Binary);
429429 out.write("oncg", 4);
430430 out.write(ReversedVersion, 4);
431431 out.write("MVLL", 4);
6767 bool BugDriver::writeProgramToFile(const std::string &Filename,
6868 const Module *M) const {
6969 std::string ErrInfo;
70 tool_output_file Out(Filename.c_str(), ErrInfo, raw_fd_ostream::F_Binary);
70 tool_output_file Out(Filename.c_str(), ErrInfo, sys::fs::F_Binary);
7171 if (ErrInfo.empty())
7272 return writeProgramToFileAux(Out, M);
7373 return true;
144144
145145 // Open the file.
146146 std::string error;
147 unsigned OpenFlags = 0;
148 if (Binary) OpenFlags |= raw_fd_ostream::F_Binary;
147 sys::fs::OpenFlags OpenFlags = sys::fs::F_None;
148 if (Binary)
149 OpenFlags |= sys::fs::F_Binary;
149150 tool_output_file *FDOut = new tool_output_file(OutputFilename.c_str(), error,
150151 OpenFlags);
151152 if (!error.empty()) {
2525 #include "llvm/Support/raw_ostream.h"
2626 #include
2727 #include
28 #include
2928 #include
3029
3130 #if !defined(_MSC_VER) && !defined(__MINGW32__)
298297 // Implement the 'x' operation. This function extracts files back to the file
299298 // system.
300299 static void doExtract(StringRef Name, object::Archive::child_iterator I) {
301 // Open up a file stream for writing
302 // FIXME: we should abstract this, O_BINARY in particular.
303 int OpenFlags = O_TRUNC | O_WRONLY | O_CREAT;
304 #ifdef O_BINARY
305 OpenFlags |= O_BINARY;
306 #endif
307
308300 // Retain the original mode.
309301 sys::fs::perms Mode = I->getAccessMode();
310
311 int FD = open(Name.str().c_str(), OpenFlags, Mode);
312 if (FD < 0)
313 fail("Could not open output file");
302 SmallString<128> Storage = Name;
303
304 int FD;
305 failIfError(
306 sys::fs::openFileForWrite(Storage.c_str(), FD, sys::fs::F_None, Mode),
307 Storage.c_str());
314308
315309 {
316310 raw_fd_ostream file(FD, false);
558552 if (I->isNewMember()) {
559553 const char *FileName = I->getNew();
560554
561 int OpenFlags = O_RDONLY;
562 #ifdef O_BINARY
563 OpenFlags |= O_BINARY;
564 #endif
565 int FD = ::open(FileName, OpenFlags);
566 if (FD == -1)
567 return failIfError(error_code(errno, posix_category()), FileName);
555 int FD;
556 failIfError(sys::fs::openFileForRead(FileName, FD), FileName);
568557
569558 sys::fs::file_status Status;
570559 failIfError(sys::fs::status(FD, Status), FileName);
6868 }
6969
7070 std::string ErrorInfo;
71 OwningPtr Out
72 (new tool_output_file(OutputFilename.c_str(), ErrorInfo,
73 raw_fd_ostream::F_Binary));
71 OwningPtr Out(new tool_output_file(
72 OutputFilename.c_str(), ErrorInfo, sys::fs::F_Binary));
7473 if (!ErrorInfo.empty()) {
7574 errs() << ErrorInfo << '\n';
7675 exit(1);
167167 }
168168
169169 std::string ErrorInfo;
170 OwningPtr
171 Out(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
172 raw_fd_ostream::F_Binary));
170 OwningPtr Out(new tool_output_file(
171 OutputFilename.c_str(), ErrorInfo, sys::fs::F_Binary));
173172 if (!ErrorInfo.empty()) {
174173 errs() << ErrorInfo << '\n';
175174 return 1;
264264 Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
265265
266266 std::string ErrorInfo;
267 tool_output_file Out(OutputFilename.c_str(), ErrorInfo,
268 raw_fd_ostream::F_Binary);
267 tool_output_file Out(OutputFilename.c_str(), ErrorInfo, sys::fs::F_Binary);
269268 if (!ErrorInfo.empty()) {
270269 errs() << ErrorInfo << '\n';
271270 return 1;
105105 if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;
106106
107107 std::string ErrorInfo;
108 tool_output_file Out(OutputFilename.c_str(), ErrorInfo,
109 raw_fd_ostream::F_Binary);
108 tool_output_file Out(OutputFilename.c_str(), ErrorInfo, sys::fs::F_Binary);
110109 if (!ErrorInfo.empty()) {
111110 errs() << ErrorInfo << '\n';
112111 return 1;
209209 OutputFilename = "-";
210210
211211 std::string Err;
212 tool_output_file *Out = new tool_output_file(OutputFilename.c_str(), Err,
213 raw_fd_ostream::F_Binary);
212 tool_output_file *Out =
213 new tool_output_file(OutputFilename.c_str(), Err, sys::fs::F_Binary);
214214 if (!Err.empty()) {
215215 errs() << Err << '\n';
216216 delete Out;
701701
702702 std::string ErrorInfo;
703703 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
704 raw_fd_ostream::F_Binary));
704 sys::fs::F_Binary));
705705 if (!ErrorInfo.empty()) {
706706 errs() << ErrorInfo << '\n';
707707 return 1;
136136
137137 // create output file
138138 std::string ErrInfo;
139 tool_output_file Out(path, ErrInfo,
140 raw_fd_ostream::F_Binary);
139 tool_output_file Out(path, ErrInfo, sys::fs::F_Binary);
141140 if (!ErrInfo.empty()) {
142141 errMsg = "could not open bitcode file for writing: ";
143142 errMsg += path;
615615
616616 std::string ErrorInfo;
617617 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
618 raw_fd_ostream::F_Binary));
618 sys::fs::F_Binary));
619619 if (!ErrorInfo.empty()) {
620620 errs() << ErrorInfo << '\n';
621621 return 1;
678678
679679 std::string ErrorInfo;
680680 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
681 raw_fd_ostream::F_Binary));
681 sys::fs::F_Binary));
682682 if (!ErrorInfo.empty()) {
683683 errs() << ErrorInfo << '\n';
684684 return 1;
349349 SmallString<128> file_pathname(TestDirectory);
350350 path::append(file_pathname, i->filename);
351351 std::string ErrMsg;
352 raw_fd_ostream file(file_pathname.c_str(), ErrMsg,
353 raw_fd_ostream::F_Binary);
352 raw_fd_ostream file(file_pathname.c_str(), ErrMsg, sys::fs::F_Binary);
354353 ASSERT_FALSE(file.has_error());
355354 StringRef magic(i->magic_str, i->magic_str_len);
356355 file << magic;
7070 << "', contents changed.\n";
7171 std::string ErrorStr;
7272 tool_output_file OutStream(OutputFilename.c_str(), ErrorStr,
73 raw_fd_ostream::F_Binary);
73 sys::fs::F_Binary);
7474 if (!ErrorStr.empty()) {
7575 errs() << argv[0] << ": Unable to write output '"
7676 << OutputFilename << "': " << ErrorStr << '\n';