LCOV - code coverage report
Current view: top level - tests/harness - unixcmds.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 7028d852e609 Lines: 25 37 67.6 %
Date: 2019-02-17 14:59:59 Functions: 5 5 100.0 %
Branches: 22 58 37.9 %

           Branch data     Line data    Source code
       1                 :            : /** @file unixcmds.cc
       2                 :            :  *  @brief C++ function versions of useful Unix commands.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2003,2004,2007,2012,2014,2015,2018 Olly Betts
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU General Public License as
       8                 :            :  * published by the Free Software Foundation; either version 2 of the
       9                 :            :  * License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This program is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :            :  * GNU General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU General Public License
      17                 :            :  * along with this program; if not, write to the Free Software
      18                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
      19                 :            :  */
      20                 :            : 
      21                 :            : #include <config.h>
      22                 :            : 
      23                 :            : #include "unixcmds.h"
      24                 :            : 
      25                 :            : #include <string>
      26                 :            : #include <cstdlib>
      27                 :            : #include <sys/types.h>
      28                 :            : #include "safeunistd.h"
      29                 :            : #include "safefcntl.h"
      30                 :            : 
      31                 :            : #include "append_filename_arg.h"
      32                 :            : #include "errno_to_string.h"
      33                 :            : #include "filetests.h"
      34                 :            : #include "str.h"
      35                 :            : 
      36                 :            : // mingw-w64 added an implementation of nftw() but it seems to be buggy and
      37                 :            : // garble paths, though I can't see where in the code things go wrong and
      38                 :            : // their code seems to work when built on Linux.
      39                 :            : //
      40                 :            : // For now, we just blacklist nftw() here and on mingw32 (which doesn't
      41                 :            : // currently have it, but let's be defensive in case somebody copies over the
      42                 :            : // buggy version).  Using nftw() is just a minor optimisation which only
      43                 :            : // makes a real difference for developers running the testsuite a lot.
      44                 :            : #if defined HAVE_NFTW && !defined __MINGW32__
      45                 :            : # include <ftw.h>
      46                 :            : # include <unistd.h>
      47                 :            : #endif
      48                 :            : 
      49                 :            : using namespace std;
      50                 :            : 
      51                 :            : /// Call system() and throw exception if it fails.
      52                 :            : static void
      53                 :          2 : checked_system(const string & cmd)
      54                 :            : {
      55                 :          2 :     int res = system(cmd.c_str());
      56         [ -  + ]:          2 :     if (res) {
      57         [ #  # ]:          0 :         string msg = "system(\"";
      58         [ #  # ]:          0 :         msg += cmd;
      59         [ #  # ]:          0 :         msg += "\") failed, returning ";
      60 [ #  # ][ #  # ]:          0 :         msg += str(res);
      61                 :          0 :         throw msg;
      62                 :            :     }
      63                 :          2 : }
      64                 :            : 
      65                 :            : /// Recursively copy a directory.
      66                 :          1 : void cp_R(const std::string &src, const std::string &dest) {
      67                 :            : #ifdef __WIN32__
      68                 :            :     // We create the target directory first to avoid being prompted as to
      69                 :            :     // whether we want to create a directory or a file (which makes no sense
      70                 :            :     // when copying a directory, but that's how xcopy seems to work!)
      71                 :            :     mkdir(dest.c_str());
      72                 :            :     string cmd("xcopy /E /q /Y");
      73                 :            : #else
      74         [ +  - ]:          1 :     string cmd("cp -R");
      75                 :            : #endif
      76 [ +  - ][ -  + ]:          1 :     if (!append_filename_argument(cmd, src)) return;
      77 [ +  - ][ -  + ]:          1 :     if (!append_filename_argument(cmd, dest)) return;
      78                 :            : #ifdef __WIN32__
      79                 :            :     // xcopy reports how many files it copied, even with /q.
      80                 :            :     cmd += " >nul";
      81                 :            : #endif
      82         [ +  - ]:          1 :     checked_system(cmd);
      83                 :            : #ifndef __WIN32__
      84                 :            :     // Allow write access to the copy (to deal with builds where srcdir is
      85                 :            :     // readonly).
      86         [ +  - ]:          1 :     cmd = "chmod -R +w";
      87 [ +  - ][ -  + ]:          1 :     if (!append_filename_argument(cmd, dest)) return;
      88 [ +  - ][ +  - ]:          1 :     checked_system(cmd);
      89                 :            : #endif
      90                 :            : }
      91                 :            : 
      92                 :            : #if defined HAVE_NFTW && !defined __MINGW32__
      93                 :            : extern "C" {
      94                 :            : static int
      95                 :       4336 : rm_rf_nftw_helper(const char* path,
      96                 :            :                   const struct stat*,
      97                 :            :                   int type,
      98                 :            :                   struct FTW*)
      99                 :            : {
     100         [ +  + ]:       4336 :     int r = (type == FTW_DP ? rmdir(path) : unlink(path));
     101                 :            :     // Return the errno value if deletion fails as the nftw() function might
     102                 :            :     // overwrite errno during clean-up.  Any non-zero return value will end
     103                 :            :     // the walk.
     104         [ -  + ]:       4336 :     return r < 0 ? errno : 0;
     105                 :            : }
     106                 :            : }
     107                 :            : #endif
     108                 :            : 
     109                 :            : /// Remove a directory and contents, just like the Unix "rm -rf" command.
     110                 :        763 : void rm_rf(const string &filename) {
     111                 :            :     // Check filename exists and is actually a directory
     112 [ +  - ][ +  + ]:        763 :     if (filename.empty() || !dir_exists(filename))
                 [ +  + ]
     113                 :        763 :         return;
     114                 :            : 
     115                 :            : #if defined HAVE_NFTW && !defined __MINGW32__
     116                 :        598 :     auto flags = FTW_DEPTH | FTW_PHYS;
     117                 :        598 :     int eno = nftw(filename.c_str(), rm_rf_nftw_helper, 10, flags);
     118         [ -  + ]:        598 :     if (eno != 0) {
     119                 :            :         // nftw() either returns 0 for OK, -1 for error, or the non-zero return
     120                 :            :         // value of the helper (which in our case is an errno value).
     121         [ #  # ]:          0 :         if (eno < 0)
     122                 :          0 :             eno = errno;
     123         [ #  # ]:          0 :         string msg = "recursive delete of \"";
     124         [ #  # ]:          0 :         msg += filename;
     125         [ #  # ]:          0 :         msg += "\") failed, errno = ";
     126         [ #  # ]:          0 :         errno_to_string(eno, msg);
     127                 :          0 :         throw msg;
     128                 :            :     }
     129                 :            : #else
     130                 :            : # ifdef __WIN32__
     131                 :            :     string cmd("rd /s /q");
     132                 :            : # else
     133                 :            :     string cmd("rm -rf");
     134                 :            : # endif
     135                 :            :     if (!append_filename_argument(cmd, filename)) return;
     136                 :            :     checked_system(cmd);
     137                 :            : #endif
     138                 :            : }
     139                 :            : 
     140                 :            : /// Touch a file, just like the Unix "touch" command.
     141                 :          3 : void touch(const string &filename) {
     142                 :          3 :     int fd = open(filename.c_str(), O_CREAT|O_WRONLY|O_BINARY, 0644);
     143         [ +  - ]:          3 :     if (fd >= 0) close(fd);
     144                 :          3 : }

Generated by: LCOV version 1.11