b2fa2e782ed65441686538707e1f1b1daa066bb3
[openafs.git] / src / libafscp / afscp_dirops.c
1 /* AUTORIGHTS
2 Copyright (C) 2003 - 2010 Chaskiel Grundman
3 All rights reserved
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 1. Redistributions of source code must retain the above copyright
10    notice, this list of conditions and the following disclaimer.
11
12 2. Redistributions in binary form must reproduce the above copyright
13    notice, this list of conditions and the following disclaimer in the
14    documentation and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #include <afs/param.h>
28 #include <afs/afsint.h>
29 #include <afs/vlserver.h>
30 #include <afs/vldbint.h>
31 #include <afs/dir.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <search.h>
36 #include "afscp.h"
37 #include "afscp_internal.h"
38
39
40 int afs_CreateFile(const struct afs_venusfid *dir, char *name,
41                struct AFSStoreStatus *sst,
42                struct afs_venusfid **ret) {
43   int code, i, j;
44   struct AFSFid df = dir->fid;
45   struct afs_volume *vol;
46   struct AFSFetchStatus dfst, fst;
47   struct AFSVolSync vs;
48   struct AFSCallBack cb;
49   struct AFSFid ff;
50   struct afs_volume *volume;
51   struct afs_server *server;
52   struct rx_call *c;
53
54   vol=afs_volumebyid(dir->cell, dir->fid.Volume);
55   if (!vol) {
56     afs_errno=ENOENT;
57     return -1;
58   }
59   code=ENOENT;
60   for (i=0;i<vol->nservers;i++) {
61     server=afs_serverbyindex(vol->servers[i]);
62     if (server && server->naddrs > 0) {
63       for (j=0;j < server->naddrs;j++) {
64         code=RXAFS_CreateFile(server->conns[j], &df, name, sst, &ff,
65                               &fst, &dfst, &cb, &vs);
66         if (code >= 0)
67           break;
68       }
69     }
70     if (code >= 0)
71       break;
72   }
73   if (code) {
74     _StatInvalidate(dir);
75     afs_errno=code;
76     return -1;
77   }
78   _StatStuff(dir, &dfst);
79   AddCallBack(server, &ff, &fst, &cb);
80   if (ret)
81     *ret=makefid(vol->cell, ff.Volume,  ff.Vnode, ff.Unique);
82   return 0;
83 }
84
85 int afs_MakeDir(const struct afs_venusfid *dir, char *name,
86                struct AFSStoreStatus *sst,
87                struct afs_venusfid **ret) {
88   int code, i, j;
89   struct AFSFid df = dir->fid;
90   struct afs_volume *vol;
91   struct AFSFetchStatus dfst, fst;
92   struct AFSVolSync vs;
93   struct AFSCallBack cb;
94   struct AFSFid ff;
95   struct afs_volume *volume;
96   struct afs_server *server;
97
98   vol=afs_volumebyid(dir->cell, dir->fid.Volume);
99   if (!vol) {
100     afs_errno=ENOENT;
101     return -1;
102   }
103   code=ENOENT;
104   for (i=0;i<vol->nservers;i++) {
105     server=afs_serverbyindex(vol->servers[i]);
106     if (server && server->naddrs > 0) {
107       for (j=0;j < server->naddrs;j++) {
108         code=RXAFS_MakeDir(server->conns[j], &df, name, sst, &ff,
109                               &fst, &dfst, &cb, &vs);
110         if (code >= 0)
111           break;
112       }
113     }
114     if (code >= 0)
115       break;
116   }
117   if (code) {
118     _StatInvalidate(dir);
119     afs_errno=code;
120     return -1;
121   }
122   _StatStuff(dir, &dfst);
123   AddCallBack(server, &ff, &fst, &cb);
124   if (ret)
125     *ret=makefid(vol->cell, ff.Volume, ff.Vnode, ff.Unique);
126   return 0;
127 }
128
129 int afs_Symlink(const struct afs_venusfid *dir, char *name,
130                 char *target,
131                 struct AFSStoreStatus *sst) {
132   int code, i, j;
133   struct AFSFid df = dir->fid;
134   struct afs_volume *vol;
135   struct AFSFetchStatus dfst, fst;
136   struct AFSVolSync vs;
137   struct AFSCallBack cb;
138   struct AFSFid ff;
139   struct afs_volume *volume;
140   struct afs_server *server;
141
142   vol=afs_volumebyid(dir->cell, dir->fid.Volume);
143   if (!vol) {
144     afs_errno=ENOENT;
145     return -1;
146   }
147   code=ENOENT;
148   for (i=0;i<vol->nservers;i++) {
149     server=afs_serverbyindex(vol->servers[i]);
150     if (server && server->naddrs > 0) {
151       for (j=0;j < server->naddrs;j++) {
152         code=RXAFS_Symlink(server->conns[j], &df, name, target, sst, &ff,
153                               &fst, &dfst, &vs);
154         if (code >= 0)
155           break;
156       }
157     }
158     if (code >= 0)
159       break;
160   }
161   if (code) {
162     _StatInvalidate(dir);
163     afs_errno=code;
164     return -1;
165   }
166   _StatStuff(dir, &dfst);
167   return 0;
168 }
169
170
171 int afs_RemoveFile(const struct afs_venusfid *dir, char *name) {
172   int code, i, j;
173   struct AFSFid df = dir->fid;
174   struct afs_volume *vol;
175   struct AFSFetchStatus dfst;
176   struct AFSVolSync vs;
177   struct AFSCallBack cb;
178   struct afs_volume *volume;
179   struct afs_server *server;
180
181   vol=afs_volumebyid(dir->cell, dir->fid.Volume);
182   if (!vol) {
183     afs_errno=ENOENT;
184     return -1;
185   }
186   code=ENOENT;
187   for (i=0;i<vol->nservers;i++) {
188     server=afs_serverbyindex(vol->servers[i]);
189     if (server && server->naddrs > 0) {
190       for (j=0;j < server->naddrs;j++) {
191         code=RXAFS_RemoveFile(server->conns[j], &df, name, &dfst, &vs);
192         if (code >= 0)
193           break;
194       }
195     }
196     if (code >= 0)
197       break;
198   }
199   if (code) {
200     _StatInvalidate(dir);
201     afs_errno=code;
202     return -1;
203   }
204   _StatStuff(dir, &dfst);
205   return 0;
206 }
207
208 int afs_RemoveDir(const struct afs_venusfid *dir, char *name) {
209   int code, i, j;
210   struct AFSFid df = dir->fid;
211   struct afs_volume *vol;
212   struct AFSFetchStatus dfst;
213   struct AFSVolSync vs;
214   struct AFSCallBack cb;
215   struct afs_volume *volume;
216   struct afs_server *server;
217
218   vol=afs_volumebyid(dir->cell, dir->fid.Volume);
219   if (!vol) {
220     afs_errno=ENOENT;
221     return -1;
222   }
223   code=ENOENT;
224   for (i=0;i<vol->nservers;i++) {
225     server=afs_serverbyindex(vol->servers[i]);
226     if (server && server->naddrs > 0) {
227       for (j=0;j < server->naddrs;j++) {
228         code=RXAFS_RemoveDir(server->conns[j], &df, name, &dfst, &vs);
229         if (code >= 0)
230           break;
231       }
232     }
233     if (code >= 0)
234       break;
235   }
236   if (code) {
237     _StatInvalidate(dir);
238     afs_errno=code;
239     return -1;
240   }
241   _StatStuff(dir, &dfst);
242   return 0;
243 }