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