LCOV - code coverage report
Current view: top level - sal/osl/unx - file_volume.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 53 88 60.2 %
Date: 2014-11-03 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.h"
      27             : #include "file_url.h"
      28             : #include "system.h"
      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          20 : 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          20 :     eRet = FileURLToPath( path, PATH_MAX, ustrDirectoryURL );
     118          20 :     if( eRet != osl_File_E_None )
     119           2 :         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          18 :     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          18 : static oslFileError osl_psz_getVolumeInformation (
     212             :     const sal_Char* pszDirectory, oslVolumeInfo* pInfo, sal_uInt32 uFieldMask)
     213             : {
     214             :     __OSL_STATFS_STRUCT sfs;
     215             : 
     216          18 :     if (!pInfo)
     217           0 :         return osl_File_E_INVAL;
     218             : 
     219          18 :     __OSL_STATFS_INIT(sfs);
     220             : 
     221          18 :     pInfo->uValidFields = 0;
     222          18 :     pInfo->uAttributes  = 0;
     223             : 
     224          18 :     if ((__OSL_STATFS(pszDirectory, &sfs)) < 0)
     225             :     {
     226           2 :         oslFileError result = oslTranslateFileError(OSL_FET_ERROR, errno);
     227           2 :         return (result);
     228             :     }
     229             : 
     230             :     /* FIXME: how to detect the kind of storage (fixed, cdrom, ...) */
     231          16 :     if (uFieldMask & osl_VolumeInfo_Mask_Attributes)
     232             :     {
     233           2 :         if (__OSL_STATFS_ISREMOTE(sfs))
     234           0 :             pInfo->uAttributes  |= osl_Volume_Attribute_Remote;
     235             : 
     236           2 :         pInfo->uValidFields |= osl_VolumeInfo_Mask_Attributes;
     237             :     }
     238             : 
     239          16 :     if (uFieldMask & osl_VolumeInfo_Mask_FileSystemCaseHandling)
     240             :     {
     241           2 :         if (__OSL_STATFS_IS_CASE_SENSITIVE_FS(sfs))
     242           2 :             pInfo->uAttributes |= osl_Volume_Attribute_Case_Sensitive;
     243             : 
     244           2 :         if (__OSL_STATFS_IS_CASE_PRESERVING_FS(sfs))
     245           2 :             pInfo->uAttributes |= osl_Volume_Attribute_Case_Is_Preserved;
     246             : 
     247           2 :         pInfo->uValidFields |= osl_VolumeInfo_Mask_Attributes;
     248             :     }
     249             : 
     250          16 :     pInfo->uTotalSpace = 0;
     251          16 :     pInfo->uFreeSpace  = 0;
     252          16 :     pInfo->uUsedSpace  = 0;
     253             : 
     254             : #if defined(__OSL_STATFS_BLKSIZ)
     255             : 
     256          30 :     if ((uFieldMask & osl_VolumeInfo_Mask_TotalSpace) ||
     257          14 :         (uFieldMask & osl_VolumeInfo_Mask_UsedSpace))
     258             :     {
     259           4 :         pInfo->uTotalSpace   = __OSL_STATFS_BLKSIZ(sfs);
     260           4 :         pInfo->uTotalSpace  *= (sal_uInt64)(sfs.f_blocks);
     261           4 :         pInfo->uValidFields |= osl_VolumeInfo_Mask_TotalSpace;
     262             :     }
     263             : 
     264          30 :     if ((uFieldMask & osl_VolumeInfo_Mask_FreeSpace) ||
     265          14 :         (uFieldMask & osl_VolumeInfo_Mask_UsedSpace))
     266             :     {
     267           4 :         pInfo->uFreeSpace = __OSL_STATFS_BLKSIZ(sfs);
     268             : 
     269           4 :         if (getuid() == 0)
     270           4 :             pInfo->uFreeSpace *= (sal_uInt64)(sfs.f_bfree);
     271             :         else
     272           0 :             pInfo->uFreeSpace *= (sal_uInt64)(sfs.f_bavail);
     273             : 
     274           4 :         pInfo->uValidFields |= osl_VolumeInfo_Mask_FreeSpace;
     275             :     }
     276             : 
     277             : #endif  /* __OSL_STATFS_BLKSIZ */
     278             : 
     279          20 :     if ((pInfo->uValidFields & osl_VolumeInfo_Mask_TotalSpace) &&
     280           4 :         (pInfo->uValidFields & osl_VolumeInfo_Mask_FreeSpace ))
     281             :     {
     282           2 :         pInfo->uUsedSpace    = pInfo->uTotalSpace - pInfo->uFreeSpace;
     283           2 :         pInfo->uValidFields |= osl_VolumeInfo_Mask_UsedSpace;
     284             :     }
     285             : 
     286          16 :     pInfo->uMaxNameLength = 0;
     287          16 :     if (uFieldMask & osl_VolumeInfo_Mask_MaxNameLength)
     288             :     {
     289           2 :         long nLen = pathconf(pszDirectory, _PC_NAME_MAX);
     290           2 :         if (nLen > 0)
     291             :         {
     292           2 :             pInfo->uMaxNameLength = (sal_uInt32)nLen;
     293           2 :             pInfo->uValidFields |= osl_VolumeInfo_Mask_MaxNameLength;
     294             :         }
     295             :     }
     296             : 
     297          16 :     pInfo->uMaxPathLength = 0;
     298          16 :     if (uFieldMask & osl_VolumeInfo_Mask_MaxPathLength)
     299             :     {
     300           2 :         long nLen = pathconf (pszDirectory, _PC_PATH_MAX);
     301           2 :         if (nLen > 0)
     302             :         {
     303           2 :             pInfo->uMaxPathLength  = (sal_uInt32)nLen;
     304           2 :             pInfo->uValidFields   |= osl_VolumeInfo_Mask_MaxPathLength;
     305             :         }
     306             :     }
     307             : 
     308             : #if defined(__OSL_STATFS_TYPENAME)
     309             : 
     310             :     if (uFieldMask & osl_VolumeInfo_Mask_FileSystemName)
     311             :     {
     312             :         rtl_string2UString(
     313             :             &(pInfo->ustrFileSystemName),
     314             :             __OSL_STATFS_TYPENAME(sfs),
     315             :             rtl_str_getLength(__OSL_STATFS_TYPENAME(sfs)),
     316             :             osl_getThreadTextEncoding(),
     317             :             OUSTRING_TO_OSTRING_CVTFLAGS);
     318             :         OSL_ASSERT(pInfo->ustrFileSystemName != 0);
     319             : 
     320             :         pInfo->uValidFields |= osl_VolumeInfo_Mask_FileSystemName;
     321             :     }
     322             : 
     323             : #endif /* __OSL_STATFS_TYPENAME */
     324             : 
     325          16 :     return osl_File_E_None;
     326             : }
     327             : 
     328             : /******************************************************************************
     329             :  *
     330             :  *                  GENERIC FLOPPY FUNCTIONS
     331             :  *
     332             :  *****************************************************************************/
     333             : 
     334             : /*****************************************
     335             :  * osl_getVolumeDeviceMountPath
     336             :  ****************************************/
     337           0 : static rtl_uString* oslMakeUStrFromPsz(const sal_Char* pszStr, rtl_uString** ustrValid)
     338             : {
     339             :     rtl_string2UString(
     340             :         ustrValid,
     341             :         pszStr,
     342             :         rtl_str_getLength( pszStr ),
     343           0 :         osl_getThreadTextEncoding(),
     344           0 :         OUSTRING_TO_OSTRING_CVTFLAGS );
     345             :     OSL_ASSERT(*ustrValid != 0);
     346             : 
     347           0 :     return *ustrValid;
     348             : }
     349             : 
     350           0 : oslFileError osl_getVolumeDeviceMountPath( oslVolumeDeviceHandle Handle, rtl_uString **pstrPath )
     351             : {
     352           0 :     oslVolumeDeviceHandleImpl* pItem = (oslVolumeDeviceHandleImpl*) Handle;
     353             :     sal_Char Buffer[PATH_MAX];
     354             : 
     355           0 :     Buffer[0] = '\0';
     356             : 
     357           0 :     if ( pItem == 0 || pstrPath == 0 )
     358             :     {
     359           0 :         return osl_File_E_INVAL;
     360             :     }
     361             : 
     362           0 :     if ( pItem->ident[0] != 'O' || pItem->ident[1] != 'V' || pItem->ident[2] != 'D' || pItem->ident[3] != 'H' )
     363             :     {
     364           0 :         return osl_File_E_INVAL;
     365             :     }
     366             : 
     367           0 :     snprintf(Buffer, sizeof(Buffer), "file://%s", pItem->pszMountPoint);
     368             : 
     369             : #ifdef DEBUG_OSL_FILE
     370             :     fprintf(stderr,"Mount Point is: '%s'\n",Buffer);
     371             : #endif
     372             : 
     373           0 :     oslMakeUStrFromPsz(Buffer, pstrPath);
     374             : 
     375           0 :     return osl_File_E_None;
     376             : }
     377             : 
     378             : /*****************************************
     379             :  * osl_acquireVolumeDeviceHandle
     380             :  ****************************************/
     381             : 
     382           0 : oslFileError SAL_CALL osl_acquireVolumeDeviceHandle( oslVolumeDeviceHandle Handle )
     383             : {
     384           0 :     oslVolumeDeviceHandleImpl* pItem =(oslVolumeDeviceHandleImpl*) Handle;
     385             : 
     386           0 :     if ( pItem == 0 )
     387             :     {
     388           0 :         return osl_File_E_INVAL;
     389             :     }
     390             : 
     391           0 :     if ( pItem->ident[0] != 'O' || pItem->ident[1] != 'V' || pItem->ident[2] != 'D' || pItem->ident[3] != 'H' )
     392             :     {
     393           0 :         return osl_File_E_INVAL;
     394             :     }
     395             : 
     396           0 :     ++pItem->RefCount;
     397             : 
     398           0 :     return osl_File_E_None;
     399             : }
     400             : 
     401             : /*****************************************
     402             :  * osl_releaseVolumeDeviceHandle
     403             :  ****************************************/
     404             : 
     405           0 : oslFileError osl_releaseVolumeDeviceHandle( oslVolumeDeviceHandle Handle )
     406             : {
     407           0 :     oslVolumeDeviceHandleImpl* pItem =(oslVolumeDeviceHandleImpl*) Handle;
     408             : 
     409           0 :     if ( pItem == 0 )
     410             :     {
     411           0 :         return osl_File_E_INVAL;
     412             :     }
     413             : 
     414           0 :     if ( pItem->ident[0] != 'O' || pItem->ident[1] != 'V' || pItem->ident[2] != 'D' || pItem->ident[3] != 'H' )
     415             :     {
     416           0 :         return osl_File_E_INVAL;
     417             :     }
     418             : 
     419           0 :     --pItem->RefCount;
     420             : 
     421           0 :     if ( pItem->RefCount == 0 )
     422             :     {
     423           0 :         rtl_freeMemory(pItem);
     424             :     }
     425             : 
     426           0 :     return osl_File_E_None;
     427             : }
     428             : 
     429             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10