df: Add --uuid option to print guest UUIDs instead of names (RHBZ#646821).
[libguestfs.git] / daemon / is.c
index a16596b..4a834f4 100644 (file)
@@ -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;
 }