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