简体   繁体   中英

Checking if a directory exists in Unix (system call)

I am not able to find a solution to my problem online.

I would like to call a function in Unix, pass in the path of a directory, and know if it exists. opendir() returns an error if a directory does not exist, but my goal is not to actually open, check the error, close it if no error, but rather just check if a file is a directory or not.

Is there any convenient way to do that please?

There are two relevant functions on POSIX systems: stat() and lstat() . These are used to find out whether a pathname refers to an actual object that you have permission to access, and if so, the data returned tells you what type of object it is. The difference between stat() and lstat() is that if the name you give is a symbolic link, stat() follows the symbolic link (or links if they are chained together) and reports on the object at the end of the chain of links, whereas lstat() reports on the symbolic link itself.

#include <sys/stat.h>

struct stat sb;

if (stat(pathname, &sb) == 0 && S_ISDIR(sb.st_mode))
{
    ...it is a directory...
}

If the function indicates it was successful, you use the S_ISDIR() macro from <sys/stat.h> to determine whether the file is actually a directory.

You can also check for other file types using other S_IS* macros:

  • S_ISDIR — directory
  • S_ISREG — regular file
  • S_ISCHR — character device
  • S_ISBLK — block device
  • S_ISFIFO — FIFO
  • S_ISLNK — symbolic link
  • S_ISSOCK — socket

(Some systems provide a few other file types too; S_ISDOOR is available on Solaris, for example.)

You can make use of the stat system call by passing it the name of the directory as the first argument. If the directory exists a 0 is returned else -1 is returned and errno will be set to ENOENT

EDIT:

If the return value is 0 you would need an additional check to ensure that the argument is actually a directory and not a file/symlink/char special file/blk special file/FIFO file. You can make use of the st_mode field of the stat structure for this.

If you're talking about from a shell, there's a good SO answer here. To summarize that answer:

if [ -d "$DIRECTORY" ]; then
    # Control will enter here if $DIRECTORY exists
fi

If you're talking from inside a C program, you can use stat() :

{
    struct stat st;
    if(stat("/tmp",&st) == 0)
        printf(" /tmp is present\n");
}

你可以使用test -d

Tested and working fine:

int file_exist (char *filename)
{
    char s[200];
    sprintf(s, "test -e %s", filename);
    if (system(s) == 0){
        return 1;
    }else{
        return 0;
    }
}

If you don't really care about type of this filesystem object, access(name, F_OK) checks for exsistence of something with this name. If you need to be sure this is directory, use stat() and check type with S_ISDIR() macro.

Another simple way would be:

int check(unsigned const char type) {
    if(type == DT_REG)
        return 1;
    if(type == DT_DIR)
        return 0;
    return -1;
}

You can then pass struct dirent* object's d_type to check function.

If check returns 1, then that path points to regular file.

If check returns 0, then that path points to a directory.

Otherwise, it is neither a file or a directory (it can be a Block Device/Symbolic Link, etc..)

I think the function stat may do what you want (I havent test it for directories). In C++, you can also use the boost::filesystem library.

C++17

Use std::filesystem::is_directory :

#include <filesystem>

void myFunc(const std::filesystem::path& directoryPath_c)
{
    if (std::filesystem::is_directory(directoryPath_c)) {
    //if (std::filesystem::exists(directoryPath_c)) { // Alternative*
        // do something.
    }
}

Or in its noexept version:

#include <filesystem>

void myFunc(const std::filesystem::path& directoryPath_c)
{
    std::error_code ec{};
    if (std::filesystem::is_directory(directoryPath_c, ec)) {
    //if (std::filesystem::exists(directoryPath_c, ec)) { // An alternative*
        // do something.
    }
}
  • The disadvantage of the alternative is, that if directoryPath_c exists as a regular-file, std::filesystem::exists(directoryPath_c) returns true despite the folder does not exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM