1*2fe1ca3dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*2fe1ca3dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*2fe1ca3dSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*2fe1ca3dSAndrew Rist * distributed with this work for additional information
6*2fe1ca3dSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*2fe1ca3dSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*2fe1ca3dSAndrew Rist * "License"); you may not use this file except in compliance
9*2fe1ca3dSAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*2fe1ca3dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*2fe1ca3dSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*2fe1ca3dSAndrew Rist * software distributed under the License is distributed on an
15*2fe1ca3dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2fe1ca3dSAndrew Rist * KIND, either express or implied. See the License for the
17*2fe1ca3dSAndrew Rist * specific language governing permissions and limitations
18*2fe1ca3dSAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*2fe1ca3dSAndrew Rist *************************************************************/
21*2fe1ca3dSAndrew Rist
22*2fe1ca3dSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_idlc.hxx"
26cdf0e10cSrcweir #include <idlc/idlc.hxx>
27cdf0e10cSrcweir #include <idlc/astmodule.hxx>
28cdf0e10cSrcweir #include <rtl/strbuf.hxx>
29cdf0e10cSrcweir #include <osl/file.hxx>
30cdf0e10cSrcweir #include <osl/thread.h>
31cdf0e10cSrcweir
32cdf0e10cSrcweir #if defined(SAL_W32) || defined(SAL_OS2)
33cdf0e10cSrcweir #include <io.h>
34cdf0e10cSrcweir #include <direct.h>
35cdf0e10cSrcweir #include <errno.h>
36cdf0e10cSrcweir #endif
37cdf0e10cSrcweir
38cdf0e10cSrcweir #ifdef SAL_UNX
39cdf0e10cSrcweir #include <unistd.h>
40cdf0e10cSrcweir #include <sys/stat.h>
41cdf0e10cSrcweir #include <errno.h>
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir
44cdf0e10cSrcweir #include <string.h>
45cdf0e10cSrcweir
46cdf0e10cSrcweir using namespace ::rtl;
47cdf0e10cSrcweir using namespace ::osl;
48cdf0e10cSrcweir
49cdf0e10cSrcweir StringList* pCreatedDirectories = NULL;
50cdf0e10cSrcweir
checkOutputPath(const OString & completeName)51cdf0e10cSrcweir static sal_Bool checkOutputPath(const OString& completeName)
52cdf0e10cSrcweir {
53cdf0e10cSrcweir OString sysPathName = convertToAbsoluteSystemPath(completeName);
54cdf0e10cSrcweir OStringBuffer buffer(sysPathName.getLength());
55cdf0e10cSrcweir
56cdf0e10cSrcweir if ( sysPathName.indexOf( SEPARATOR ) == -1 )
57cdf0e10cSrcweir return sal_True;
58cdf0e10cSrcweir
59cdf0e10cSrcweir sal_Int32 nIndex = 0;
60cdf0e10cSrcweir OString token(sysPathName.getToken(0, SEPARATOR, nIndex));
61cdf0e10cSrcweir const sal_Char* p = token.getStr();
62cdf0e10cSrcweir if (strcmp(p, "..") == 0
63cdf0e10cSrcweir || *(p+1) == ':'
64cdf0e10cSrcweir || strcmp(p, ".") == 0)
65cdf0e10cSrcweir {
66cdf0e10cSrcweir buffer.append(token);
67cdf0e10cSrcweir buffer.append(SEPARATOR);
68cdf0e10cSrcweir }
69cdf0e10cSrcweir else
70cdf0e10cSrcweir nIndex = 0;
71cdf0e10cSrcweir
72cdf0e10cSrcweir do
73cdf0e10cSrcweir {
74cdf0e10cSrcweir buffer.append(sysPathName.getToken(0, SEPARATOR, nIndex));
75cdf0e10cSrcweir
76cdf0e10cSrcweir if ( buffer.getLength() > 0 && nIndex != -1 )
77cdf0e10cSrcweir {
78cdf0e10cSrcweir #if defined(SAL_UNX) || defined(SAL_OS2)
79cdf0e10cSrcweir if (mkdir((char*)buffer.getStr(), 0777) == -1)
80cdf0e10cSrcweir #else
81cdf0e10cSrcweir if (mkdir((char*)buffer.getStr()) == -1)
82cdf0e10cSrcweir #endif
83cdf0e10cSrcweir {
84cdf0e10cSrcweir if (errno == ENOENT)
85cdf0e10cSrcweir {
86cdf0e10cSrcweir fprintf(stderr, "%s: cannot create directory '%s'\n",
87cdf0e10cSrcweir idlc()->getOptions()->getProgramName().getStr(), buffer.getStr());
88cdf0e10cSrcweir return sal_False;
89cdf0e10cSrcweir }
90cdf0e10cSrcweir } else
91cdf0e10cSrcweir {
92cdf0e10cSrcweir if ( !pCreatedDirectories )
93cdf0e10cSrcweir pCreatedDirectories = new StringList();
94cdf0e10cSrcweir pCreatedDirectories->push_front(buffer.getStr());
95cdf0e10cSrcweir }
96cdf0e10cSrcweir }
97cdf0e10cSrcweir buffer.append(SEPARATOR);
98cdf0e10cSrcweir } while( nIndex != -1 );
99cdf0e10cSrcweir return sal_True;
100cdf0e10cSrcweir }
101cdf0e10cSrcweir
cleanPath()102cdf0e10cSrcweir static sal_Bool cleanPath()
103cdf0e10cSrcweir {
104cdf0e10cSrcweir if ( pCreatedDirectories )
105cdf0e10cSrcweir {
106cdf0e10cSrcweir StringList::iterator iter = pCreatedDirectories->begin();
107cdf0e10cSrcweir StringList::iterator end = pCreatedDirectories->end();
108cdf0e10cSrcweir while ( iter != end )
109cdf0e10cSrcweir {
110cdf0e10cSrcweir //#ifdef SAL_UNX
111cdf0e10cSrcweir // if (rmdir((char*)(*iter).getStr(), 0777) == -1)
112cdf0e10cSrcweir //#else
113cdf0e10cSrcweir if (rmdir((char*)(*iter).getStr()) == -1)
114cdf0e10cSrcweir //#endif
115cdf0e10cSrcweir {
116cdf0e10cSrcweir fprintf(stderr, "%s: cannot remove directory '%s'\n",
117cdf0e10cSrcweir idlc()->getOptions()->getProgramName().getStr(), (*iter).getStr());
118cdf0e10cSrcweir return sal_False;
119cdf0e10cSrcweir }
120cdf0e10cSrcweir ++iter;
121cdf0e10cSrcweir }
122cdf0e10cSrcweir delete pCreatedDirectories;
123cdf0e10cSrcweir }
124cdf0e10cSrcweir return sal_True;
125cdf0e10cSrcweir }
126cdf0e10cSrcweir
removeIfExists(const OString & pathname)127cdf0e10cSrcweir void removeIfExists(const OString& pathname)
128cdf0e10cSrcweir {
129cdf0e10cSrcweir unlink(pathname.getStr());
130cdf0e10cSrcweir }
131cdf0e10cSrcweir
produceFile(const OString & regFileName)132cdf0e10cSrcweir sal_Int32 SAL_CALL produceFile(const OString& regFileName)
133cdf0e10cSrcweir {
134cdf0e10cSrcweir Options* pOptions = idlc()->getOptions();
135cdf0e10cSrcweir
136cdf0e10cSrcweir OString regTmpName = regFileName.replaceAt(regFileName.getLength() -3, 3, "_idlc_");
137cdf0e10cSrcweir
138cdf0e10cSrcweir if ( !checkOutputPath(regFileName) )
139cdf0e10cSrcweir {
140cdf0e10cSrcweir fprintf(stderr, "%s: could not create path of registry file '%s'.\n",
141cdf0e10cSrcweir pOptions->getProgramName().getStr(), regFileName.getStr());
142cdf0e10cSrcweir return 1;
143cdf0e10cSrcweir }
144cdf0e10cSrcweir
145cdf0e10cSrcweir removeIfExists(regTmpName);
146cdf0e10cSrcweir OString urlRegTmpName = convertToFileUrl(regTmpName);
147cdf0e10cSrcweir
148cdf0e10cSrcweir Registry regFile;
149cdf0e10cSrcweir if ( regFile.create(OStringToOUString(urlRegTmpName, RTL_TEXTENCODING_UTF8)) != REG_NO_ERROR )
150cdf0e10cSrcweir {
151cdf0e10cSrcweir fprintf(stderr, "%s: could not create registry file '%s'\n",
152cdf0e10cSrcweir pOptions->getProgramName().getStr(), regTmpName.getStr());
153cdf0e10cSrcweir removeIfExists(regTmpName);
154cdf0e10cSrcweir removeIfExists(regFileName);
155cdf0e10cSrcweir cleanPath();
156cdf0e10cSrcweir return 1;
157cdf0e10cSrcweir }
158cdf0e10cSrcweir
159cdf0e10cSrcweir RegistryKey rootKey;
160cdf0e10cSrcweir if ( regFile.openRootKey(rootKey) != REG_NO_ERROR )
161cdf0e10cSrcweir {
162cdf0e10cSrcweir fprintf(stderr, "%s: could not open root of registry file '%s'\n",
163cdf0e10cSrcweir pOptions->getProgramName().getStr(), regFileName.getStr());
164cdf0e10cSrcweir removeIfExists(regTmpName);
165cdf0e10cSrcweir removeIfExists(regFileName);
166cdf0e10cSrcweir cleanPath();
167cdf0e10cSrcweir return 1;
168cdf0e10cSrcweir }
169cdf0e10cSrcweir
170cdf0e10cSrcweir // produce registry file
171cdf0e10cSrcweir if ( !idlc()->getRoot()->dump(rootKey) )
172cdf0e10cSrcweir {
173cdf0e10cSrcweir rootKey.releaseKey();
174cdf0e10cSrcweir regFile.close();
175cdf0e10cSrcweir regFile.destroy(OStringToOUString(regFileName, RTL_TEXTENCODING_UTF8));
176cdf0e10cSrcweir removeIfExists(regFileName);
177cdf0e10cSrcweir cleanPath();
178cdf0e10cSrcweir return 1;
179cdf0e10cSrcweir }
180cdf0e10cSrcweir
181cdf0e10cSrcweir rootKey.releaseKey();
182cdf0e10cSrcweir if ( regFile.close() != REG_NO_ERROR )
183cdf0e10cSrcweir {
184cdf0e10cSrcweir fprintf(stderr, "%s: could not close registry file '%s'\n",
185cdf0e10cSrcweir pOptions->getProgramName().getStr(), regFileName.getStr());
186cdf0e10cSrcweir removeIfExists(regTmpName);
187cdf0e10cSrcweir removeIfExists(regFileName);
188cdf0e10cSrcweir cleanPath();
189cdf0e10cSrcweir return 1;
190cdf0e10cSrcweir }
191cdf0e10cSrcweir
192cdf0e10cSrcweir removeIfExists(regFileName);
193cdf0e10cSrcweir
194cdf0e10cSrcweir if ( File::move(OStringToOUString(regTmpName, osl_getThreadTextEncoding()),
195cdf0e10cSrcweir OStringToOUString(regFileName, osl_getThreadTextEncoding())) != FileBase::E_None ) {
196cdf0e10cSrcweir fprintf(stderr, "%s: cannot rename temporary registry '%s' to '%s'\n",
197cdf0e10cSrcweir idlc()->getOptions()->getProgramName().getStr(),
198cdf0e10cSrcweir regTmpName.getStr(), regFileName.getStr());
199cdf0e10cSrcweir removeIfExists(regTmpName);
200cdf0e10cSrcweir cleanPath();
201cdf0e10cSrcweir return 1;
202cdf0e10cSrcweir }
203cdf0e10cSrcweir removeIfExists(regTmpName);
204cdf0e10cSrcweir
205cdf0e10cSrcweir return 0;
206cdf0e10cSrcweir }
207