Debian Codegraph @

libnpclient/chmod.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 <utime.h>
31 #include <sys/stat.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdarg.h>
36 #include <unistd.h>
37 #include <pthread.h>
38 #include <errno.h>
39 #include <stdint.h>
40 #include <inttypes.h>
41 #include <libgen.h>
42
43 #include "9p.h"
44 #include "npfs.h"
45 #include "npclient.h"
46 #include "npcimpl.h"
47
48 int
49 npc_setattr (Npcfid *fid, u32 valid, u32 mode, u32 uid, u32 gid, u64 size,
50 u64 atime_sec, u64 atime_nsec, u64 mtime_sec, u64 mtime_nsec)
51 {
52 Npfcall *tc = NULL, *rc = NULL;
53 int ret = -1;
54
55 if (!(tc = np_create_tsetattr (fid->fid, valid, mode, uid, gid, size,
56 atime_sec, atime_nsec,
57 mtime_sec, mtime_nsec))) {
58 np_uerror (ENOMEM);
59 goto done;
60 }
61 if (fid->fsys->rpc(fid->fsys, tc, &rc) < 0)
62 goto done;
63 ret = 0;
64 done:
65 if (tc)
66 free(tc);
67 if (rc)
68 free(rc);
69 return ret;
70 }
71
72 int
73 npc_fchmod (Npcfid *fid, mode_t mode)
74 {
75 u32 valid = P9_ATTR_MODE;
76
77 return npc_setattr (fid, valid, mode, 0, 0, 0, 0, 0, 0, 0);
78 }
79
80 int
81 npc_chmod (Npcfid *root, char *path, mode_t mode)
82 {
83 Npcfid *fid;
84
85 if (!(fid = npc_walk (root, path)))
86 return -1;
87 if (npc_fchmod (fid, mode) < 0) {
88 int saved_err = np_rerror ();
89 (void)npc_clunk (fid);
90 np_uerror (saved_err);
91 return -1;
92 }
93 if (npc_clunk (fid) < 0)
94 return -1;
95 return 0;
96 }
97
98 int
99 npc_fchown (Npcfid *fid, uid_t owner, gid_t group)
100 {
101 u32 valid = 0;
102
103 if (owner != -1)
104 valid |= P9_ATTR_UID;
105 if (group != -1)
106 valid |= P9_ATTR_GID;
107 return npc_setattr (fid, valid, 0, owner, group, 0, 0, 0, 0, 0);
108 }
109
110 int
111 npc_chown (Npcfid *root, char *path, uid_t owner, gid_t group)
112 {
113 Npcfid *fid;
114
115 if (!(fid = npc_walk (root, path)))
116 return -1;
117 if (npc_fchown (fid, owner, group) < 0) {
118 int saved_err = np_rerror ();
119 (void)npc_clunk (fid);
120 np_uerror (saved_err);
121 return -1;
122 }
123 if (npc_clunk (fid) < 0)
124 return -1;
125 return 0;
126 }
127
128 int
129 npc_ftruncate (Npcfid *fid, off_t length)
130 {
131 u32 valid = P9_ATTR_SIZE;
132
133 return npc_setattr (fid, valid, 0, 0, 0, length, 0, 0, 0, 0);
134 }
135
136 int
137 npc_truncate (Npcfid *root, char *path, off_t length)
138 {
139 Npcfid *fid;
140
141 if (!(fid = npc_walk (root, path)))
142 return -1;
143 if (npc_ftruncate (fid, length) < 0) {
144 int saved_err = np_rerror ();
145 (void)npc_clunk (fid);
146 np_uerror (saved_err);
147 return -1;
148 }
149 if (npc_clunk (fid) < 0)
150 return -1;
151 return 0;
152 }
153
154 int
155 npc_futime (Npcfid *fid, const struct utimbuf *times)
156 {
157 u32 valid = 0;
158 u64 atime_sec = 0, atime_nsec = 0;
159 u64 mtime_sec = 0, mtime_nsec = 0;
160
161 valid |= P9_ATTR_ATIME;
162 valid |= P9_ATTR_MTIME;
163 if (times) {
164 atime_sec = times->actime;
165 valid |= P9_ATTR_ATIME_SET;
166 mtime_sec = times->modtime;
167 valid |= P9_ATTR_MTIME_SET;
168 }
169
170 return npc_setattr (fid, valid, 0, 0, 0, 0,
171 atime_sec, atime_nsec, mtime_sec, mtime_nsec);
172 }
173
174 int
175 npc_utime(Npcfid *root, char *path, const struct utimbuf *times)
176 {
177 Npcfid *fid;
178
179 if (!(fid = npc_walk (root, path)))
180 return -1;
181 if (npc_futime (fid, times) < 0) {
182 int saved_err = np_rerror ();
183 (void)npc_clunk (fid);
184 np_uerror (saved_err);
185 return -1;
186 }
187 if (npc_clunk (fid) < 0)
188 return -1;
189 return 0;
190 }
191