Debian Codegraph @

libnpclient/walk.c

raw
1 /*
2 * Copyright (C) 2006 by Latchesar Ionkov <[email protected]>
3 * Copyright (C) 2010-2014 by Lawrence Livermore National Security, LLC
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #if HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <stdarg.h>
33 #include <pthread.h>
34 #include <errno.h>
35 #include <stdint.h>
36 #include <inttypes.h>
37
38 #include "9p.h"
39 #include "npfs.h"
40 #include "npclient.h"
41 #include "npcimpl.h"
42
43 Npcfid *
44 npc_clone(Npcfid *fid)
45 {
46 Npcfsys *fsys = fid->fsys;
47 Npcfid *nfid = NULL;
48 Npfcall *tc = NULL, *rc = NULL;
49
50 if (!(nfid = npc_fid_alloc(fsys)))
51 goto error;
52 if (!(tc = np_create_twalk(fid->fid, nfid->fid, 0, NULL))) {
53 np_uerror(ENOMEM);
54 goto error;
55 }
56 if (fsys->rpc(fsys, tc, &rc) < 0)
57 goto error;
58 free(tc);
59 free(rc);
60
61 return nfid;
62 error:
63 if (rc)
64 free(rc);
65 if (tc)
66 free(tc);
67 if (nfid)
68 npc_fid_free (nfid);
69 return NULL;
70 }
71
72 Npcfid *
73 npc_walk(Npcfid *nfid, char *path)
74 {
75 int n;
76 char *fname, *s, *t = NULL;
77 char *wnames[P9_MAXWELEM];
78 Npfcall *tc = NULL, *rc = NULL;
79 Npcfid *fid = NULL;
80
81 if (path == NULL)
82 return npc_clone(nfid);
83
84 while (*path == '/')
85 path++;
86
87 fname = strdup(path);
88 if (!fname) {
89 np_uerror(ENOMEM);
90 goto error;
91 }
92 fid = npc_fid_alloc(nfid->fsys);
93 if (!fid)
94 goto error;
95 s = fname;
96 while (1) {
97 n = 0;
98 while (n<P9_MAXWELEM && *s!='\0') {
99 if (*s == '/') {
100 s++;
101 continue;
102 }
103
104 wnames[n++] = s;
105 t = strchr(s, '/');
106 if (!t)
107 break;
108
109 *t = '\0';
110 s = t + 1;
111 }
112
113 if (!(tc = np_create_twalk(nfid->fid, fid->fid, n, wnames))) {
114 np_uerror(ENOMEM);
115 goto error;
116 }
117 if (nfid->fsys->rpc(nfid->fsys, tc, &rc) < 0)
118 goto error;
119
120 nfid = fid;
121 if (rc->u.rwalk.nwqid != n) {
122 np_uerror(ENOENT);
123 goto error;
124 }
125 free(tc);
126 free(rc);
127 if (!t || *s=='\0')
128 break;
129 }
130
131 free(fname);
132 return fid;
133
134 error:
135 if (rc)
136 free(rc);
137 if (tc)
138 free(tc);
139 if (fid && nfid->fid == fid->fid) {
140 int saved_err = np_rerror ();
141 (void)npc_clunk (fid);
142 np_uerror (saved_err);
143 } else if (fid)
144 npc_fid_free (fid);
145 if (fname)
146 free(fname);
147 return NULL;
148 }