installer - Add hammer2 support to the installer
[dragonfly.git] / usr.sbin / installer / libaura / fspred.c
blobd7f517af1cb2adcecfa844738fcff967f87050fe
1 /*
2 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Chris Pressey <cpressey@catseye.mine.nu>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
37 * fspred.c
38 * Filesystem predicates.
39 * $Id: fspred.c,v 1.3 2005/02/10 03:33:49 cpressey Exp $
42 #include <sys/stat.h>
43 #include <sys/param.h>
44 #include <sys/ucred.h>
45 #include <sys/mount.h>
47 #include <stdarg.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
52 #include "fspred.h"
54 /** PREDICATES **/
56 int
57 is_dir(const char *fmt, ...)
59 va_list args;
60 char *filename;
61 int result;
62 struct stat sb;
64 va_start(args, fmt);
65 vasprintf(&filename, fmt, args);
66 va_end(args);
68 result = stat(filename, &sb);
69 free(filename);
71 if (result == 0)
72 return(sb.st_mode & S_IFDIR);
73 else
74 return(0);
77 int
78 is_file(const char *fmt, ...)
80 va_list args;
81 char *filename;
82 int result;
83 struct stat sb;
85 va_start(args, fmt);
86 vasprintf(&filename, fmt, args);
87 va_end(args);
89 result = stat(filename, &sb);
90 free(filename);
92 if (result == 0)
93 return(sb.st_mode & S_IFREG);
94 else
95 return(0);
98 int
99 is_program(const char *fmt, ...)
101 va_list args;
102 char *filename;
103 int result;
104 struct stat sb;
106 va_start(args, fmt);
107 vasprintf(&filename, fmt, args);
108 va_end(args);
110 result = stat(filename, &sb);
111 free(filename);
113 if (result == 0)
114 return((sb.st_mode & S_IFREG) && (sb.st_mode & S_IXOTH));
115 else
116 return(0);
120 is_device(const char *fmt, ...)
122 va_list args;
123 char *filename;
124 int result;
125 struct stat sb;
127 va_start(args, fmt);
128 vasprintf(&filename, fmt, args);
129 va_end(args);
131 result = stat(filename, &sb);
132 free(filename);
134 if (result == 0)
135 return((sb.st_mode & S_IFCHR) | (sb.st_mode & S_IFBLK));
136 else
137 return(0);
141 is_named_pipe(const char *fmt, ...)
143 va_list args;
144 char *filename;
145 int result;
146 struct stat sb;
148 va_start(args, fmt);
149 vasprintf(&filename, fmt, args);
150 va_end(args);
152 result = stat(filename, &sb);
154 free(filename);
156 if (result == 0)
157 return(sb.st_mode & S_IFIFO);
158 else
159 return(0);
163 is_mountpoint_mounted(const char *mtpt)
165 struct statfs *mt_array, *mt_ptr;
166 int count;
168 count = getmntinfo(&mt_array, MNT_WAIT);
169 for (mt_ptr = mt_array; count > 0; mt_ptr++, count--) {
170 if (strcmp(mt_ptr->f_mntonname, mtpt) == 0)
171 return(1);
173 return(0);
177 is_device_mounted(const char *device)
179 struct statfs *mt_array, *mt_ptr;
180 int count;
182 count = getmntinfo(&mt_array, MNT_WAIT);
183 for (mt_ptr = mt_array; count > 0; mt_ptr++, count--) {
184 if (strcmp(mt_ptr->f_mntfromname, device) == 0)
185 return(1);
187 return(0);
191 is_any_slice_mounted(const char *diskdev)
193 struct statfs *mt_array, *mt_ptr;
194 int count;
196 count = getmntinfo(&mt_array, MNT_WAIT);
197 for (mt_ptr = mt_array; count > 0; mt_ptr++, count--) {
198 if (strstr(mt_ptr->f_mntfromname, diskdev) ==
199 mt_ptr->f_mntfromname)
200 return(1);
202 return(0);