daemon: Move 'exists', 'is-file' and 'is-dir' to separate file.
[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 int
45 do_is_file (const char *path)
46 {
47   int r;
48   struct stat buf;
49
50   CHROOT_IN;
51   r = lstat (path, &buf);
52   CHROOT_OUT;
53
54   if (r == -1) {
55     if (errno != ENOENT && errno != ENOTDIR) {
56       reply_with_perror ("stat: %s", path);
57       return -1;
58     }
59     else
60       return 0;                 /* Not a file. */
61   }
62
63   return S_ISREG (buf.st_mode);
64 }
65
66 int
67 do_is_dir (const char *path)
68 {
69   int r;
70   struct stat buf;
71
72   CHROOT_IN;
73   r = lstat (path, &buf);
74   CHROOT_OUT;
75
76   if (r == -1) {
77     if (errno != ENOENT && errno != ENOTDIR) {
78       reply_with_perror ("stat: %s", path);
79       return -1;
80     }
81     else
82       return 0;                 /* Not a directory. */
83   }
84
85   return S_ISDIR (buf.st_mode);
86 }