Version 1.5.23.
[libguestfs.git] / daemon / is.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2010 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27
28 #include "../src/guestfs_protocol.h"
29 #include "daemon.h"
30 #include "actions.h"
31
32 int
33 do_exists (const char *path)
34 {
35   int r;
36
37   CHROOT_IN;
38   r = access (path, F_OK);
39   CHROOT_OUT;
40
41   return r == 0;
42 }
43
44 static int get_mode (const char *path, mode_t *mode);
45
46 int
47 do_is_file (const char *path)
48 {
49   mode_t mode;
50   int r = get_mode (path, &mode);
51   if (r <= 0) return r;
52   return S_ISREG (mode);
53 }
54
55 int
56 do_is_dir (const char *path)
57 {
58   mode_t mode;
59   int r = get_mode (path, &mode);
60   if (r <= 0) return r;
61   return S_ISDIR (mode);
62 }
63
64 int
65 do_is_chardev (const char *path)
66 {
67   mode_t mode;
68   int r = get_mode (path, &mode);
69   if (r <= 0) return r;
70   return S_ISCHR (mode);
71 }
72
73 int
74 do_is_blockdev (const char *path)
75 {
76   mode_t mode;
77   int r = get_mode (path, &mode);
78   if (r <= 0) return r;
79   return S_ISBLK (mode);
80 }
81
82 int
83 do_is_fifo (const char *path)
84 {
85   mode_t mode;
86   int r = get_mode (path, &mode);
87   if (r <= 0) return r;
88   return S_ISFIFO (mode);
89 }
90
91 int
92 do_is_symlink (const char *path)
93 {
94   mode_t mode;
95   int r = get_mode (path, &mode);
96   if (r <= 0) return r;
97   return S_ISLNK (mode);
98 }
99
100 int
101 do_is_socket (const char *path)
102 {
103   mode_t mode;
104   int r = get_mode (path, &mode);
105   if (r <= 0) return r;
106   return S_ISSOCK (mode);
107 }
108
109 static int
110 get_mode (const char *path, mode_t *mode)
111 {
112   int r;
113   struct stat buf;
114
115   CHROOT_IN;
116   r = lstat (path, &buf);
117   CHROOT_OUT;
118
119   if (r == -1) {
120     if (errno != ENOENT && errno != ENOTDIR) {
121       reply_with_perror ("stat: %s", path);
122       return -1;
123     }
124     else
125       return 0;                 /* Doesn't exist, means return false. */
126   }
127
128   *mode = buf.st_mode;
129   return 1;
130 }