LCOV - code coverage report
Current view: top level - src/common - rmtree.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 76.0 % 50 38
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 1 1
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 41.9 % 43 18

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * rmtree.c
       4                 :             :  *
       5                 :             :  * Portions Copyright (c) 1996-2026, 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
      50                 :          33 : rmtree(const char *path, bool rmtopdir)
      51                 :             : {
      52                 :          33 :         char            pathbuf[MAXPGPATH];
      53                 :          33 :         DIR                *dir;
      54                 :          33 :         struct dirent *de;
      55                 :          33 :         bool            result = true;
      56                 :          33 :         size_t          dirnames_size = 0;
      57                 :          33 :         size_t          dirnames_capacity = 8;
      58                 :          33 :         char      **dirnames;
      59                 :             : 
      60                 :          33 :         dir = OPENDIR(path);
      61         [ +  - ]:          33 :         if (dir == NULL)
      62                 :             :         {
      63   [ #  #  #  # ]:           0 :                 pg_log_warning("could not open directory \"%s\": %m", path);
      64                 :           0 :                 return false;
      65                 :             :         }
      66                 :             : 
      67                 :          33 :         dirnames = palloc_array(char *, dirnames_capacity);
      68                 :             : 
      69         [ +  + ]:        2434 :         while (errno = 0, (de = readdir(dir)))
      70                 :             :         {
      71   [ +  +  +  + ]:        2401 :                 if (strcmp(de->d_name, ".") == 0 ||
      72                 :        2368 :                         strcmp(de->d_name, "..") == 0)
      73                 :          66 :                         continue;
      74                 :        2335 :                 snprintf(pathbuf, sizeof(pathbuf), "%s/%s", path, de->d_name);
      75      [ -  +  + ]:        2335 :                 switch (get_dirent_type(pathbuf, de, false, LOG_LEVEL))
      76                 :             :                 {
      77                 :             :                         case PGFILETYPE_ERROR:
      78                 :             :                                 /* already logged, press on */
      79                 :             :                                 break;
      80                 :             :                         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         [ +  + ]:          29 :                                 if (dirnames_size == dirnames_capacity)
      87                 :             :                                 {
      88                 :           4 :                                         dirnames = repalloc(dirnames,
      89                 :           2 :                                                                                 sizeof(char *) * dirnames_capacity * 2);
      90                 :           2 :                                         dirnames_capacity *= 2;
      91                 :           2 :                                 }
      92                 :          29 :                                 dirnames[dirnames_size++] = pstrdup(pathbuf);
      93                 :          29 :                                 break;
      94                 :             :                         default:
      95   [ -  +  #  # ]:        2306 :                                 if (unlink(pathbuf) != 0 && errno != ENOENT)
      96                 :             :                                 {
      97   [ #  #  #  # ]:           0 :                                         pg_log_warning("could not remove file \"%s\": %m", pathbuf);
      98                 :           0 :                                         result = false;
      99                 :           0 :                                 }
     100                 :        2306 :                                 break;
     101                 :             :                 }
     102                 :             :         }
     103                 :             : 
     104         [ +  - ]:          33 :         if (errno != 0)
     105                 :             :         {
     106   [ #  #  #  # ]:           0 :                 pg_log_warning("could not read directory \"%s\": %m", path);
     107                 :           0 :                 result = false;
     108                 :           0 :         }
     109                 :             : 
     110                 :          33 :         CLOSEDIR(dir);
     111                 :             : 
     112                 :             :         /* Now recurse into the subdirectories we found. */
     113         [ +  + ]:          62 :         for (size_t i = 0; i < dirnames_size; ++i)
     114                 :             :         {
     115         [ +  - ]:          29 :                 if (!rmtree(dirnames[i], true))
     116                 :           0 :                         result = false;
     117                 :          29 :                 pfree(dirnames[i]);
     118                 :          29 :         }
     119                 :             : 
     120         [ -  + ]:          33 :         if (rmtopdir)
     121                 :             :         {
     122         [ +  - ]:          33 :                 if (rmdir(path) != 0)
     123                 :             :                 {
     124   [ #  #  #  # ]:           0 :                         pg_log_warning("could not remove directory \"%s\": %m", path);
     125                 :           0 :                         result = false;
     126                 :           0 :                 }
     127                 :          33 :         }
     128                 :             : 
     129                 :          33 :         pfree(dirnames);
     130                 :             : 
     131                 :          33 :         return result;
     132                 :          33 : }
        

Generated by: LCOV version 2.3.2-1