1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
#include "osl/process.h"
#include "rtl/strbuf.hxx"
#include "rtl/ustring.hxx"
#include "osl/thread.h"
#include "osl/file.hxx"
#include <string.h>
#if defined(SAL_W32)
#include <io.h>
#include <direct.h>
#include <errno.h>
#endif
#ifdef UNX
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#endif
#include "codemaker/global.hxx"
#ifdef SAL_UNX
#define SEPARATOR '/'
#else
#define SEPARATOR '\\'
#endif
using namespace ::rtl;
using namespace ::osl;
OString getTempDir(const OString& sFileName)
{
sal_Int32 index = 0;
#ifdef SAL_UNX
if ((index=sFileName.lastIndexOf('/')) > 0)
return sFileName.copy(0, index);
#else
if ((index=sFileName.lastIndexOf('\\')) > 0)
return sFileName.copy(0, index);
#endif
return ".";
}
OString createFileNameFromType( const OString& destination,
const OString typeName,
const OString postfix,
sal_Bool bLowerCase,
const OString prefix )
{
OString type(typeName);
if (bLowerCase)
{
type = typeName.toAsciiLowerCase();
}
sal_uInt32 length = destination.getLength();
sal_Bool withPoint = sal_False;
if (length == 0)
{
length++;
withPoint = sal_True;
}
length += prefix.getLength() + type.getLength() + postfix.getLength();
sal_Bool withSeperator = sal_False;
if (destination.getStr()[destination.getLength()] != '\\' &&
destination.getStr()[destination.getLength()] != '/' &&
type.getStr()[0] != '\\' &&
type.getStr()[0] != '/')
{
length++;
withSeperator = sal_True;
}
OStringBuffer fileNameBuf(length);
if (withPoint)
fileNameBuf.append('.');
else
fileNameBuf.append(destination.getStr(), destination.getLength());
if (withSeperator)
fileNameBuf.append("/", 1);
OString tmpStr(type);
if (prefix.getLength() > 0)
{
tmpStr = type.replaceAt(type.lastIndexOf('/')+1, 0, prefix);
}
fileNameBuf.append(tmpStr.getStr(), tmpStr.getLength());
fileNameBuf.append(postfix.getStr(), postfix.getLength());
OString fileName(fileNameBuf.makeStringAndClear());
sal_Char token;
#ifdef SAL_UNX
fileName = fileName.replace('\\', '/');
token = '/';
#else
fileName = fileName.replace('/', '\\');
token = '\\';
#endif
OStringBuffer buffer(length);
sal_Int32 nIndex = 0;
do
{
buffer.append(fileName.getToken(0, token, nIndex).getStr());
if( nIndex == -1 )
break;
if (buffer.getLength() == 0 || OString(".") == buffer.getStr())
{
buffer.append(token);
continue;
}
#if defined(SAL_UNX)
if (mkdir((char*)buffer.getStr(), 0777) == -1)
#else
if (mkdir((char*)buffer.getStr()) == -1)
#endif
{
if ( errno == ENOENT )
return OString();
}
buffer.append(token);
} while( nIndex != -1 );
OUString uSysFileName;
OSL_VERIFY( FileBase::getSystemPathFromFileURL(
convertToFileUrl(fileName), uSysFileName) == FileBase::E_None );
return OUStringToOString(uSysFileName, osl_getThreadTextEncoding());;
}
sal_Bool fileExists(const OString& fileName)
{
FILE *f= fopen(fileName.getStr(), "r");
if (f != NULL)
{
fclose(f);
return sal_True;
}
return sal_False;
}
sal_Bool checkFileContent(const OString& targetFileName, const OString& tmpFileName)
{
FILE *target = fopen(targetFileName.getStr(), "r");
FILE *tmp = fopen(tmpFileName.getStr(), "r");
sal_Bool bFindChanges = sal_False;
if (target != NULL && tmp != NULL)
{
sal_Char buffer1[1024+1];
sal_Char buffer2[1024+1];
sal_Int32 n1 = 0;
sal_Int32 n2 = 0;
while ( !bFindChanges && !feof(target) && !feof(tmp))
{
n1 = fread(buffer1, sizeof(sal_Char), 1024, target);
n2 = fread(buffer2, sizeof(sal_Char), 1024, tmp);
if ( n1 != n2 )
bFindChanges = sal_True;
else
if ( rtl_compareMemory(buffer1, buffer2, n2) != 0 )
bFindChanges = sal_True;
}
}
if (target) fclose(target);
if (tmp) fclose(tmp);
return bFindChanges;
}
sal_Bool makeValidTypeFile(const OString& targetFileName, const OString& tmpFileName,
sal_Bool bFileCheck)
{
if (bFileCheck) {
if (checkFileContent(targetFileName, tmpFileName)) {
if ( !unlink(targetFileName.getStr()) )
if ( !rename(tmpFileName.getStr(), targetFileName.getStr()) )
return sal_True;
} else
return removeTypeFile(tmpFileName);
} else {
if (fileExists(targetFileName))
if (!removeTypeFile(targetFileName))
return sal_False;
if ( rename(tmpFileName.getStr(), targetFileName.getStr()) ) {
if (errno == EEXIST)
return sal_True;
} else
return sal_True;
}
return sal_False;
}
sal_Bool removeTypeFile(const OString& fileName)
{
if ( !unlink(fileName.getStr()) )
return sal_True;
return sal_False;
}
static sal_Bool isFileUrl(const OString& fileName)
{
if (fileName.indexOf("file://") == 0 )
return sal_True;
return sal_False;
}
OUString convertToFileUrl(const OString& fileName)
{
if ( isFileUrl(fileName) )
{
return OStringToOUString(fileName, osl_getThreadTextEncoding());
}
OUString uUrlFileName;
OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding());
if ( fileName.indexOf('.') == 0 || fileName.indexOf(SEPARATOR) < 0 )
{
OUString uWorkingDir;
if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None)
{
OSL_ASSERT(false);
}
if (FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uUrlFileName)
!= FileBase::E_None)
{
OSL_ASSERT(false);
}
} else
{
if (FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName)
!= FileBase::E_None)
{
OSL_ASSERT(false);
}
}
return uUrlFileName;
}
//*************************************************************************
// FileStream
//*************************************************************************
FileStream::FileStream()
: m_file(NULL)
{
}
FileStream::FileStream(const OString& name, FileAccessMode mode)
: m_file(NULL)
{
if ( name.getLength() > 0 )
{
OUString sUrl(convertToFileUrl(name));
#ifdef SAL_UNX
sal_uInt64 uAttr = osl_File_Attribute_OwnWrite |
osl_File_Attribute_OwnRead |
osl_File_Attribute_GrpWrite |
osl_File_Attribute_GrpRead |
osl_File_Attribute_OthRead;
if (osl_openFile(sUrl.pData, &m_file, checkAccessMode(mode)) == osl_File_E_None &&
osl_setFileAttributes(sUrl.pData, uAttr) == osl_File_E_None)
#else
if (osl_openFile(sUrl.pData, &m_file, checkAccessMode(mode)) == osl_File_E_None)
#endif
m_name = name;
else
m_file = NULL;
}
}
FileStream::~FileStream()
{
if ( isValid() )
osl_closeFile(m_file);
}
sal_Bool FileStream::isValid()
{
if ( m_file )
return sal_True;
return sal_False;
}
void FileStream::createTempFile(const OString& sPath)
{
OString sTmp(".");
OUString sTmpPath;
OUString sTmpName;
if (sPath.getLength() > 0)
sTmp = sPath;
sTmpPath = convertToFileUrl(sTmp);
if (osl_createTempFile(sTmpPath.pData, &m_file, &sTmpName.pData) == osl_File_E_None) {
#ifdef SAL_UNX
sal_uInt64 uAttr = osl_File_Attribute_OwnWrite |
osl_File_Attribute_OwnRead |
osl_File_Attribute_GrpWrite |
osl_File_Attribute_GrpRead |
osl_File_Attribute_OthRead;
if (osl_setFileAttributes(sTmpName.pData, uAttr) != osl_File_E_None) {
m_file = NULL;
return;
}
#endif
OUString sSysTmpName;
FileBase::getSystemPathFromFileURL(sTmpName, sSysTmpName);
m_name = OUStringToOString(sSysTmpName, osl_getThreadTextEncoding());
} else
m_file = NULL;
}
void FileStream::open(const OString& name, FileAccessMode mode)
{
if ( name.getLength() > 0 )
{
oslFileError ret = osl_File_E_None;
if ((ret = osl_openFile(convertToFileUrl(name).pData, &m_file, checkAccessMode(mode))) == osl_File_E_None)
m_name = name;
else
m_file = NULL;
}
}
void FileStream::close()
{
if ( isValid() )
{
osl_closeFile(m_file);
m_file = NULL;
m_name = OString();
}
}
sal_uInt32 FileStream::checkAccessMode(FileAccessMode mode)
{
switch( mode )
{
case FAM_READ:
return osl_File_OpenFlag_Read;
case FAM_WRITE:
return osl_File_OpenFlag_Write;
case FAM_READWRITE_EXIST:
return osl_File_OpenFlag_Read | osl_File_OpenFlag_Write;
case FAM_READWRITE:
return osl_File_OpenFlag_Read | osl_File_OpenFlag_Write | osl_File_OpenFlag_Create;
}
return osl_File_OpenFlag_Read | osl_File_OpenFlag_Write | osl_File_OpenFlag_Create;
}
bool FileStream::write(void const * buffer, sal_uInt64 size) {
while (size > 0) {
sal_uInt64 written;
if (osl_writeFile(m_file, buffer, size, &written) != osl_File_E_None) {
return false;
}
OSL_ASSERT(written <= size);
size -= written;
buffer = static_cast< char const * >(buffer) + written;
}
return true;
}
FileStream &operator<<(FileStream& o, sal_uInt32 i) {
sal_uInt64 writtenBytes;
OString s = OString::valueOf((sal_Int32)i);
osl_writeFile(o.m_file, s.getStr(), s.getLength() * sizeof(sal_Char), &writtenBytes);
return o;
}
FileStream &operator<<(FileStream& o, char const * s) {
sal_uInt64 writtenBytes;
osl_writeFile(o.m_file, s, strlen(s), &writtenBytes);
return o;
}
FileStream &operator<<(FileStream& o, ::rtl::OString* s) {
sal_uInt64 writtenBytes;
osl_writeFile(o.m_file, s->getStr(), s->getLength() * sizeof(sal_Char), &writtenBytes);
return o;
}
FileStream &operator<<(FileStream& o, const ::rtl::OString& s) {
sal_uInt64 writtenBytes;
osl_writeFile(o.m_file, s.getStr(), s.getLength() * sizeof(sal_Char), &writtenBytes);
return o;
}
FileStream &operator<<(FileStream& o, ::rtl::OStringBuffer* s) {
sal_uInt64 writtenBytes;
osl_writeFile(o.m_file, s->getStr(), s->getLength() * sizeof(sal_Char), &writtenBytes);
return o;
}
FileStream &operator<<(FileStream& o, const ::rtl::OStringBuffer& s) {
sal_uInt64 writtenBytes;
osl_writeFile(
o.m_file, s.getStr(), s.getLength() * sizeof(sal_Char), &writtenBytes);
return o;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|