update dev300-m58
[ooovba.git] / configmgr / source / misc / filehelper.cxx
blob279aa47d49559e95e1d1b609556ed0f90b176666
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: filehelper.cxx,v $
10 * $Revision: 1.19 $
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>
37 #endif
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)
45 namespace configmgr
47 //==========================================================================
48 //= FileHelper
49 //==========================================================================
51 // -----------------------------------------------------------------------------
52 bool FileHelper::tryToRemoveFile(const rtl::OUString& _aURL, bool tryBackupFirst)
54 if (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)
59 return true;
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;
105 rtl::OUString aURL;
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);
116 #else
117 aURL = _sURL;
118 #endif
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();
132 return aSize;
135 // -----------------------------------------------------------------------------
136 rtl::OUString FileHelper::createOSLErrorString(osl::FileBase::RC eError)
138 rtl::OUString aRet;
139 switch(eError)
141 case osl::FileBase::E_None:
142 break;
144 case osl::FileBase::E_PERM:
145 aRet = ASCII("Operation not permitted");
146 break;
148 case osl::FileBase::E_NOENT:
149 aRet = ASCII("No such file or directory");
150 break;
152 case osl::FileBase::E_SRCH:
153 aRet = ASCII("unknown error: osl_File_E_SRCH");
154 break;
156 case osl::FileBase::E_INTR:
157 aRet = ASCII("function call was interrupted");
158 break;
160 case osl::FileBase::E_IO:
161 aRet = ASCII("I/O error");
162 break;
164 case osl::FileBase::E_NXIO:
165 aRet = ASCII("No such device or address");
166 break;
168 case osl::FileBase::E_2BIG:
169 aRet = ASCII("unknown error: osl_File_E_2BIG");
170 break;
172 case osl::FileBase::E_NOEXEC:
173 aRet = ASCII("unknown error: osl_File_E_NOEXEC");
174 break;
176 case osl::FileBase::E_BADF:
177 aRet = ASCII("Bad file");
178 break;
180 case osl::FileBase::E_CHILD:
181 aRet = ASCII("unknown error: osl_File_E_CHILD");
182 break;
184 case osl::FileBase::E_AGAIN:
185 aRet = ASCII("Operation would block");
186 break;
188 case osl::FileBase::E_NOMEM:
189 aRet = ASCII("not enough memory for allocating structures");
190 break;
192 case osl::FileBase::E_ACCES:
193 aRet = ASCII("Permission denied");
194 break;
196 case osl::FileBase::E_FAULT:
197 aRet = ASCII("Bad address");
198 break;
200 case osl::FileBase::E_BUSY:
201 aRet = ASCII("Text file busy");
202 break;
204 case osl::FileBase::E_EXIST:
205 aRet = ASCII("File exists");
206 break;
208 case osl::FileBase::E_XDEV:
209 aRet = ASCII("unknown error: osl_File_E_XDEV");
210 break;
212 case osl::FileBase::E_NODEV:
213 aRet = ASCII("No such device");
214 break;
216 case osl::FileBase::E_NOTDIR:
217 aRet = ASCII("Not a directory");
218 break;
220 case osl::FileBase::E_ISDIR:
221 aRet = ASCII("Is a director");
222 break;
224 case osl::FileBase::E_INVAL:
225 aRet = ASCII("the format of the parameters was not valid");
226 break;
228 case osl::FileBase::E_NFILE:
229 aRet = ASCII("too many open files in the system");
230 break;
232 case osl::FileBase::E_MFILE:
233 aRet = ASCII("too many open files used by the process");
234 break;
236 case osl::FileBase::E_NOTTY:
237 aRet = ASCII("unknown error: osl_File_E_NOTTY");
238 break;
240 case osl::FileBase::E_FBIG:
241 aRet = ASCII("File too large");
242 break;
244 case osl::FileBase::E_NOSPC:
245 aRet = ASCII("No space left on device");
246 break;
248 case osl::FileBase::E_SPIPE:
249 aRet = ASCII("unknown error: osl_File_E_SPIPE");
250 break;
252 case osl::FileBase::E_ROFS:
253 aRet = ASCII("Read-only file system");
254 break;
256 case osl::FileBase::E_MLINK:
257 aRet = ASCII("Too many links");
258 break;
260 case osl::FileBase::E_PIPE:
261 aRet = ASCII("unknown error: osl_File_E_PIPE");
262 break;
264 case osl::FileBase::E_DOM:
265 aRet = ASCII("unknown error: osl_File_E_DOM");
266 break;
268 case osl::FileBase::E_RANGE:
269 aRet = ASCII("unknown error: osl_File_E_RANGE");
270 break;
272 case osl::FileBase::E_DEADLK:
273 aRet = ASCII("unknown error: osl_File_E_DEADLK");
274 break;
276 case osl::FileBase::E_NAMETOOLONG:
277 aRet = ASCII("File name too long");
278 break;
280 case osl::FileBase::E_NOLCK:
281 aRet = ASCII("No record locks available");
282 break;
284 case osl::FileBase::E_NOSYS:
285 aRet = ASCII("Function not implemente");
286 break;
288 case osl::FileBase::E_NOTEMPTY:
289 aRet = ASCII("Directory not empt");
290 break;
292 case osl::FileBase::E_LOOP:
293 aRet = ASCII("Too many symbolic links encountered");
294 break;
296 case osl::FileBase::E_ILSEQ:
297 aRet = ASCII("unknown error: osl_File_E_ILSEQ");
298 break;
300 case osl::FileBase::E_NOLINK:
301 aRet = ASCII("Link has been severed");
302 break;
304 case osl::FileBase::E_MULTIHOP:
305 aRet = ASCII("Multihop attempted");
306 break;
308 case osl::FileBase::E_USERS:
309 aRet = ASCII("unknown error: osl_File_E_USERS");
310 break;
312 case osl::FileBase::E_OVERFLOW:
313 aRet = ASCII("Value too large for defined data type");
314 break;
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");
320 break;
322 return aRet;
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());
341 if (nIdx > 0) {
342 aParentDirectory = aFileUrl.copy(0, nIdx);
343 aFileName = aFileUrl.copy(nIdx + 1) ;
345 else {
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) ;
357 return 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
370 return eError;
373 // -----------------------------------------------------------------------------
374 osl::FileBase::RC FileHelper::mkdirs(rtl::OUString const& _sDirURL)
376 osl::FileBase::RC eError = mkdir(_sDirURL);
377 switch (eError)
379 case osl::FileBase::E_EXIST: OSL_ASSERT(false);
380 case osl::FileBase::E_None:
381 break;
383 case osl::FileBase::E_NOENT:
385 rtl::OUString sParentDir = FileHelper::getParentDir(_sDirURL);
386 if (sParentDir.getLength() == 0)
387 break;
389 eError = mkdirs(sParentDir);
390 if (eError != osl::FileBase::E_None)
391 break;
393 eError = mkdir(_sDirURL);
395 break;
397 default: OSL_TRACE("configmgr: Could not create directory (%d).", int(eError));
398 break;
400 return eError;
403 } // namespace configmgr