>grand.central.org GCO Public CellServDB 25 Oct 2007
[arla.git] / arlad / bsd-subr.c
blob8c43b3a5a9732168d63e0b225ca61b23a3a38850
1 /*
2 * Copyright (c) 1995 - 2004 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.
34 #include "arla_local.h"
35 RCSID("$Id$");
37 #ifdef __linux__
38 #include <nnpfs/nnpfs_dirent.h>
39 #else
40 #define NNPFS_DIRENT_BLOCKSIZE 512
41 #define nnpfs_dirent dirent
42 #endif
44 static long blocksize = NNPFS_DIRENT_BLOCKSIZE; /* XXX */
47 * Write out all remaining data in `args'
50 static void
51 flushbuf (void *vargs)
53 struct write_dirent_args *args = (struct write_dirent_args *)vargs;
54 unsigned inc = blocksize - (args->ptr - args->buf);
55 struct nnpfs_dirent *last = (struct nnpfs_dirent *)args->last;
57 last->d_reclen += inc;
58 if (write (args->fd, args->buf, blocksize) != blocksize)
59 arla_warn (ADEBWARN, errno, "write");
60 args->ptr = args->buf;
61 args->last = NULL;
62 memset(args->buf, 0, blocksize);
66 * Write a dirent to the args buf in `arg' containg `fid' and `name'.
69 static int
70 write_dirent(VenusFid *fid, const char *name, void *arg)
72 struct nnpfs_dirent dirent, *real;
73 struct write_dirent_args *args = (struct write_dirent_args *)arg;
75 dirent.d_namlen = strlen (name);
76 #ifdef _GENERIC_DIRSIZ
77 dirent.d_reclen = _GENERIC_DIRSIZ(&dirent);
78 #elif defined(DIRENT_SIZE)
79 dirent.d_reclen = DIRENT_SIZE(&dirent);
80 #elif defined(_DIRENT_SIZE)
81 dirent.d_reclen = _DIRENT_SIZE(&dirent);
82 #else
83 dirent.d_reclen = DIRSIZ(&dirent);
84 #endif
86 if (args->ptr + dirent.d_reclen > args->buf + blocksize)
87 flushbuf (args);
88 real = (struct nnpfs_dirent *)args->ptr;
90 real->d_namlen = dirent.d_namlen;
91 real->d_reclen = dirent.d_reclen;
92 #if defined(HAVE_STRUCT_DIRENT_D_TYPE) && !defined(__linux__)
93 real->d_type = DT_UNKNOWN;
94 #endif
96 real->d_fileno = dentry2ino (name, fid, args->e);
97 strlcpy (real->d_name, name, sizeof(real->d_name));
98 args->ptr += real->d_reclen;
99 args->off += real->d_reclen;
100 args->last = real;
101 return 0;
105 conv_dir (FCacheEntry *e, CredCacheEntry *ce, u_int tokens)
107 return conv_dir_sub(e, ce, tokens, write_dirent, flushbuf, blocksize);
111 * remove `filename` from the converted directory for `e'
114 #ifndef DIRBLKSIZ
115 #define DIRBLKSIZ 1024
116 #endif
119 dir_remove_name (FCacheEntry *e, const char *filename)
121 char cache_name[MAXPATHLEN];
122 int ret;
123 int fd;
124 fbuf fb;
125 struct stat sb;
126 char *buf;
127 char *p;
128 size_t len;
129 struct nnpfs_dirent *dp;
130 struct nnpfs_dirent *last_dp;
132 fcache_extra_file_name (e, cache_name, MAXPATHLEN);
133 fd = open (cache_name, O_RDWR, 0);
134 if (fd < 0)
135 return errno;
137 /* fcache_fhget (cache_name, cache_handle); */
139 if (fstat (fd, &sb) < 0) {
140 ret = errno;
141 close (fd);
142 return ret;
144 len = sb.st_size;
146 ret = fbuf_create (&fb, fd, len, FBUF_READ|FBUF_WRITE);
147 if (ret) {
148 close (fd);
149 return ret;
151 last_dp = NULL;
152 ret = ENOENT;
153 for (p = buf = fbuf_buf (&fb); p < buf + len; p += dp->d_reclen) {
155 dp = (struct nnpfs_dirent *)p;
157 assert (dp->d_reclen > 0);
159 if (strcmp (filename, dp->d_name) == 0) {
160 if (last_dp != NULL) {
161 size_t off1, off2;
162 unsigned len;
165 * d_reclen can be as largest (in worst case)
166 * DIRBLKSIZ, and may not cause the entry to cross a
167 * DIRBLKSIZ boundery.
169 len = last_dp->d_reclen + dp->d_reclen;
171 off1 = (char *)last_dp - buf;
172 off2 = off1 + len;
173 off1 /= DIRBLKSIZ;
174 off2 /= DIRBLKSIZ;
176 if (len < DIRBLKSIZ && off1 == off2)
177 last_dp->d_reclen = len;
179 dp->d_fileno = 0;
180 ret = 0;
181 break;
183 last_dp = dp;
185 fbuf_end (&fb);
186 close (fd);
187 return ret;