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 10370 : static sal_Bool checkOutputPath(const OString& completeName)
46 : {
47 10370 : OString sysPathName = convertToAbsoluteSystemPath(completeName);
48 10370 : OStringBuffer buffer(sysPathName.getLength());
49 :
50 10370 : if ( sysPathName.indexOf( SEPARATOR ) == -1 )
51 0 : return sal_True;
52 :
53 10370 : sal_Int32 nIndex = 0;
54 10370 : OString token(sysPathName.getToken(0, SEPARATOR, nIndex));
55 10370 : const sal_Char* p = token.getStr();
56 31110 : if (strcmp(p, "..") == 0
57 10370 : || *(p+1) == ':'
58 10370 : || strcmp(p, ".") == 0)
59 : {
60 0 : buffer.append(token);
61 0 : buffer.append(SEPARATOR);
62 : }
63 : else
64 10370 : nIndex = 0;
65 :
66 149663 : do
67 : {
68 149663 : buffer.append(sysPathName.getToken(0, SEPARATOR, nIndex));
69 :
70 149663 : if ( buffer.getLength() > 0 && nIndex != -1 )
71 : {
72 : #if defined(SAL_UNX)
73 128923 : if (mkdir((char*)buffer.getStr(), 0777) == -1)
74 : #else
75 : if (mkdir((char*)buffer.getStr()) == -1)
76 : #endif
77 : {
78 128923 : if (errno == ENOENT)
79 : {
80 : fprintf(stderr, "%s: cannot create directory '%s'\n",
81 0 : idlc()->getOptions()->getProgramName().getStr(), buffer.getStr());
82 0 : return sal_False;
83 : }
84 : } else
85 : {
86 0 : if ( !pCreatedDirectories )
87 0 : pCreatedDirectories = new StringList();
88 0 : pCreatedDirectories->push_front(buffer.getStr());
89 : }
90 : }
91 149663 : buffer.append(SEPARATOR);
92 : } while( nIndex != -1 );
93 10370 : return sal_True;
94 : }
95 :
96 0 : static sal_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 sal_False;
113 : }
114 0 : ++iter;
115 : }
116 0 : delete pCreatedDirectories;
117 : }
118 0 : return sal_True;
119 : }
120 :
121 31110 : void removeIfExists(const OString& pathname)
122 : {
123 31110 : osl::File::remove(OStringToOUString(pathname, RTL_TEXTENCODING_UTF8));
124 31110 : }
125 :
126 : sal_Int32 SAL_CALL
127 5185 : produceFile(const OString& regFileName, sPair_t const*const pDepFile)
128 : {
129 5185 : Options* pOptions = idlc()->getOptions();
130 :
131 5185 : OString regTmpName = regFileName.replaceAt(regFileName.getLength() -3, 3, "_idlc_");
132 :
133 5185 : 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 5185 : OString depTmpName;
141 5185 : if (pDepFile)
142 : {
143 : depTmpName = pDepFile->first.replaceAt(
144 5185 : regFileName.getLength() -3, 3, "_idlc_");
145 5185 : 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 5185 : removeIfExists(depTmpName);
152 : }
153 :
154 5185 : removeIfExists(regTmpName);
155 5185 : OString urlRegTmpName = convertToFileUrl(regTmpName);
156 :
157 5185 : Registry regFile;
158 5185 : 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 5185 : RegistryKey rootKey;
169 5185 : 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 5185 : 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 5185 : rootKey.releaseKey();
191 5185 : 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 5185 : 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 5185 : removeIfExists(regFileName);
214 :
215 15555 : if ( File::move(OStringToOUString(regTmpName, osl_getThreadTextEncoding()),
216 15555 : 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 5185 : removeIfExists(regTmpName);
225 :
226 5185 : if (pDepFile)
227 : {
228 5185 : removeIfExists(pDepFile->first);
229 15555 : if ( File::move(OStringToOUString(depTmpName, osl_getThreadTextEncoding()),
230 15555 : 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 5185 : removeIfExists(depTmpName);
241 : }
242 :
243 5185 : return 0;
244 : }
245 :
246 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|