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