LCOV - code coverage report
Current view: top level - src/common - pgfnames.c (source / functions) Hit Total Coverage
Test: PostgreSQL Lines: 21 31 67.7 %
Date: 2017-09-29 13:40:31 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * pgfnames.c
       4             :  *    directory handling functions
       5             :  *
       6             :  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/common/pgfnames.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : 
      15             : #ifndef FRONTEND
      16             : #include "postgres.h"
      17             : #else
      18             : #include "postgres_fe.h"
      19             : #endif
      20             : 
      21             : #include <dirent.h>
      22             : 
      23             : /*
      24             :  * pgfnames
      25             :  *
      26             :  * return a list of the names of objects in the argument directory.  Caller
      27             :  * must call pgfnames_cleanup later to free the memory allocated by this
      28             :  * function.
      29             :  */
      30             : char      **
      31          52 : pgfnames(const char *path)
      32             : {
      33             :     DIR        *dir;
      34             :     struct dirent *file;
      35             :     char      **filenames;
      36          52 :     int         numnames = 0;
      37          52 :     int         fnsize = 200;   /* enough for many small dbs */
      38             : 
      39          52 :     dir = opendir(path);
      40          52 :     if (dir == NULL)
      41             :     {
      42             : #ifndef FRONTEND
      43           0 :         elog(WARNING, "could not open directory \"%s\": %m", path);
      44             : #else
      45           0 :         fprintf(stderr, _("could not open directory \"%s\": %s\n"),
      46           0 :                 path, strerror(errno));
      47             : #endif
      48           0 :         return NULL;
      49             :     }
      50             : 
      51          52 :     filenames = (char **) palloc(fnsize * sizeof(char *));
      52             : 
      53        3083 :     while (errno = 0, (file = readdir(dir)) != NULL)
      54             :     {
      55        2979 :         if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0)
      56             :         {
      57        2875 :             if (numnames + 1 >= fnsize)
      58             :             {
      59           6 :                 fnsize *= 2;
      60           6 :                 filenames = (char **) repalloc(filenames,
      61             :                                                fnsize * sizeof(char *));
      62             :             }
      63        2875 :             filenames[numnames++] = pstrdup(file->d_name);
      64             :         }
      65             :     }
      66             : 
      67          52 :     if (errno)
      68             :     {
      69             : #ifndef FRONTEND
      70           0 :         elog(WARNING, "could not read directory \"%s\": %m", path);
      71             : #else
      72           0 :         fprintf(stderr, _("could not read directory \"%s\": %s\n"),
      73           0 :                 path, strerror(errno));
      74             : #endif
      75             :     }
      76             : 
      77          52 :     filenames[numnames] = NULL;
      78             : 
      79          52 :     if (closedir(dir))
      80             :     {
      81             : #ifndef FRONTEND
      82           0 :         elog(WARNING, "could not close directory \"%s\": %m", path);
      83             : #else
      84           0 :         fprintf(stderr, _("could not close directory \"%s\": %s\n"),
      85           0 :                 path, strerror(errno));
      86             : #endif
      87             :     }
      88             : 
      89          52 :     return filenames;
      90             : }
      91             : 
      92             : 
      93             : /*
      94             :  *  pgfnames_cleanup
      95             :  *
      96             :  *  deallocate memory used for filenames
      97             :  */
      98             : void
      99          52 : pgfnames_cleanup(char **filenames)
     100             : {
     101             :     char      **fn;
     102             : 
     103        2927 :     for (fn = filenames; *fn; fn++)
     104        2875 :         pfree(*fn);
     105             : 
     106          52 :     pfree(filenames);
     107          52 : }

Generated by: LCOV version 1.11