Debian Codegraph @

libnpclient/xattr.c

raw
1 /*****************************************************************************
2 * Copyright (C) 2010-14 Lawrence Livermore National Security, LLC.
3 * Written by Jim Garlick <[email protected]> LLNL-CODE-423279
4 * All Rights Reserved.
5 *
6 * This file is part of the Distributed I/O Daemon (diod).
7 * For details, see http://code.google.com/p/diod.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the license, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 * See also: http://www.gnu.org/licenses
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 ssize_t
51 npc_xattrwalk (Npcfid *fid, Npcfid *attrfid, char *name)
52 {
53 Npfcall *tc = NULL, *rc = NULL;
54 ssize_t ret = -1;
55
56 if (!(tc = np_create_txattrwalk (fid->fid, attrfid->fid, name))) {
57 np_uerror (ENOMEM);
58 goto done;
59 }
60 if (fid->fsys->rpc(fid->fsys, tc, &rc) < 0)
61 goto done;
62 ret = rc->u.rxattrwalk.size;
63 done:
64 if (tc)
65 free (tc);
66 if (rc)
67 free (rc);
68 return ret;
69 }
70
71 int
72 npc_xattrcreate (Npcfid *fid, char *name, u64 attr_size, u32 flags)
73 {
74 Npfcall *tc = NULL, *rc = NULL;
75 int ret = -1;
76
77 if (!(tc = np_create_txattrcreate (fid->fid, name, attr_size, flags))) {
78 np_uerror (ENOMEM);
79 goto done;
80 }
81 if (fid->fsys->rpc(fid->fsys, tc, &rc) < 0)
82 goto done;
83 ret = 0;
84 done:
85 if (tc)
86 free (tc);
87 if (rc)
88 free (rc);
89 return ret;
90 }
91
92 ssize_t
93 npc_listxattr (Npcfid *root, char *path, char *buf, size_t size)
94 {
95 Npcfid *fid = NULL;
96 Npcfid *attrfid = NULL;
97 ssize_t ret = -1;
98 size_t n, tot;
99
100 if (!(fid = npc_walk (root, path)))
101 goto done;
102 if (!(attrfid = npc_fid_alloc(fid->fsys)))
103 goto done;
104 ret = npc_xattrwalk (fid, attrfid, NULL);
105 if (ret < 0)
106 goto done;
107 if (buf == NULL || size == 0)
108 goto done;
109 if (ret > size) {
110 np_uerror (ERANGE);
111 goto done;
112 }
113 for (tot = 0; tot < size; ) {
114 n = npc_read (attrfid, buf + tot, size - tot);
115 if (n <= 0)
116 break;
117 tot += n;
118 }
119 ret = tot;
120 done:
121 if (fid)
122 (void)npc_clunk (fid);
123 if (attrfid)
124 (void)npc_clunk (attrfid);
125 return ret;
126 }
127
128 ssize_t
129 npc_getxattr (Npcfid *root, char *path, char *attr, char *buf, size_t size)
130 {
131 Npcfid *fid = NULL;
132 Npcfid *attrfid = NULL;
133 ssize_t ret = -1;
134 size_t n, tot;
135
136 if (!(fid = npc_walk (root, path)))
137 goto done;
138 if (!(attrfid = npc_fid_alloc(fid->fsys)))
139 goto done;
140 ret = npc_xattrwalk (fid, attrfid, attr);
141 if (ret < 0)
142 goto done;
143 if (buf == NULL || size == 0)
144 goto done;
145 if (ret > size) {
146 np_uerror (ERANGE);
147 goto done;
148 }
149 for (tot = 0; tot < size; ) {
150 n = npc_read (attrfid, buf + tot, size - tot);
151 if (n <= 0)
152 break;
153 tot += n;
154 }
155 ret = tot;
156 done:
157 if (fid)
158 (void)npc_clunk (fid);
159 if (attrfid)
160 (void)npc_clunk (attrfid);
161 return ret;
162 }
163
164 int
165 npc_setxattr (Npcfid *root, char *path, char *name, char *val, size_t size,
166 int flags)
167 {
168 Npcfid *fid = NULL;
169 int n, tot = 0;
170 int ret = -1;
171
172 if (!(fid = npc_walk (root, path)))
173 goto done;
174 if (npc_xattrcreate (fid, name, size, flags) < 0)
175 goto done;
176 for (tot = 0; tot < size; tot += n) {
177 n = npc_write(fid, val + tot, size - tot);
178 if (n < 0)
179 goto done;
180 }
181 ret = 0;
182 done:
183 if (fid)
184 ret = npc_clunk(fid);
185 return ret;
186 }