llvm.org GIT mirror llvm / 4874476
Make the sys::Path::GetTemporaryDirectory method not throw exceptions and adjust users of it to compensate. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29831 91177308-0d34-0410-b5e6-96231b3b80d8 Reid Spencer 14 years ago
4 changed file(s) with 70 addition(s) and 37 deletion(s). Raw diff Collapse all Expand all
243243 sys::Path WriteGraph(const GraphType &G,
244244 const std::string& Name,
245245 const std::string& Title = "") {
246 sys::Path Filename = sys::Path::GetTemporaryDirectory();;
246 std::string ErrMsg;
247 sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
248 if (Filename.isEmpty()) {
249 std::cerr << "Error: " << ErrMsg << "\n";
250 return Filename;
251 }
247252 Filename.appendComponent(Name + ".dot");
248253 Filename.makeUnique();
249254 std::cerr << "Writing '" << Filename << "'... ";
9696 /// a "standard" place for the operating system. The directory is
9797 /// guaranteed to be created on exit from this function. If the directory
9898 /// cannot be created, the function will throw an exception.
99 /// @throws std::string indicating why the directory could not be created.
99 /// @returns an invalid path (empty) on error
100 /// @param ErrMsg Optional place for an error message if an error occurs
100101 /// @brief Constrct a path to an new, unique, existing temporary
101102 /// directory.
102 static Path GetTemporaryDirectory();
103 static Path GetTemporaryDirectory(std::string* ErrMsg);
103104
104105 /// Construct a vector of sys::Path that contains the "standard" system
105106 /// library paths suitable for linking into programs. This function *must*
170171 /// @throws std::string if \p unverified_path is not legal.
171172 /// @param unverified_path The path to verify and assign.
172173 /// @brief Construct a Path from a string.
173 explicit Path(const std::string& unverified_path);
174 explicit Path(const std::string& p) : path(p) {}
174175
175176 /// @}
176177 /// @name Operators
6262 namespace llvm {
6363 using namespace sys;
6464
65 Path::Path(const std::string& unverified_path) : path(unverified_path) {
66 if (unverified_path.empty())
67 return;
68 if (this->isValid())
69 return;
70 // oops, not valid.
71 path.clear();
72 ThrowErrno(unverified_path + ": path is not valid");
73 }
74
7565 bool
7666 Path::isValid() const {
7767 // Check some obvious things
9686 }
9787
9888 Path
99 Path::GetTemporaryDirectory() {
89 Path::GetTemporaryDirectory(std::string* ErrMsg ) {
10090 #if defined(HAVE_MKDTEMP)
10191 // The best way is with mkdtemp but that's not available on many systems,
10292 // Linux and FreeBSD have it. Others probably won't.
10393 char pathname[MAXPATHLEN];
10494 strcpy(pathname,"/tmp/llvm_XXXXXX");
105 if (0 == mkdtemp(pathname))
106 ThrowErrno(std::string(pathname) + ": can't create temporary directory");
95 if (0 == mkdtemp(pathname)) {
96 MakeErrMsg(ErrMsg,
97 std::string(pathname) + ": can't create temporary directory");
98 return Path();
99 }
107100 Path result;
108101 result.set(pathname);
109102 assert(result.isValid() && "mkdtemp didn't create a valid pathname!");
117110 char pathname[MAXPATHLEN];
118111 strcpy(pathname, "/tmp/llvm_XXXXXX");
119112 int fd = 0;
120 if (-1 == (fd = mkstemp(pathname)))
121 ThrowErrno(std::string(pathname) + ": can't create temporary directory");
113 if (-1 == (fd = mkstemp(pathname))) {
114 MakeErrMsg(ErrMsg,
115 std::string(pathname) + ": can't create temporary directory");
116 return Path();
117 }
122118 ::close(fd);
123119 ::unlink(pathname); // start race condition, ignore errors
124 if (-1 == ::mkdir(pathname, S_IRWXU)) // end race condition
125 ThrowErrno(std::string(pathname) + ": can't create temporary directory");
120 if (-1 == ::mkdir(pathname, S_IRWXU)) { // end race condition
121 MakeErrMsg(ErrMsg,
122 std::string(pathname) + ": can't create temporary directory");
123 return Path();
124 }
126125 Path result;
127126 result.set(pathname);
128127 assert(result.isValid() && "mkstemp didn't create a valid pathname!");
136135 char pathname[MAXPATHLEN];
137136 strcpy(pathname, "/tmp/llvm_XXXXXX");
138137 char *TmpName = ::mktemp(pathname);
139 if (TmpName == 0)
140 ThrowErrno(std::string(TmpName) + ": can't create unique directory name");
141 if (-1 == ::mkdir(TmpName, S_IRWXU))
142 ThrowErrno(std::string(TmpName) + ": can't create temporary directory");
138 if (TmpName == 0) {
139 MakeErrMsg(ErrMsg,
140 std::string(TmpName) + ": can't create unique directory name");
141 return Path();
142 }
143 if (-1 == ::mkdir(TmpName, S_IRWXU)) {
144 MakeErrMsg(ErrMsg,
145 std::string(TmpName) + ": can't create temporary directory");
146 return Path();
147 }
143148 Path result;
144149 result.set(TmpName);
145150 assert(result.isValid() && "mktemp didn't create a valid pathname!");
159164 num++;
160165 sprintf(pathname, "/tmp/llvm_%010u", unsigned(num));
161166 } while ( 0 == access(pathname, F_OK ) );
162 if (-1 == ::mkdir(pathname, S_IRWXU))
163 ThrowErrno(std::string(pathname) + ": can't create temporary directory");
167 if (-1 == ::mkdir(pathname, S_IRWXU)) {
168 MakeErrMsg(ErrMsg,
169 std::string(pathname) + ": can't create temporary directory");
170 return Path();
164171 Path result;
165172 result.set(pathname);
166173 assert(result.isValid() && "mkstemp didn't create a valid pathname!");
8282 , TempDir()
8383 , AdditionalArgs()
8484 {
85 TempDir = sys::Path::GetTemporaryDirectory();
86 sys::RemoveDirectoryOnSignal(TempDir);
8785 AdditionalArgs.reserve(NUM_PHASES);
8886 StringVector emptyVec;
8987 for (unsigned i = 0; i < NUM_PHASES; ++i)
195193 }
196194
197195 sys::Path MakeTempFile(const std::string& basename,
198 const std::string& suffix) {
196 const std::string& suffix,
197 std::string* ErrMsg) {
198 if (TempDir.isEmpty()) {
199 TempDir = sys::Path::GetTemporaryDirectory(ErrMsg);
200 if (TempDir.isEmpty())
201 return sys::Path();
202 sys::RemoveDirectoryOnSignal(TempDir);
203 }
199204 sys::Path result(TempDir);
200 if (!result.appendComponent(basename))
201 throw basename + ": can't use this file name";
202 if (!result.appendSuffix(suffix))
203 throw suffix + ": can't use this file suffix";
205 if (!result.appendComponent(basename)) {
206 if (ErrMsg)
207 *ErrMsg = basename + ": can't use this file name";
208 return sys::Path();
209 }
210 if (!result.appendSuffix(suffix)) {
211 if (ErrMsg)
212 *ErrMsg = suffix + ": can't use this file suffix";
213 return sys::Path();
214 }
204215 return result;
205216 }
206217
699710 actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
700711 }
701712 } else {
702 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"E"));
713 sys::Path TempFile(
714 MakeTempFile(I->first.getBasename(),"E",&ErrMsg));
715 if (TempFile.isEmpty())
716 return 1;
703717 actions.push_back(GetAction(cd,InFile,TempFile,
704718 PREPROCESSING));
705719 InFile = TempFile;
730744 actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
731745 }
732746 } else {
733 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"trans"));
747 sys::Path TempFile(
748 MakeTempFile(I->first.getBasename(),"trans", &ErrMsg));
749 if (TempFile.isEmpty())
750 return 1;
734751 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
735752 InFile = TempFile;
736753 }
773790 actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
774791 }
775792 } else {
776 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"opt"));
793 sys::Path TempFile(
794 MakeTempFile(I->first.getBasename(),"opt", &ErrMsg));
795 if (TempFile.isEmpty())
796 return 1;
777797 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
778798 InFile = TempFile;
779799 }