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 "osl/process.h"
21 : #include "rtl/strbuf.hxx"
22 : #include "rtl/ustring.hxx"
23 : #include "osl/thread.h"
24 : #include "osl/file.hxx"
25 :
26 : #include <string.h>
27 : #if defined(SAL_W32)
28 : #include <io.h>
29 :
30 : #include <direct.h>
31 : #include <errno.h>
32 : #endif
33 :
34 : #ifdef UNX
35 : #include <sys/stat.h>
36 : #include <errno.h>
37 : #include <unistd.h>
38 : #endif
39 :
40 : #include "codemaker/global.hxx"
41 :
42 : #ifdef SAL_UNX
43 : #define SEPARATOR '/'
44 : #else
45 : #define SEPARATOR '\\'
46 : #endif
47 :
48 : using namespace ::rtl;
49 : using namespace ::osl;
50 :
51 :
52 0 : OString getTempDir(const OString& sFileName)
53 : {
54 0 : sal_Int32 index = 0;
55 : #ifdef SAL_UNX
56 0 : if ((index=sFileName.lastIndexOf('/')) > 0)
57 0 : return sFileName.copy(0, index);
58 : #else
59 : if ((index=sFileName.lastIndexOf('\\')) > 0)
60 : return sFileName.copy(0, index);
61 : #endif
62 0 : return ".";
63 : }
64 :
65 0 : OString createFileNameFromType( const OString& destination,
66 : const OString& typeName,
67 : const OString& postfix,
68 : bool bLowerCase,
69 : const OString& prefix )
70 : {
71 0 : OString type(typeName.replace('.', '/'));
72 :
73 0 : if (bLowerCase)
74 : {
75 0 : type = typeName.toAsciiLowerCase();
76 : }
77 :
78 0 : sal_uInt32 length = destination.getLength();
79 :
80 0 : bool withPoint = false;
81 0 : if (length == 0)
82 : {
83 0 : length++;
84 0 : withPoint = true;
85 : }
86 :
87 0 : length += prefix.getLength() + type.getLength() + postfix.getLength();
88 :
89 0 : bool withSeparator = false;
90 0 : if (!(destination.endsWith("\\") || destination.endsWith("/"))
91 0 : && !(type.startsWith("\\") || type.startsWith("/")))
92 : {
93 0 : length++;
94 0 : withSeparator = true;
95 : }
96 :
97 0 : OStringBuffer fileNameBuf(length);
98 :
99 0 : if (withPoint)
100 0 : fileNameBuf.append('.');
101 : else
102 0 : fileNameBuf.append(destination.getStr(), destination.getLength());
103 :
104 0 : if (withSeparator)
105 0 : fileNameBuf.append("/", 1);
106 :
107 0 : OString tmpStr(type);
108 0 : if (!prefix.isEmpty())
109 : {
110 0 : tmpStr = type.replaceAt(type.lastIndexOf('/')+1, 0, prefix);
111 : }
112 :
113 0 : fileNameBuf.append(tmpStr.getStr(), tmpStr.getLength());
114 0 : fileNameBuf.append(postfix.getStr(), postfix.getLength());
115 :
116 0 : OString fileName(fileNameBuf.makeStringAndClear());
117 :
118 : sal_Char token;
119 : #ifdef SAL_UNX
120 0 : fileName = fileName.replace('\\', '/');
121 0 : token = '/';
122 : #else
123 : fileName = fileName.replace('/', '\\');
124 : token = '\\';
125 : #endif
126 :
127 0 : OStringBuffer buffer(length);
128 :
129 0 : sal_Int32 nIndex = 0;
130 0 : do
131 : {
132 0 : buffer.append(fileName.getToken(0, token, nIndex).getStr());
133 0 : if( nIndex == -1 )
134 0 : break;
135 :
136 0 : if (buffer.isEmpty() || OString(".") == buffer.getStr())
137 : {
138 0 : buffer.append(token);
139 0 : continue;
140 : }
141 :
142 : #if defined(SAL_UNX)
143 0 : if (mkdir((char*)buffer.getStr(), 0777) == -1)
144 : #else
145 : if (mkdir((char*)buffer.getStr()) == -1)
146 : #endif
147 : {
148 0 : if ( errno == ENOENT )
149 0 : return OString();
150 : }
151 :
152 0 : buffer.append(token);
153 0 : } while( nIndex != -1 );
154 :
155 0 : OUString uSysFileName;
156 0 : OSL_VERIFY( FileBase::getSystemPathFromFileURL(
157 : convertToFileUrl(fileName), uSysFileName) == FileBase::E_None );
158 0 : return OUStringToOString(uSysFileName, osl_getThreadTextEncoding());;
159 : }
160 :
161 0 : bool fileExists(const OString& fileName)
162 : {
163 0 : FILE *f= fopen(fileName.getStr(), "r");
164 :
165 0 : if (f != NULL)
166 : {
167 0 : fclose(f);
168 0 : return true;
169 : }
170 :
171 0 : return false;
172 : }
173 :
174 0 : bool checkFileContent(const OString& targetFileName, const OString& tmpFileName)
175 : {
176 0 : FILE *target = fopen(targetFileName.getStr(), "r");
177 0 : FILE *tmp = fopen(tmpFileName.getStr(), "r");
178 0 : bool bFindChanges = false;
179 :
180 0 : if (target != NULL && tmp != NULL)
181 : {
182 : sal_Char buffer1[1024+1];
183 : sal_Char buffer2[1024+1];
184 0 : sal_Int32 n1 = 0;
185 0 : sal_Int32 n2 = 0;
186 :
187 0 : while ( !bFindChanges && !feof(target) && !feof(tmp))
188 : {
189 0 : n1 = fread(buffer1, sizeof(sal_Char), 1024, target);
190 0 : n2 = fread(buffer2, sizeof(sal_Char), 1024, tmp);
191 :
192 0 : if ( n1 != n2 )
193 0 : bFindChanges = true;
194 : else
195 0 : if ( memcmp(buffer1, buffer2, n2) != 0 )
196 0 : bFindChanges = true;
197 : }
198 : }
199 :
200 0 : if (target) fclose(target);
201 0 : if (tmp) fclose(tmp);
202 :
203 0 : return bFindChanges;
204 : }
205 :
206 0 : bool makeValidTypeFile(const OString& targetFileName, const OString& tmpFileName,
207 : bool bFileCheck)
208 : {
209 0 : if (bFileCheck) {
210 0 : if (checkFileContent(targetFileName, tmpFileName)) {
211 0 : if ( !unlink(targetFileName.getStr()) )
212 0 : if ( !rename(tmpFileName.getStr(), targetFileName.getStr()) )
213 0 : return true;
214 : } else
215 0 : return removeTypeFile(tmpFileName);
216 : } else {
217 0 : if (fileExists(targetFileName))
218 0 : if (!removeTypeFile(targetFileName))
219 0 : return false;
220 :
221 0 : if ( rename(tmpFileName.getStr(), targetFileName.getStr()) ) {
222 0 : if (errno == EEXIST)
223 0 : return true;
224 : } else
225 0 : return true;
226 : }
227 0 : return false;
228 : }
229 :
230 0 : bool removeTypeFile(const OString& fileName)
231 : {
232 0 : if ( !unlink(fileName.getStr()) )
233 0 : return true;
234 :
235 0 : return false;
236 : }
237 :
238 0 : OUString convertToFileUrl(const OString& fileName)
239 : {
240 0 : if ( fileName.startsWith("file://") )
241 : {
242 0 : return OStringToOUString(fileName, osl_getThreadTextEncoding());
243 : }
244 :
245 0 : OUString uUrlFileName;
246 0 : OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding());
247 0 : if ( fileName.startsWith(".") || fileName.indexOf(SEPARATOR) < 0 )
248 : {
249 0 : OUString uWorkingDir;
250 0 : if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None)
251 : {
252 : OSL_ASSERT(false);
253 : }
254 0 : if (FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uUrlFileName)
255 : != FileBase::E_None)
256 : {
257 : OSL_ASSERT(false);
258 0 : }
259 : } else
260 : {
261 0 : if (FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName)
262 : != FileBase::E_None)
263 : {
264 : OSL_ASSERT(false);
265 : }
266 : }
267 :
268 0 : return uUrlFileName;
269 : }
270 :
271 :
272 :
273 : // FileStream
274 :
275 0 : FileStream::FileStream()
276 0 : : m_file(NULL)
277 : {
278 0 : }
279 :
280 0 : FileStream::~FileStream()
281 : {
282 0 : if ( isValid() )
283 0 : osl_closeFile(m_file);
284 0 : }
285 :
286 0 : bool FileStream::isValid()
287 : {
288 0 : if ( m_file )
289 0 : return true;
290 :
291 0 : return false;
292 : }
293 :
294 0 : void FileStream::createTempFile(const OString& sPath)
295 : {
296 0 : OString sTmp(".");
297 0 : OUString sTmpPath;
298 0 : OUString sTmpName;
299 :
300 0 : if (!sPath.isEmpty())
301 0 : sTmp = sPath;
302 :
303 0 : sTmpPath = convertToFileUrl(sTmp);
304 :
305 0 : if (osl_createTempFile(sTmpPath.pData, &m_file, &sTmpName.pData) == osl_File_E_None) {
306 : #ifdef SAL_UNX
307 : sal_uInt64 uAttr = osl_File_Attribute_OwnWrite |
308 : osl_File_Attribute_OwnRead |
309 : osl_File_Attribute_GrpWrite |
310 : osl_File_Attribute_GrpRead |
311 0 : osl_File_Attribute_OthRead;
312 0 : if (osl_setFileAttributes(sTmpName.pData, uAttr) != osl_File_E_None) {
313 0 : m_file = NULL;
314 0 : return;
315 : }
316 : #endif
317 0 : OUString sSysTmpName;
318 0 : FileBase::getSystemPathFromFileURL(sTmpName, sSysTmpName);
319 0 : m_name = OUStringToOString(sSysTmpName, osl_getThreadTextEncoding());
320 : } else
321 0 : m_file = NULL;
322 : }
323 :
324 0 : void FileStream::close()
325 : {
326 0 : if ( isValid() )
327 : {
328 0 : osl_closeFile(m_file);
329 0 : m_file = NULL;
330 0 : m_name = OString();
331 : }
332 0 : }
333 :
334 0 : bool FileStream::write(void const * buffer, sal_uInt64 size) {
335 0 : while (size > 0) {
336 : sal_uInt64 written;
337 0 : if (osl_writeFile(m_file, buffer, size, &written) != osl_File_E_None) {
338 0 : return false;
339 : }
340 : OSL_ASSERT(written <= size);
341 0 : size -= written;
342 0 : buffer = static_cast< char const * >(buffer) + written;
343 : }
344 0 : return true;
345 : }
346 :
347 0 : FileStream &operator<<(FileStream& o, sal_uInt32 i) {
348 : sal_uInt64 writtenBytes;
349 0 : OString s = OString::number((sal_Int32)i);
350 0 : osl_writeFile(o.m_file, s.getStr(), s.getLength() * sizeof(sal_Char), &writtenBytes);
351 0 : return o;
352 : }
353 0 : FileStream &operator<<(FileStream& o, char const * s) {
354 : sal_uInt64 writtenBytes;
355 0 : osl_writeFile(o.m_file, s, strlen(s), &writtenBytes);
356 0 : return o;
357 : }
358 0 : FileStream &operator<<(FileStream& o, ::rtl::OString* s) {
359 : sal_uInt64 writtenBytes;
360 0 : osl_writeFile(o.m_file, s->getStr(), s->getLength() * sizeof(sal_Char), &writtenBytes);
361 0 : return o;
362 : }
363 0 : FileStream &operator<<(FileStream& o, const ::rtl::OString& s) {
364 : sal_uInt64 writtenBytes;
365 0 : osl_writeFile(o.m_file, s.getStr(), s.getLength() * sizeof(sal_Char), &writtenBytes);
366 0 : return o;
367 :
368 : }
369 0 : FileStream &operator<<(FileStream& o, ::rtl::OStringBuffer* s) {
370 : sal_uInt64 writtenBytes;
371 0 : osl_writeFile(o.m_file, s->getStr(), s->getLength() * sizeof(sal_Char), &writtenBytes);
372 0 : return o;
373 : }
374 0 : FileStream &operator<<(FileStream& o, const ::rtl::OStringBuffer& s) {
375 : sal_uInt64 writtenBytes;
376 : osl_writeFile(
377 0 : o.m_file, s.getStr(), s.getLength() * sizeof(sal_Char), &writtenBytes);
378 0 : return o;
379 : }
380 :
381 0 : FileStream & operator <<(FileStream & out, rtl::OUString const & s) {
382 0 : return out << OUStringToOString(s, RTL_TEXTENCODING_UTF8);
383 : }
384 :
385 0 : CannotDumpException::~CannotDumpException() throw () {}
386 :
387 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|