From 43eed091129212dd29996838cf1d76af0f8fc135 Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Thu, 9 Sep 2010 22:43:32 +0100 Subject: New APIs: is-chardev, is-blockdev, is-fifo, is-symlink, is-socket These complement the existing is-file and is-dir APIs. --- daemon/is.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 61 insertions(+), 17 deletions(-) (limited to 'daemon/is.c') diff --git a/daemon/is.c b/daemon/is.c index a16596be..4a834f49 100644 --- a/daemon/is.c +++ b/daemon/is.c @@ -41,30 +41,73 @@ do_exists (const char *path) return r == 0; } +static int get_mode (const char *path, mode_t *mode); + int do_is_file (const char *path) { - int r; - struct stat buf; + mode_t mode; + int r = get_mode (path, &mode); + if (r <= 0) return r; + return S_ISREG (mode); +} - CHROOT_IN; - r = lstat (path, &buf); - CHROOT_OUT; +int +do_is_dir (const char *path) +{ + mode_t mode; + int r = get_mode (path, &mode); + if (r <= 0) return r; + return S_ISDIR (mode); +} - if (r == -1) { - if (errno != ENOENT && errno != ENOTDIR) { - reply_with_perror ("stat: %s", path); - return -1; - } - else - return 0; /* Not a file. */ - } +int +do_is_chardev (const char *path) +{ + mode_t mode; + int r = get_mode (path, &mode); + if (r <= 0) return r; + return S_ISCHR (mode); +} - return S_ISREG (buf.st_mode); +int +do_is_blockdev (const char *path) +{ + mode_t mode; + int r = get_mode (path, &mode); + if (r <= 0) return r; + return S_ISBLK (mode); } int -do_is_dir (const char *path) +do_is_fifo (const char *path) +{ + mode_t mode; + int r = get_mode (path, &mode); + if (r <= 0) return r; + return S_ISFIFO (mode); +} + +int +do_is_symlink (const char *path) +{ + mode_t mode; + int r = get_mode (path, &mode); + if (r <= 0) return r; + return S_ISLNK (mode); +} + +int +do_is_socket (const char *path) +{ + mode_t mode; + int r = get_mode (path, &mode); + if (r <= 0) return r; + return S_ISSOCK (mode); +} + +static int +get_mode (const char *path, mode_t *mode) { int r; struct stat buf; @@ -79,8 +122,9 @@ do_is_dir (const char *path) return -1; } else - return 0; /* Not a directory. */ + return 0; /* Doesn't exist, means return false. */ } - return S_ISDIR (buf.st_mode); + *mode = buf.st_mode; + return 1; } -- cgit