Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef _OSL_FILE_HXX_
30 : : #define _OSL_FILE_HXX_
31 : :
32 : : #include "sal/config.h"
33 : :
34 : : #include <string.h>
35 : :
36 : : #include <cassert>
37 : :
38 : : #include <osl/time.h>
39 : : #include <rtl/memory.h>
40 : : #include <rtl/ustring.hxx>
41 : :
42 : : #include <osl/file.h>
43 : : #include <rtl/byteseq.hxx>
44 : :
45 : : #include <stdio.h>
46 : :
47 : : namespace osl
48 : : {
49 : :
50 : :
51 : : // -----------------------------------------------------------------------------
52 : : /** Base class for all File System specific objects.
53 : :
54 : : @see Directory
55 : : @see DirectoryItem
56 : : @see File
57 : : */
58 : :
59 : 444005 : class FileBase
60 : : {
61 : : public:
62 : :
63 : : enum RC {
64 : : E_None = osl_File_E_None,
65 : : E_PERM = osl_File_E_PERM,
66 : : E_NOENT = osl_File_E_NOENT,
67 : : E_SRCH = osl_File_E_SRCH,
68 : : E_INTR = osl_File_E_INTR,
69 : : E_IO = osl_File_E_IO,
70 : : E_NXIO = osl_File_E_NXIO,
71 : : E_2BIG = osl_File_E_2BIG,
72 : : E_NOEXEC = osl_File_E_NOEXEC,
73 : : E_BADF = osl_File_E_BADF,
74 : : E_CHILD = osl_File_E_CHILD,
75 : : E_AGAIN = osl_File_E_AGAIN,
76 : : E_NOMEM = osl_File_E_NOMEM,
77 : : E_ACCES = osl_File_E_ACCES,
78 : : E_FAULT = osl_File_E_FAULT,
79 : : E_BUSY = osl_File_E_BUSY,
80 : : E_EXIST = osl_File_E_EXIST,
81 : : E_XDEV = osl_File_E_XDEV,
82 : : E_NODEV = osl_File_E_NODEV,
83 : : E_NOTDIR = osl_File_E_NOTDIR,
84 : : E_ISDIR = osl_File_E_ISDIR,
85 : : E_INVAL = osl_File_E_INVAL,
86 : : E_NFILE = osl_File_E_NFILE,
87 : : E_MFILE = osl_File_E_MFILE,
88 : : E_NOTTY = osl_File_E_NOTTY,
89 : : E_FBIG = osl_File_E_FBIG,
90 : : E_NOSPC = osl_File_E_NOSPC,
91 : : E_SPIPE = osl_File_E_SPIPE,
92 : : E_ROFS = osl_File_E_ROFS,
93 : : E_MLINK = osl_File_E_MLINK,
94 : : E_PIPE = osl_File_E_PIPE,
95 : : E_DOM = osl_File_E_DOM,
96 : : E_RANGE = osl_File_E_RANGE,
97 : : E_DEADLK = osl_File_E_DEADLK,
98 : : E_NAMETOOLONG = osl_File_E_NAMETOOLONG,
99 : : E_NOLCK = osl_File_E_NOLCK,
100 : : E_NOSYS = osl_File_E_NOSYS,
101 : : E_NOTEMPTY = osl_File_E_NOTEMPTY,
102 : : E_LOOP = osl_File_E_LOOP,
103 : : E_ILSEQ = osl_File_E_ILSEQ,
104 : : E_NOLINK = osl_File_E_NOLINK,
105 : : E_MULTIHOP = osl_File_E_MULTIHOP,
106 : : E_USERS = osl_File_E_USERS,
107 : : E_OVERFLOW = osl_File_E_OVERFLOW,
108 : : E_NOTREADY = osl_File_E_NOTREADY,
109 : : E_invalidError = osl_File_E_invalidError, /* unmapped error: always last entry in enum! */
110 : : E_TIMEDOUT = osl_File_E_TIMEDOUT,
111 : : E_NETWORK = osl_File_E_NETWORK
112 : : };
113 : :
114 : :
115 : : public:
116 : :
117 : : /** Determine a valid unused canonical name for a requested name.
118 : :
119 : : Determines a valid unused canonical name for a requested name.
120 : : Depending on the Operating System and the File System the illegal characters are replaced by valid ones.
121 : : If a file or directory with the requested name already exists a new name is generated following
122 : : the common rules on the actual Operating System and File System.
123 : :
124 : : @param ustrRequestedURL [in]
125 : : Requested name of a file or directory.
126 : :
127 : : @param ustrValidURL [out]
128 : : On success receives a name which is unused and valid on the actual Operating System and
129 : : File System.
130 : :
131 : : @return
132 : : E_None on success
133 : : E_INVAL the format of the parameters was not valid
134 : :
135 : : @see DirectoryItem::getFileStatus()
136 : : */
137 : :
138 : : static inline RC getCanonicalName( const ::rtl::OUString& ustrRequestedURL, ::rtl::OUString& ustrValidURL )
139 : : {
140 : : return (RC) osl_getCanonicalName( ustrRequestedURL.pData, &ustrValidURL.pData );
141 : : }
142 : :
143 : : /** Convert a path relative to a given directory into an full qualified file URL.
144 : :
145 : : Convert a path relative to a given directory into an full qualified file URL.
146 : : The function resolves symbolic links if possible and path ellipses, so on success
147 : : the resulting absolute path is fully resolved.
148 : :
149 : : @param ustrBaseDirectoryURL [in]
150 : : Base directory URL to which the relative path is related to.
151 : :
152 : : @param ustrRelativeFileURL [in]
153 : : An URL of a file or directory relative to the directory path specified by ustrBaseDirectoryURL
154 : : or an absolute path.
155 : : If ustrRelativeFileURL denotes an absolute path ustrBaseDirectoryURL will be ignored.
156 : :
157 : : @param ustrAbsoluteFileURL [out]
158 : : On success it receives the full qualified absoulte file URL.
159 : :
160 : : @return
161 : : E_None on success
162 : : E_INVAL the format of the parameters was not valid
163 : : E_NOMEM not enough memory for allocating structures
164 : : E_NOTDIR not a directory
165 : : E_ACCES permission denied
166 : : E_NOENT no such file or directory
167 : : E_NAMETOOLONG file name too long
168 : : E_OVERFLOW value too large for defined data type
169 : : E_FAULT bad address
170 : : E_INTR function call was interrupted
171 : : E_LOOP too many symbolic links encountered
172 : : E_MULTIHOP multihop attempted
173 : : E_NOLINK link has been severed
174 : :
175 : : @see DirectoryItem::getFileStatus()
176 : : */
177 : :
178 : 29150 : static inline RC getAbsoluteFileURL( const ::rtl::OUString& ustrBaseDirectoryURL, const ::rtl::OUString& ustrRelativeFileURL, ::rtl::OUString& ustrAbsoluteFileURL )
179 : : {
180 : 29150 : return (RC) osl_getAbsoluteFileURL( ustrBaseDirectoryURL.pData, ustrRelativeFileURL.pData, &ustrAbsoluteFileURL.pData );
181 : : }
182 : :
183 : : /** Convert a file URL into a system dependend path.
184 : :
185 : : @param ustrFileURL [in]
186 : : A File URL.
187 : :
188 : : @param ustrSystemPath [out]
189 : : On success it receives the system path.
190 : :
191 : : @return
192 : : E_None on success
193 : : E_INVAL the format of the parameters was not valid
194 : :
195 : : @see getFileURLFromSystemPath()
196 : : */
197 : :
198 : 470748 : static inline RC getSystemPathFromFileURL( const ::rtl::OUString& ustrFileURL, ::rtl::OUString& ustrSystemPath )
199 : : {
200 : 470748 : return (RC) osl_getSystemPathFromFileURL( ustrFileURL.pData, &ustrSystemPath.pData );
201 : : }
202 : :
203 : : /** Convert a system dependend path into a file URL.
204 : :
205 : : @param ustrSystemPath [in]
206 : : A System dependent path of a file or directory.
207 : :
208 : : @param ustrFileURL [out]
209 : : On success it receives the file URL.
210 : :
211 : : @return
212 : : E_None on success
213 : : E_INVAL the format of the parameters was not valid
214 : :
215 : : @see getSystemPathFromFileURL()
216 : : */
217 : :
218 : 192480 : static inline RC getFileURLFromSystemPath( const ::rtl::OUString& ustrSystemPath, ::rtl::OUString& ustrFileURL )
219 : : {
220 : 192480 : return (RC) osl_getFileURLFromSystemPath( ustrSystemPath.pData, &ustrFileURL.pData );
221 : : }
222 : :
223 : : /** Searche a full qualified system path or a file URL.
224 : :
225 : : @param ustrFileName [in]
226 : : A system dependent path, a file URL, a file or relative directory
227 : :
228 : : @param ustrSearchPath [in]
229 : : A list of system paths, in which a given file has to be searched. The Notation of a path list is
230 : : system dependend, e.g. on UNIX system "/usr/bin:/bin" and on Windows "C:\BIN;C:\BATCH".
231 : : These paths are only for the search of a file or a relative path, otherwise it will be ignored.
232 : : If ustrSearchPath is NULL or while using the search path the search failed, the function searches for
233 : : a matching file in all system directories and in the directories listed in the PATH environment
234 : : variable.
235 : : The value of an environment variable should be used (e.g. LD_LIBRARY_PATH) if the caller is not
236 : : aware of the Operating System and so doesn't know which path list delimiter to use.
237 : :
238 : : @param ustrFileURL [out]
239 : : On success it receives the full qualified file URL.
240 : :
241 : : @return
242 : : E_None on success
243 : : E_INVAL the format of the parameters was not valid
244 : : E_NOTDIR not a directory
245 : : E_NOENT no such file or directory not found
246 : :
247 : : @see getFileURLFromSystemPath()
248 : : @see getSystemPathFromFileURL()
249 : : */
250 : :
251 : 0 : static inline RC searchFileURL( const ::rtl::OUString& ustrFileName, const ::rtl::OUString& ustrSearchPath, ::rtl::OUString& ustrFileURL )
252 : : {
253 : 0 : return (RC) osl_searchFileURL( ustrFileName.pData, ustrSearchPath.pData, &ustrFileURL.pData );
254 : : }
255 : :
256 : : /** Retrieves the file URL of the system's temporary directory path.
257 : :
258 : : @param[out] ustrTempDirURL
259 : : On success receives the URL of system's temporary directory path.
260 : :
261 : : @return
262 : : E_None on success
263 : : E_NOENT no such file or directory not found
264 : : */
265 : :
266 : 310 : static inline RC getTempDirURL( ::rtl::OUString& ustrTempDirURL )
267 : : {
268 : 310 : return (RC) osl_getTempDirURL( &ustrTempDirURL.pData );
269 : : }
270 : :
271 : : /** Creates a temporary file in the directory provided by the caller or the
272 : : directory returned by getTempDirURL.
273 : : Under UNIX Operating Systems the file will be created with read and write
274 : : access for the user exclusively.
275 : : If the caller requests only a handle to the open file but not the name of
276 : : it, the file will be automatically removed on close else the caller is
277 : : responsible for removing the file on success.<br><br>
278 : :
279 : : @param pustrDirectoryURL [in]
280 : : Specifies the full qualified URL where the temporary file should be created.
281 : : If pustrDirectoryURL is 0 the path returned by osl_getTempDirURL will be used.
282 : :
283 : : @param pHandle [out]
284 : : On success receives a handle to the open file.
285 : : If pHandle is 0 the file will be closed on return, in this case
286 : : pustrTempFileURL must not be 0.
287 : :
288 : : @param pustrTempFileURL [out]
289 : : On success receives the full qualified URL of the temporary file.
290 : : If pustrTempFileURL is 0 the file will be automatically removed
291 : : on close, in this case pHandle must not be 0.
292 : : If pustrTempFileURL is not 0 the caller receives the name of the
293 : : created file and is responsible for removing the file.
294 : :
295 : : @descr
296 : : Description of the different pHandle, ppustrTempFileURL parameter combinations.
297 : : pHandle is 0 and pustrTempDirURL is 0 - this combination is invalid<br>
298 : : pHandle is not 0 and pustrTempDirURL is 0 - a handle to the open file
299 : : will be returned on success and the file will be automatically removed on close<br>
300 : : pHandle is 0 and pustrTempDirURL is not 0 - the name of the file will be
301 : : returned, the caller is responsible for opening, closing and removing the file.<br>
302 : : pHandle is not 0 and pustrTempDirURL is not 0 - a handle to the open file as well as
303 : : the file name will be returned, the caller is responsible for closing and removing
304 : : the file.<br>
305 : :
306 : : @return
307 : : E_None on success
308 : : E_INVAL the format of the parameter is invalid
309 : : E_NOMEM not enough memory for allocating structures
310 : : E_ACCES Permission denied
311 : : E_NOENT No such file or directory
312 : : E_NOTDIR Not a directory
313 : : E_ROFS Read-only file system
314 : : E_NOSPC No space left on device
315 : : E_DQUOT Quota exceeded
316 : :
317 : : @see getTempDirURL()
318 : : */
319 : :
320 : 3242 : static inline RC createTempFile(
321 : : ::rtl::OUString* pustrDirectoryURL,
322 : : oslFileHandle* pHandle,
323 : : ::rtl::OUString* pustrTempFileURL)
324 : : {
325 [ + + ]: 3242 : rtl_uString* pustr_dir_url = pustrDirectoryURL ? pustrDirectoryURL->pData : 0;
326 [ + - ]: 3242 : rtl_uString** ppustr_tmp_file_url = pustrTempFileURL ? &pustrTempFileURL->pData : 0;
327 : :
328 : 3242 : return (RC) osl_createTempFile(pustr_dir_url, pHandle, ppustr_tmp_file_url);
329 : : }
330 : : };
331 : :
332 : :
333 : : // -----------------------------------------------------------------------------
334 : : /** The VolumeDevice class.
335 : :
336 : : @see VolumeInfo
337 : : */
338 : :
339 : : class VolumeDevice : public FileBase
340 : : {
341 : : oslVolumeDeviceHandle _aHandle;
342 : :
343 : : public:
344 : :
345 : : /** Constructor.
346 : : */
347 : :
348 : 57080 : VolumeDevice() : _aHandle( NULL )
349 : : {
350 : 57080 : }
351 : :
352 : : /** Copy constructor.
353 : :
354 : : @param rDevice
355 : : The other volume device.
356 : : */
357 : :
358 : : VolumeDevice( const VolumeDevice & rDevice )
359 : : {
360 : : _aHandle = rDevice._aHandle;
361 : : if ( _aHandle )
362 : : osl_acquireVolumeDeviceHandle( _aHandle );
363 : : }
364 : :
365 : : /** Destructor.
366 : : */
367 : :
368 : 57080 : ~VolumeDevice()
369 : : {
370 [ - + ]: 57080 : if ( _aHandle )
371 : 0 : osl_releaseVolumeDeviceHandle( _aHandle );
372 : 57080 : }
373 : :
374 : : /** Assignment operator.
375 : :
376 : : @param rDevice
377 : : The other volume device.
378 : : */
379 : :
380 : : inline VolumeDevice & operator =( const VolumeDevice & rDevice )
381 : : {
382 : : oslVolumeDeviceHandle newHandle = rDevice._aHandle;
383 : :
384 : : if ( newHandle )
385 : : osl_acquireVolumeDeviceHandle( newHandle );
386 : :
387 : : if ( _aHandle )
388 : : osl_releaseVolumeDeviceHandle( _aHandle );
389 : :
390 : : _aHandle = newHandle;
391 : :
392 : : return *this;
393 : : }
394 : :
395 : : /** Get the full qualified URL where a device is mounted to.
396 : :
397 : : @return
398 : : The full qualified URL where the device is mounted to.
399 : : */
400 : : inline rtl::OUString getMountPath()
401 : : {
402 : : rtl::OUString aPath;
403 : : osl_getVolumeDeviceMountPath( _aHandle, &aPath.pData );
404 : : return aPath;
405 : : }
406 : :
407 : : friend class VolumeInfo;
408 : : };
409 : :
410 : : // -----------------------------------------------------------------------------
411 : :
412 : : class Directory;
413 : :
414 : : /** The VolumeInfo class.
415 : :
416 : : Neither copy nor assignment is allowed for this class.
417 : :
418 : : @see Directory::getVolumeInfo
419 : : */
420 : :
421 : :
422 : : class VolumeInfo
423 : : {
424 : : oslVolumeInfo _aInfo;
425 : : sal_uInt32 _nMask;
426 : : VolumeDevice _aDevice;
427 : :
428 : : /** Copy constructor.
429 : : */
430 : :
431 : : VolumeInfo( VolumeInfo& );
432 : :
433 : : /** Assginment operator.
434 : : */
435 : :
436 : : VolumeInfo& operator = ( VolumeInfo& );
437 : :
438 : : public:
439 : :
440 : : /** Constructor.
441 : :
442 : : @param nMask
443 : : Set of flags decribing the demanded information.
444 : : */
445 : :
446 : 57080 : VolumeInfo( sal_uInt32 nMask ): _nMask( nMask )
447 : : {
448 : 57080 : _aInfo.uStructSize = sizeof( oslVolumeInfo );
449 : 57080 : memset( &_aInfo.uValidFields, 0, sizeof( oslVolumeInfo ) - sizeof( sal_uInt32 ) );
450 : 57080 : _aInfo.pDeviceHandle = &_aDevice._aHandle;
451 : 57080 : }
452 : :
453 : : /** Destructor.
454 : : */
455 : :
456 : 57080 : ~VolumeInfo()
457 : 57080 : {
458 [ - + ]: 57080 : if( _aInfo.ustrFileSystemName )
459 : 0 : rtl_uString_release( _aInfo.ustrFileSystemName );
460 : 57080 : }
461 : :
462 : : /** Check if specified fields are valid.
463 : :
464 : : @param nMask
465 : : Set of flags for the fields to check.
466 : :
467 : : @return sal_True if all fields are valid else sal_False.
468 : : */
469 : :
470 : 40 : inline sal_Bool isValid( sal_uInt32 nMask ) const
471 : : {
472 : 40 : return ( nMask & _aInfo.uValidFields ) == nMask;
473 : : }
474 : :
475 : : /** Check the remote flag.
476 : :
477 : : @return
478 : : sal_True if Attributes are valid and the volume is remote else sal_False.
479 : : */
480 : :
481 : 5 : inline sal_Bool getRemoteFlag() const
482 : : {
483 : 5 : return 0 != (_aInfo.uAttributes & osl_Volume_Attribute_Remote);
484 : : }
485 : :
486 : : /** Check the removeable flag.
487 : :
488 : : @return
489 : : sal_True if attributes are valid and the volume is removable else sal_False.
490 : : */
491 : :
492 : 5 : inline sal_Bool getRemoveableFlag() const
493 : : {
494 : 5 : return 0 != (_aInfo.uAttributes & osl_Volume_Attribute_Removeable);
495 : : }
496 : :
497 : : /** Check the compact disc flag.
498 : :
499 : : @return
500 : : sal_True if attributes are valid and the volume is a CDROM else sal_False.
501 : : */
502 : :
503 : 5 : inline sal_Bool getCompactDiscFlag() const
504 : : {
505 : 5 : return 0 != (_aInfo.uAttributes & osl_Volume_Attribute_CompactDisc);
506 : : }
507 : :
508 : : /** Check the floppy disc flag.
509 : :
510 : : @return
511 : : sal_True if attributes are valid and the volume is a floppy disk else sal_False.
512 : : */
513 : :
514 : 5 : inline sal_Bool getFloppyDiskFlag() const
515 : : {
516 : 5 : return 0 != (_aInfo.uAttributes & osl_Volume_Attribute_FloppyDisk);
517 : : }
518 : :
519 : : /** Check the fixed disk flag.
520 : :
521 : : @return
522 : : sal_True if attributes are valid and the volume is a fixed disk else sal_False.
523 : : */
524 : :
525 : 5 : inline sal_Bool getFixedDiskFlag() const
526 : : {
527 : 5 : return 0 != (_aInfo.uAttributes & osl_Volume_Attribute_FixedDisk);
528 : : }
529 : :
530 : : /** Check the RAM disk flag.
531 : :
532 : : @return
533 : : sal_True if attributes are valid and the volume is a RAM disk else sal_False.
534 : : */
535 : :
536 : 5 : inline sal_Bool getRAMDiskFlag() const
537 : : {
538 : 5 : return 0 != (_aInfo.uAttributes & osl_Volume_Attribute_RAMDisk);
539 : : }
540 : :
541 : : /** Determine the total space of a volume device.
542 : :
543 : : @return
544 : : The total diskspace of this volume if this information is valid,
545 : : 0 otherwise.
546 : : */
547 : :
548 : 5 : inline sal_uInt64 getTotalSpace() const
549 : : {
550 : 5 : return _aInfo.uTotalSpace;
551 : : }
552 : :
553 : : /** Determine the free space of a volume device.
554 : :
555 : : @return
556 : : The free diskspace of this volume if this information is valid,
557 : : 0 otherwise.
558 : : */
559 : :
560 : 5 : inline sal_uInt64 getFreeSpace() const
561 : : {
562 : 5 : return _aInfo.uFreeSpace;
563 : : }
564 : :
565 : : /** Determine the used space of a volume device.
566 : :
567 : : @return
568 : : The used diskspace of this volume if this information is valid,
569 : : 0 otherwise.
570 : : */
571 : :
572 : 5 : inline sal_uInt64 getUsedSpace() const
573 : : {
574 : 5 : return _aInfo.uUsedSpace;
575 : : }
576 : :
577 : : /** Determine the maximal length of a file name.
578 : :
579 : : @return
580 : : The maximal length of a file name if this information is valid,
581 : : 0 otherwise.
582 : : */
583 : :
584 : 5 : inline sal_uInt32 getMaxNameLength() const
585 : : {
586 : 5 : return _aInfo.uMaxNameLength;
587 : : }
588 : :
589 : : /** Determine the maximal length of a path name.
590 : :
591 : : @return
592 : : The maximal length of a path if this information is valid,
593 : : 0 otherwise.
594 : : */
595 : :
596 : 5 : inline sal_uInt32 getMaxPathLength() const
597 : : {
598 : 5 : return _aInfo.uMaxPathLength;
599 : : }
600 : :
601 : : /** Determine the name of the volume device's File System.
602 : :
603 : : @return
604 : : The name of the volume's fielsystem if this information is valid,
605 : : otherwise an empty string.
606 : : */
607 : :
608 : 0 : inline ::rtl::OUString getFileSystemName() const
609 : : {
610 [ # # ]: 0 : return _aInfo.ustrFileSystemName ? ::rtl::OUString( _aInfo.ustrFileSystemName ) : ::rtl::OUString();
611 : : }
612 : :
613 : :
614 : : /** Get the volume device handle.
615 : :
616 : : @return
617 : : The device handle of the volume if this information is valid,
618 : : otherwise returns NULL;
619 : : */
620 : :
621 : : inline VolumeDevice getDeviceHandle() const
622 : : {
623 : : return _aDevice;
624 : : }
625 : :
626 : : /** Return whether the file system is case sensitive or
627 : : case insensitive
628 : :
629 : : @return
630 : : true if the file system is case sensitive false otherwise
631 : : */
632 : 0 : bool isCaseSensitiveFileSystem() const
633 : : {
634 : 0 : return (_aInfo.uAttributes & osl_Volume_Attribute_Case_Sensitive);
635 : : }
636 : :
637 : : /** Return whether the file system preserves the case of
638 : : file and directory names or not
639 : :
640 : : @return
641 : : true if the file system preserves the case of file and
642 : : directory names false otherwise
643 : : */
644 : : bool isCasePreservingFileSystem() const
645 : : {
646 : : return (_aInfo.uAttributes & osl_Volume_Attribute_Case_Is_Preserved);
647 : : }
648 : :
649 : : friend class Directory;
650 : : };
651 : :
652 : : // -----------------------------------------------------------------------------
653 : : class DirectoryItem;
654 : :
655 : : /** The FileStatus class.
656 : :
657 : : @see DirectoryItem::getFileStatus
658 : : */
659 : :
660 : : class FileStatus
661 : : {
662 : : oslFileStatus _aStatus;
663 : : sal_uInt32 _nMask;
664 : :
665 : : /** Copy constructor.
666 : : */
667 : :
668 : : FileStatus( FileStatus& );
669 : :
670 : : /** Assignment operator.
671 : : */
672 : :
673 : : FileStatus& operator = ( FileStatus& );
674 : :
675 : : public:
676 : :
677 : : enum Type {
678 : : Directory = osl_File_Type_Directory,
679 : : Volume = osl_File_Type_Volume,
680 : : Regular = osl_File_Type_Regular,
681 : : Fifo = osl_File_Type_Fifo,
682 : : Socket = osl_File_Type_Socket,
683 : : Link = osl_File_Type_Link,
684 : : Special = osl_File_Type_Special,
685 : : Unknown = osl_File_Type_Unknown
686 : : };
687 : :
688 : : /** Constructor.
689 : :
690 : : @param nMask
691 : : Set of flags decribing the demanded information.
692 : : */
693 : :
694 : 277305 : FileStatus( sal_uInt32 nMask ): _nMask( nMask )
695 : : {
696 : 277305 : _aStatus.uStructSize = sizeof( oslFileStatus );
697 : 277305 : memset( &_aStatus.uValidFields, 0, sizeof( oslFileStatus ) - sizeof( sal_uInt32 ) );
698 : 277305 : }
699 : :
700 : : /** Destructor.
701 : : */
702 : :
703 : 277305 : ~FileStatus()
704 : : {
705 [ + + ]: 277305 : if ( _aStatus.ustrFileURL )
706 : 116414 : rtl_uString_release( _aStatus.ustrFileURL );
707 [ + + ]: 277305 : if ( _aStatus.ustrLinkTargetURL )
708 : 1808 : rtl_uString_release( _aStatus.ustrLinkTargetURL );
709 [ + + ]: 277305 : if ( _aStatus.ustrFileName )
710 : 124168 : rtl_uString_release( _aStatus.ustrFileName );
711 : 277305 : }
712 : :
713 : : /** Check if specified fields are valid.
714 : :
715 : : @param nMask
716 : : Set of flags for the fields to check.
717 : :
718 : : @return
719 : : sal_True if all fields are valid else sal_False.
720 : : */
721 : :
722 : 1262867 : inline sal_Bool isValid( sal_uInt32 nMask ) const
723 : : {
724 : 1262867 : return ( nMask & _aStatus.uValidFields ) == nMask;
725 : : }
726 : :
727 : : /** Get the file type.
728 : :
729 : : @return
730 : : The file type.
731 : : */
732 : 375938 : inline Type getFileType() const
733 : : {
734 : : SAL_INFO_IF(
735 : : !isValid(osl_FileStatus_Mask_Type), "sal",
736 : : "no FileStatus Type determined");
737 : 375938 : return isValid(osl_FileStatus_Mask_Type)
738 [ + - ]: 375938 : ? static_cast< Type >(_aStatus.eType) : Unknown;
739 : : }
740 : :
741 : : /** Is it a directory?
742 : : This method returns True for both directories, and volumes.
743 : :
744 : : @return
745 : : True if it's a directory, False otherwise.
746 : :
747 : : @see getFileType
748 : : @since LibreOffice 3.6
749 : : */
750 : : inline sal_Bool isDirectory() const
751 : : {
752 : : return ( getFileType() == Directory || getFileType() == Volume );
753 : : }
754 : :
755 : : /** Is it a regular file?
756 : :
757 : : @return
758 : : True if it's a regular file, False otherwise.
759 : :
760 : : @see getFileType
761 : : @see isFile
762 : : @see isLink
763 : : @since LibreOffice 3.6
764 : : */
765 : : inline sal_Bool isRegular() const
766 : : {
767 : : return ( getFileType() == Regular );
768 : : }
769 : :
770 : : /** Is it a link?
771 : :
772 : : @return
773 : : True if it's a link, False otherwise.
774 : :
775 : : @see getFileType
776 : : @since LibreOffice 3.6
777 : : */
778 : : inline sal_Bool isLink() const
779 : : {
780 : : return ( getFileType() == Link );
781 : : }
782 : :
783 : : /** Get the file attributes.
784 : :
785 : : @return
786 : : The set of attribute flags of this file.
787 : : */
788 : :
789 : 114848 : inline sal_uInt64 getAttributes() const
790 : : {
791 : : SAL_INFO_IF(
792 : : !isValid(osl_FileStatus_Mask_Attributes), "sal",
793 : : "no FileStatus Attributes determined");
794 : 114848 : return _aStatus.uAttributes;
795 : : }
796 : :
797 : : /** Get the creation time of this file.
798 : :
799 : : @return
800 : : The creation time if this information is valid, an uninitialized
801 : : TimeValue otherwise.
802 : : */
803 : :
804 : 0 : inline TimeValue getCreationTime() const
805 : : {
806 : : SAL_INFO_IF(
807 : : !isValid(osl_FileStatus_Mask_CreationTime), "sal",
808 : : "no FileStatus CreationTime determined");
809 : 0 : return _aStatus.aCreationTime;
810 : : }
811 : :
812 : : /** Get the file access time.
813 : :
814 : : @return
815 : : The last access time if this information is valid, an uninitialized
816 : : TimeValue otherwise.
817 : : */
818 : :
819 : 0 : inline TimeValue getAccessTime() const
820 : : {
821 : : SAL_INFO_IF(
822 : : !isValid(osl_FileStatus_Mask_AccessTime), "sal",
823 : : "no FileStatus AccessTime determined");
824 : 0 : return _aStatus.aAccessTime;
825 : : }
826 : :
827 : : /** Get the file modification time.
828 : :
829 : : @return
830 : : The last modified time if this information is valid, an uninitialized
831 : : TimeValue otherwise.
832 : : */
833 : :
834 : 58104 : inline TimeValue getModifyTime() const
835 : : {
836 : : SAL_INFO_IF(
837 : : !isValid(osl_FileStatus_Mask_ModifyTime), "sal",
838 : : "no FileStatus ModifyTime determined");
839 : 58104 : return _aStatus.aModifyTime;
840 : : }
841 : :
842 : : /** Get the size of the file.
843 : :
844 : : @return
845 : : The actual file size if this information is valid, 0 otherwise.
846 : : */
847 : :
848 : 32491 : inline sal_uInt64 getFileSize() const
849 : : {
850 : : SAL_INFO_IF(
851 : : !isValid(osl_FileStatus_Mask_FileSize), "sal",
852 : : "no FileStatus FileSize determined");
853 : 32491 : return _aStatus.uFileSize;
854 : : }
855 : :
856 : : /** Get the file name.
857 : :
858 : : @return
859 : : The file name if this information is valid, an empty string otherwise.
860 : : */
861 : :
862 : 138616 : inline ::rtl::OUString getFileName() const
863 : : {
864 : : SAL_INFO_IF(
865 : : !isValid(osl_FileStatus_Mask_FileName), "sal",
866 : : "no FileStatus FileName determined");
867 : 138616 : return isValid(osl_FileStatus_Mask_FileName)
868 [ + - ]: 138616 : ? rtl::OUString(_aStatus.ustrFileName) : rtl::OUString();
869 : : }
870 : :
871 : :
872 : : /** Get the URL of the file.
873 : :
874 : : @return
875 : : The full qualified URL of the file if this information is valid, an
876 : : empty string otherwise.
877 : : */
878 : :
879 : 129466 : inline ::rtl::OUString getFileURL() const
880 : : {
881 : : SAL_INFO_IF(
882 : : !isValid(osl_FileStatus_Mask_FileURL), "sal",
883 : : "no FileStatus FileURL determined");
884 : 129466 : return isValid(osl_FileStatus_Mask_FileURL)
885 [ + - ]: 129466 : ? rtl::OUString(_aStatus.ustrFileURL) : rtl::OUString();
886 : : }
887 : :
888 : : /** Get the link target URL.
889 : :
890 : : @return
891 : : The link target URL if this information is valid, an empty string
892 : : otherwise.
893 : : */
894 : :
895 : 124 : inline ::rtl::OUString getLinkTargetURL() const
896 : : {
897 : : SAL_INFO_IF(
898 : : !isValid(osl_FileStatus_Mask_LinkTargetURL), "sal",
899 : : "no FileStatus LinkTargetURL determined");
900 : 124 : return isValid(osl_FileStatus_Mask_LinkTargetURL)
901 [ + - ]: 124 : ? rtl::OUString(_aStatus.ustrLinkTargetURL) : rtl::OUString();
902 : : }
903 : :
904 : : friend class DirectoryItem;
905 : : };
906 : :
907 : :
908 : : // -----------------------------------------------------------------------------
909 : : /** The file class object provides access to file contents and attributes.
910 : :
911 : : @see Directory
912 : : @see DirectoryItem
913 : : */
914 : :
915 : : class File: public FileBase
916 : : {
917 : : oslFileHandle _pData;
918 : : ::rtl::OUString _aPath;
919 : :
920 : : /** Copy constructor.
921 : : */
922 : :
923 : : File( File& );
924 : :
925 : : /** Assginment operator.
926 : : */
927 : :
928 : : File& operator = ( File& );
929 : :
930 : : public:
931 : :
932 : : /** Constructor.
933 : :
934 : : @param ustrFileURL [in]
935 : : The full qualified URL of the file. Relative paths are not allowed.
936 : : */
937 : :
938 : 76024 : File( const ::rtl::OUString& ustrFileURL ): _pData( 0 ), _aPath( ustrFileURL ) {}
939 : :
940 : : /** Destructor
941 : : */
942 : :
943 : 75823 : inline ~File()
944 : 75823 : {
945 [ + - ]: 75823 : close();
946 : 75823 : }
947 : :
948 : : /** Open a regular file.
949 : :
950 : : Open a file. Only regular files can be openend.
951 : :
952 : : @param uFlags [in]
953 : : Specifies the open mode.
954 : :
955 : : @return
956 : : E_None on success
957 : : E_NOMEM not enough memory for allocating structures
958 : : E_INVAL the format of the parameters was not valid
959 : : E_NAMETOOLONG pathname was too long
960 : : E_NOENT no such file or directory
961 : : E_ACCES permission denied
962 : : E_AGAIN a write lock could not be established
963 : : E_NOTDIR not a directory
964 : : E_NXIO no such device or address
965 : : E_NODEV no such device
966 : : E_ROFS read-only file system
967 : : E_TXTBSY text file busy
968 : : E_FAULT bad address
969 : : E_LOOP too many symbolic links encountered
970 : : E_NOSPC no space left on device
971 : : E_ISDIR is a directory
972 : : E_MFILE too many open files used by the process
973 : : E_NFILE too many open files in the system
974 : : E_DQUOT quota exceeded
975 : : E_EXIST file exists
976 : : E_INTR function call was interrupted
977 : : E_IO on I/O errors
978 : : E_MULTIHOP multihop attempted
979 : : E_NOLINK link has been severed
980 : : E_EOVERFLOW value too large for defined data type
981 : :
982 : : @see close()
983 : : @see setPos()
984 : : @see getPos()
985 : : @see read()
986 : : @see write()
987 : : @see getSize()
988 : : @see setSize()
989 : : */
990 : :
991 : 85098 : inline RC open( sal_uInt32 uFlags )
992 : : {
993 : 85098 : return (RC) osl_openFile( _aPath.pData, &_pData, uFlags );
994 : : }
995 : :
996 : : /** Close an open file.
997 : :
998 : : @return
999 : : E_None on success
1000 : : E_INVAL the format of the parameters was not valid
1001 : : E_BADF Bad file
1002 : : E_INTR function call was interrupted
1003 : : E_NOLINK link has been severed
1004 : : E_NOSPC no space left on device
1005 : : E_IO on I/O errors
1006 : :
1007 : : @see open()
1008 : : */
1009 : :
1010 : 182193 : inline RC close()
1011 : : {
1012 : 182193 : oslFileError Error = osl_File_E_BADF;
1013 : :
1014 [ + + ]: 182193 : if( _pData )
1015 : : {
1016 : 71285 : Error=osl_closeFile( _pData );
1017 : 71285 : _pData = NULL;
1018 : : }
1019 : :
1020 : 182193 : return (RC) Error;
1021 : : }
1022 : :
1023 : : /** Set the internal position pointer of an open file.
1024 : :
1025 : : @param uHow [in]
1026 : : Distance to move the internal position pointer (from uPos).
1027 : :
1028 : : @param uPos [in]
1029 : : Absolute position from the beginning of the file.
1030 : :
1031 : : @return
1032 : : E_None on success
1033 : : E_INVAL the format of the parameters was not valid
1034 : : E_OVERFLOW the resulting file offset would be a value which cannot be represented correctly for regular files
1035 : :
1036 : : @see open()
1037 : : @see getPos()
1038 : : */
1039 : :
1040 : 284759 : inline RC setPos( sal_uInt32 uHow, sal_Int64 uPos ) SAL_WARN_UNUSED_RESULT
1041 : : {
1042 : 284759 : return (RC) osl_setFilePos( _pData, uHow, uPos );
1043 : : }
1044 : :
1045 : : /** Retrieve the current position of the internal pointer of an open file.
1046 : :
1047 : : @param uPos [out]
1048 : : On success receives the current position of the file pointer.
1049 : :
1050 : : @return
1051 : : E_None on success
1052 : : E_INVAL the format of the parameters was not valid
1053 : : E_OVERFLOW the resulting file offset would be a value which cannot be represented correctly for regular files
1054 : :
1055 : : @see open()
1056 : : @see setPos()
1057 : : @see read()
1058 : : @see write()
1059 : : */
1060 : :
1061 : 646765 : inline RC getPos( sal_uInt64& uPos )
1062 : : {
1063 : 646765 : return (RC) osl_getFilePos( _pData, &uPos );
1064 : : }
1065 : :
1066 : : /** Test if the end of a file is reached.
1067 : :
1068 : : @param pIsEOF [out]
1069 : : Points to a variable that receives the end-of-file status.
1070 : :
1071 : : @return
1072 : : E_None on success
1073 : : E_INVAL the format of the parameters was not valid
1074 : : E_INTR function call was interrupted
1075 : : E_IO on I/O errors
1076 : : E_ISDIR is a directory
1077 : : E_BADF bad file
1078 : : E_FAULT bad address
1079 : : E_AGAIN operation would block
1080 : : E_NOLINK link has been severed
1081 : :
1082 : : @see open()
1083 : : @see read()
1084 : : @see readLine()
1085 : : @see setPos()
1086 : : */
1087 : :
1088 : 0 : inline RC isEndOfFile( sal_Bool *pIsEOF )
1089 : : {
1090 : 0 : return (RC) osl_isEndOfFile( _pData, pIsEOF );
1091 : : }
1092 : :
1093 : : /** Set the file size of an open file.
1094 : :
1095 : : Sets the file size of an open file. The file can be truncated or enlarged by the function.
1096 : : The position of the file pointer is not affeced by this function.
1097 : :
1098 : : @param uSize [in]
1099 : : New size in bytes.
1100 : :
1101 : : @return
1102 : : E_None on success
1103 : : E_INVAL the format of the parameters was not valid
1104 : : E_OVERFLOW the resulting file offset would be a value which cannot be represented correctly for regular files
1105 : :
1106 : : @see open()
1107 : : @see setPos()
1108 : : @see getStatus()
1109 : : */
1110 : :
1111 : 5960 : inline RC setSize( sal_uInt64 uSize )
1112 : : {
1113 : 5960 : return (RC) osl_setFileSize( _pData, uSize );
1114 : : }
1115 : :
1116 : : /** Get the file size of an open file.
1117 : :
1118 : : Gets the file size of an open file.
1119 : : The position of the file pointer is not affeced by this function.
1120 : :
1121 : : @param rSize [out]
1122 : : Current size in bytes.
1123 : :
1124 : : @return
1125 : : E_None on success
1126 : : E_INVAL the format of the parameters was not valid
1127 : : E_OVERFLOW the resulting file offset would be a value which cannot be represented correctly for regular files
1128 : :
1129 : : @see open()
1130 : : @see setPos()
1131 : : @see getSize()
1132 : : @see setSize()
1133 : : @see getStatus()
1134 : : */
1135 : :
1136 : 52408 : inline RC getSize( sal_uInt64 &rSize )
1137 : : {
1138 : 52408 : return (RC) osl_getFileSize( _pData, &rSize );
1139 : : }
1140 : :
1141 : : /** Read a number of bytes from a file.
1142 : :
1143 : : Reads a number of bytes from a file. The internal file pointer is
1144 : : increased by the number of bytes read.
1145 : :
1146 : : @param pBuffer [out]
1147 : : Points to a buffer which receives data. The buffer must be large enough
1148 : : to hold uBytesRequested bytes.
1149 : :
1150 : : @param uBytesRequested [in]
1151 : : Number of bytes which should be retrieved.
1152 : :
1153 : : @param rBytesRead [out]
1154 : : On success the number of bytes which have actually been retrieved.
1155 : :
1156 : : @return
1157 : : E_None on success
1158 : : E_INVAL the format of the parameters was not valid
1159 : : E_INTR function call was interrupted
1160 : : E_IO on I/O errors
1161 : : E_ISDIR is a directory
1162 : : E_BADF bad file
1163 : : E_FAULT bad address
1164 : : E_AGAIN operation would block
1165 : : E_NOLINK link has been severed
1166 : :
1167 : : @see open()
1168 : : @see write()
1169 : : @see readLine()
1170 : : @see setPos()
1171 : : */
1172 : :
1173 : 449340 : inline RC read( void *pBuffer, sal_uInt64 uBytesRequested, sal_uInt64& rBytesRead )
1174 : : {
1175 : 449340 : return (RC) osl_readFile( _pData, pBuffer, uBytesRequested, &rBytesRead );
1176 : : }
1177 : :
1178 : : /** Write a number of bytes to a file.
1179 : :
1180 : : Writes a number of bytes to a file.
1181 : : The internal file pointer is increased by the number of bytes read.
1182 : :
1183 : : @param pBuffer [in]
1184 : : Points to a buffer which contains the data.
1185 : :
1186 : : @param uBytesToWrite [in]
1187 : : Number of bytes which should be written.
1188 : :
1189 : : @param rBytesWritten [out]
1190 : : On success the number of bytes which have actually been written.
1191 : :
1192 : : @return
1193 : : E_None on success
1194 : : E_INVAL the format of the parameters was not valid
1195 : : E_FBIG file too large
1196 : : E_DQUOT quota exceeded
1197 : : E_AGAIN operation would block
1198 : : E_BADF bad file
1199 : : E_FAULT bad address
1200 : : E_INTR function call was interrupted
1201 : : E_IO on I/O errosr
1202 : : E_NOLCK no record locks available
1203 : : E_NOLINK link has been severed
1204 : : E_NOSPC no space left on device
1205 : : E_NXIO no such device or address
1206 : :
1207 : : @see open()
1208 : : @see read()
1209 : : @see setPos()
1210 : : */
1211 : :
1212 : 3591405 : inline RC write(const void *pBuffer, sal_uInt64 uBytesToWrite, sal_uInt64& rBytesWritten)
1213 : : {
1214 : 3591405 : return (RC) osl_writeFile( _pData, pBuffer, uBytesToWrite, &rBytesWritten );
1215 : : }
1216 : :
1217 : :
1218 : : /** Read a line from a file.
1219 : :
1220 : : Reads a line from a file. The new line delimiter is NOT returned!
1221 : :
1222 : : @param aSeq [in/out]
1223 : : A reference to a ::rtl::ByteSequence that will hold the line read on success.
1224 : :
1225 : : @return
1226 : : E_None on success
1227 : : E_INVAL the format of the parameters was not valid
1228 : : E_INTR function call was interrupted
1229 : : E_IO on I/O errors
1230 : : E_ISDIR is a directory
1231 : : E_BADF bad file
1232 : : E_FAULT bad address
1233 : : E_AGAIN operation would block
1234 : : E_NOLINK link has been severed
1235 : :
1236 : : @see open()
1237 : : @see read()
1238 : : @see write()
1239 : : @see setPos()
1240 : : */
1241 : :
1242 : 96 : inline RC readLine( ::rtl::ByteSequence& aSeq )
1243 : : {
1244 : 96 : return (RC) osl_readLine( _pData, reinterpret_cast<sal_Sequence**>(&aSeq) );
1245 : : }
1246 : :
1247 : : /** Synchronize the memory representation of a file with that on the physical medium.
1248 : :
1249 : : The function ensures that all modified data and attributes of the file associated with
1250 : : the given file handle have been written to the physical medium.
1251 : : In case the hard disk has a write cache enabled, the data may not really be on
1252 : : permanent storage when osl_syncFile returns.
1253 : :
1254 : : @return
1255 : : <dl>
1256 : : <dt>E_None</dt>
1257 : : <dd>On success</dd>
1258 : : <dt>E_INVAL</dt>
1259 : : <dd>The value of the input parameter is invalid</dd>
1260 : : </dl>
1261 : : <br><p><strong>In addition to these error codes others may occur as well, for instance:</strong></p><br>
1262 : : <dt>E_BADF</dt>
1263 : : <dd>The file is not open for writing</dd>
1264 : : <dt>E_IO</dt>
1265 : : <dd>An I/O error occurred</dd>
1266 : : <dt>E_NOSPC</dt>
1267 : : <dd>There is no enough space on the target device</dd>
1268 : : <dt>E_ROFS</dt>
1269 : : <dd>The file is located on a read only file system</dd>
1270 : : <dt>E_TIMEDOUT</dt>
1271 : : <dd>A remote connection timed out. This may happen when a file is on a remote location</dd>
1272 : : </dl>
1273 : :
1274 : : @see osl_syncFile()
1275 : : @see open()
1276 : : @see write()
1277 : : */
1278 : 522 : inline RC sync() const
1279 : : {
1280 : : OSL_PRECOND(_pData, "File::sync(): File not open");
1281 : 522 : return (RC)osl_syncFile(_pData);
1282 : : }
1283 : :
1284 : : /** Copy a file to a new destination.
1285 : :
1286 : : Copies a file to a new destination. Copies only files not directories.
1287 : : No assumptions should be made about preserving attributes or file time.
1288 : :
1289 : : @param ustrSourceFileURL [in]
1290 : : Full qualified URL of the source file.
1291 : :
1292 : : @param ustrDestFileURL [in]
1293 : : Full qualified URL of the destination file. A directory is NOT a valid destination file!
1294 : :
1295 : : @return
1296 : : E_None on success
1297 : : E_INVAL the format of the parameters was not valid
1298 : : E_NOMEM not enough memory for allocating structures
1299 : : E_ACCES permission denied
1300 : : E_PERM operation not permitted
1301 : : E_NAMETOOLONG file name too long
1302 : : E_NOENT no such file or directory
1303 : : E_ISDIR is a directory
1304 : : E_ROFS read-only file system
1305 : :
1306 : : @see move()
1307 : : @see remove()
1308 : : */
1309 : :
1310 : 3168 : inline static RC copy( const ::rtl::OUString& ustrSourceFileURL, const ::rtl::OUString& ustrDestFileURL )
1311 : : {
1312 : 3168 : return (RC) osl_copyFile( ustrSourceFileURL.pData, ustrDestFileURL.pData );
1313 : : }
1314 : :
1315 : : /** Move a file or directory to a new destination or renames it.
1316 : :
1317 : : Moves a file or directory to a new destination or renames it.
1318 : : File time and attributes are preserved.
1319 : :
1320 : : @param ustrSourceFileURL [in]
1321 : : Full qualified URL of the source file.
1322 : :
1323 : : @param ustrDestFileURL [in]
1324 : : Full qualified URL of the destination file. An existing directory is NOT a valid destination !
1325 : :
1326 : : @return
1327 : : E_None on success
1328 : : E_INVAL the format of the parameters was not valid
1329 : : E_NOMEM not enough memory for allocating structures
1330 : : E_ACCES permission denied
1331 : : E_PERM operation not permitted
1332 : : E_NAMETOOLONG file name too long
1333 : : E_NOENT no such file or directory
1334 : : E_ROFS read-only file system
1335 : :
1336 : : @see copy()
1337 : : */
1338 : :
1339 : 14535 : inline static RC move( const ::rtl::OUString& ustrSourceFileURL, const ::rtl::OUString& ustrDestFileURL )
1340 : : {
1341 : 14535 : return (RC) osl_moveFile( ustrSourceFileURL.pData, ustrDestFileURL.pData );
1342 : : }
1343 : :
1344 : : /** Remove a regular file.
1345 : :
1346 : : @param ustrFileURL [in]
1347 : : Full qualified URL of the file to remove.
1348 : :
1349 : : @return
1350 : : E_None on success
1351 : : E_INVAL the format of the parameters was not valid
1352 : : E_NOMEM not enough memory for allocating structures
1353 : : E_ACCES permission denied
1354 : : E_PERM operation not permitted
1355 : : E_NAMETOOLONG file name too long
1356 : : E_NOENT no such file or directory
1357 : : E_ISDIR is a directory
1358 : : E_ROFS read-only file system
1359 : : E_FAULT bad address
1360 : : E_LOOP too many symbolic links encountered
1361 : : E_IO on I/O errors
1362 : : E_BUSY device or resource busy
1363 : : E_INTR function call was interrupted
1364 : : E_LOOP too many symbolic links encountered
1365 : : E_MULTIHOP multihop attempted
1366 : : E_NOLINK link has been severed
1367 : : E_TXTBSY text file busy
1368 : :
1369 : : @see open()
1370 : : */
1371 : :
1372 : 7893 : inline static RC remove( const ::rtl::OUString& ustrFileURL )
1373 : : {
1374 : 7893 : return (RC) osl_removeFile( ustrFileURL.pData );
1375 : : }
1376 : :
1377 : : /** Set file attributes.
1378 : :
1379 : : @param ustrFileURL [in]
1380 : : The full qualified file URL.
1381 : :
1382 : : @param uAttributes [in]
1383 : : Attributes of the file to be set.
1384 : :
1385 : : @return
1386 : : E_None on success
1387 : : E_INVAL the format of the parameters was not valid
1388 : :
1389 : : @see FileStatus
1390 : : */
1391 : :
1392 : 1020 : inline static RC setAttributes( const ::rtl::OUString& ustrFileURL, sal_uInt64 uAttributes )
1393 : : {
1394 : 1020 : return (RC) osl_setFileAttributes( ustrFileURL.pData, uAttributes );
1395 : : }
1396 : :
1397 : : /** Set the file time.
1398 : :
1399 : : @param ustrFileURL [in]
1400 : : The full qualified URL of the file.
1401 : :
1402 : : @param rCreationTime [in]
1403 : : Creation time of the given file.
1404 : :
1405 : : @param rLastAccessTime [in]
1406 : : Time of the last access of the given file.
1407 : :
1408 : : @param rLastWriteTime [in]
1409 : : Time of the last modifying of the given file.
1410 : :
1411 : : @return
1412 : : E_None on success
1413 : : E_INVAL the format of the parameters was not valid
1414 : : E_NOENT no such file or directory not found
1415 : :
1416 : : @see FileStatus
1417 : : */
1418 : :
1419 : 0 : inline static RC setTime(
1420 : : const ::rtl::OUString& ustrFileURL,
1421 : : const TimeValue& rCreationTime,
1422 : : const TimeValue& rLastAccessTime,
1423 : : const TimeValue& rLastWriteTime )
1424 : : {
1425 : : return (RC) osl_setFileTime(
1426 : : ustrFileURL.pData,
1427 : : &rCreationTime,
1428 : : &rLastAccessTime,
1429 : 0 : &rLastWriteTime );
1430 : : }
1431 : :
1432 : : friend class DirectoryItem;
1433 : : };
1434 : :
1435 : : // -----------------------------------------------------------------------------
1436 : : /** The directory item class object provides access to file status information.
1437 : :
1438 : : @see FileStatus
1439 : : */
1440 : :
1441 : : class DirectoryItem: public FileBase
1442 : : {
1443 : : oslDirectoryItem _pData;
1444 : :
1445 : : public:
1446 : :
1447 : : /** Constructor.
1448 : : */
1449 : :
1450 : 253137 : DirectoryItem(): _pData( NULL )
1451 : : {
1452 : 253137 : }
1453 : :
1454 : : /** Copy constructor.
1455 : : */
1456 : :
1457 : 0 : DirectoryItem( const DirectoryItem& rItem ): _pData( rItem._pData)
1458 : : {
1459 [ # # ]: 0 : if( _pData )
1460 : 0 : osl_acquireDirectoryItem( _pData );
1461 : 0 : }
1462 : :
1463 : : /** Destructor.
1464 : : */
1465 : :
1466 : 253137 : ~DirectoryItem()
1467 : : {
1468 [ + + ]: 253137 : if( _pData )
1469 : 183754 : osl_releaseDirectoryItem( _pData );
1470 : 253137 : }
1471 : :
1472 : : /** Assignment operator.
1473 : : */
1474 : :
1475 : 0 : DirectoryItem& operator=(const DirectoryItem& rItem )
1476 : : {
1477 [ # # ]: 0 : if (&rItem != this)
1478 : : {
1479 [ # # ]: 0 : if( _pData )
1480 : 0 : osl_releaseDirectoryItem( _pData );
1481 : :
1482 : 0 : _pData = rItem._pData;
1483 : :
1484 [ # # ]: 0 : if( _pData )
1485 : 0 : osl_acquireDirectoryItem( _pData );
1486 : : }
1487 : 0 : return *this;
1488 : : }
1489 : :
1490 : : /** Check for validity of this instance.
1491 : :
1492 : : @return
1493 : : sal_True if object is valid directory item else sal_False.
1494 : : */
1495 : :
1496 : 0 : inline sal_Bool is()
1497 : : {
1498 : 0 : return _pData != NULL;
1499 : : }
1500 : :
1501 : : /** Retrieve a single directory item.
1502 : :
1503 : : Retrieves a single directory item. The returned handle has an initial refcount of 1.
1504 : : Due to performance issues it is not recommended to use this function while
1505 : : enumerating the contents of a directory. In this case use osl_getNextDirectoryItem() instead.
1506 : :
1507 : : @param ustrFileURL [in]
1508 : : An absolute file URL.
1509 : :
1510 : : @param rItem [out]
1511 : : On success it receives a handle which can be used for subsequent calls to osl_getFileStatus().
1512 : : The handle has to be released by a call to osl_releaseDirectoryItem().
1513 : :
1514 : : @return
1515 : : E_None on success
1516 : : E_INVAL the format of the parameters was not valid
1517 : : E_NOMEM not enough memory for allocating structures
1518 : : E_ACCES permission denied
1519 : : E_MFILE too many open files used by the process
1520 : : E_NFILE too many open files in the system
1521 : : E_NOENT no such file or directory
1522 : : E_LOOP too many symbolic links encountered
1523 : : E_NAMETOOLONG the file name is too long
1524 : : E_NOTDIR a component of the path prefix of path is not a directory
1525 : : E_IO on I/O errors
1526 : : E_MULTIHOP multihop attempted
1527 : : E_NOLINK link has been severed
1528 : : E_FAULT bad address
1529 : : E_INTR the function call was interrupted
1530 : :
1531 : : @see FileStatus
1532 : : @see Directory::getNextItem()
1533 : : */
1534 : :
1535 : 179442 : static inline RC get( const ::rtl::OUString& ustrFileURL, DirectoryItem& rItem )
1536 : : {
1537 [ - + ]: 179442 : if( rItem._pData)
1538 : : {
1539 : 0 : osl_releaseDirectoryItem( rItem._pData );
1540 : 0 : rItem._pData = NULL;
1541 : : }
1542 : :
1543 : 179442 : return (RC) osl_getDirectoryItem( ustrFileURL.pData, &rItem._pData );
1544 : : }
1545 : :
1546 : : /** Retrieve information about a single file or directory.
1547 : :
1548 : : @param rStatus [in|out]
1549 : : Reference to a class which receives the information of the file or directory
1550 : : represented by this directory item.
1551 : :
1552 : : @return
1553 : : E_None on success
1554 : : E_NOMEM not enough memory for allocating structures
1555 : : E_INVAL the format of the parameters was not valid
1556 : : E_LOOP too many symbolic links encountered
1557 : : E_ACCES permission denied
1558 : : E_NOENT no such file or directory
1559 : : E_NAMETOOLONG file name too long
1560 : : E_BADF invalid oslDirectoryItem parameter
1561 : : E_FAULT bad address
1562 : : E_OVERFLOW value too large for defined data type
1563 : : E_INTR function call was interrupted
1564 : : E_NOLINK link has been severed
1565 : : E_MULTIHOP components of path require hopping to multiple remote machines and the file system does not allow it
1566 : : E_MFILE too many open files used by the process
1567 : : E_NFILE too many open files in the system
1568 : : E_NOSPC no space left on device
1569 : : E_NXIO no such device or address
1570 : : E_IO on I/O errors
1571 : : E_NOSYS function not implemented
1572 : :
1573 : : @see get()
1574 : : @see Directory::getNextItem()
1575 : : @see FileStatus
1576 : : */
1577 : :
1578 : 293407 : inline RC getFileStatus( FileStatus& rStatus )
1579 : : {
1580 : 293407 : return (RC) osl_getFileStatus( _pData, &rStatus._aStatus, rStatus._nMask );
1581 : : }
1582 : :
1583 : : /** Determine if a directory item point the the same underlying file
1584 : :
1585 : : The comparison is done first by URL, and then by resolving links to
1586 : : find the target, and finally by comparing inodes on unix.
1587 : :
1588 : : @param[in] pOther
1589 : : A directory handle to compare with the underlying object's item
1590 : :
1591 : : @return
1592 : : sal_True: if the items point to an identical resource<br>
1593 : : sal_False: if the items point to a different resource, or a fatal error occured<br>
1594 : :
1595 : : @see osl_getDirectoryItem()
1596 : :
1597 : : @since LibreOffice 3.6
1598 : : */
1599 : 0 : inline sal_Bool isIdenticalTo( const DirectoryItem &pOther )
1600 : : {
1601 : 0 : return osl_identicalDirectoryItem( _pData, pOther._pData );
1602 : : }
1603 : :
1604 : : friend class Directory;
1605 : : };
1606 : :
1607 : : //###########################################
1608 : :
1609 : : /** Base class for observers of directory creation notifications.
1610 : :
1611 : : Clients which uses the method createDirectoryPath of the class
1612 : : Directory may want to be informed about the directories that
1613 : : have been created. This may be accomplished by deriving from
1614 : : this base class and overwriting the virtual function
1615 : : DirectoryCreated.
1616 : :
1617 : : @see Directory::createPath
1618 : : */
1619 : 5 : class DirectoryCreationObserver
1620 : : {
1621 : : public:
1622 [ - + ]: 5 : virtual ~DirectoryCreationObserver() {}
1623 : :
1624 : : /** This method will be called when a new directory has been
1625 : : created and needs to be overwritten by derived classes.
1626 : : You must not delete the directory that was just created
1627 : : otherwise you will run into an endless loop.
1628 : :
1629 : : @param aDirectoryUrl
1630 : : [in]The absolute file URL of the directory that was just created by
1631 : : ::osl::Directory::createPath.
1632 : : */
1633 : : virtual void DirectoryCreated(const rtl::OUString& aDirectoryUrl) = 0;
1634 : : };
1635 : :
1636 : : //###########################################
1637 : : // This just an internal helper function for
1638 : : // private use.
1639 : 10 : extern "C" inline void SAL_CALL onDirectoryCreated(void* pData, rtl_uString* aDirectoryUrl)
1640 : : {
1641 [ + - ]: 10 : (static_cast<DirectoryCreationObserver*>(pData))->DirectoryCreated(aDirectoryUrl);
1642 : 10 : }
1643 : :
1644 : : /** The directory class object provides a enumeration of DirectoryItems.
1645 : :
1646 : : @see DirectoryItem
1647 : : @see File
1648 : : */
1649 : :
1650 : : class Directory: public FileBase
1651 : : {
1652 : : oslDirectory _pData;
1653 : : ::rtl::OUString _aPath;
1654 : :
1655 : : /** Copy constructor.
1656 : : */
1657 : :
1658 : : Directory( Directory& );
1659 : :
1660 : : /** Assignment operator.
1661 : : */
1662 : :
1663 : : Directory& operator = ( Directory& );
1664 : :
1665 : : public:
1666 : :
1667 : : /** Constructor.
1668 : :
1669 : : @param strPath [in]
1670 : : The full qualified URL of the directory.
1671 : : Relative URLs are not allowed.
1672 : : */
1673 : :
1674 : 54013 : Directory( const ::rtl::OUString& strPath ): _pData( 0 ), _aPath( strPath )
1675 : : {
1676 : 54013 : }
1677 : :
1678 : : /** Destructor.
1679 : : */
1680 : :
1681 : 54013 : ~Directory()
1682 : 54013 : {
1683 [ + - ]: 54013 : close();
1684 : 54013 : }
1685 : :
1686 : : /** Open a directory for enumerating its contents.
1687 : :
1688 : : @return
1689 : : E_None on success
1690 : : E_INVAL the format of the parameters was not valid
1691 : : E_NOENT the specified path doesn't exist
1692 : : E_NOTDIR the specified path is not an directory
1693 : : E_NOMEM not enough memory for allocating structures
1694 : : E_ACCES permission denied
1695 : : E_MFILE too many open files used by the process
1696 : : E_NFILE too many open files in the system
1697 : : E_NAMETOOLONG File name too long
1698 : : E_LOOP Too many symbolic links encountered
1699 : :
1700 : : @see getNextItem()
1701 : : @see close()
1702 : : */
1703 : :
1704 : 53903 : inline RC open()
1705 : : {
1706 : 53903 : return (RC) osl_openDirectory( _aPath.pData, &_pData );
1707 : : }
1708 : :
1709 : : /** Query if directory is open.
1710 : :
1711 : : Query if directory is open and so item enumeration is valid.
1712 : :
1713 : : @return
1714 : : sal_True if the directory is open else sal_False.
1715 : :
1716 : : @see open()
1717 : : @see close()
1718 : : */
1719 : :
1720 : 120 : inline sal_Bool isOpen() { return _pData != NULL; }
1721 : :
1722 : : /** Close a directory.
1723 : :
1724 : : @return
1725 : : E_None on success
1726 : : E_INVAL the format of the parameters was not valid
1727 : : E_NOMEM not enough memory for allocating structures
1728 : : E_BADF invalid oslDirectory parameter
1729 : : E_INTR the function call was interrupted
1730 : :
1731 : : @see open()
1732 : : */
1733 : :
1734 : 100978 : inline RC close()
1735 : : {
1736 : 100978 : oslFileError Error = osl_File_E_BADF;
1737 : :
1738 [ + + ]: 100978 : if( _pData )
1739 : : {
1740 : 50628 : Error=osl_closeDirectory( _pData );
1741 : 50628 : _pData = NULL;
1742 : : }
1743 : :
1744 : 100978 : return (RC) Error;
1745 : : }
1746 : :
1747 : :
1748 : : /** Resets the directory item enumeration to the beginning.
1749 : :
1750 : : @return
1751 : : E_None on success
1752 : : E_INVAL the format of the parameters was not valid
1753 : : E_NOENT the specified path doesn't exist
1754 : : E_NOTDIR the specified path is not an directory
1755 : : E_NOMEM not enough memory for allocating structures
1756 : : E_ACCES permission denied
1757 : : E_MFILE too many open files used by the process
1758 : : E_NFILE too many open files in the system
1759 : : E_NAMETOOLONG File name too long
1760 : : E_LOOP Too many symbolic links encountered
1761 : :
1762 : : @see open()
1763 : : */
1764 : :
1765 : 20 : inline RC reset()
1766 : : {
1767 : 20 : close();
1768 : 20 : return open();
1769 : : }
1770 : :
1771 : : /** Retrieve the next item of a previously opened directory.
1772 : :
1773 : : Retrieves the next item of a previously opened directory.
1774 : :
1775 : : @param rItem [out]
1776 : : On success a valid DirectoryItem.
1777 : :
1778 : : @param nHint [in]
1779 : : With this parameter the caller can tell the implementation that (s)he
1780 : : is going to call this function uHint times afterwards. This enables the implementation to
1781 : : get the information for more than one file and cache it until the next calls.
1782 : :
1783 : : @return
1784 : : E_None on success
1785 : : E_INVAL the format of the parameters was not valid
1786 : : E_NOMEM not enough memory for allocating structures
1787 : : E_NOENT no more entries in this directory
1788 : : E_BADF invalid oslDirectory parameter
1789 : : E_OVERFLOW the value too large for defined data type
1790 : :
1791 : : @see DirectoryItem
1792 : : */
1793 : :
1794 : 172106 : inline RC getNextItem( DirectoryItem& rItem, sal_uInt32 nHint = 0 )
1795 : : {
1796 [ + + ]: 172106 : if( rItem._pData )
1797 : : {
1798 : 99338 : osl_releaseDirectoryItem( rItem._pData );
1799 : 99338 : rItem._pData = 0;
1800 : : }
1801 : 172106 : return ( RC) osl_getNextDirectoryItem( _pData, &rItem._pData, nHint );
1802 : : }
1803 : :
1804 : :
1805 : : /** Retrieve information about a volume.
1806 : :
1807 : : Retrieves information about a volume. A volume can either be a mount point, a network
1808 : : resource or a drive depending on Operating System and File System.
1809 : :
1810 : : @param ustrDirectoryURL [in]
1811 : : Full qualified URL of the volume
1812 : :
1813 : : @param rInfo [out]
1814 : : On success it receives information about the volume.
1815 : :
1816 : : @return
1817 : : E_None on success
1818 : : E_NOMEM not enough memory for allocating structures
1819 : : E_INVAL the format of the parameters was not valid
1820 : : E_NOTDIR not a directory
1821 : : E_NAMETOOLONG file name too long
1822 : : E_NOENT no such file or directory
1823 : : E_ACCES permission denied
1824 : : E_LOOP too many symbolic links encountered
1825 : : E_FAULT Bad address
1826 : : E_IO on I/O errors
1827 : : E_NOSYS function not implemented
1828 : : E_MULTIHOP multihop attempted
1829 : : E_NOLINK link has been severed
1830 : : E_INTR function call was interrupted
1831 : :
1832 : : @see FileStatus
1833 : : @see VolumeInfo
1834 : : */
1835 : :
1836 : 50 : inline static RC getVolumeInfo( const ::rtl::OUString& ustrDirectoryURL, VolumeInfo& rInfo )
1837 : : {
1838 : 50 : return (RC) osl_getVolumeInformation( ustrDirectoryURL.pData, &rInfo._aInfo, rInfo._nMask );
1839 : : }
1840 : :
1841 : : /** Create a directory.
1842 : :
1843 : : @param ustrDirectoryURL [in]
1844 : : Full qualified URL of the directory to create.
1845 : :
1846 : : @return
1847 : : E_None on success
1848 : : E_INVAL the format of the parameters was not valid
1849 : : E_NOMEM not enough memory for allocating structures
1850 : : E_EXIST file exists
1851 : : E_ACCES permission denied
1852 : : E_NAMETOOLONG file name too long
1853 : : E_NOENT no such file or directory
1854 : : E_NOTDIR not a directory
1855 : : E_ROFS read-only file system
1856 : : E_NOSPC no space left on device
1857 : : E_DQUOT quota exceeded
1858 : : E_LOOP too many symbolic links encountered
1859 : : E_FAULT bad address
1860 : : E_IO on I/O errors
1861 : : E_MLINK too many links
1862 : : E_MULTIHOP multihop attempted
1863 : : E_NOLINK link has been severed
1864 : :
1865 : : @see remove()
1866 : : */
1867 : :
1868 : 6348 : inline static RC create( const ::rtl::OUString& ustrDirectoryURL )
1869 : : {
1870 : 6348 : return (RC) osl_createDirectory( ustrDirectoryURL.pData );
1871 : : }
1872 : :
1873 : : /** Remove an empty directory.
1874 : :
1875 : : @param ustrDirectoryURL [in]
1876 : : Full qualified URL of the directory.
1877 : :
1878 : : @return
1879 : : E_None on success
1880 : : E_INVAL the format of the parameters was not valid
1881 : : E_NOMEM not enough memory for allocating structures
1882 : : E_PERM operation not permitted
1883 : : E_ACCES permission denied
1884 : : E_NOENT no such file or directory
1885 : : E_NOTDIR not a directory
1886 : : E_NOTEMPTY directory not empty
1887 : : E_FAULT bad address
1888 : : E_NAMETOOLONG file name too long
1889 : : E_BUSY device or resource busy
1890 : : E_ROFS read-only file system
1891 : : E_LOOP too many symbolic links encountered
1892 : : E_BUSY device or resource busy
1893 : : E_EXIST file exists
1894 : : E_IO on I/O errors
1895 : : E_MULTIHOP multihop attempted
1896 : : E_NOLINK link has been severed
1897 : :
1898 : : @see create()
1899 : : */
1900 : :
1901 : 1946 : inline static RC remove( const ::rtl::OUString& ustrDirectoryURL )
1902 : : {
1903 : 1946 : return (RC) osl_removeDirectory( ustrDirectoryURL.pData );
1904 : : }
1905 : :
1906 : : /** Create a directory path.
1907 : :
1908 : : The osl_createDirectoryPath function creates a specified directory path.
1909 : : All nonexisting sub directories will be created.
1910 : : <p><strong>PLEASE NOTE:</strong> You cannot rely on getting the error code
1911 : : E_EXIST for existing directories. Programming against this error code is
1912 : : in general a strong indication of a wrong usage of osl_createDirectoryPath.</p>
1913 : :
1914 : : @param aDirectoryUrl
1915 : : [in] The absolute file URL of the directory path to create.
1916 : : A relative file URL will not be accepted.
1917 : :
1918 : : @param aDirectoryCreationObserver
1919 : : [in] Pointer to an instance of type DirectoryCreationObserver that will
1920 : : be informed about the creation of a directory. The value of this
1921 : : parameter may be NULL, in this case notifications will not be sent.
1922 : :
1923 : : @return
1924 : : <dl>
1925 : : <dt>E_None</dt>
1926 : : <dd>On success</dd>
1927 : : <dt>E_INVAL</dt>
1928 : : <dd>The format of the parameters was not valid</dd>
1929 : : <dt>E_ACCES</dt>
1930 : : <dd>Permission denied</dd>
1931 : : <dt>E_EXIST</dt>
1932 : : <dd>The final node of the specified directory path already exist</dd>
1933 : : <dt>E_NAMETOOLONG</dt>
1934 : : <dd>The name of the specified directory path exceeds the maximum allowed length</dd>
1935 : : <dt>E_NOTDIR</dt>
1936 : : <dd>A component of the specified directory path already exist as file in any part of the directory path</dd>
1937 : : <dt>E_ROFS</dt>
1938 : : <dd>Read-only file system</dd>
1939 : : <dt>E_NOSPC</dt>
1940 : : <dd>No space left on device</dd>
1941 : : <dt>E_DQUOT</dt>
1942 : : <dd>Quota exceeded</dd>
1943 : : <dt>E_FAULT</dt>
1944 : : <dd>Bad address</dd>
1945 : : <dt>E_IO</dt>
1946 : : <dd>I/O error</dd>
1947 : : <dt>E_LOOP</dt>
1948 : : <dd>Too many symbolic links encountered</dd>
1949 : : <dt>E_NOLINK</dt>
1950 : : <dd>Link has been severed</dd>
1951 : : <dt>E_invalidError</dt>
1952 : : <dd>An unknown error occurred</dd>
1953 : : </dl>
1954 : :
1955 : : @see DirectoryCreationObserver
1956 : : @see create
1957 : : */
1958 : 2864 : static RC createPath(
1959 : : const ::rtl::OUString& aDirectoryUrl,
1960 : : DirectoryCreationObserver* aDirectoryCreationObserver = NULL)
1961 : : {
1962 : : return (RC)osl_createDirectoryPath(
1963 : : aDirectoryUrl.pData,
1964 : : (aDirectoryCreationObserver) ? onDirectoryCreated : NULL,
1965 [ + + ]: 2864 : aDirectoryCreationObserver);
1966 : : }
1967 : : };
1968 : :
1969 : : } /* namespace osl */
1970 : :
1971 : : #endif /* _OSL_FILE_HXX_ */
1972 : :
1973 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|