*** empty log message ***
[arla.git] / arlad / irix-subr.c
blobc385bca107d555c65bd64b27c71e917798bcf074
1 /*
2 * Copyright (c) 1995 - 2000 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 #define _KERNEL
35 #include "arla_local.h"
36 RCSID("$Id$");
38 static long blocksize = 1024; /* XXX */
40 static void
41 flushbuf (void *vargs)
43 struct write_dirent_args *args = (struct write_dirent_args *)vargs;
44 struct dirent *last = (struct dirent *)args->last;
45 unsigned inc = blocksize - (args->ptr - args->buf);
47 last->d_reclen += inc;
48 last->d_off += inc;
49 if (write (args->fd, args->buf, blocksize) != blocksize)
50 arla_warn (ADEBWARN, errno, "write");
51 args->ptr = args->buf;
52 args->last = NULL;
53 memset(args->buf, 0, blocksize);
56 static int
57 write_dirent(VenusFid *fid, const char *name, void *arg)
59 struct dirent64 dirent, *real;
60 struct write_dirent_args *args = (struct write_dirent_args *)arg;
61 size_t namelen = strlen (name);
63 dirent.d_reclen = DIRENT64SIZE(namelen);
65 if (args->ptr + dirent.d_reclen > args->buf + blocksize)
66 flushbuf (args);
67 real = (struct dirent64 *)args->ptr;
69 real->d_reclen = dirent.d_reclen;
70 real->d_ino = dentry2ino (name, fid, args->e);
71 strlcpy (real->d_name, name, sizeof(real->d_name));
72 args->ptr += real->d_reclen;
73 args->off += real->d_reclen;
74 real->d_off = args->off;
75 args->last = real;
76 return 0;
79 int
80 conv_dir (FCacheEntry *e, CredCacheEntry *ce, u_int tokens,
81 fcache_cache_handle *cache_handle,
82 char *cache_name, size_t cache_name_sz)
84 return conv_dir_sub (e, ce, tokens, cache_handle, cache_name,
85 cache_name_sz, write_dirent, flushbuf, blocksize);
89 * remove `filename` from the converted directory for `e'
92 int
93 dir_remove_name (FCacheEntry *e, const char *filename,
94 fcache_cache_handle *cache_handle,
95 char *cache_name, size_t cache_name_sz)
97 int ret;
98 int fd;
99 fbuf fb;
100 struct stat sb;
101 char *buf;
102 char *p;
103 size_t len;
104 struct dirent64 *dp;
105 struct dirent64 *last_dp;
107 fcache_extra_file_name (e, cache_name, cache_name_sz);
108 fd = open (cache_name, O_RDWR, 0);
109 if (fd < 0)
110 return errno;
111 fcache_fhget (cache_name, cache_handle);
112 if (fstat (fd, &sb) < 0) {
113 ret = errno;
114 close (fd);
115 return ret;
117 len = sb.st_size;
119 ret = fbuf_create (&fb, fd, len, FBUF_READ|FBUF_WRITE|FBUF_SHARED);
120 if (ret) {
121 close (fd);
122 return ret;
124 last_dp = NULL;
125 ret = ENOENT;
126 for (p = buf = fbuf_buf (&fb); p < buf + len; p += dp->d_reclen) {
127 dp = (struct dirent64 *)p;
129 if (strcmp (filename, dp->d_name) == 0) {
130 if (last_dp != NULL) {
131 struct dirent new_last;
133 new_last.d_reclen = last_dp->d_reclen + dp->d_reclen;
134 if (new_last.d_reclen >= last_dp->d_reclen)
135 last_dp->d_reclen = new_last.d_reclen;
137 dp->d_ino = 0;
138 ret = 0;
139 break;
141 last_dp = dp;
143 fbuf_end (&fb);
144 close (fd);
145 return ret;