Header And Logo

PostgreSQL
| The world's most advanced open source database.

Functions

pgcheckdir.c File Reference

#include "c.h"
#include <dirent.h>
Include dependency graph for pgcheckdir.c:

Go to the source code of this file.

Functions

int pg_check_dir (const char *dir)

Function Documentation

int pg_check_dir ( const char *  dir  ) 

Definition at line 29 of file pgcheckdir.c.

References closedir(), dirent::d_name, NULL, opendir(), and readdir().

Referenced by create_data_directory(), create_xlog_symlink(), and verify_dir_is_empty_or_create().

{
    int         result = 1;
    DIR        *chkdir;
    struct dirent *file;
    bool        dot_found = false;

    errno = 0;

    chkdir = opendir(dir);

    if (chkdir == NULL)
        return (errno == ENOENT) ? 0 : -1;

    while ((file = readdir(chkdir)) != NULL)
    {
        if (strcmp(".", file->d_name) == 0 ||
            strcmp("..", file->d_name) == 0)
        {
            /* skip this and parent directory */
            continue;
        }
#ifndef WIN32
        /* file starts with "." */
        else if (file->d_name[0] == '.')
        {
            dot_found = true;
        }
        else if (strcmp("lost+found", file->d_name) == 0)
        {
            result = 3;         /* not empty, mount point */
            break;
        }
#endif
        else
        {
            result = 4;         /* not empty */
            break;
        }
    }

#ifdef WIN32
    /*
     * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
     * released version
     */
    if (GetLastError() == ERROR_NO_MORE_FILES)
        errno = 0;
#endif

    closedir(chkdir);

    if (errno != 0)
        result = -1;            /* some kind of I/O error? */

    /* We report on dot-files if we _only_ find dot files */
    if (result == 1 && dot_found)
        result = 2;

    return result;
}