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 144 : static bool checkOutputPath(const OString& completeName)
45 : {
46 144 : OString sysPathName = convertToAbsoluteSystemPath(completeName);
47 288 : OStringBuffer buffer(sysPathName.getLength());
48 :
49 144 : if ( sysPathName.indexOf( SEPARATOR ) == -1 )
50 0 : return true;
51 :
52 144 : sal_Int32 nIndex = 0;
53 288 : OString token(sysPathName.getToken(0, SEPARATOR, nIndex));
54 144 : const sal_Char* p = token.getStr();
55 144 : if (strcmp(p, "..") == 0
56 144 : || *(p+1) == ':'
57 144 : || strcmp(p, ".") == 0)
58 : {
59 0 : buffer.append(token);
60 0 : buffer.append(SEPARATOR);
61 : }
62 : else
63 144 : nIndex = 0;
64 :
65 1584 : do
66 : {
67 1584 : buffer.append(sysPathName.getToken(0, SEPARATOR, nIndex));
68 :
69 1584 : if ( !buffer.isEmpty() && nIndex != -1 )
70 : {
71 : #if defined(SAL_UNX)
72 1296 : if (mkdir(buffer.getStr(), 0777) == -1)
73 : #else
74 : if (mkdir((char*)buffer.getStr()) == -1)
75 : #endif
76 : {
77 1296 : 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 1584 : buffer.append(SEPARATOR);
91 1584 : } while( nIndex != -1 );
92 288 : 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((*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 617 : void removeIfExists(const OString& pathname)
121 : {
122 617 : osl::File::remove(OStringToOUString(pathname, RTL_TEXTENCODING_UTF8));
123 617 : }
124 :
125 : sal_Int32 SAL_CALL
126 144 : produceFile(const OString& regFileName, sPair_t const*const pDepFile)
127 : {
128 144 : Options* pOptions = idlc()->getOptions();
129 :
130 144 : OString regTmpName = regFileName.replaceAt(regFileName.getLength() -3, 3, "_idlc_");
131 :
132 144 : 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 288 : OString depTmpName;
140 144 : 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 144 : removeIfExists(regTmpName);
154 288 : OString urlRegTmpName = convertToFileUrl(regTmpName);
155 :
156 288 : Registry regFile;
157 144 : if ( regFile.create(OStringToOUString(urlRegTmpName, RTL_TEXTENCODING_UTF8)) != RegError::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 288 : RegistryKey rootKey;
168 144 : if ( regFile.openRootKey(rootKey) != RegError::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 144 : if ( !idlc()->getRoot()->dump(rootKey) )
180 : {
181 0 : rootKey.releaseKey();
182 0 : if (regFile.close() != RegError::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 144 : rootKey.releaseKey();
194 144 : if ( regFile.close() != RegError::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 144 : 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 144 : removeIfExists(regFileName);
217 :
218 432 : if ( File::move(OStringToOUString(regTmpName, osl_getThreadTextEncoding()),
219 432 : 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 144 : removeIfExists(regTmpName);
228 :
229 144 : 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 288 : return 0;
247 : }
248 :
249 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|