Branch data 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 : 35900 : OString getTempDir(const OString& sFileName)
53 : : {
54 : 35900 : sal_Int32 index = 0;
55 : : #ifdef SAL_UNX
56 [ + - ]: 35900 : if ((index=sFileName.lastIndexOf('/')) > 0)
57 : 35900 : return sFileName.copy(0, index);
58 : : #else
59 : : if ((index=sFileName.lastIndexOf('\\')) > 0)
60 : : return sFileName.copy(0, index);
61 : : #endif
62 : 35900 : return ".";
63 : : }
64 : :
65 : 35900 : OString createFileNameFromType( const OString& destination,
66 : : const OString typeName,
67 : : const OString postfix,
68 : : sal_Bool bLowerCase,
69 : : const OString prefix )
70 : : {
71 : 35900 : OString type(typeName);
72 : :
73 [ - + ]: 35900 : if (bLowerCase)
74 : : {
75 : 0 : type = typeName.toAsciiLowerCase();
76 : : }
77 : :
78 : 35900 : sal_uInt32 length = destination.getLength();
79 : :
80 : 35900 : sal_Bool withPoint = sal_False;
81 [ - + ]: 35900 : if (length == 0)
82 : : {
83 : 0 : length++;
84 : 0 : withPoint = sal_True;
85 : : }
86 : :
87 : 35900 : length += prefix.getLength() + type.getLength() + postfix.getLength();
88 : :
89 : 35900 : sal_Bool withSeperator = sal_False;
90 [ + - ][ + - : 143600 : if (destination.getStr()[destination.getLength()] != '\\' &&
+ - + - +
- ]
91 : 35900 : destination.getStr()[destination.getLength()] != '/' &&
92 : 35900 : type.getStr()[0] != '\\' &&
93 : 35900 : type.getStr()[0] != '/')
94 : : {
95 : 35900 : length++;
96 : 35900 : withSeperator = sal_True;
97 : : }
98 : :
99 : 35900 : OStringBuffer fileNameBuf(length);
100 : :
101 [ - + ]: 35900 : if (withPoint)
102 [ # # ]: 0 : fileNameBuf.append('.');
103 : : else
104 [ + - ]: 35900 : fileNameBuf.append(destination.getStr(), destination.getLength());
105 : :
106 [ + - ]: 35900 : if (withSeperator)
107 [ + - ]: 35900 : fileNameBuf.append("/", 1);
108 : :
109 : 35900 : OString tmpStr(type);
110 [ - + ]: 35900 : if (!prefix.isEmpty())
111 : : {
112 : 0 : tmpStr = type.replaceAt(type.lastIndexOf('/')+1, 0, prefix);
113 : : }
114 : :
115 [ + - ]: 35900 : fileNameBuf.append(tmpStr.getStr(), tmpStr.getLength());
116 [ + - ]: 35900 : fileNameBuf.append(postfix.getStr(), postfix.getLength());
117 : :
118 : 35900 : OString fileName(fileNameBuf.makeStringAndClear());
119 : :
120 : : sal_Char token;
121 : : #ifdef SAL_UNX
122 : 35900 : fileName = fileName.replace('\\', '/');
123 : 35900 : token = '/';
124 : : #else
125 : : fileName = fileName.replace('/', '\\');
126 : : token = '\\';
127 : : #endif
128 : :
129 : 35900 : OStringBuffer buffer(length);
130 : :
131 : 35900 : sal_Int32 nIndex = 0;
132 [ + - ]: 502356 : do
133 : : {
134 [ + - ]: 538256 : buffer.append(fileName.getToken(0, token, nIndex).getStr());
135 [ + + ]: 538256 : if( nIndex == -1 )
136 : 35900 : break;
137 : :
138 [ + + ][ - + ]: 502356 : if (buffer.getLength() == 0 || OString(".") == buffer.getStr())
[ + + ][ + + ]
[ + + ][ + + ]
139 : : {
140 [ + - ]: 35884 : buffer.append(token);
141 : 35884 : continue;
142 : : }
143 : :
144 : : #if defined(SAL_UNX)
145 [ + + ]: 466472 : if (mkdir((char*)buffer.getStr(), 0777) == -1)
146 : : #else
147 : : if (mkdir((char*)buffer.getStr()) == -1)
148 : : #endif
149 : : {
150 [ - + ]: 465912 : if ( errno == ENOENT )
151 : 0 : return OString();
152 : : }
153 : :
154 [ + - ]: 466472 : buffer.append(token);
155 : : } while( nIndex != -1 );
156 : :
157 : 35900 : OUString uSysFileName;
158 [ + - ][ + - ]: 35900 : OSL_VERIFY( FileBase::getSystemPathFromFileURL(
159 : : convertToFileUrl(fileName), uSysFileName) == FileBase::E_None );
160 [ + - ][ + - ]: 35900 : return OUStringToOString(uSysFileName, osl_getThreadTextEncoding());;
161 : : }
162 : :
163 : 56969 : sal_Bool fileExists(const OString& fileName)
164 : : {
165 : 56969 : FILE *f= fopen(fileName.getStr(), "r");
166 : :
167 [ + + ]: 56969 : if (f != NULL)
168 : : {
169 : 15679 : fclose(f);
170 : 15679 : return sal_True;
171 : : }
172 : :
173 : 56969 : return sal_False;
174 : : }
175 : :
176 : 14831 : sal_Bool checkFileContent(const OString& targetFileName, const OString& tmpFileName)
177 : : {
178 : 14831 : FILE *target = fopen(targetFileName.getStr(), "r");
179 : 14831 : FILE *tmp = fopen(tmpFileName.getStr(), "r");
180 : 14831 : sal_Bool bFindChanges = sal_False;
181 : :
182 [ + - ][ + - ]: 14831 : if (target != NULL && tmp != NULL)
183 : : {
184 : : sal_Char buffer1[1024+1];
185 : : sal_Char buffer2[1024+1];
186 : 14831 : sal_Int32 n1 = 0;
187 : 14831 : sal_Int32 n2 = 0;
188 : :
189 [ + - ][ + + ]: 61464 : while ( !bFindChanges && !feof(target) && !feof(tmp))
[ + - ][ + + ]
190 : : {
191 [ + - ]: 46633 : n1 = fread(buffer1, sizeof(sal_Char), 1024, target);
192 [ + - ]: 46633 : n2 = fread(buffer2, sizeof(sal_Char), 1024, tmp);
193 : :
194 [ - + ]: 46633 : if ( n1 != n2 )
195 : 0 : bFindChanges = sal_True;
196 : : else
197 [ + - ][ - + ]: 46633 : if ( rtl_compareMemory(buffer1, buffer2, n2) != 0 )
198 : 0 : bFindChanges = sal_True;
199 : : }
200 : : }
201 : :
202 [ + - ]: 14831 : if (target) fclose(target);
203 [ + - ]: 14831 : if (tmp) fclose(tmp);
204 : :
205 : 14831 : return bFindChanges;
206 : : }
207 : :
208 : 35900 : sal_Bool makeValidTypeFile(const OString& targetFileName, const OString& tmpFileName,
209 : : sal_Bool bFileCheck)
210 : : {
211 [ + + ]: 35900 : if (bFileCheck) {
212 [ - + ]: 14831 : if (checkFileContent(targetFileName, tmpFileName)) {
213 [ # # ]: 0 : if ( !unlink(targetFileName.getStr()) )
214 [ # # ]: 0 : if ( !rename(tmpFileName.getStr(), targetFileName.getStr()) )
215 : 0 : return sal_True;
216 : : } else
217 : 14831 : return removeTypeFile(tmpFileName);
218 : : } else {
219 [ + + ]: 21069 : if (fileExists(targetFileName))
220 [ - + ]: 424 : if (!removeTypeFile(targetFileName))
221 : 0 : return sal_False;
222 : :
223 [ - + ]: 21069 : if ( rename(tmpFileName.getStr(), targetFileName.getStr()) ) {
224 [ # # ]: 0 : if (errno == EEXIST)
225 : 0 : return sal_True;
226 : : } else
227 : 21069 : return sal_True;
228 : : }
229 : 35900 : return sal_False;
230 : : }
231 : :
232 : 15255 : sal_Bool removeTypeFile(const OString& fileName)
233 : : {
234 [ + - ]: 15255 : if ( !unlink(fileName.getStr()) )
235 : 15255 : return sal_True;
236 : :
237 : 15255 : return sal_False;
238 : : }
239 : :
240 : 71850 : static sal_Bool isFileUrl(const OString& fileName)
241 : : {
242 [ - + ]: 71850 : if (fileName.indexOf("file://") == 0 )
243 : 0 : return sal_True;
244 : 71850 : return sal_False;
245 : : }
246 : :
247 : 71850 : OUString convertToFileUrl(const OString& fileName)
248 : : {
249 [ + - ][ - + ]: 71850 : if ( isFileUrl(fileName) )
250 : : {
251 [ # # ][ # # ]: 0 : return OStringToOUString(fileName, osl_getThreadTextEncoding());
252 : : }
253 : :
254 : 71850 : OUString uUrlFileName;
255 [ + - ][ + - ]: 71850 : OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding());
256 [ + + ][ - + ]: 71850 : if ( fileName.indexOf('.') == 0 || fileName.indexOf(SEPARATOR) < 0 )
[ + + ]
257 : : {
258 : 16 : OUString uWorkingDir;
259 [ + - ]: 16 : if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None)
260 : : {
261 : : OSL_ASSERT(false);
262 : : }
263 [ + - ]: 16 : if (FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uUrlFileName)
264 : : != FileBase::E_None)
265 : : {
266 : : OSL_ASSERT(false);
267 : 16 : }
268 : : } else
269 : : {
270 [ + - ]: 71834 : if (FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName)
271 : : != FileBase::E_None)
272 : : {
273 : : OSL_ASSERT(false);
274 : : }
275 : : }
276 : :
277 : 71850 : return uUrlFileName;
278 : : }
279 : :
280 : :
281 : : //*************************************************************************
282 : : // FileStream
283 : : //*************************************************************************
284 : 35900 : FileStream::FileStream()
285 : 35900 : : m_file(NULL)
286 : : {
287 : 35900 : }
288 : :
289 : 35900 : FileStream::~FileStream()
290 : : {
291 [ - + ]: 35900 : if ( isValid() )
292 [ # # ]: 0 : osl_closeFile(m_file);
293 [ - + ]: 35900 : }
294 : :
295 : 107700 : sal_Bool FileStream::isValid()
296 : : {
297 [ + + ]: 107700 : if ( m_file )
298 : 71800 : return sal_True;
299 : :
300 : 107700 : return sal_False;
301 : : }
302 : :
303 : 35900 : void FileStream::createTempFile(const OString& sPath)
304 : : {
305 : 35900 : OString sTmp(".");
306 : 35900 : OUString sTmpPath;
307 : 35900 : OUString sTmpName;
308 : :
309 [ + - ]: 35900 : if (!sPath.isEmpty())
310 : 35900 : sTmp = sPath;
311 : :
312 [ + - ]: 35900 : sTmpPath = convertToFileUrl(sTmp);
313 : :
314 [ + - ][ + - ]: 35900 : if (osl_createTempFile(sTmpPath.pData, &m_file, &sTmpName.pData) == osl_File_E_None) {
315 : : #ifdef SAL_UNX
316 : : sal_uInt64 uAttr = osl_File_Attribute_OwnWrite |
317 : : osl_File_Attribute_OwnRead |
318 : : osl_File_Attribute_GrpWrite |
319 : : osl_File_Attribute_GrpRead |
320 : 35900 : osl_File_Attribute_OthRead;
321 [ + - ][ - + ]: 35900 : if (osl_setFileAttributes(sTmpName.pData, uAttr) != osl_File_E_None) {
322 : 0 : m_file = NULL;
323 : 35900 : return;
324 : : }
325 : : #endif
326 : 35900 : OUString sSysTmpName;
327 [ + - ]: 35900 : FileBase::getSystemPathFromFileURL(sTmpName, sSysTmpName);
328 [ + - ][ + - ]: 35900 : m_name = OUStringToOString(sSysTmpName, osl_getThreadTextEncoding());
329 : : } else
330 [ - + ][ - + ]: 35900 : m_file = NULL;
[ + - ]
331 : : }
332 : :
333 : 0 : void FileStream::open(const OString& name, FileAccessMode mode)
334 : : {
335 [ # # ]: 0 : if ( !name.isEmpty() )
336 : : {
337 : 0 : oslFileError ret = osl_File_E_None;
338 [ # # ][ # # ]: 0 : if ((ret = osl_openFile(convertToFileUrl(name).pData, &m_file, checkAccessMode(mode))) == osl_File_E_None)
339 : 0 : m_name = name;
340 : : else
341 : 0 : m_file = NULL;
342 : : }
343 : 0 : }
344 : :
345 : 35900 : void FileStream::close()
346 : : {
347 [ + - ]: 35900 : if ( isValid() )
348 : : {
349 : 35900 : osl_closeFile(m_file);
350 : 35900 : m_file = NULL;
351 : 35900 : m_name = OString();
352 : : }
353 : 35900 : }
354 : :
355 : 0 : sal_uInt32 FileStream::checkAccessMode(FileAccessMode mode)
356 : : {
357 [ # # # # : 0 : switch( mode )
# ]
358 : : {
359 : : case FAM_READ:
360 : 0 : return osl_File_OpenFlag_Read;
361 : : case FAM_WRITE:
362 : 0 : return osl_File_OpenFlag_Write;
363 : : case FAM_READWRITE_EXIST:
364 : 0 : return osl_File_OpenFlag_Read | osl_File_OpenFlag_Write;
365 : : case FAM_READWRITE:
366 : 0 : return osl_File_OpenFlag_Read | osl_File_OpenFlag_Write | osl_File_OpenFlag_Create;
367 : : }
368 : 0 : return osl_File_OpenFlag_Read | osl_File_OpenFlag_Write | osl_File_OpenFlag_Create;
369 : : }
370 : :
371 : 88523 : bool FileStream::write(void const * buffer, sal_uInt64 size) {
372 [ + + ]: 177046 : while (size > 0) {
373 : : sal_uInt64 written;
374 [ + - ][ - + ]: 88523 : if (osl_writeFile(m_file, buffer, size, &written) != osl_File_E_None) {
375 : 0 : return false;
376 : : }
377 : : OSL_ASSERT(written <= size);
378 : 88523 : size -= written;
379 : 88523 : buffer = static_cast< char const * >(buffer) + written;
380 : : }
381 : 88523 : return true;
382 : : }
383 : :
384 : 386640 : FileStream &operator<<(FileStream& o, sal_uInt32 i) {
385 : : sal_uInt64 writtenBytes;
386 : 386640 : OString s = OString::valueOf((sal_Int32)i);
387 [ + - ]: 386640 : osl_writeFile(o.m_file, s.getStr(), s.getLength() * sizeof(sal_Char), &writtenBytes);
388 : 386640 : return o;
389 : : }
390 : 4201804 : FileStream &operator<<(FileStream& o, char const * s) {
391 : : sal_uInt64 writtenBytes;
392 [ + - ]: 4201804 : osl_writeFile(o.m_file, s, strlen(s), &writtenBytes);
393 : 4201804 : return o;
394 : : }
395 : 0 : FileStream &operator<<(FileStream& o, ::rtl::OString* s) {
396 : : sal_uInt64 writtenBytes;
397 [ # # ]: 0 : osl_writeFile(o.m_file, s->getStr(), s->getLength() * sizeof(sal_Char), &writtenBytes);
398 : 0 : return o;
399 : : }
400 : 1724892 : FileStream &operator<<(FileStream& o, const ::rtl::OString& s) {
401 : : sal_uInt64 writtenBytes;
402 [ + - ]: 1724892 : osl_writeFile(o.m_file, s.getStr(), s.getLength() * sizeof(sal_Char), &writtenBytes);
403 : 1724892 : return o;
404 : :
405 : : }
406 : 0 : FileStream &operator<<(FileStream& o, ::rtl::OStringBuffer* s) {
407 : : sal_uInt64 writtenBytes;
408 [ # # ]: 0 : osl_writeFile(o.m_file, s->getStr(), s->getLength() * sizeof(sal_Char), &writtenBytes);
409 : 0 : return o;
410 : : }
411 : 0 : FileStream &operator<<(FileStream& o, const ::rtl::OStringBuffer& s) {
412 : : sal_uInt64 writtenBytes;
413 : : osl_writeFile(
414 [ # # ]: 0 : o.m_file, s.getStr(), s.getLength() * sizeof(sal_Char), &writtenBytes);
415 : 0 : return o;
416 : : }
417 : :
418 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|