LCOV - differential code coverage report
Current view: top level - src/common - rmtree.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 78.6 % 42 33 9 1 32 1
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 1 1 1
Baseline: 16@8cea358b128 Branches: 57.1 % 35 20 15 20
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed (240..) days: 78.6 % 42 33 9 1 32
Function coverage date bins:
(240..) days: 100.0 % 1 1 1
Branch coverage date bins:
(240..) days: 57.1 % 35 20 15 20

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * rmtree.c
                                  4                 :                :  *
                                  5                 :                :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
                                  6                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  7                 :                :  *
                                  8                 :                :  * IDENTIFICATION
                                  9                 :                :  *    src/common/rmtree.c
                                 10                 :                :  *
                                 11                 :                :  *-------------------------------------------------------------------------
                                 12                 :                :  */
                                 13                 :                : 
                                 14                 :                : #ifndef FRONTEND
                                 15                 :                : #include "postgres.h"
                                 16                 :                : #else
                                 17                 :                : #include "postgres_fe.h"
                                 18                 :                : #endif
                                 19                 :                : 
                                 20                 :                : #include <unistd.h>
                                 21                 :                : #include <sys/stat.h>
                                 22                 :                : 
                                 23                 :                : #include "common/file_utils.h"
                                 24                 :                : 
                                 25                 :                : #ifndef FRONTEND
                                 26                 :                : #include "storage/fd.h"
                                 27                 :                : #define pg_log_warning(...) elog(WARNING, __VA_ARGS__)
                                 28                 :                : #define LOG_LEVEL WARNING
                                 29                 :                : #define OPENDIR(x) AllocateDir(x)
                                 30                 :                : #define CLOSEDIR(x) FreeDir(x)
                                 31                 :                : #else
                                 32                 :                : #include "common/logging.h"
                                 33                 :                : #define LOG_LEVEL PG_LOG_WARNING
                                 34                 :                : #define OPENDIR(x) opendir(x)
                                 35                 :                : #define CLOSEDIR(x) closedir(x)
                                 36                 :                : #endif
                                 37                 :                : 
                                 38                 :                : /*
                                 39                 :                :  *  rmtree
                                 40                 :                :  *
                                 41                 :                :  *  Delete a directory tree recursively.
                                 42                 :                :  *  Assumes path points to a valid directory.
                                 43                 :                :  *  Deletes everything under path.
                                 44                 :                :  *  If rmtopdir is true deletes the directory too.
                                 45                 :                :  *  Returns true if successful, false if there was any problem.
                                 46                 :                :  *  (The details of the problem are reported already, so caller
                                 47                 :                :  *  doesn't really have to say anything more, but most do.)
                                 48                 :                :  */
                                 49                 :                : bool
 3830 peter_e@gmx.net            50                 :CBC        3150 : rmtree(const char *path, bool rmtopdir)
                                 51                 :                : {
                                 52                 :                :     char        pathbuf[MAXPGPATH];
                                 53                 :                :     DIR        *dir;
                                 54                 :                :     struct dirent *de;
  439 tmunro@postgresql.or       55                 :           3150 :     bool        result = true;
                                 56                 :           3150 :     size_t      dirnames_size = 0;
                                 57                 :           3150 :     size_t      dirnames_capacity = 8;
                                 58                 :                :     char      **dirnames;
                                 59                 :                : 
                                 60                 :           3150 :     dir = OPENDIR(path);
                                 61         [ +  + ]:           3150 :     if (dir == NULL)
                                 62                 :                :     {
                                 63         [ +  - ]:              3 :         pg_log_warning("could not open directory \"%s\": %m", path);
 3830 peter_e@gmx.net            64                 :              3 :         return false;
                                 65                 :                :     }
                                 66                 :                : 
  258 michael@paquier.xyz        67                 :GNC        3147 :     dirnames = (char **) palloc(sizeof(char *) * dirnames_capacity);
                                 68                 :                : 
  439 tmunro@postgresql.or       69         [ +  + ]:CBC      143304 :     while (errno = 0, (de = readdir(dir)))
                                 70                 :                :     {
                                 71         [ +  + ]:         140159 :         if (strcmp(de->d_name, ".") == 0 ||
                                 72         [ +  + ]:         137012 :             strcmp(de->d_name, "..") == 0)
 3830 peter_e@gmx.net            73                 :           6294 :             continue;
  439 tmunro@postgresql.or       74                 :         133865 :         snprintf(pathbuf, sizeof(pathbuf), "%s/%s", path, de->d_name);
                                 75      [ -  +  + ]:         133865 :         switch (get_dirent_type(pathbuf, de, false, LOG_LEVEL))
                                 76                 :                :         {
  439 tmunro@postgresql.or       77                 :UBC           0 :             case PGFILETYPE_ERROR:
                                 78                 :                :                 /* already logged, press on */
                                 79                 :              0 :                 break;
  439 tmunro@postgresql.or       80                 :CBC        2482 :             case PGFILETYPE_DIR:
                                 81                 :                : 
                                 82                 :                :                 /*
                                 83                 :                :                  * Defer recursion until after we've closed this directory, to
                                 84                 :                :                  * avoid using more than one file descriptor at a time.
                                 85                 :                :                  */
                                 86         [ +  + ]:           2482 :                 if (dirnames_size == dirnames_capacity)
                                 87                 :                :                 {
                                 88                 :            176 :                     dirnames = repalloc(dirnames,
                                 89                 :                :                                         sizeof(char *) * dirnames_capacity * 2);
                                 90                 :            176 :                     dirnames_capacity *= 2;
                                 91                 :                :                 }
                                 92                 :           2482 :                 dirnames[dirnames_size++] = pstrdup(pathbuf);
                                 93                 :           2482 :                 break;
                                 94                 :         131383 :             default:
                                 95   [ -  +  -  - ]:         131383 :                 if (unlink(pathbuf) != 0 && errno != ENOENT)
                                 96                 :                :                 {
  331 peter@eisentraut.org       97         [ #  # ]:UBC           0 :                     pg_log_warning("could not remove file \"%s\": %m", pathbuf);
 3830 peter_e@gmx.net            98                 :              0 :                     result = false;
                                 99                 :                :                 }
  439 tmunro@postgresql.or      100                 :CBC      131381 :                 break;
                                101                 :                :         }
                                102                 :                :     }
                                103                 :                : 
                                104         [ -  + ]:           3145 :     if (errno != 0)
                                105                 :                :     {
  439 tmunro@postgresql.or      106         [ #  # ]:UBC           0 :         pg_log_warning("could not read directory \"%s\": %m", path);
                                107                 :              0 :         result = false;
                                108                 :                :     }
                                109                 :                : 
  439 tmunro@postgresql.or      110                 :CBC        3145 :     CLOSEDIR(dir);
                                111                 :                : 
                                112                 :                :     /* Now recurse into the subdirectories we found. */
                                113         [ +  + ]:           5627 :     for (size_t i = 0; i < dirnames_size; ++i)
                                114                 :                :     {
                                115         [ -  + ]:           2482 :         if (!rmtree(dirnames[i], true))
  439 tmunro@postgresql.or      116                 :UBC           0 :             result = false;
  439 tmunro@postgresql.or      117                 :CBC        2482 :         pfree(dirnames[i]);
                                118                 :                :     }
                                119                 :                : 
 3830 peter_e@gmx.net           120         [ +  - ]:           3145 :     if (rmtopdir)
                                121                 :                :     {
                                122         [ -  + ]:           3145 :         if (rmdir(path) != 0)
                                123                 :                :         {
  439 tmunro@postgresql.or      124         [ #  # ]:UBC           0 :             pg_log_warning("could not remove directory \"%s\": %m", path);
 3830 peter_e@gmx.net           125                 :              0 :             result = false;
                                126                 :                :         }
                                127                 :                :     }
                                128                 :                : 
  439 tmunro@postgresql.or      129                 :CBC        3145 :     pfree(dirnames);
                                130                 :                : 
 3830 peter_e@gmx.net           131                 :           3145 :     return result;
                                132                 :                : }
        

Generated by: LCOV version 2.1-beta2-3-g6141622