1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: filehelper.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
35 #ifndef _RTL_USTRING_H_
36 #include <rtl/ustring.hxx>
38 #include <osl/file.hxx>
39 #include "filehelper.hxx"
40 #include <rtl/ustrbuf.hxx>
41 #include <osl/diagnose.h>
43 #define ASCII(x) rtl::OUString::createFromAscii(x)
47 //==========================================================================
49 //==========================================================================
51 // -----------------------------------------------------------------------------
52 bool FileHelper::tryToRemoveFile(const rtl::OUString
& _aURL
, bool tryBackupFirst
)
56 rtl::OUString aBakURL
= _aURL
.concat( ASCII(".bak") );
57 osl::File::RC eBakError
= osl::File::move(_aURL
,aBakURL
);
58 if (eBakError
== osl::File::E_None
)
61 osl::File::RC eError
= osl::File::remove(_aURL
);
62 return eError
== osl::File::E_None
|| eError
== osl::File::E_NOENT
;
64 // -----------------------------------------------------------------------------
65 void FileHelper::replaceFile(
66 const rtl::OUString
& _aToURL
, const rtl::OUString
&_aFromURL
) SAL_THROW((io::IOException
))
68 osl::File::remove(_aToURL
);
69 osl::File::RC eError
= osl::File::move(_aFromURL
, _aToURL
);
70 if (eError
!= osl::File::E_None
&&
71 eError
!= osl::File::E_NOENT
)
73 rtl::OUStringBuffer sErrorBuf
;
74 sErrorBuf
.appendAscii("Configmgr: replaceFile failed ");
75 sErrorBuf
.appendAscii("for replacing file \"").append(_aFromURL
).appendAscii("\". ");
76 sErrorBuf
.appendAscii("by file \"").append(_aToURL
).appendAscii("\". ");
77 sErrorBuf
.appendAscii("Error = \"").append(FileHelper::createOSLErrorString(eError
)).appendAscii("\" ");
78 sErrorBuf
.appendAscii("[").append(sal_Int32(eError
)).appendAscii("] ");
80 rtl::OUString
const sError
= sErrorBuf
.makeStringAndClear();
81 OSL_ENSURE(0, rtl::OUStringToOString(sError
,RTL_TEXTENCODING_ASCII_US
).getStr());
82 throw io::IOException(sError
, NULL
);
86 // -----------------------------------------------------------------------------
87 bool FileHelper::fileExists(rtl::OUString
const& _sFileURL
)
89 osl::DirectoryItem aItem
;
90 return osl::DirectoryItem::get(_sFileURL
, aItem
) == osl::Directory::E_None
;
93 // -----------------------------------------------------------------------------
94 bool FileHelper::dirExists(rtl::OUString
const& _sDirURL
)
96 return osl::Directory(_sDirURL
).open() == osl::Directory::E_None
;
99 // -----------------------------------------------------------------------------
100 sal_uInt64
FileHelper::getModifyStatus(rtl::OUString
const& _sURL
, TimeValue
& rModifyTime
)
102 static const TimeValue k_NullTime
= {0,0};
103 sal_uInt64 aSize
= 0;
104 rModifyTime
= k_NullTime
;
107 osl::DirectoryItem aItem
;
109 #ifndef STAT_LOTS_OF_UNUSED_FILES
110 // Statting every file takes way to long - we can use the directory
111 // time-stamp & size instead, if we need to provoke an update
112 // a simple cat > foo; rm foo will do that.
113 // The dir size is (hopefully) non-0 and a good enough proxy value.
115 aURL
= FileHelper::getParentDir(_sURL
);
120 if (osl::FileBase::E_None
== osl::DirectoryItem::get(aURL
, aItem
))
122 osl::FileStatus
aStatus(osl_FileStatus_Mask_ModifyTime
|osl_FileStatus_Mask_Type
|osl_FileStatus_Mask_FileSize
);
123 if (osl::FileBase::E_None
== aItem
.getFileStatus(aStatus
))
125 if (aStatus
.isValid(osl_FileStatus_Mask_ModifyTime
))
126 rModifyTime
= aStatus
.getModifyTime();
128 if (aStatus
.isValid(osl_FileStatus_Mask_FileSize
))
129 aSize
= aStatus
.getFileSize();
135 // -----------------------------------------------------------------------------
136 rtl::OUString
FileHelper::createOSLErrorString(osl::FileBase::RC eError
)
141 case osl::FileBase::E_None
:
144 case osl::FileBase::E_PERM
:
145 aRet
= ASCII("Operation not permitted");
148 case osl::FileBase::E_NOENT
:
149 aRet
= ASCII("No such file or directory");
152 case osl::FileBase::E_SRCH
:
153 aRet
= ASCII("unknown error: osl_File_E_SRCH");
156 case osl::FileBase::E_INTR
:
157 aRet
= ASCII("function call was interrupted");
160 case osl::FileBase::E_IO
:
161 aRet
= ASCII("I/O error");
164 case osl::FileBase::E_NXIO
:
165 aRet
= ASCII("No such device or address");
168 case osl::FileBase::E_2BIG
:
169 aRet
= ASCII("unknown error: osl_File_E_2BIG");
172 case osl::FileBase::E_NOEXEC
:
173 aRet
= ASCII("unknown error: osl_File_E_NOEXEC");
176 case osl::FileBase::E_BADF
:
177 aRet
= ASCII("Bad file");
180 case osl::FileBase::E_CHILD
:
181 aRet
= ASCII("unknown error: osl_File_E_CHILD");
184 case osl::FileBase::E_AGAIN
:
185 aRet
= ASCII("Operation would block");
188 case osl::FileBase::E_NOMEM
:
189 aRet
= ASCII("not enough memory for allocating structures");
192 case osl::FileBase::E_ACCES
:
193 aRet
= ASCII("Permission denied");
196 case osl::FileBase::E_FAULT
:
197 aRet
= ASCII("Bad address");
200 case osl::FileBase::E_BUSY
:
201 aRet
= ASCII("Text file busy");
204 case osl::FileBase::E_EXIST
:
205 aRet
= ASCII("File exists");
208 case osl::FileBase::E_XDEV
:
209 aRet
= ASCII("unknown error: osl_File_E_XDEV");
212 case osl::FileBase::E_NODEV
:
213 aRet
= ASCII("No such device");
216 case osl::FileBase::E_NOTDIR
:
217 aRet
= ASCII("Not a directory");
220 case osl::FileBase::E_ISDIR
:
221 aRet
= ASCII("Is a director");
224 case osl::FileBase::E_INVAL
:
225 aRet
= ASCII("the format of the parameters was not valid");
228 case osl::FileBase::E_NFILE
:
229 aRet
= ASCII("too many open files in the system");
232 case osl::FileBase::E_MFILE
:
233 aRet
= ASCII("too many open files used by the process");
236 case osl::FileBase::E_NOTTY
:
237 aRet
= ASCII("unknown error: osl_File_E_NOTTY");
240 case osl::FileBase::E_FBIG
:
241 aRet
= ASCII("File too large");
244 case osl::FileBase::E_NOSPC
:
245 aRet
= ASCII("No space left on device");
248 case osl::FileBase::E_SPIPE
:
249 aRet
= ASCII("unknown error: osl_File_E_SPIPE");
252 case osl::FileBase::E_ROFS
:
253 aRet
= ASCII("Read-only file system");
256 case osl::FileBase::E_MLINK
:
257 aRet
= ASCII("Too many links");
260 case osl::FileBase::E_PIPE
:
261 aRet
= ASCII("unknown error: osl_File_E_PIPE");
264 case osl::FileBase::E_DOM
:
265 aRet
= ASCII("unknown error: osl_File_E_DOM");
268 case osl::FileBase::E_RANGE
:
269 aRet
= ASCII("unknown error: osl_File_E_RANGE");
272 case osl::FileBase::E_DEADLK
:
273 aRet
= ASCII("unknown error: osl_File_E_DEADLK");
276 case osl::FileBase::E_NAMETOOLONG
:
277 aRet
= ASCII("File name too long");
280 case osl::FileBase::E_NOLCK
:
281 aRet
= ASCII("No record locks available");
284 case osl::FileBase::E_NOSYS
:
285 aRet
= ASCII("Function not implemente");
288 case osl::FileBase::E_NOTEMPTY
:
289 aRet
= ASCII("Directory not empt");
292 case osl::FileBase::E_LOOP
:
293 aRet
= ASCII("Too many symbolic links encountered");
296 case osl::FileBase::E_ILSEQ
:
297 aRet
= ASCII("unknown error: osl_File_E_ILSEQ");
300 case osl::FileBase::E_NOLINK
:
301 aRet
= ASCII("Link has been severed");
304 case osl::FileBase::E_MULTIHOP
:
305 aRet
= ASCII("Multihop attempted");
308 case osl::FileBase::E_USERS
:
309 aRet
= ASCII("unknown error: osl_File_E_USERS");
312 case osl::FileBase::E_OVERFLOW
:
313 aRet
= ASCII("Value too large for defined data type");
316 /* unmapped error: always last entry in enum! */
317 default: OSL_ENSURE(false, "Found unknown OSL File Error");
318 case osl::FileBase::E_invalidError
:
319 aRet
= ASCII("unmapped Error");
325 // -----------------------------------------------------------------------------
326 rtl::OUString
FileHelper::getParentDir(rtl::OUString
const& _sURL
)
328 rtl::OUString parentDirectory
;
329 rtl::OUString fileName
;
331 splitFileUrl(_sURL
, parentDirectory
, fileName
) ;
332 return parentDirectory
;
335 // -----------------------------------------------------------------------------
336 void FileHelper::splitFileUrl(const rtl::OUString
& aFileUrl
,
337 rtl::OUString
& aParentDirectory
,
338 rtl::OUString
& aFileName
) {
339 // goto last '/' and cut the rest.
340 sal_Int32 nIdx
= aFileUrl
.lastIndexOf(delimiter
, aFileUrl
.getLength());
342 aParentDirectory
= aFileUrl
.copy(0, nIdx
);
343 aFileName
= aFileUrl
.copy(nIdx
+ 1) ;
346 aParentDirectory
= rtl::OUString() ;
347 aFileName
= aFileUrl
;
351 // -----------------------------------------------------------------------------
352 rtl::OUString
FileHelper::getFileName(const rtl::OUString
& aFileUrl
) {
353 rtl::OUString parentDirectory
;
354 rtl::OUString fileName
;
356 splitFileUrl(aFileUrl
, parentDirectory
, fileName
) ;
359 // -----------------------------------------------------------------------------
360 osl::FileBase::RC
FileHelper::mkdir(rtl::OUString
const& _sDirURL
)
362 // direct create a directory
363 osl::FileBase::RC eError
= osl::Directory::create(_sDirURL
); // try to create the directory
364 if (eError
== osl::FileBase::E_EXIST
||
365 eError
== osl::FileBase::E_None
||
366 FileHelper::dirExists(_sDirURL
))
368 eError
= osl::FileBase::E_None
; // Exists or created
373 // -----------------------------------------------------------------------------
374 osl::FileBase::RC
FileHelper::mkdirs(rtl::OUString
const& _sDirURL
)
376 osl::FileBase::RC eError
= mkdir(_sDirURL
);
379 case osl::FileBase::E_EXIST
: OSL_ASSERT(false);
380 case osl::FileBase::E_None
:
383 case osl::FileBase::E_NOENT
:
385 rtl::OUString sParentDir
= FileHelper::getParentDir(_sDirURL
);
386 if (sParentDir
.getLength() == 0)
389 eError
= mkdirs(sParentDir
);
390 if (eError
!= osl::FileBase::E_None
)
393 eError
= mkdir(_sDirURL
);
397 default: OSL_TRACE("configmgr: Could not create directory (%d).", int(eError
));
403 } // namespace configmgr