nss: upgrade to release 3.73
[LibreOffice.git] / idlc / source / idlcproduce.cxx
blobff9ca2f2d47fae94ba7786d219c69334c6d9f373
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <idlc.hxx>
21 #include <astmodule.hxx>
22 #include <rtl/strbuf.hxx>
23 #include <osl/file.hxx>
24 #include <osl/thread.h>
26 #if defined(_WIN32)
27 #include <io.h>
28 #include <direct.h>
29 #include <errno.h>
30 #endif
32 #ifdef SAL_UNX
33 #include <unistd.h>
34 #include <sys/stat.h>
35 #include <errno.h>
36 #endif
38 #include <string.h>
40 using namespace ::osl;
42 static std::list< OString >* pCreatedDirectories = nullptr;
44 static bool checkOutputPath(const OString& completeName)
46 OString sysPathName = convertToAbsoluteSystemPath(completeName);
47 OStringBuffer buffer(sysPathName.getLength()+16);
49 if ( sysPathName.indexOf( SEPARATOR ) == -1 )
50 return true;
52 sal_Int32 nIndex = 0;
53 OString token(sysPathName.getToken(0, SEPARATOR, nIndex));
54 if (token.startsWith("..")
55 || (token.getLength() >= 2 && token[1] == ':')
56 || token.startsWith("."))
58 buffer.append(token);
59 buffer.append(SEPARATOR);
61 else
62 nIndex = 0;
66 buffer.append(sysPathName.getToken(0, SEPARATOR, nIndex));
68 if ( !buffer.isEmpty() && nIndex != -1 )
70 #if defined(SAL_UNX)
71 if (mkdir(buffer.getStr(), 0777) == -1)
72 #else
73 if (mkdir(buffer.getStr()) == -1)
74 #endif
76 if (errno == ENOENT)
78 fprintf(stderr, "%s: cannot create directory '%s'\n",
79 idlc()->getOptions()->getProgramName().getStr(), buffer.getStr());
80 return false;
82 } else
84 if ( !pCreatedDirectories )
85 pCreatedDirectories = new std::list< OString >;
86 pCreatedDirectories->push_front(buffer.getStr());
89 buffer.append(SEPARATOR);
90 } while( nIndex != -1 );
91 return true;
94 static bool cleanPath()
96 if ( pCreatedDirectories )
98 for (auto const& createdDirectory : *pCreatedDirectories)
100 //#ifdef SAL_UNX
101 // if (rmdir((char*)createdDirectory.getStr(), 0777) == -1)
102 //#else
103 if (rmdir(createdDirectory.getStr()) == -1)
104 //#endif
106 fprintf(stderr, "%s: cannot remove directory '%s'\n",
107 idlc()->getOptions()->getProgramName().getStr(), createdDirectory.getStr());
108 return false;
111 delete pCreatedDirectories;
113 return true;
116 void removeIfExists(const OString& pathname)
118 osl::File::remove(OStringToOUString(pathname, RTL_TEXTENCODING_UTF8));
121 sal_Int32
122 produceFile(const OString& regFileName, sPair_t const*const pDepFile)
124 Options* pOptions = idlc()->getOptions();
126 OString regTmpName = regFileName.replaceAt(regFileName.getLength() -3, 3, "_idlc_");
128 if ( !checkOutputPath(regFileName) )
130 fprintf(stderr, "%s: could not create path of registry file '%s'.\n",
131 pOptions->getProgramName().getStr(), regFileName.getStr());
132 return 1;
135 OString depTmpName;
136 if (pDepFile)
138 depTmpName = pDepFile->first.replaceAt(
139 regFileName.getLength() -3, 3, "_idlc_");
140 if ( !checkOutputPath(depTmpName) )
142 fprintf(stderr, "%s: could not create path of dep file '%s'.\n",
143 pOptions->getProgramName().getStr(), pDepFile->first.getStr());
144 return 1;
146 removeIfExists(depTmpName);
149 removeIfExists(regTmpName);
150 OString urlRegTmpName = convertToFileUrl(regTmpName);
152 Registry regFile;
153 if ( regFile.create(OStringToOUString(urlRegTmpName, RTL_TEXTENCODING_UTF8)) != RegError::NO_ERROR )
155 fprintf(stderr, "%s: could not create registry file '%s'\n",
156 pOptions->getProgramName().getStr(), regTmpName.getStr());
157 removeIfExists(regTmpName);
158 removeIfExists(regFileName);
159 cleanPath();
160 return 1;
163 RegistryKey rootKey;
164 if ( regFile.openRootKey(rootKey) != RegError::NO_ERROR )
166 fprintf(stderr, "%s: could not open root of registry file '%s'\n",
167 pOptions->getProgramName().getStr(), regFileName.getStr());
168 removeIfExists(regTmpName);
169 removeIfExists(regFileName);
170 cleanPath();
171 return 1;
174 // produce registry file
175 if ( !idlc()->getRoot()->dump(rootKey) )
177 rootKey.releaseKey();
178 if (regFile.close() != RegError::NO_ERROR)
180 fprintf(stderr, "%s: could not close registry file '%s'\n",
181 pOptions->getProgramName().getStr(), regFileName.getStr());
183 regFile.destroy(OStringToOUString(regFileName, RTL_TEXTENCODING_UTF8));
184 removeIfExists(regFileName);
185 cleanPath();
186 return 1;
189 rootKey.releaseKey();
190 if ( regFile.close() != RegError::NO_ERROR )
192 fprintf(stderr, "%s: could not close registry file '%s'\n",
193 pOptions->getProgramName().getStr(), regFileName.getStr());
194 removeIfExists(regTmpName);
195 removeIfExists(regFileName);
196 cleanPath();
197 return 1;
200 if (pDepFile && !idlc()->dumpDeps(depTmpName, pDepFile->second))
202 fprintf(stderr, "%s: could not write dep file '%s'\n",
203 pOptions->getProgramName().getStr(), pDepFile->first.getStr());
204 removeIfExists(depTmpName);
205 removeIfExists(pDepFile->first);
206 removeIfExists(regTmpName);
207 removeIfExists(regFileName);
208 cleanPath();
209 return 1;
212 removeIfExists(regFileName);
214 if ( File::move(OStringToOUString(regTmpName, osl_getThreadTextEncoding()),
215 OStringToOUString(regFileName, osl_getThreadTextEncoding())) != FileBase::E_None ) {
216 fprintf(stderr, "%s: cannot rename temporary registry '%s' to '%s'\n",
217 idlc()->getOptions()->getProgramName().getStr(),
218 regTmpName.getStr(), regFileName.getStr());
219 removeIfExists(regTmpName);
220 cleanPath();
221 return 1;
223 removeIfExists(regTmpName);
225 if (pDepFile)
227 removeIfExists(pDepFile->first);
228 if ( File::move(OStringToOUString(depTmpName, osl_getThreadTextEncoding()),
229 OStringToOUString(pDepFile->first, osl_getThreadTextEncoding())) != FileBase::E_None ) {
230 fprintf(stderr, "%s: cannot rename dep file '%s' to '%s'\n",
231 idlc()->getOptions()->getProgramName().getStr(),
232 depTmpName.getStr(), pDepFile->first.getStr());
233 removeIfExists(depTmpName);
234 removeIfExists(pDepFile->first);
235 removeIfExists(regFileName);
236 cleanPath();
237 return 1;
239 removeIfExists(depTmpName);
242 return 0;
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */