xref: /haiku/src/kits/storage/Statable.cpp (revision d5cd5d63ff0ad395989db6cf4841a64d5b545d1d)
1 //----------------------------------------------------------------------
2 //  This software is part of the OpenBeOS distribution and is covered
3 //  by the OpenBeOS license.
4 //---------------------------------------------------------------------
5 /*!
6 	\file Statable.cpp
7 	BStatable implementation.
8 */
9 
10 #include <Statable.h>
11 #include <Node.h>
12 #include <Volume.h>
13 
14 #include <sys/stat.h>
15 
16 #include "fsproto.h"
17 #include "kernel_interface.h"
18 
19 /*!	\fn status_t GetStat(struct stat *st) const
20 	\brief Returns the stat stucture for the node.
21 	\param st the stat structure to be filled in.
22 	\return
23 	- \c B_OK: Worked fine
24 	- \c B_NO_MEMORY: Could not allocate the memory for the call.
25 	- \c B_BAD_VALUE: The current node does not exist.
26 	- \c B_NOT_ALLOWED: Read only node or volume.
27 */
28 
29 /*!	\brief Returns if the current node is a file.
30 	\return \c true, if the BNode is properly initialized and is a file,
31 			\c false otherwise.
32 */
33 bool
34 BStatable::IsFile() const
35 {
36 	struct stat statData;
37 	if ( GetStat(&statData) == B_OK )
38 		return S_ISREG(statData.st_mode);
39 	else
40 		return false;
41 }
42 
43 /*!	\brief Returns if the current node is a directory.
44 	\return \c true, if the BNode is properly initialized and is a file,
45 			\c false otherwise.
46 */
47 bool
48 BStatable::IsDirectory() const
49 {
50 	struct stat statData;
51 	if ( GetStat(&statData) == B_OK )
52 		return S_ISDIR(statData.st_mode);
53 	else
54 		return false;
55 }
56 
57 /*!	\brief Returns if the current node is a symbolic link.
58 	\return \c true, if the BNode is properly initialized and is a symlink,
59 			\c false otherwise.
60 */
61 bool
62 BStatable::IsSymLink() const
63 {
64 	struct stat statData;
65 	if ( GetStat(&statData) == B_OK )
66 		return S_ISLNK(statData.st_mode);
67 	else
68 		return false;
69 }
70 
71 /*!	\brief Returns a node_ref for the current node.
72 	\param ref the node_ref structure to be filled in
73 	\see GetStat() for return codes
74 */
75 status_t
76 BStatable::GetNodeRef(node_ref *ref) const
77 {
78 	status_t error = (ref ? B_OK : B_BAD_VALUE);
79 	struct stat statData;
80 	if (error == B_OK)
81 		error = GetStat(&statData);
82 	if (error == B_OK) {
83 		ref->device  = statData.st_dev;
84 		ref->node = statData.st_ino;
85 	}
86 	return error;
87 }
88 
89 /*!	\brief Returns the owner of the node.
90 	\param owner a pointer to a uid_t variable to be set to the result
91 	\see GetStat() for return codes
92 */
93 status_t
94 BStatable::GetOwner(uid_t *owner) const
95 {
96 	status_t error = (owner ? B_OK : B_BAD_VALUE);
97 	struct stat statData;
98 	if (error == B_OK)
99 		error = GetStat(&statData);
100 	if (error == B_OK)
101 		*owner = statData.st_uid;
102 	return error;
103 }
104 
105 /*!	\brief Sets the owner of the node.
106 	\param owner the new owner
107 	\see GetStat() for return codes
108 */
109 status_t
110 BStatable::SetOwner(uid_t owner)
111 {
112 	status_t error;
113 	struct stat statData;
114 	error = GetStat(&statData);
115 	if (error == B_OK) {
116 		statData.st_uid = owner;
117 		error = set_stat(statData, WSTAT_UID);
118 	}
119 	return error;
120 }
121 
122 /*!	\brief Returns the group owner of the node.
123 	\param group a pointer to a gid_t variable to be set to the result
124 	\see GetStat() for return codes
125 */
126 status_t
127 BStatable::GetGroup(gid_t *group) const
128 {
129 	status_t error = (group ? B_OK : B_BAD_VALUE);
130 	struct stat statData;
131 	if (error == B_OK)
132 		error = GetStat(&statData);
133 	if (error == B_OK)
134 		*group = statData.st_gid;
135 	return error;
136 }
137 
138 /*!	\brief Sets the group owner of the node.
139 	\param group the new group
140 	\see GetStat() for return codes
141 */
142 status_t
143 BStatable::SetGroup(gid_t group)
144 {
145 	status_t error;
146 	struct stat statData;
147 	error = GetStat(&statData);
148 	if (error == B_OK) {
149 		statData.st_gid = group;
150 		error = set_stat(statData, WSTAT_GID);
151 	}
152 	return error;
153 }
154 
155 /*!	\brief Returns the permissions of the node.
156 	\param perms a pointer to a mode_t variable to be set to the result
157 	\see GetStat() for return codes
158 */
159 status_t
160 BStatable::GetPermissions(mode_t *perms) const
161 {
162 	status_t error = (perms ? B_OK : B_BAD_VALUE);
163 	struct stat statData;
164 	if (error == B_OK)
165 		error = GetStat(&statData);
166 	if (error == B_OK)
167 		*perms = (statData.st_mode & S_IUMSK);
168 	return error;
169 }
170 
171 /*!	\brief Sets the permissions of the node.
172 	\param perms the new permissions
173 	\see GetStat() for return codes
174 */
175 status_t
176 BStatable::SetPermissions(mode_t perms)
177 {
178 	status_t error;
179 	struct stat statData;
180 	error = GetStat(&statData);
181 	if (error == B_OK) {
182 		statData.st_mode = (statData.st_mode & ~S_IUMSK) | (perms & S_IUMSK);
183 		error = set_stat(statData, WSTAT_MODE);
184 	}
185 	return error;
186 }
187 
188 /*!	\brief Get the size of the node's data (not counting attributes).
189 	\param size a pointer to a variable to be set to the result
190 	\see GetStat() for return codes
191 */
192 status_t
193 BStatable::GetSize(off_t *size) const
194 {
195 	status_t error = (size ? B_OK : B_BAD_VALUE);
196 	struct stat statData;
197 	if (error == B_OK)
198 		error = GetStat(&statData);
199 	if (error == B_OK)
200 		*size = statData.st_size;
201 	return error;
202 }
203 
204 /*!	\brief Returns the last time the node was modified.
205 	\param mtime a pointer to a variable to be set to the result
206 	\see GetStat() for return codes
207 */
208 status_t
209 BStatable::GetModificationTime(time_t *mtime) const
210 {
211 	status_t error = (mtime ? B_OK : B_BAD_VALUE);
212 	struct stat statData;
213 	if (error == B_OK)
214 		error = GetStat(&statData);
215 	if (error == B_OK)
216 		*mtime = statData.st_mtime;
217 	return error;
218 }
219 
220 /*!	\brief Sets the last time the node was modified.
221 	\param mtime the new modification time
222 	\see GetStat() for return codes
223 */
224 status_t
225 BStatable::SetModificationTime(time_t mtime)
226 {
227 	status_t error;
228 	struct stat statData;
229 	error = GetStat(&statData);
230 	if (error == B_OK) {
231 		statData.st_mtime = mtime;
232 		error = set_stat(statData, WSTAT_MTIME);
233 	}
234 	return error;
235 }
236 
237 /*!	\brief Returns the time the node was created.
238 	\param ctime a pointer to a variable to be set to the result
239 	\see GetStat() for return codes
240 */
241 status_t
242 BStatable::GetCreationTime(time_t *ctime) const
243 {
244 	status_t error = (ctime ? B_OK : B_BAD_VALUE);
245 	struct stat statData;
246 	if (error == B_OK)
247 		error = GetStat(&statData);
248 	if (error == B_OK)
249 		*ctime = statData.st_crtime;
250 	return error;
251 }
252 
253 /*!	\brief Sets the time the node was created.
254 	\param ctime the new creation time
255 	\see GetStat() for return codes
256 */
257 status_t
258 BStatable::SetCreationTime(time_t ctime)
259 {
260 	status_t error;
261 	struct stat statData;
262 	error = GetStat(&statData);
263 	if (error == B_OK) {
264 		statData.st_crtime = ctime;
265 		error = set_stat(statData, WSTAT_CRTIME);
266 	}
267 	return error;
268 }
269 
270 /*!	\brief Returns the time the node was accessed.
271 	Not used.
272 	\see GetModificationTime()
273 	\see GetStat() for return codes
274 */
275 status_t
276 BStatable::GetAccessTime(time_t *atime) const
277 {
278 	status_t error = (atime ? B_OK : B_BAD_VALUE);
279 	struct stat statData;
280 	if (error == B_OK)
281 		error = GetStat(&statData);
282 	if (error == B_OK)
283 		*atime = statData.st_atime;
284 	return error;
285 }
286 
287 /*!	\brief Sets the time the node was accessed.
288 	Not used.
289 	\see GetModificationTime()
290 	\see GetStat() for return codes
291 */
292 status_t
293 BStatable::SetAccessTime(time_t atime)
294 {
295 	status_t error;
296 	struct stat statData;
297 	error = GetStat(&statData);
298 	if (error == B_OK) {
299 		statData.st_atime = atime;
300 		error = set_stat(statData, WSTAT_ATIME);
301 	}
302 	return error;
303 }
304 
305 /*!	\brief Returns the volume the node lives on.
306 	\param vol a pointer to a variable to be set to the result
307 	\see BVolume
308 	\see GetStat() for return codes
309 */
310 status_t
311 BStatable::GetVolume(BVolume *vol) const
312 {
313 	status_t error = (vol ? B_OK : B_BAD_VALUE);
314 	struct stat statData;
315 	if (error == B_OK)
316 		error = GetStat(&statData);
317 	if (error == B_OK)
318 		error = vol->SetTo(statData.st_dev);
319 	return error;
320 }
321 
322 void BStatable::_OhSoStatable1() {}
323 void BStatable::_OhSoStatable2() {}
324 void BStatable::_OhSoStatable3() {}
325 
326 
327 
328