Libinfo-278.0.3.tar.gz
[apple/libinfo.git] / lookup.subproj / lu_fstab.c
1 /*
2 * Copyright (c) 1999-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * fstab entry lookup
26 * Copyright (C) 1989 by NeXT, Inc.
27 */
28
29 #include <stdlib.h>
30 #include <mach/mach.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <fstab.h>
34 #include <pthread.h>
35 #include "lu_utils.h"
36 #include "lu_overrides.h"
37
38 #define ENTRY_SIZE sizeof(struct fstab)
39 #define ENTRY_KEY _li_data_key_fstab
40
41 static pthread_mutex_t _fstab_lock = PTHREAD_MUTEX_INITIALIZER;
42
43 #define FS_GET_SPEC 1
44 #define FS_GET_FILE 2
45 #define FS_GET_ENT 3
46
47 static struct fstab *
48 copy_fstab(struct fstab *in)
49 {
50 if (in == NULL) return NULL;
51
52 return (struct fstab *)LI_ils_create("sssss44", in->fs_spec, in->fs_file, in->fs_vfstype, in->fs_mntops, in->fs_type, in->fs_freq, in->fs_passno);
53 }
54
55 /*
56 * Extract the next fstab entry from a kvarray.
57 */
58 static void *
59 extract_fstab(kvarray_t *in)
60 {
61 struct fstab tmp;
62 uint32_t d, k, kcount;
63
64 if (in == NULL) return NULL;
65
66 d = in->curr;
67 in->curr++;
68
69 if (d >= in->count) return NULL;
70
71 memset(&tmp, 0, ENTRY_SIZE);
72
73 kcount = in->dict[d].kcount;
74
75 for (k = 0; k < kcount; k++)
76 {
77 if (!strcmp(in->dict[d].key[k], "fs_spec"))
78 {
79 if (tmp.fs_spec != NULL) continue;
80 if (in->dict[d].vcount[k] == 0) continue;
81
82 tmp.fs_spec = (char *)in->dict[d].val[k][0];
83 }
84 else if (!strcmp(in->dict[d].key[k], "fs_file"))
85 {
86 if (tmp.fs_file != NULL) continue;
87 if (in->dict[d].vcount[k] == 0) continue;
88
89 tmp.fs_file = (char *)in->dict[d].val[k][0];
90 }
91 else if (!strcmp(in->dict[d].key[k], "fs_vfstype"))
92 {
93 if (tmp.fs_vfstype != NULL) continue;
94 if (in->dict[d].vcount[k] == 0) continue;
95
96 tmp.fs_vfstype = (char *)in->dict[d].val[k][0];
97 }
98 else if (!strcmp(in->dict[d].key[k], "fs_mntops"))
99 {
100 if (tmp.fs_mntops != NULL) continue;
101 if (in->dict[d].vcount[k] == 0) continue;
102
103 tmp.fs_mntops = (char *)in->dict[d].val[k][0];
104 }
105 else if (!strcmp(in->dict[d].key[k], "fs_type"))
106 {
107 if (tmp.fs_type != NULL) continue;
108 if (in->dict[d].vcount[k] == 0) continue;
109
110 tmp.fs_type = (char *)in->dict[d].val[k][0];
111 }
112 else if (!strcmp(in->dict[d].key[k], "fs_freq"))
113 {
114 if (in->dict[d].vcount[k] == 0) continue;
115 tmp.fs_freq = atoi(in->dict[d].val[k][0]);
116 }
117 else if (!strcmp(in->dict[d].key[k], "fs_passno"))
118 {
119 if (in->dict[d].vcount[k] == 0) continue;
120 tmp.fs_passno = atoi(in->dict[d].val[k][0]);
121 }
122 }
123
124 if (tmp.fs_spec == NULL) tmp.fs_spec = "";
125 if (tmp.fs_file == NULL) tmp.fs_file = "";
126 if (tmp.fs_vfstype == NULL) tmp.fs_vfstype = "";
127 if (tmp.fs_mntops == NULL) tmp.fs_mntops = "";
128 if (tmp.fs_type == NULL) tmp.fs_type = "";
129
130 return copy_fstab(&tmp);
131 }
132
133 static struct fstab *
134 ds_getfsspec(const char *name)
135 {
136 static int proc = -1;
137
138 return (struct fstab *)LI_getone("getfsbyname", &proc, extract_fstab, "name", name);
139 }
140
141 static void
142 ds_endfsent(void)
143 {
144 LI_data_free_kvarray(LI_data_find_key(ENTRY_KEY));
145 }
146
147 static int
148 ds_setfsent(void)
149 {
150 ds_endfsent();
151 return 1;
152 }
153
154 static struct fstab *
155 ds_getfsent()
156 {
157 static int proc = -1;
158
159 return (struct fstab *)LI_getent("getfsent", &proc, extract_fstab, ENTRY_KEY, ENTRY_SIZE);
160 }
161
162 static struct fstab *
163 ds_getfsfile(const char *name)
164 {
165 struct fstab *fs;
166
167 if (name == NULL) return NULL;
168
169 ds_setfsent();
170
171 for (fs = ds_getfsent(); fs != NULL; fs = ds_getfsent())
172 {
173 if (!strcmp(fs->fs_file, name)) return fs;
174 }
175
176 ds_endfsent();
177
178 return NULL;
179 }
180
181 static struct fstab *
182 getfs(const char *spec, const char *file, int source)
183 {
184 struct fstab *res = NULL;
185 struct li_thread_info *tdata;
186
187 tdata = LI_data_create_key(ENTRY_KEY, ENTRY_SIZE);
188 if (tdata == NULL) return NULL;
189
190 if (_ds_running())
191 {
192 switch (source)
193 {
194 case FS_GET_SPEC:
195 res = ds_getfsspec(spec);
196 break;
197 case FS_GET_FILE:
198 res = ds_getfsfile(file);
199 break;
200 case FS_GET_ENT:
201 res = ds_getfsent();
202 break;
203 default: res = NULL;
204 }
205 }
206 else
207 {
208 pthread_mutex_lock(&_fstab_lock);
209
210 switch (source)
211 {
212 case FS_GET_SPEC:
213 res = copy_fstab(_old_getfsspec(spec));
214 break;
215 case FS_GET_FILE:
216 res = copy_fstab(_old_getfsfile(file));
217 break;
218 case FS_GET_ENT:
219 res = copy_fstab(_old_getfsent());
220 break;
221 default: res = NULL;
222 }
223
224 pthread_mutex_unlock(&_fstab_lock);
225 }
226
227 LI_data_recycle(tdata, res, ENTRY_SIZE);
228 return (struct fstab *)tdata->li_entry;
229 }
230
231
232 struct fstab *
233 getfsbyname(const char *name)
234 {
235 return getfs(name, NULL, FS_GET_SPEC);
236 }
237
238 struct fstab *
239 getfsspec(const char *name)
240 {
241 return getfs(name, NULL, FS_GET_SPEC);
242 }
243
244 struct fstab *
245 getfsfile(const char *name)
246 {
247 return getfs(NULL, name, FS_GET_FILE);
248 }
249
250 struct fstab *
251 getfsent(void)
252 {
253 return getfs(NULL, NULL, FS_GET_ENT);
254 }
255
256 int
257 setfsent(void)
258 {
259 if (_ds_running()) return (ds_setfsent());
260 return (_old_setfsent());
261 }
262
263 void
264 endfsent(void)
265 {
266 if (_ds_running()) ds_endfsent();
267 else _old_endfsent();
268 }