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