>grand.central.org GCO Public CellServDB 25 Oct 2007
[arla.git] / arlad / adir.c
blobc115bd524e7c9b36843d9ccb9ccef31e2dc7437c
1 /*
2 * Copyright (c) 1995 - 2002, 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * 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 the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
35 * Routines for reading an AFS directory
38 #include "arla_local.h"
40 RCSID("$Id$") ;
46 static int
47 get_fbuf_from_centry (CredCacheEntry **ce,
48 fbuf *fbuf,
49 FCacheEntry **centry,
50 int fbuf_flags)
52 int ret;
54 ret = fcache_get_data (centry, ce, 0, 0);
55 if (ret)
56 return ret;
58 ret = fcache_get_fbuf (*centry, fbuf, fbuf_flags);
59 if (ret) {
60 return ret;
62 return 0;
66 * Lookup `name' in the AFS directory identified by `centry' and return
67 * the Fid in `file'. All operations are done as `cred' and return
68 * value is 0 or error code.
71 * Locking:
72 * In Out Fail
73 * centry: Locked Locked Locked
77 int
78 adir_lookup (FCacheEntry *centry, const char *name, VenusFid *file)
80 int ret;
81 fbuf the_fbuf;
83 ret = fcache_get_fbuf (centry, &the_fbuf, FBUF_READ);
84 if (ret)
85 return ret;
87 ret = fdir_lookup (&the_fbuf, &centry->fid, name, file);
88 abuf_end(&the_fbuf);
89 return ret;
93 * Lookup `name' in the AFS directory identified by `dir' and change the
94 * fid to `fid'.
97 int
98 adir_changefid (FCacheEntry **centry,
99 const char *name,
100 VenusFid *file,
101 CredCacheEntry **ce)
103 int ret;
104 fbuf the_fbuf;
106 ret = get_fbuf_from_centry(ce, &the_fbuf, centry,
107 FBUF_READ|FBUF_WRITE);
108 if (ret)
109 return ret;
111 ret = fdir_changefid (&the_fbuf, name, file);
112 abuf_end (&the_fbuf);
113 return ret;
117 * Return TRUE if dir is empty.
121 adir_emptyp (FCacheEntry **centry,
122 CredCacheEntry **ce)
124 int ret;
125 fbuf the_fbuf;
127 ret = get_fbuf_from_centry(ce, &the_fbuf, centry, FBUF_READ);
128 if (ret)
129 return ret;
131 ret = fdir_emptyp (&the_fbuf);
132 abuf_end (&the_fbuf);
133 return ret;
137 * Read all entries in the AFS directory identified by `dir' and call
138 * `func' on each entry with the fid, the name, and `arg'.
142 adir_readdir (FCacheEntry **centry,
143 fdir_readdir_func func,
144 void *arg,
145 CredCacheEntry **ce)
147 fbuf the_fbuf;
148 int ret;
150 ret = get_fbuf_from_centry(ce, &the_fbuf, centry, FBUF_READ);
151 if (ret)
152 return ret;
154 ret = fdir_readdir (&the_fbuf, func, arg, (*centry)->fid, NULL);
155 abuf_end (&the_fbuf);
156 return ret;
160 * Create a new directory with only . and ..
164 adir_mkdir (FCacheEntry *dir,
165 AFSFid dot,
166 AFSFid dot_dot)
168 fbuf the_fbuf;
169 int ret;
171 AssertExclLocked(&dir->lock);
173 ret = abuf_create(&the_fbuf, dir, 0, FBUF_READ|FBUF_WRITE);
174 if (ret)
175 return ret;
177 /* fcache_set_have_all(dir, fbuf_len(&the_fbuf)); */
178 ret = fdir_mkdir (&the_fbuf, dot, dot_dot, 0);
179 fcache_set_have_all(dir, fbuf_len(&the_fbuf));
180 abuf_end(&the_fbuf);
181 return ret;
185 * Create a new entry with name `filename' and contents `fid' in `dir'.
189 adir_creat (FCacheEntry *dir,
190 const char *name,
191 AFSFid fid)
193 fbuf the_fbuf;
194 int ret;
196 ret = fcache_get_fbuf(dir, &the_fbuf, FBUF_READ|FBUF_WRITE);
197 if (ret)
198 return ret;
200 ret = fdir_creat (&the_fbuf, name, NULL, fid);
201 fcache_set_have_all(dir, fbuf_len(&the_fbuf));
202 abuf_end(&the_fbuf);
203 return ret;
207 * Remove the entry named `name' in dir.
211 adir_remove (FCacheEntry *dir,
212 const char *name)
214 fbuf the_fbuf;
215 int ret;
217 ret = fcache_get_fbuf (dir, &the_fbuf, FBUF_READ|FBUF_WRITE);
218 if (ret)
219 return ret;
221 ret = fdir_remove(&the_fbuf, name, NULL);
222 fcache_set_have_all(dir, fbuf_len(&the_fbuf));
223 abuf_end (&the_fbuf);
224 return ret;