summaryrefslogtreecommitdiffstats
path: root/daemon/is.c
diff options
context:
space:
mode:
authorRichard Jones <rjones@redhat.com>2010-09-09 22:43:32 +0100
committerRichard Jones <rjones@redhat.com>2010-09-09 22:45:22 +0100
commit43eed091129212dd29996838cf1d76af0f8fc135 (patch)
tree6364ed606233bdf92dee5c8977e5ba262faaefbc /daemon/is.c
parent3a99114360636806078bbf614c241e89661bcc7f (diff)
downloadlibguestfs-43eed091129212dd29996838cf1d76af0f8fc135.tar.gz
libguestfs-43eed091129212dd29996838cf1d76af0f8fc135.tar.xz
libguestfs-43eed091129212dd29996838cf1d76af0f8fc135.zip
New APIs: is-chardev, is-blockdev, is-fifo, is-symlink, is-socket
These complement the existing is-file and is-dir APIs.
Diffstat (limited to 'daemon/is.c')
-rw-r--r--daemon/is.c78
1 files changed, 61 insertions, 17 deletions
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;
}