*** empty log message ***
[arla.git] / arlad / adir.c
blob553a08d8d55f001380fac20bbe48af41bade5f7c
1 /*
2 * Copyright (c) 1995 - 2002 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 int *fd, fbuf *fbuf,
49 FCacheEntry **centry,
50 int open_flags, int fbuf_flags)
52 int ret;
54 ret = fcache_get_data (centry, ce, 0);
55 if (ret)
56 return ret;
58 ret = fcache_get_fbuf (*centry, fd, fbuf, open_flags, 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 int fd;
82 fbuf the_fbuf;
84 ret = fcache_get_fbuf (centry, &fd, &the_fbuf, O_RDONLY,
85 FBUF_READ|FBUF_PRIVATE);
86 if (ret)
87 return ret;
89 ret = fdir_lookup (&the_fbuf, &centry->fid, name, file);
90 fbuf_end (&the_fbuf);
91 close (fd);
92 return ret;
96 * Lookup `name' in the AFS directory identified by `dir' and change the
97 * fid to `fid'.
101 adir_changefid (FCacheEntry **centry,
102 const char *name,
103 VenusFid *file,
104 CredCacheEntry **ce)
106 int ret;
107 int fd;
108 fbuf the_fbuf;
110 ret = get_fbuf_from_centry (ce, &fd, &the_fbuf, centry, O_RDWR,
111 FBUF_READ|FBUF_WRITE|FBUF_SHARED);
112 if (ret)
113 return ret;
115 ret = fdir_changefid (&the_fbuf, name, file);
116 fbuf_end (&the_fbuf);
117 close (fd);
118 return ret;
122 * Return TRUE if dir is empty.
126 adir_emptyp (FCacheEntry **centry,
127 CredCacheEntry **ce)
129 int ret;
130 int fd;
131 fbuf the_fbuf;
133 ret = get_fbuf_from_centry (ce, &fd, &the_fbuf, centry,
134 O_RDONLY, FBUF_READ|FBUF_PRIVATE);
135 if (ret)
136 return ret;
138 ret = fdir_emptyp (&the_fbuf);
139 fbuf_end (&the_fbuf);
140 close (fd);
141 return ret;
145 * Read all entries in the AFS directory identified by `dir' and call
146 * `func' on each entry with the fid, the name, and `arg'.
150 adir_readdir (FCacheEntry **centry,
151 fdir_readdir_func func,
152 void *arg,
153 CredCacheEntry **ce)
155 int fd;
156 fbuf the_fbuf;
157 int ret;
159 ret = get_fbuf_from_centry (ce, &fd, &the_fbuf, centry,
160 O_RDONLY, FBUF_READ|FBUF_PRIVATE);
161 if (ret)
162 return ret;
164 ret = fdir_readdir (&the_fbuf, func, arg, (*centry)->fid, NULL);
165 fbuf_end (&the_fbuf);
166 close (fd);
167 return ret;
171 * Create a new directory with only . and ..
175 adir_mkdir (FCacheEntry *dir,
176 AFSFid dot,
177 AFSFid dot_dot)
179 int fd;
180 fbuf the_fbuf;
181 int ret;
183 AssertExclLocked(&dir->lock);
185 ret = fcache_get_fbuf (dir, &fd, &the_fbuf, O_RDWR,
186 FBUF_READ|FBUF_WRITE|FBUF_SHARED);
187 if (ret)
188 return ret;
190 ret = fdir_mkdir (&the_fbuf, dot, dot_dot, 0);
191 fcache_update_length (dir, fbuf_len(&the_fbuf), fbuf_len(&the_fbuf));
192 fbuf_end (&the_fbuf);
193 close (fd);
194 return ret;
198 * Create a new entry with name `filename' and contents `fid' in `dir'.
202 adir_creat (FCacheEntry *dir,
203 const char *name,
204 AFSFid fid)
206 int fd;
207 fbuf the_fbuf;
208 int ret;
210 ret = fcache_get_fbuf (dir, &fd, &the_fbuf, O_RDWR,
211 FBUF_READ|FBUF_WRITE|FBUF_SHARED);
212 if (ret)
213 return ret;
215 ret = fdir_creat (&the_fbuf, name, NULL, fid);
216 fcache_update_length (dir, fbuf_len(&the_fbuf), fbuf_len(&the_fbuf));
217 fbuf_end (&the_fbuf);
218 close (fd);
219 return ret;
223 * Remove the entry named `name' in dir.
227 adir_remove (FCacheEntry *dir,
228 const char *name)
230 int fd;
231 fbuf the_fbuf;
232 int ret;
234 ret = fcache_get_fbuf (dir, &fd, &the_fbuf, O_RDWR,
235 FBUF_READ|FBUF_WRITE|FBUF_SHARED);
236 if (ret)
237 return ret;
239 ret = fdir_remove(&the_fbuf, name, NULL);
240 fcache_update_length (dir, fbuf_len(&the_fbuf), fbuf_len(&the_fbuf));
241 fbuf_end (&the_fbuf);
242 close (fd);
243 return ret;