Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <idlc/idlc.hxx>
21 : #include <idlc/astmodule.hxx>
22 : #include <rtl/strbuf.hxx>
23 : #include <osl/file.hxx>
24 : #include <osl/thread.h>
25 :
26 : #if defined(SAL_W32)
27 : #include <io.h>
28 : #include <direct.h>
29 : #include <errno.h>
30 : #endif
31 :
32 : #ifdef SAL_UNX
33 : #include <unistd.h>
34 : #include <sys/stat.h>
35 : #include <errno.h>
36 : #endif
37 :
38 : #include <string.h>
39 :
40 : using namespace ::rtl;
41 : using namespace ::osl;
42 :
43 : StringList* pCreatedDirectories = NULL;
44 :
45 0 : static bool checkOutputPath(const OString& completeName)
46 : {
47 0 : OString sysPathName = convertToAbsoluteSystemPath(completeName);
48 0 : OStringBuffer buffer(sysPathName.getLength());
49 :
50 0 : if ( sysPathName.indexOf( SEPARATOR ) == -1 )
51 0 : return true;
52 :
53 0 : sal_Int32 nIndex = 0;
54 0 : OString token(sysPathName.getToken(0, SEPARATOR, nIndex));
55 0 : const sal_Char* p = token.getStr();
56 0 : if (strcmp(p, "..") == 0
57 0 : || *(p+1) == ':'
58 0 : || strcmp(p, ".") == 0)
59 : {
60 0 : buffer.append(token);
61 0 : buffer.append(SEPARATOR);
62 : }
63 : else
64 0 : nIndex = 0;
65 :
66 0 : do
67 : {
68 0 : buffer.append(sysPathName.getToken(0, SEPARATOR, nIndex));
69 :
70 0 : if ( !buffer.isEmpty() && nIndex != -1 )
71 : {
72 : #if defined(SAL_UNX)
73 0 : if (mkdir((char*)buffer.getStr(), 0777) == -1)
74 : #else
75 : if (mkdir((char*)buffer.getStr()) == -1)
76 : #endif
77 : {
78 0 : if (errno == ENOENT)
79 : {
80 : fprintf(stderr, "%s: cannot create directory '%s'\n",
81 0 : idlc()->getOptions()->getProgramName().getStr(), buffer.getStr());
82 0 : return false;
83 : }
84 : } else
85 : {
86 0 : if ( !pCreatedDirectories )
87 0 : pCreatedDirectories = new StringList();
88 0 : pCreatedDirectories->push_front(buffer.getStr());
89 : }
90 : }
91 0 : buffer.append(SEPARATOR);
92 0 : } while( nIndex != -1 );
93 0 : return true;
94 : }
95 :
96 0 : static bool cleanPath()
97 : {
98 0 : if ( pCreatedDirectories )
99 : {
100 0 : StringList::iterator iter = pCreatedDirectories->begin();
101 0 : StringList::iterator end = pCreatedDirectories->end();
102 0 : while ( iter != end )
103 : {
104 : //#ifdef SAL_UNX
105 : // if (rmdir((char*)(*iter).getStr(), 0777) == -1)
106 : //#else
107 0 : if (rmdir((char*)(*iter).getStr()) == -1)
108 : //#endif
109 : {
110 : fprintf(stderr, "%s: cannot remove directory '%s'\n",
111 0 : idlc()->getOptions()->getProgramName().getStr(), (*iter).getStr());
112 0 : return false;
113 : }
114 0 : ++iter;
115 : }
116 0 : delete pCreatedDirectories;
117 : }
118 0 : return true;
119 : }
120 :
121 0 : void removeIfExists(const OString& pathname)
122 : {
123 0 : osl::File::remove(OStringToOUString(pathname, RTL_TEXTENCODING_UTF8));
124 0 : }
125 :
126 : sal_Int32 SAL_CALL
127 0 : produceFile(const OString& regFileName, sPair_t const*const pDepFile)
128 : {
129 0 : Options* pOptions = idlc()->getOptions();
130 :
131 0 : OString regTmpName = regFileName.replaceAt(regFileName.getLength() -3, 3, "_idlc_");
132 :
133 0 : if ( !checkOutputPath(regFileName) )
134 : {
135 : fprintf(stderr, "%s: could not create path of registry file '%s'.\n",
136 0 : pOptions->getProgramName().getStr(), regFileName.getStr());
137 0 : return 1;
138 : }
139 :
140 0 : OString depTmpName;
141 0 : if (pDepFile)
142 : {
143 0 : depTmpName = pDepFile->first.replaceAt(
144 0 : regFileName.getLength() -3, 3, "_idlc_");
145 0 : if ( !checkOutputPath(depTmpName) )
146 : {
147 : fprintf(stderr, "%s: could not create path of dep file '%s'.\n",
148 0 : pOptions->getProgramName().getStr(), pDepFile->first.getStr());
149 0 : return 1;
150 : }
151 0 : removeIfExists(depTmpName);
152 : }
153 :
154 0 : removeIfExists(regTmpName);
155 0 : OString urlRegTmpName = convertToFileUrl(regTmpName);
156 :
157 0 : Registry regFile;
158 0 : if ( regFile.create(OStringToOUString(urlRegTmpName, RTL_TEXTENCODING_UTF8)) != REG_NO_ERROR )
159 : {
160 : fprintf(stderr, "%s: could not create registry file '%s'\n",
161 0 : pOptions->getProgramName().getStr(), regTmpName.getStr());
162 0 : removeIfExists(regTmpName);
163 0 : removeIfExists(regFileName);
164 0 : cleanPath();
165 0 : return 1;
166 : }
167 :
168 0 : RegistryKey rootKey;
169 0 : if ( regFile.openRootKey(rootKey) != REG_NO_ERROR )
170 : {
171 : fprintf(stderr, "%s: could not open root of registry file '%s'\n",
172 0 : pOptions->getProgramName().getStr(), regFileName.getStr());
173 0 : removeIfExists(regTmpName);
174 0 : removeIfExists(regFileName);
175 0 : cleanPath();
176 0 : return 1;
177 : }
178 :
179 : // produce registry file
180 0 : if ( !idlc()->getRoot()->dump(rootKey) )
181 : {
182 0 : rootKey.releaseKey();
183 0 : regFile.close();
184 0 : regFile.destroy(OStringToOUString(regFileName, RTL_TEXTENCODING_UTF8));
185 0 : removeIfExists(regFileName);
186 0 : cleanPath();
187 0 : return 1;
188 : }
189 :
190 0 : rootKey.releaseKey();
191 0 : if ( regFile.close() != REG_NO_ERROR )
192 : {
193 : fprintf(stderr, "%s: could not close registry file '%s'\n",
194 0 : pOptions->getProgramName().getStr(), regFileName.getStr());
195 0 : removeIfExists(regTmpName);
196 0 : removeIfExists(regFileName);
197 0 : cleanPath();
198 0 : return 1;
199 : }
200 :
201 0 : if (pDepFile && !idlc()->dumpDeps(depTmpName, pDepFile->second))
202 : {
203 : fprintf(stderr, "%s: could not write dep file '%s'\n",
204 0 : pOptions->getProgramName().getStr(), pDepFile->first.getStr());
205 0 : removeIfExists(depTmpName);
206 0 : removeIfExists(pDepFile->first);
207 0 : removeIfExists(regTmpName);
208 0 : removeIfExists(regFileName);
209 0 : cleanPath();
210 0 : return 1;
211 : }
212 :
213 0 : removeIfExists(regFileName);
214 :
215 0 : if ( File::move(OStringToOUString(regTmpName, osl_getThreadTextEncoding()),
216 0 : OStringToOUString(regFileName, osl_getThreadTextEncoding())) != FileBase::E_None ) {
217 : fprintf(stderr, "%s: cannot rename temporary registry '%s' to '%s'\n",
218 0 : idlc()->getOptions()->getProgramName().getStr(),
219 0 : regTmpName.getStr(), regFileName.getStr());
220 0 : removeIfExists(regTmpName);
221 0 : cleanPath();
222 0 : return 1;
223 : }
224 0 : removeIfExists(regTmpName);
225 :
226 0 : if (pDepFile)
227 : {
228 0 : removeIfExists(pDepFile->first);
229 0 : if ( File::move(OStringToOUString(depTmpName, osl_getThreadTextEncoding()),
230 0 : OStringToOUString(pDepFile->first, osl_getThreadTextEncoding())) != FileBase::E_None ) {
231 : fprintf(stderr, "%s: cannot rename dep file '%s' to '%s'\n",
232 0 : idlc()->getOptions()->getProgramName().getStr(),
233 0 : depTmpName.getStr(), pDepFile->first.getStr());
234 0 : removeIfExists(depTmpName);
235 0 : removeIfExists(pDepFile->first);
236 0 : removeIfExists(regFileName);
237 0 : cleanPath();
238 0 : return 1;
239 : }
240 0 : removeIfExists(depTmpName);
241 : }
242 :
243 0 : return 0;
244 : }
245 :
246 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|