LCOV - code coverage report
Current view: top level - sal/osl/unx - file_volume.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 55 90 61.1 %
Date: 2015-06-13 12:38:46 Functions: 2 6 33.3 %
Legend: Lines: hit not hit

          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             : #include "osl/file.h"
      21             : 
      22             : #include "osl/diagnose.h"
      23             : #include "osl/thread.h"
      24             : #include "rtl/alloc.h"
      25             : 
      26             : #include "file_error_transl.hxx"
      27             : #include "file_url.hxx"
      28             : #include "system.hxx"
      29             : 
      30             : #include <errno.h>
      31             : #include <limits.h>
      32             : #include <stdio.h>
      33             : #include <string.h>
      34             : #include <unistd.h>
      35             : #include <sys/wait.h>
      36             : #include <sal/macros.h>
      37             : 
      38             : #ifdef HAVE_STATFS_H
      39             : #undef HAVE_STATFS_H
      40             : #endif
      41             : 
      42             : #if defined(LINUX) && defined(__FreeBSD_kernel__)
      43             : #undef LINUX
      44             : #define FREEBSD 1
      45             : #endif
      46             : 
      47             : #if defined(SOLARIS)
      48             : 
      49             : #include <sys/mnttab.h>
      50             : #include <sys/statvfs.h>
      51             : #define  HAVE_STATFS_H
      52             : 
      53             : #elif defined(LINUX)
      54             : 
      55             : #include <mntent.h>
      56             : #include <sys/vfs.h>
      57             : #define  HAVE_STATFS_H
      58             : 
      59             : #elif defined(NETBSD) || defined(FREEBSD) || defined(OPENBSD) || defined(DRAGONFLY)
      60             : 
      61             : #include <sys/param.h>
      62             : #include <sys/ucred.h>
      63             : #include <sys/mount.h>
      64             : #define  HAVE_STATFS_H
      65             : 
      66             : #elif defined(MACOSX)
      67             : 
      68             : #include <sys/param.h>
      69             : #include <sys/mount.h>
      70             : #define HAVE_STATFS_H
      71             : 
      72             : #endif /* HAVE_STATFS_H */
      73             : 
      74             : /************************************************************************
      75             :  *   ToDo
      76             :  *
      77             :  *   - Fix: check for corresponding struct sizes in exported functions
      78             :  *   - check size/use of oslVolumeDeviceHandle
      79             :  *   - check size/use of oslVolumeInfo
      80             :  ***********************************************************************/
      81             : /******************************************************************************
      82             :  *
      83             :  *                  Data Type Definition
      84             :  *
      85             :  ******************************************************************************/
      86             : 
      87             : typedef struct _oslVolumeDeviceHandleImpl
      88             : {
      89             :     sal_Char pszMountPoint[PATH_MAX];
      90             :     sal_Char pszFilePath[PATH_MAX];
      91             :     sal_Char pszDevice[PATH_MAX];
      92             :     sal_Char ident[4];
      93             :     sal_uInt32   RefCount;
      94             : } oslVolumeDeviceHandleImpl;
      95             : 
      96             : /******************************************************************************
      97             :  *
      98             :  *                  C-String Function Declarations
      99             :  *
     100             :  *****************************************************************************/
     101             : 
     102             : static oslFileError osl_psz_getVolumeInformation(const sal_Char* , oslVolumeInfo* pInfo, sal_uInt32 uFieldMask);
     103             : 
     104             : /****************************************************************************/
     105             : /*  osl_getVolumeInformation */
     106             : /****************************************************************************/
     107             : 
     108          10 : oslFileError osl_getVolumeInformation( rtl_uString* ustrDirectoryURL, oslVolumeInfo* pInfo, sal_uInt32 uFieldMask )
     109             : {
     110             :     char path[PATH_MAX];
     111             :     oslFileError eRet;
     112             : 
     113             :     OSL_ASSERT( ustrDirectoryURL );
     114             :     OSL_ASSERT( pInfo );
     115             : 
     116             :     /* convert directory url to system path */
     117          10 :     eRet = FileURLToPath( path, PATH_MAX, ustrDirectoryURL );
     118          10 :     if( eRet != osl_File_E_None )
     119           1 :         return eRet;
     120             : 
     121             : #ifdef MACOSX
     122             :     if ( macxp_resolveAlias( path, PATH_MAX ) != 0 )
     123             :       return oslTranslateFileError( OSL_FET_ERROR, errno );
     124             : #endif/* MACOSX */
     125             : 
     126           9 :     return osl_psz_getVolumeInformation( path, pInfo, uFieldMask);
     127             : }
     128             : 
     129             : /******************************************************************************
     130             :  *
     131             :  *                  C-String Versions of Exported Module Functions
     132             :  *
     133             :  *****************************************************************************/
     134             : 
     135             : #ifdef HAVE_STATFS_H
     136             : 
     137             : #if defined(FREEBSD) || defined(MACOSX) || defined(OPENBSD) || defined(DRAGONFLY)
     138             : #   define __OSL_STATFS_STRUCT                  struct statfs
     139             : #   define __OSL_STATFS(dir, sfs)               statfs((dir), (sfs))
     140             : #   define __OSL_STATFS_BLKSIZ(a)               ((sal_uInt64)((a).f_bsize))
     141             : #   define __OSL_STATFS_TYPENAME(a)             ((a).f_fstypename)
     142             : #if defined(OPENBSD)
     143             : #   define __OSL_STATFS_ISREMOTE(a)             (rtl_str_compare((a).f_fstypename, "nfs") == 0)
     144             : #else
     145             : #   define __OSL_STATFS_ISREMOTE(a)             (((a).f_type & MNT_LOCAL) == 0)
     146             : #endif
     147             : 
     148             : /* always return true if queried for the properties of
     149             :    the file system. If you think this is wrong under any
     150             :    of the target platforms fix it!!!! */
     151             : #   define __OSL_STATFS_IS_CASE_SENSITIVE_FS(a)  (true)
     152             : #   define __OSL_STATFS_IS_CASE_PRESERVING_FS(a) (true)
     153             : #endif /* FREEBSD || MACOSX || OPENBSD */
     154             : 
     155             : #if defined(NETBSD)
     156             : 
     157             : #include <sys/param.h>
     158             : 
     159             : #   define __OSL_STATFS_STRUCT              struct statvfs
     160             : #   define __OSL_STATFS(dir, sfs)           statvfs((dir), (sfs))
     161             : #   define __OSL_STATFS_ISREMOTE(a)         (((a).f_flag & ST_LOCAL) == 0)
     162             : 
     163             : #   define __OSL_STATFS_BLKSIZ(a)           ((sal_uInt64)((a).f_bsize))
     164             : #   define __OSL_STATFS_TYPENAME(a)         ((a).f_fstypename)
     165             : 
     166             : #   define __OSL_STATFS_IS_CASE_SENSITIVE_FS(a) (strcmp((a).f_fstypename, "msdos") != 0 && strcmp((a).f_fstypename, "ntfs") != 0 && strcmp((a).f_fstypename, "smbfs") != 0)
     167             : #   define __OSL_STATFS_IS_CASE_PRESERVING_FS(a)    (strcmp((a).f_fstypename, "msdos") != 0)
     168             : #endif /* NETBSD */
     169             : 
     170             : #if defined(LINUX)
     171             : #   define __OSL_NFS_SUPER_MAGIC                 0x6969
     172             : #   define __OSL_SMB_SUPER_MAGIC                 0x517B
     173             : #   define __OSL_MSDOS_SUPER_MAGIC               0x4d44
     174             : #   define __OSL_NTFS_SUPER_MAGIC                0x5346544e
     175             : #   define __OSL_STATFS_STRUCT                   struct statfs
     176             : #   define __OSL_STATFS(dir, sfs)                statfs((dir), (sfs))
     177             : #   define __OSL_STATFS_BLKSIZ(a)                ((sal_uInt64)((a).f_bsize))
     178             : #   define __OSL_STATFS_IS_NFS(a)                (__OSL_NFS_SUPER_MAGIC == (a).f_type)
     179             : #   define __OSL_STATFS_IS_SMB(a)                (__OSL_SMB_SUPER_MAGIC == (a).f_type)
     180             : #   define __OSL_STATFS_ISREMOTE(a)              (__OSL_STATFS_IS_NFS((a)) || __OSL_STATFS_IS_SMB((a)))
     181             : #   define __OSL_STATFS_IS_CASE_SENSITIVE_FS(a)  ((__OSL_MSDOS_SUPER_MAGIC != (a).f_type) && (__OSL_NTFS_SUPER_MAGIC != (a).f_type))
     182             : #   define __OSL_STATFS_IS_CASE_PRESERVING_FS(a) ((__OSL_MSDOS_SUPER_MAGIC != (a).f_type))
     183             : #endif /* LINUX */
     184             : 
     185             : #if defined(SOLARIS)
     186             : #   define __OSL_STATFS_STRUCT                   struct statvfs
     187             : #   define __OSL_STATFS(dir, sfs)                statvfs((dir), (sfs))
     188             : #   define __OSL_STATFS_BLKSIZ(a)                ((sal_uInt64)((a).f_frsize))
     189             : #   define __OSL_STATFS_TYPENAME(a)              ((a).f_basetype)
     190             : #   define __OSL_STATFS_ISREMOTE(a)              (rtl_str_compare((a).f_basetype, "nfs") == 0)
     191             : 
     192             : /* always return true if queried for the properties of
     193             :    the file system. If you think this is wrong under any
     194             :    of the target platforms fix it!!!! */
     195             : #   define __OSL_STATFS_IS_CASE_SENSITIVE_FS(a)  (true)
     196             : #   define __OSL_STATFS_IS_CASE_PRESERVING_FS(a) (true)
     197             : #endif /* SOLARIS */
     198             : 
     199             : #   define __OSL_STATFS_INIT(a)         (memset(&(a), 0, sizeof(__OSL_STATFS_STRUCT)))
     200             : 
     201             : #else /* no statfs available */
     202             : 
     203             : #   define __OSL_STATFS_STRUCT                   struct dummy {int i;}
     204             : #   define __OSL_STATFS_INIT(a)                  ((void)a)
     205             : #   define __OSL_STATFS(dir, sfs)                (1)
     206             : #   define __OSL_STATFS_ISREMOTE(sfs)            (false)
     207             : #   define __OSL_STATFS_IS_CASE_SENSITIVE_FS(a)  (true)
     208             : #   define __OSL_STATFS_IS_CASE_PRESERVING_FS(a) (true)
     209             : #endif /* HAVE_STATFS_H */
     210             : 
     211           9 : static oslFileError osl_psz_getVolumeInformation (
     212             :     const sal_Char* pszDirectory, oslVolumeInfo* pInfo, sal_uInt32 uFieldMask)
     213             : {
     214           9 :     if (!pInfo)
     215           0 :         return osl_File_E_INVAL;
     216             : 
     217           9 :     pInfo->uValidFields = 0;
     218           9 :     pInfo->uAttributes  = 0;
     219           9 :     pInfo->uTotalSpace = 0;
     220           9 :     pInfo->uFreeSpace  = 0;
     221           9 :     pInfo->uUsedSpace  = 0;
     222             : 
     223           9 :     if ((uFieldMask
     224           9 :          & (osl_VolumeInfo_Mask_Attributes | osl_VolumeInfo_Mask_TotalSpace
     225             :             | osl_VolumeInfo_Mask_UsedSpace | osl_VolumeInfo_Mask_FreeSpace
     226             :             | osl_VolumeInfo_Mask_FileSystemName
     227             :             | osl_VolumeInfo_Mask_FileSystemCaseHandling))
     228             :         != 0)
     229             :     {
     230             :         __OSL_STATFS_STRUCT sfs;
     231           7 :         __OSL_STATFS_INIT(sfs);
     232           7 :         if ((__OSL_STATFS(pszDirectory, &sfs)) < 0)
     233             :         {
     234           1 :             oslFileError result = oslTranslateFileError(OSL_FET_ERROR, errno);
     235           1 :             return result;
     236             :         }
     237             : 
     238             :         /* FIXME: how to detect the kind of storage (fixed, cdrom, ...) */
     239           6 :         if (uFieldMask & osl_VolumeInfo_Mask_Attributes)
     240             :         {
     241           1 :             if (__OSL_STATFS_ISREMOTE(sfs))
     242           0 :                 pInfo->uAttributes  |= osl_Volume_Attribute_Remote;
     243             : 
     244           1 :             pInfo->uValidFields |= osl_VolumeInfo_Mask_Attributes;
     245             :         }
     246             : 
     247           6 :         if (uFieldMask & osl_VolumeInfo_Mask_FileSystemCaseHandling)
     248             :         {
     249           1 :             if (__OSL_STATFS_IS_CASE_SENSITIVE_FS(sfs))
     250           1 :                 pInfo->uAttributes |= osl_Volume_Attribute_Case_Sensitive;
     251             : 
     252           1 :             if (__OSL_STATFS_IS_CASE_PRESERVING_FS(sfs))
     253           1 :                 pInfo->uAttributes |= osl_Volume_Attribute_Case_Is_Preserved;
     254             : 
     255           1 :             pInfo->uValidFields |= osl_VolumeInfo_Mask_Attributes;
     256             :         }
     257             : 
     258             : #if defined(__OSL_STATFS_BLKSIZ)
     259             : 
     260          11 :         if ((uFieldMask & osl_VolumeInfo_Mask_TotalSpace) ||
     261           5 :             (uFieldMask & osl_VolumeInfo_Mask_UsedSpace))
     262             :         {
     263           2 :             pInfo->uTotalSpace   = __OSL_STATFS_BLKSIZ(sfs);
     264           2 :             pInfo->uTotalSpace  *= (sal_uInt64)(sfs.f_blocks);
     265           2 :             pInfo->uValidFields |= osl_VolumeInfo_Mask_TotalSpace;
     266             :         }
     267             : 
     268          11 :         if ((uFieldMask & osl_VolumeInfo_Mask_FreeSpace) ||
     269           5 :             (uFieldMask & osl_VolumeInfo_Mask_UsedSpace))
     270             :         {
     271           2 :             pInfo->uFreeSpace = __OSL_STATFS_BLKSIZ(sfs);
     272             : 
     273           2 :             if (getuid() == 0)
     274           0 :                 pInfo->uFreeSpace *= (sal_uInt64)(sfs.f_bfree);
     275             :             else
     276           2 :                 pInfo->uFreeSpace *= (sal_uInt64)(sfs.f_bavail);
     277             : 
     278           2 :             pInfo->uValidFields |= osl_VolumeInfo_Mask_FreeSpace;
     279             :         }
     280             : 
     281           8 :         if ((pInfo->uValidFields & osl_VolumeInfo_Mask_TotalSpace) &&
     282           2 :             (pInfo->uValidFields & osl_VolumeInfo_Mask_FreeSpace ))
     283             :         {
     284           1 :             pInfo->uUsedSpace    = pInfo->uTotalSpace - pInfo->uFreeSpace;
     285           1 :             pInfo->uValidFields |= osl_VolumeInfo_Mask_UsedSpace;
     286             :         }
     287             : 
     288             : #endif  /* __OSL_STATFS_BLKSIZ */
     289             : 
     290             : #if defined(__OSL_STATFS_TYPENAME)
     291             : 
     292             :         if (uFieldMask & osl_VolumeInfo_Mask_FileSystemName)
     293             :         {
     294             :             rtl_string2UString(
     295             :                 &(pInfo->ustrFileSystemName),
     296             :                 __OSL_STATFS_TYPENAME(sfs),
     297             :                 rtl_str_getLength(__OSL_STATFS_TYPENAME(sfs)),
     298             :                 osl_getThreadTextEncoding(),
     299             :                 OUSTRING_TO_OSTRING_CVTFLAGS);
     300             :             OSL_ASSERT(pInfo->ustrFileSystemName != 0);
     301             : 
     302             :             pInfo->uValidFields |= osl_VolumeInfo_Mask_FileSystemName;
     303             :         }
     304             : 
     305             : #endif /* __OSL_STATFS_TYPENAME */
     306             :     }
     307             : 
     308           8 :     pInfo->uMaxNameLength = 0;
     309           8 :     if (uFieldMask & osl_VolumeInfo_Mask_MaxNameLength)
     310             :     {
     311           1 :         long nLen = pathconf(pszDirectory, _PC_NAME_MAX);
     312           1 :         if (nLen > 0)
     313             :         {
     314           1 :             pInfo->uMaxNameLength = (sal_uInt32)nLen;
     315           1 :             pInfo->uValidFields |= osl_VolumeInfo_Mask_MaxNameLength;
     316             :         }
     317             :     }
     318             : 
     319           8 :     pInfo->uMaxPathLength = 0;
     320           8 :     if (uFieldMask & osl_VolumeInfo_Mask_MaxPathLength)
     321             :     {
     322           1 :         long nLen = pathconf (pszDirectory, _PC_PATH_MAX);
     323           1 :         if (nLen > 0)
     324             :         {
     325           1 :             pInfo->uMaxPathLength  = (sal_uInt32)nLen;
     326           1 :             pInfo->uValidFields   |= osl_VolumeInfo_Mask_MaxPathLength;
     327             :         }
     328             :     }
     329             : 
     330           8 :     return osl_File_E_None;
     331             : }
     332             : 
     333             : /******************************************************************************
     334             :  *
     335             :  *                  GENERIC FLOPPY FUNCTIONS
     336             :  *
     337             :  *****************************************************************************/
     338             : 
     339             : /*****************************************
     340             :  * osl_getVolumeDeviceMountPath
     341             :  ****************************************/
     342           0 : static rtl_uString* oslMakeUStrFromPsz(const sal_Char* pszStr, rtl_uString** ustrValid)
     343             : {
     344             :     rtl_string2UString(
     345             :         ustrValid,
     346             :         pszStr,
     347             :         rtl_str_getLength( pszStr ),
     348           0 :         osl_getThreadTextEncoding(),
     349           0 :         OUSTRING_TO_OSTRING_CVTFLAGS );
     350             :     OSL_ASSERT(*ustrValid != 0);
     351             : 
     352           0 :     return *ustrValid;
     353             : }
     354             : 
     355           0 : oslFileError osl_getVolumeDeviceMountPath( oslVolumeDeviceHandle Handle, rtl_uString **pstrPath )
     356             : {
     357           0 :     oslVolumeDeviceHandleImpl* pItem = static_cast<oslVolumeDeviceHandleImpl*>(Handle);
     358             :     sal_Char Buffer[PATH_MAX];
     359             : 
     360           0 :     Buffer[0] = '\0';
     361             : 
     362           0 :     if ( pItem == 0 || pstrPath == 0 )
     363             :     {
     364           0 :         return osl_File_E_INVAL;
     365             :     }
     366             : 
     367           0 :     if ( pItem->ident[0] != 'O' || pItem->ident[1] != 'V' || pItem->ident[2] != 'D' || pItem->ident[3] != 'H' )
     368             :     {
     369           0 :         return osl_File_E_INVAL;
     370             :     }
     371             : 
     372           0 :     snprintf(Buffer, sizeof(Buffer), "file://%s", pItem->pszMountPoint);
     373             : 
     374             : #ifdef DEBUG_OSL_FILE
     375             :     fprintf(stderr,"Mount Point is: '%s'\n",Buffer);
     376             : #endif
     377             : 
     378           0 :     oslMakeUStrFromPsz(Buffer, pstrPath);
     379             : 
     380           0 :     return osl_File_E_None;
     381             : }
     382             : 
     383             : /*****************************************
     384             :  * osl_acquireVolumeDeviceHandle
     385             :  ****************************************/
     386             : 
     387           0 : oslFileError SAL_CALL osl_acquireVolumeDeviceHandle( oslVolumeDeviceHandle Handle )
     388             : {
     389           0 :     oslVolumeDeviceHandleImpl* pItem =static_cast<oslVolumeDeviceHandleImpl*>(Handle);
     390             : 
     391           0 :     if ( pItem == 0 )
     392             :     {
     393           0 :         return osl_File_E_INVAL;
     394             :     }
     395             : 
     396           0 :     if ( pItem->ident[0] != 'O' || pItem->ident[1] != 'V' || pItem->ident[2] != 'D' || pItem->ident[3] != 'H' )
     397             :     {
     398           0 :         return osl_File_E_INVAL;
     399             :     }
     400             : 
     401           0 :     ++pItem->RefCount;
     402             : 
     403           0 :     return osl_File_E_None;
     404             : }
     405             : 
     406             : /*****************************************
     407             :  * osl_releaseVolumeDeviceHandle
     408             :  ****************************************/
     409             : 
     410           0 : oslFileError osl_releaseVolumeDeviceHandle( oslVolumeDeviceHandle Handle )
     411             : {
     412           0 :     oslVolumeDeviceHandleImpl* pItem =static_cast<oslVolumeDeviceHandleImpl*>(Handle);
     413             : 
     414           0 :     if ( pItem == 0 )
     415             :     {
     416           0 :         return osl_File_E_INVAL;
     417             :     }
     418             : 
     419           0 :     if ( pItem->ident[0] != 'O' || pItem->ident[1] != 'V' || pItem->ident[2] != 'D' || pItem->ident[3] != 'H' )
     420             :     {
     421           0 :         return osl_File_E_INVAL;
     422             :     }
     423             : 
     424           0 :     --pItem->RefCount;
     425             : 
     426           0 :     if ( pItem->RefCount == 0 )
     427             :     {
     428           0 :         rtl_freeMemory(pItem);
     429             :     }
     430             : 
     431           0 :     return osl_File_E_None;
     432             : }
     433             : 
     434             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11