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