| 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 <stdarg.h> |
| 32 |
#include <unistd.h> |
| 33 |
#include <pthread.h> |
| 34 |
#include <errno.h> |
| 35 |
#include <stdint.h> |
| 36 |
#include <inttypes.h> |
| 37 |
#include <sys/types.h> |
| 38 |
#include <sys/stat.h> |
| 39 |
#include <fcntl.h> |
| 40 |
|
| 41 |
#include "9p.h" |
| 42 |
#include "npfs.h" |
| 43 |
#include "npclient.h" |
| 44 |
#include "npcimpl.h" |
| 45 |
|
| 46 |
int |
| 47 |
npc_pread(Npcfid *fid, void *buf, u32 count, u64 offset) |
| 48 |
{ |
| 49 |
int maxio = fid->fsys->msize - P9_IOHDRSZ; |
| 50 |
Npfcall *tc = NULL, *rc = NULL; |
| 51 |
int ret = -1; |
| 52 |
|
| 53 |
if (count > maxio) |
| 54 |
count = maxio; |
| 55 |
if (!(tc = np_create_tread(fid->fid, offset, count))) { |
| 56 |
np_uerror (ENOMEM); |
| 57 |
goto done; |
| 58 |
} |
| 59 |
if (fid->fsys->rpc(fid->fsys, tc, &rc) < 0) |
| 60 |
goto done; |
| 61 |
memmove(buf, rc->u.rread.data, rc->u.rread.count); |
| 62 |
ret = rc->u.rread.count; |
| 63 |
done: |
| 64 |
if (rc) |
| 65 |
free(rc); |
| 66 |
if (tc) |
| 67 |
free(tc); |
| 68 |
|
| 69 |
return ret; |
| 70 |
} |
| 71 |
|
| 72 |
int |
| 73 |
npc_read(Npcfid *fid, void *buf, u32 count) |
| 74 |
{ |
| 75 |
int ret; |
| 76 |
|
| 77 |
ret = npc_pread (fid, buf, count, fid->offset); |
| 78 |
if (ret > 0) |
| 79 |
fid->offset += ret; |
| 80 |
return ret; |
| 81 |
} |
| 82 |
|
| 83 |
int |
| 84 |
npc_get(Npcfid *root, char *path, void *buf, u32 count) |
| 85 |
{ |
| 86 |
int n, len = 0; |
| 87 |
Npcfid *fid; |
| 88 |
|
| 89 |
if (!(fid = npc_open_bypath(root, path, O_RDONLY))) |
| 90 |
return -1; |
| 91 |
while (len < count) { |
| 92 |
n = npc_read(fid, buf + len, count - len); |
| 93 |
if (n < 0) |
| 94 |
return -1; |
| 95 |
if (n == 0) |
| 96 |
break; |
| 97 |
len += n; |
| 98 |
if ((fid->fsys->flags & NPC_SHORTREAD_EOF) |
| 99 |
&& (len - n < count - len)) |
| 100 |
break; |
| 101 |
} |
| 102 |
if (npc_clunk (fid) < 0) |
| 103 |
return -1; |
| 104 |
return len; |
| 105 |
} |
| 106 |
|
| 107 |
#define AGET_CHUNK 4096 |
| 108 |
char * |
| 109 |
npc_aget(Npcfid *root, char *path) |
| 110 |
{ |
| 111 |
int n, len; |
| 112 |
Npcfid *fid = NULL; |
| 113 |
char *s = NULL;; |
| 114 |
int ssize = 0; |
| 115 |
|
| 116 |
if (!(fid = npc_open_bypath(root, path, O_RDONLY))) |
| 117 |
goto error; |
| 118 |
len = 0; |
| 119 |
do { |
| 120 |
if (!s) { |
| 121 |
ssize = AGET_CHUNK; |
| 122 |
s = malloc (ssize); |
| 123 |
} else if (ssize - len == 1) { |
| 124 |
ssize += AGET_CHUNK; |
| 125 |
s = realloc (s, ssize); |
| 126 |
} |
| 127 |
if (!s) { |
| 128 |
np_uerror (ENOMEM); |
| 129 |
goto error; |
| 130 |
} |
| 131 |
n = npc_read(fid, s + len, ssize - len - 1); |
| 132 |
if (n > 0) { |
| 133 |
len += n; |
| 134 |
if ((fid->fsys->flags & NPC_SHORTREAD_EOF) |
| 135 |
&& (len - n < ssize - len - 1)) |
| 136 |
break; |
| 137 |
} |
| 138 |
} while (n > 0); |
| 139 |
if (n < 0) |
| 140 |
goto error; |
| 141 |
if (npc_clunk (fid) < 0) |
| 142 |
goto error; |
| 143 |
s[len] = '\0'; |
| 144 |
return s; |
| 145 |
error: |
| 146 |
if (s) |
| 147 |
free (s); |
| 148 |
if (fid) |
| 149 |
(void)npc_clunk (fid); |
| 150 |
return NULL; |
| 151 |
} |
| 152 |
|
| 153 |
static int |
| 154 |
_buf_save (Npcfid *fid, char *buf, int len) |
| 155 |
{ |
| 156 |
if (fid->buf) { |
| 157 |
if (!(fid->buf = realloc (fid->buf, fid->buf_size + len))) |
| 158 |
goto nomem; |
| 159 |
memcpy (fid->buf + fid->buf_size, buf, len); |
| 160 |
fid->buf_size += len; |
| 161 |
} else { |
| 162 |
if (!(fid->buf = malloc (len))) |
| 163 |
goto nomem; |
| 164 |
memcpy (fid->buf, buf, len); |
| 165 |
fid->buf_size = len; |
| 166 |
} |
| 167 |
return 0; |
| 168 |
nomem: |
| 169 |
np_uerror (ENOMEM); |
| 170 |
return -1; |
| 171 |
} |
| 172 |
|
| 173 |
static int |
| 174 |
_buf_restore (Npcfid *fid, char *buf, int len) |
| 175 |
{ |
| 176 |
int ret = 0; |
| 177 |
|
| 178 |
if (fid->buf) { |
| 179 |
ret = len > fid->buf_size ? fid->buf_size : len; |
| 180 |
memcpy (buf, fid->buf, ret); |
| 181 |
if (fid->buf_size > ret) { |
| 182 |
memmove (fid->buf, fid->buf + ret, fid->buf_size - ret); |
| 183 |
fid->buf_size -= ret; |
| 184 |
} else { |
| 185 |
free (fid->buf); |
| 186 |
fid->buf = NULL; |
| 187 |
fid->buf_size = 0; |
| 188 |
} |
| 189 |
} |
| 190 |
return ret; |
| 191 |
} |
| 192 |
|
| 193 |
static char * |
| 194 |
_strnchr (char *s, char c, int len) |
| 195 |
{ |
| 196 |
int i; |
| 197 |
|
| 198 |
for (i = 0; i < len; i++) |
| 199 |
if (s[i] == c) |
| 200 |
return &s[i]; |
| 201 |
return NULL; |
| 202 |
} |
| 203 |
|
| 204 |
char * |
| 205 |
npc_gets(Npcfid *fid, char *buf, u32 count) |
| 206 |
{ |
| 207 |
int n = 0, done = 0, extra = 0; |
| 208 |
char *nlp = NULL; |
| 209 |
|
| 210 |
n = _buf_restore (fid, buf, count - 1); |
| 211 |
while (done < count && !nlp) { |
| 212 |
if (done > 0 || n == 0) { |
| 213 |
n = npc_read (fid, buf + done, count - done - 1); |
| 214 |
if (n < 0) |
| 215 |
goto error; |
| 216 |
if (n == 0) |
| 217 |
break; |
| 218 |
} |
| 219 |
nlp = _strnchr (buf + done, '\n', n); |
| 220 |
done += n; |
| 221 |
} |
| 222 |
if (nlp) { |
| 223 |
*nlp = '\0'; |
| 224 |
extra = done - (nlp - buf) - 1; |
| 225 |
if (extra > 0 && _buf_save (fid, nlp + 1, extra) < 0) |
| 226 |
goto error; |
| 227 |
done = nlp - buf; |
| 228 |
} |
| 229 |
buf[done] = '\0'; |
| 230 |
return done > 0 ? buf : NULL; |
| 231 |
error: |
| 232 |
return NULL; |
| 233 |
} |