Debian Codegraph @

libnpclient/write.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 #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_pwrite(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_twrite(fid->fid, offset, count, buf))) {
56 np_uerror (ENOMEM);
57 goto done;
58 }
59 if (fid->fsys->rpc(fid->fsys, tc, &rc) < 0)
60 goto done;
61 ret = rc->u.rwrite.count;
62 done:
63 if (tc)
64 free(tc);
65 if (rc)
66 free(rc);
67 return ret;
68 }
69
70 int
71 npc_write(Npcfid *fid, void *buf, u32 count)
72 {
73 int ret;
74
75 ret = npc_pwrite (fid, buf, count, fid->offset);
76 if (ret > 0)
77 fid->offset += ret;
78 return ret;
79 }
80
81 int
82 npc_put(Npcfid *root, char *path, void *buf, u32 count)
83 {
84 int n, done = 0;
85 Npcfid *fid;
86
87 if (!(fid = npc_open_bypath (root, path, O_WRONLY)))
88 return -1;
89 while (done < count) {
90 n = npc_write(fid, buf + done, count - done);
91 if (n < 0) {
92 done = -1;
93 break;
94 }
95 done += n;
96 }
97 if (npc_clunk (fid) < 0)
98 done = -1;
99 return done;
100 }
101
102 int
103 npc_puts (Npcfid *fid, char *buf)
104 {
105 int n, count = strlen (buf), done = 0;
106
107 while (done < count) {
108 n = npc_write(fid, buf + done, count - done);
109 if (n < 0) {
110 done = -1;
111 break;
112 }
113 done += n;
114 }
115 return done;
116 }