Debian Codegraph @

libnpclient/readdir.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 <unistd.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <dirent.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdarg.h>
37 #include <unistd.h>
38 #include <pthread.h>
39 #include <errno.h>
40 #include <stdint.h>
41 #include <inttypes.h>
42 #include <libgen.h>
43 #include <sys/param.h>
44
45 #include "9p.h"
46 #include "npfs.h"
47 #include "npclient.h"
48 #include "npcimpl.h"
49
50 int
51 npc_readdir (Npcfid *fid, u64 offset, char *data, u32 count)
52 {
53 Npfcall *tc = NULL, *rc = NULL;
54 int ret = -1;
55
56 if (!(tc = np_create_treaddir(fid->fid, offset, count))) {
57 np_uerror (ENOMEM);
58 goto done;
59 }
60 if (fid->fsys->rpc(fid->fsys, tc, &rc) < 0)
61 goto done;
62 NP_ASSERT(rc->u.rreaddir.count <= count);
63 memcpy (data, rc->u.rreaddir.data, rc->u.rreaddir.count);
64 ret = rc->u.rreaddir.count;
65 done:
66 if (tc)
67 free(tc);
68 if (rc)
69 free(rc);
70 return ret;
71 }
72
73 Npcfid *
74 npc_opendir (Npcfid *root, char *path)
75 {
76 Npcfid *fid = NULL;
77 struct stat sb;
78
79 if ((fid = npc_open_bypath (root, path, O_RDONLY))) {
80 if (npc_fstat (fid, &sb) < 0)
81 goto error;
82 if (!S_ISDIR (sb.st_mode)) {
83 np_uerror (ENOTDIR);
84 goto error;
85 }
86 fid->buf_size = root->fsys->msize - P9_IOHDRSZ;
87 if (!(fid->buf = malloc (fid->buf_size))) {
88 (void)npc_clunk (fid);
89 np_uerror (ENOMEM);
90 fid = NULL;
91 }
92 fid->offset = 0;
93 fid->buf_len = 0;
94 fid->buf_used = 0;
95 }
96 return fid;
97 error:
98 if (fid)
99 npc_clunk(fid);
100 return NULL;
101 }
102
103 /* Returns error code > 0 on error.
104 * Returns 0 on success: result set to NULL at EOF, o/w set to entry
105 */
106 int
107 npc_readdir_r (Npcfid *fid, struct dirent *entry, struct dirent **result)
108 {
109 Npqid qid;
110 int dname_size = PATH_MAX + 1;
111 u64 offset;
112 u8 type;
113 int res;
114
115 if (!fid->buf) /* not opened with npc_opendir */
116 return EINVAL;
117
118 if (fid->buf_used >= fid->buf_len) {
119 fid->buf_len = npc_readdir (fid, fid->offset, fid->buf,
120 fid->buf_size);
121 if (fid->buf_len < 0)
122 return np_rerror ();
123 if (fid->buf_len == 0) { /* EOF */
124 *result = NULL;
125 return 0;
126 }
127 fid->buf_used = 0;
128 }
129 res = np_deserialize_p9dirent (&qid, &offset, &type,
130 entry->d_name, dname_size,
131 (u8 *)fid->buf + fid->buf_used,
132 fid->buf_len - fid->buf_used);
133 if (res == 0)
134 return EIO;
135 entry->d_off = offset;
136 entry->d_type = type;
137 entry->d_ino = qid.path;
138 //entry->d_reclen
139 fid->offset = offset;
140 fid->buf_used += res;
141 *result = entry;
142 return 0;
143 }
144
145 void
146 npc_seekdir (Npcfid *fid, long offset)
147 {
148 fid->offset = offset;
149 fid->buf_used = fid->buf_len; /* force a 9p readdir call */
150 }
151
152 long
153 npc_telldir (Npcfid *fid)
154 {
155 return fid->offset;
156 }