LCOV - code coverage report
Current view: top level - common - filetests.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 2463e5ebed6d Lines: 26 28 92.9 %
Date: 2019-03-18 07:48:51 Functions: 9 9 100.0 %
Branches: 13 16 81.2 %

           Branch data     Line data    Source code
       1                 :            : /** @file filetests.h
       2                 :            :  * @brief Utility functions for testing files.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2012,2018 Olly Betts
       5                 :            :  *
       6                 :            :  * Permission is hereby granted, free of charge, to any person obtaining a copy
       7                 :            :  * of this software and associated documentation files (the "Software"), to
       8                 :            :  * deal in the Software without restriction, including without limitation the
       9                 :            :  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
      10                 :            :  * sell copies of the Software, and to permit persons to whom the Software is
      11                 :            :  * furnished to do so, subject to the following conditions:
      12                 :            :  *
      13                 :            :  * The above copyright notice and this permission notice shall be included in
      14                 :            :  * all copies or substantial portions of the Software.
      15                 :            :  *
      16                 :            :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      17                 :            :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      18                 :            :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      19                 :            :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      20                 :            :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      21                 :            :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
      22                 :            :  * IN THE SOFTWARE.
      23                 :            :  */
      24                 :            : 
      25                 :            : #ifndef XAPIAN_INCLUDED_FILETESTS_H
      26                 :            : #define XAPIAN_INCLUDED_FILETESTS_H
      27                 :            : 
      28                 :            : #include "safesysstat.h"
      29                 :            : #include <cerrno>
      30                 :            : #include <string>
      31                 :            : 
      32                 :            : /** Test if a file exists.
      33                 :            :  *
      34                 :            :  *  @param path The path to test
      35                 :            :  *
      36                 :            :  *  @return true if @a path is a regular file, or a symbolic link which
      37                 :            :  *          resolves to a regular file.
      38                 :            :  */
      39                 :       5440 : inline bool file_exists(const char * path) {
      40                 :            :     struct stat st;
      41 [ +  + ][ +  + ]:       5440 :     return stat(path, &st) == 0 && S_ISREG(st.st_mode);
      42                 :            : }
      43                 :            : 
      44                 :            : /** Test if a file exists.
      45                 :            :  *
      46                 :            :  *  @param path The path to test
      47                 :            :  *
      48                 :            :  *  @return true if @a path is a regular file, or a symbolic link which
      49                 :            :  *          resolves to a regular file.
      50                 :            :  */
      51                 :       5440 : inline bool file_exists(const std::string & path) {
      52                 :       5440 :     return file_exists(path.c_str());
      53                 :            : }
      54                 :            : 
      55                 :            : /** Returns the size of a file.
      56                 :            :  *
      57                 :            :  *  @param path The path to test
      58                 :            :  *
      59                 :            :  *  errno is set to 0 (upon success), or the error returned by stat(), or
      60                 :            :  *  EINVAL (if the path isn't a regular file or a symlink resolving to a
      61                 :            :  *  regular file).
      62                 :            :  *
      63                 :            :  *  If the file's size is larger than the maximum value off_t can represent,
      64                 :            :  *  then stat() will fail with errno=EOVERFLOW, and so will this function.
      65                 :            :  *  There doesn't seem to be a way to determine the file size in this case,
      66                 :            :  *  short of reading it all.  This is only likely if the LFS check in configure
      67                 :            :  *  doesn't work out how to enable largefile support.
      68                 :            :  *
      69                 :            :  *  @return The size of the file, or 0 if it doesn't exist or isn't a file.
      70                 :            :  */
      71                 :       3720 : inline off_t file_size(const char * path) {
      72                 :            :     struct stat st;
      73         [ +  + ]:       3720 :     if (stat(path, &st) == 0) {
      74         [ +  - ]:       2876 :         if (S_ISREG(st.st_mode)) {
      75                 :       2876 :             errno = 0;
      76                 :       2876 :             return st.st_size;
      77                 :            :         }
      78                 :          0 :         errno = EINVAL;
      79                 :            :     }
      80                 :       3720 :     return 0;
      81                 :            : }
      82                 :            : 
      83                 :            : /** Returns the size of a file.
      84                 :            :  *
      85                 :            :  *  @param path The path to test
      86                 :            :  *
      87                 :            :  *  Note: If the file's size is larger than the maximum value off_t can
      88                 :            :  *  represent, then stat() will fail with EOVERFLOW, and so will this
      89                 :            :  *  function.  There doesn't seem to be a way to determine the file size
      90                 :            :  *  in this case, short of reading it all.  This is only likely if the LFS
      91                 :            :  *  check in configure doesn't work out how to enable largefile support.
      92                 :            :  *
      93                 :            :  *  @return The size of the file, or 0 if it doesn't exist or isn't a file;
      94                 :            :  *          errno is set to 0 (upon success), or the error returned by
      95                 :            :  *          stat(), or EINVAL (if the path isn't a regular file or a symlink
      96                 :            :  *          resolving to a regular file).
      97                 :            :  */
      98                 :       3720 : inline off_t file_size(const std::string & path) {
      99                 :       3720 :     return file_size(path.c_str());
     100                 :            : }
     101                 :            : 
     102                 :            : /** Returns the size of a file.
     103                 :            :  *
     104                 :            :  *  @param fd   The file descriptor for the file.
     105                 :            :  *
     106                 :            :  *  Note: If the file's size is larger than the maximum value off_t can
     107                 :            :  *  represent, then stat() will fail with EOVERFLOW, and so will this
     108                 :            :  *  function.  There doesn't seem to be a way to determine the file size
     109                 :            :  *  in this case, short of reading it all.  This is only likely if the LFS
     110                 :            :  *  check in configure doesn't work out how to enable largefile support.
     111                 :            :  *
     112                 :            :  *  @return The size of the file, or 0 if it doesn't exist or isn't a file;
     113                 :            :  *          errno is set to 0 (upon success), or the error returned by
     114                 :            :  *          stat(), or EINVAL (if the path isn't a regular file or a symlink
     115                 :            :  *          resolving to a regular file).
     116                 :            :  */
     117                 :        170 : inline off_t file_size(int fd) {
     118                 :            :     struct stat st;
     119         [ +  - ]:        170 :     if (fstat(fd, &st) == 0) {
     120         [ +  - ]:        170 :         if (S_ISREG(st.st_mode)) {
     121                 :        170 :             errno = 0;
     122                 :        170 :             return st.st_size;
     123                 :            :         }
     124                 :          0 :         errno = EINVAL;
     125                 :            :     }
     126                 :        170 :     return 0;
     127                 :            : }
     128                 :            : 
     129                 :            : /** Test if a directory exists.
     130                 :            :  *
     131                 :            :  *  @param path The path to test
     132                 :            :  *
     133                 :            :  *  @return true if @a path is a directory, or a symbolic link which resolves
     134                 :            :  *          to a directory.
     135                 :            :  */
     136                 :       2167 : inline bool dir_exists(const char * path) {
     137                 :            :     struct stat st;
     138 [ +  + ][ +  + ]:       2167 :     return stat(path, &st) == 0 && S_ISDIR(st.st_mode);
     139                 :            : }
     140                 :            : 
     141                 :            : /** Test if a directory exists.
     142                 :            :  *
     143                 :            :  *  @param path The path to test
     144                 :            :  *
     145                 :            :  *  @return true if @a path is a directory, or a symbolic link which resolves
     146                 :            :  *          to a directory.
     147                 :            :  */
     148                 :       2167 : inline bool dir_exists(const std::string & path) {
     149                 :       2167 :     return dir_exists(path.c_str());
     150                 :            : }
     151                 :            : 
     152                 :            : /** Test if a path exists.
     153                 :            :  *
     154                 :            :  *  @param path The path to test
     155                 :            :  *
     156                 :            :  *  @return true if @a path exists (and is not a dangling symlink).
     157                 :            :  */
     158                 :         72 : inline bool path_exists(const char * path) {
     159                 :            :     struct stat st;
     160                 :         72 :     return stat(path, &st) == 0;
     161                 :            : }
     162                 :            : 
     163                 :            : /** Test if a path exists.
     164                 :            :  *
     165                 :            :  *  @param path The path to test
     166                 :            :  *
     167                 :            :  *  @return true if @a path exists (and is not a dangling symlink).
     168                 :            :  */
     169                 :         72 : inline bool path_exists(const std::string & path) {
     170                 :         72 :     return path_exists(path.c_str());
     171                 :            : }
     172                 :            : 
     173                 :            : #endif // XAPIAN_INCLUDED_FILETESTS_H

Generated by: LCOV version 1.11