xref: /haiku/docs/user/storage/Entry.dox (revision 541ff51a6ef4c47f8ab105ba6ff895cdbba83aca)
1/*
2 * Copyright 2011 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Tyler Dauwalder
7 *		Simon Cusack, scusack@users.sf.net
8 *		John Scipione, jscipione@gmail.com
9 * Corresponds to:
10 *		headers/os/storage/Entry.h  hrev43528
11 *		src/kits/storage/Entry.cpp  hrev43528
12 */
13
14
15/*!
16	\file Entry.h
17	Provides the BEntry class and entry_ref implementations.
18*/
19
20
21/*!
22	\struct entry_ref
23	\brief A filesystem entry represented as a name in a concrete directory.
24
25	entry_refs may refer to pre-existing (concrete) files, as well as
26	non-existing (abstract) files. However, the parent directory of the file
27	\b must exist.
28
29	The result of this dichotomy is a blending of the persistence gained by
30	referring to entries with a reference to their internal filesystem node and
31	the flexibility gained by referring to entries by name.
32
33	For example, if the directory in which the entry resides (or a directory
34	further up in the hierarchy) is moved or renamed, the entry_ref will still
35	refer to the correct file (whereas a pathname to the previous location of
36	the file would now be invalid).
37
38	On the other hand, say that the entry_ref refers to a concrete file. If the
39	file itself is renamed, the entry_ref now refers to an abstract file with
40	the old name (the upside in this case is that abstract entries may be
41	represented by entry_refs without preallocating an internal filesystem node
42	for them).
43*/
44
45
46/*!
47	\fn entry_ref::entry_ref()
48	\brief Creates an uninitialized entry_ref object.
49*/
50
51
52/*!
53	\fn entry_ref::entry_ref(dev_t dev, ino_t dir, const char* name)
54	\brief Creates an entry_ref object initialized to the given file name in
55		the given directory on the given device.
56
57	\a name may refer to either a pre-existing file in the given directory, or
58	a non-existent file. No explicit checking is done to verify validity of the
59	given arguments, but later use of the entry_ref will fail if \p dev is not
60	a valid device or \a dir is a not a directory on \p dev.
61
62	\param dev the device on which the entry's parent directory resides
63	\param dir the directory in which the entry resides
64	\param name the leaf name of the entry, which is not required to exist
65*/
66
67
68/*!
69	\fn entry_ref::entry_ref(const entry_ref& ref)
70	\brief Returns a copy of the passed in entry_ref object.
71
72	\param ref A reference to an entry_ref to copy.
73*/
74
75
76/*!
77	\fn entry_ref::~entry_ref()
78	\brief Destroys the object and frees the storage allocated for the leaf
79		name, if necessary.
80*/
81
82
83/*!
84	\fn status_t entry_ref::set_name(const char* name)
85	\brief Set the entry_ref's leaf name, freeing the storage allocated for any
86		previous name and then making a copy of the new name.
87
88	\param name Pointer to a null-terminated string containing the new name for
89		the entry. May be \c NULL.
90*/
91
92
93/*!
94	\fn bool entry_ref::operator==(const entry_ref& ref) const
95	\brief Compares the entry_ref object with the passed in entry_ref,
96		returning \c true if they are equal.
97
98	\returns \c true if he entry_refs are equal, \c false otherwise.
99*/
100
101
102/*!
103	\fn bool entry_ref::operator!=(const entry_ref& ref) const
104	\brief Compares the entry_ref object with the passed in entry_ref,
105		returning \c true if they are NOT equal.
106
107	\returns \c true if the entry_refs are NOT equal, \c false otherwise.
108*/
109
110
111/*!
112	\fn entry_ref& entry_ref::operator=(const entry_ref& ref)
113	\brief Makes the entry_ref object a copy of the passed in entry_ref.
114
115	\param ref The entry_ref to copy.
116
117	\returns A pointer to the resulting entry_ref copy.
118*/
119
120
121/*!
122	\var dev_t entry_ref::device
123	\brief The device id of the storage device on which the entry resides
124
125*/
126
127
128/*!
129	\var ino_t entry_ref::directory
130	\brief The inode number of the directory in which the entry resides
131*/
132
133
134/*!
135	\var char *entry_ref::name
136	\brief The leaf name of the entry
137*/
138
139
140/*!
141	\class BEntry
142	\ingroup storage
143	\ingroup libbe
144	\brief A location in the filesystem.
145
146	The BEntry class defines objects that represent "locations" in the file
147	system hierarchy. Each location (or entry) is given as a name within a
148	directory. For example, if you create a BEntry like this:
149
150\code
151BEntry entry("/boot/home/fido");
152\endcode
153
154	the resulting BEntry object represents the location of the file \c fido
155	within the <tt>/boot/home</tt> directory.
156*/
157
158
159/*!
160	\fn BEntry::BEntry()
161	\brief Creates an uninitialized BEntry object.
162
163	Should be followed by a call to one of the SetTo() methods, or an
164	assignment.
165		- SetTo(const BDirectory*, const char*, bool)
166		- SetTo(const entry_ref*, bool)
167		- SetTo(const char*, bool)
168		- operator=(const BEntry&)
169*/
170
171
172/*!
173	\fn BEntry::BEntry(const BDirectory* dir, const char* path, bool traverse)
174	\brief Creates a BEntry initialized to the given directory and path
175		combination.
176
177	If traverse is \c true and \a path refers to a symlink, the BEntry will
178	refer to the linked file; if \c false, the BEntry will refer to the
179	symlink itself.
180
181	\param dir The base directory in which the \a path resides.
182	\param path Relative path based off of \a dir.
183	\param traverse Whether or not to traverse symbolic links.
184
185	\see BEntry::SetTo(const BDirectory*, const char *, bool)
186*/
187
188
189/*!
190	\fn BEntry::BEntry(const entry_ref* ref, bool traverse)
191	\brief Creates a BEntry for the file referred to by the passed in
192		entry_ref.
193
194	If traverse is \c true and \a ref refers to a symlink, the BEntry
195	will refer to the linked file; if \c false, the BEntry will refer
196	to the symlink itself.
197
198	\param ref The entry_ref referring to the given file.
199	\param traverse Whether or not symlinks are to be traversed.
200
201	\sa BEntry::SetTo(const entry_ref*, bool)
202*/
203
204
205/*!
206	\fn BEntry::BEntry(const char* path, bool traverse)
207	\brief Creates a BEntry initialized to the given path.
208
209	If \a path is relative, it will be reckoned off the current working
210	directory. If \a path refers to a symlink and traverse is \c true, the
211	BEntry will refer to the linked file. If traverse is \c false, the
212	BEntry will refer to the symlink itself.
213
214	\param path The path of the file.
215	\param traverse Whether or not symlinks are to be traversed.
216
217	\sa BEntry::SetTo(const char*, bool)
218*/
219
220
221/*!
222	\fn BEntry::BEntry(const BEntry& entry)
223	\brief Creates a copy of the given BEntry.
224
225	\param entry the entry to be copied
226
227	\sa operator=(const BEntry&)
228*/
229
230
231/*!
232	\fn BEntry::~BEntry()
233	\brief Frees all of the allocated resources of the BEntry.
234
235	\sa Unset()
236*/
237
238
239/*!
240	\fn status_t BEntry::InitCheck() const
241	\brief Returns the result of the most recent construction or SetTo() call.
242
243	\returns A status code.
244	\retval B_OK Success
245	\retval B_NO_INIT The object has been Unset() or is uninitialized.
246*/
247
248
249/*!
250	\fn bool BEntry::Exists() const
251	\brief Returns whether or not the entry exists in the filesystem.
252
253	\returns \c true if the entry exists, \c false if the entry does not exist.
254*/
255
256
257/*!
258	\fn status_t BEntry::GetStat(struct stat *result) const
259	\brief Fills in a stat structure for the entry.
260
261	The information is copied into the \c stat structure pointed to
262	by \a result.
263
264	\note The BStatable object does not cache the stat structure -- each time
265		you call GetStat() fresh stat information is retrieved.
266
267	\param result A pointer to a pre-allocated structure into which the stat
268		information is copied.
269
270	\returns \c B_OK on success, or an error code on failure.
271*/
272
273
274/*!
275	\fn status_t BEntry::SetTo(const BDirectory* dir, const char* path,
276		bool traverse)
277	\brief Reinitializes the BEntry to the path or directory path combination,
278		resolving symlinks if traverse is \c true.
279
280	\param dir The base directory in which the \a path resides.
281	\param path Relative path based off of \a dir.
282	\param traverse Whether or not to traverse symbolic links.
283
284	\returns \c B_OK on success, or an error code on failure.
285*/
286
287
288/*!
289	\fn status_t BEntry::SetTo(const entry_ref* ref, bool traverse)
290	\brief Reinitializes the BEntry to the passed in entry_ref object
291		resolving symlinks if traverse is \c true.
292
293	\param ref The entry_ref referring to the given file.
294	\param traverse Whether or not symlinks are to be traversed.
295
296	\returns \c B_OK on success, or an error code on failure.
297
298	\sa BEntry::BEntry(const entry_ref* ref, bool traverse)
299*/
300
301
302/*!
303	\fn status_t BEntry::SetTo(const char* path, bool traverse)
304	\brief Reinitializes the BEntry object to the path, resolving symlinks if
305		traverse is \c true.
306
307	\param path The path of the file.
308	\param traverse Whether or not symlinks are to be traversed.
309
310	\returns \c B_OK on success, or an error code on failure.
311
312	\sa BEntry::BEntry(const char* path, bool traverse)
313*/
314
315
316/*!
317	void BEntry::Unset()
318	\brief Reinitializes the BEntry to an uninitialized BEntry object
319*/
320
321
322/*!
323	\fn status_t BEntry::GetRef(entry_ref* ref) const
324	\brief Gets an entry_ref structure for the BEntry.
325
326	\param ref A pointer to a preallocated entry_ref object into which the
327		result is copied.
328
329	\returns \c B_OK on success, or an error code on failure.
330*/
331
332
333/*!
334	\fn status_t BEntry::GetPath(BPath* path) const
335	\brief Gets the path for the BEntry.
336
337	\param path A pointer to a pre-allocated BPath object into which the
338		result is copied.
339
340	\returns \c B_OK on success, or an error code on failure.
341*/
342
343
344/*!
345	\fn status_t BEntry::GetParent(BEntry* entry) const
346	\brief Gets the parent of the BEntry as a BEntry.
347
348	If the function fails, the argument is Unset(). Destructive calls to
349	GetParent() are allowed, i.e.:
350
351\code
352BEntry entry("/boot/home/fido");
353status_t err;
354char name[B_FILE_NAME_LENGTH];
355
356// Spit out the path components backwards, one at a time.
357do {
358	entry.GetName(name);
359	printf("> %s\n", name);
360} while ((err=entry.GetParent(&entry)) == B_OK);
361
362// Complain for reasons other than reaching the top.
363if (err != B_ENTRY_NOT_FOUND)
364	printf(">> Error: %s\n", strerror(err));
365\endcode
366
367	will output:
368
369\code
370> fido
371> home
372> boot
373> .
374\endcode
375
376	\param entry A pointer to a pre-allocated BEntry object into which the
377		result is stored.
378
379	\returns A status code.
380	\retval B_OK Success
381	\retval B_ENTRY_NOT_FOUND Attempted to get the parent of the root
382		directory.
383*/
384
385
386/*!
387	\fn status_t BEntry::GetParent(BDirectory* dir) const
388	\brief Gets the parent of the BEntry as a BDirectory.
389
390	If the function fails, the argument is Unset().
391
392	\param dir A pointer to a pre-allocated BDirectory object into which the
393		result is copied.
394
395	\returns A status code.
396	\retval B_OK Success
397	\retval B_ENTRY_NOT_FOUND Attempted to get the parent of the root
398		directory.
399*/
400
401
402/*!
403	\fn status_t BEntry::GetName(char* buffer) const
404	\brief Gets the name of the leaf of the BEntry object.
405
406	\c buffer must be pre-allocated and of sufficient length to hold the
407	entire string. A length of \c B_FILE_NAME_LENGTH is recommended.
408
409	\param buffer A pointer to a pre-allocated string into which the result
410		is copied.
411
412	\returns \c B_OK on success, or an error code on failure.
413*/
414
415
416/*!
417	\fn status_t BEntry::Rename(const char* path, bool clobber)
418	\brief Renames the BEntry to \a path replacing an existing entry
419		if \a clobber is \c true.
420
421	\note The BEntry object must refer to an existing file, if it is abstract,
422		this method will fail.
423
424	\param path A pointer to a string containing the new name for the entry.
425		It may be an absolute or relative path. If it is a relative path the
426		entry is renamed within its current directory.
427	\param clobber If \c false and a file with the name given by \c path
428		already exists, the method will fail. If \c true and such a file
429		exists, it will be overwritten.
430
431	\returns A status code.
432	\retval B_OK Success
433	\retval B_ENTRY_EXISTS The new location already exists and \c clobber
434		is \c false.
435	\retval B_ENTRY_NOT_FOUND Attempted to rename an abstract entry.
436*/
437
438
439/*!
440	\fn status_t BEntry::MoveTo(BDirectory* dir, const char* path,
441		bool clobber)
442	\brief Moves the BEntry to directory or directory and path combination,
443		replacing an existing entry if clobber is true.
444
445	\note The BEntry object must refer to an existing file, if it is abstract,
446		this method will fail.
447
448	\param dir A pointer to a pre-allocated BDirectory into which the entry
449		should be moved.
450	\param path (optional) new leaf name for the entry. May be a simple leaf
451		or a relative path; either way, \c path is reckoned off of \c dir. If
452		\c NULL, the entry retains its previous leaf name.
453	\param clobber If \c false and an entry already exists at the specified
454		destination, the method will fail. If \c true and such an entry exists,
455		it will be overwritten.
456
457	\returns A status code.
458	\retval B_OK Success
459	\retval B_ENTRY_EXISTS The new location already exists and \c clobber
460		is \c false.
461	\retval B_ENTRY_NOT_FOUND Attempted to rename an abstract entry.
462*/
463
464
465/*!
466	\fn status_t BEntry::Remove()
467	\brief Removes the entry from the file system.
468
469	\note If any file descriptors are open on the file when Remove() is called
470	the chunk of data they refer to will continue to exist until all such file
471	descriptors are closed. The BEntry object, however, becomes abstract and
472	no longer refers to any actual data in the filesystem.
473
474	\returns \c B_OK on success, or an error code on failure.
475*/
476
477
478/*!
479	\fn bool BEntry::operator==(const BEntry& item) const
480	\brief Returns \c true if the BEntry and \a item refer to the same entry
481		or if they are both uninitialized.
482
483	\retval true Both BEntry objects refer to the same entry or they are
484		both uninitialized.
485	\retval false The BEntry objects refer to different entries.
486*/
487
488
489/*!
490	\fn bool BEntry::operator!=(const BEntry& item) const
491	\brief Returns false if the BEntry and \c item refer to the same entry or
492		if they are both uninitialized.
493
494	\retval true The BEntry objects refer to different entries.
495	\retval false Both BEntry objects refer to the same entry or they are
496		both uninitialized.
497*/
498
499
500/*!
501	\fn BEntry& BEntry::operator=(const BEntry& item)
502	\brief Reinitializes the BEntry to be a copy of \a item.
503
504	\returns A pointer to the copy.
505*/
506
507
508/*!
509	\fn status_t BEntry::set_stat(struct stat& st, uint32 what)
510	\brief Updates the BEntry with the data from the stat structure according
511		to the \a what mask.
512
513	\param st The stat structure to set.
514	\param what A mask
515
516	\returns A status code.
517	\retval B_OK Everything went fine.
518	\retval B_FILE_ERROR There was an error writing to the BEntry object.
519*/
520
521
522/*!
523	\fn status_t BEntry::_SetTo(int dirFD, const char* path, bool traverse)
524	\brief Sets the entry to point to the entry specified by the path \a path
525	relative to the given directory.
526
527	If \a traverse is \c true and the given entry is a symbolic link, the
528	object is recursively set to point to the entry pointed to by the symlink.
529
530	If \a path is an absolute path, \a dirFD is ignored.
531
532	If \a dirFD is -1, \a path is considered relative to the current directory
533	(unless it is an absolute path).
534
535	The ownership of the file descriptor \a dirFD is transferred to the
536	method, regardless of whether it succeeds or fails. The caller must not
537	close the FD afterwards.
538
539	\param dirFD File descriptor of a directory relative to which path is to
540		be considered. May be -1 if the current directory shall be considered.
541	\param path Pointer to a path relative to the given directory.
542	\param traverse If \c true and the given entry is a symbolic link, the
543		object is recursively set to point to the entry linked to by the
544		symbolic link.
545
546	\returns \c B_OK on success, or an error code on failure.
547*/
548
549
550/*!
551	\fn status_t BEntry::_SetName(const char* name)
552	\brief Handles string allocation, deallocation, and copying for the
553		leaf name of the entry.
554
555	\param name The leaf \a name of the entry.
556
557	\returns A status code.
558	\retval B_OK Everything went fine.
559	\retval B_BAD_VALUE \a name is \c NULL.
560	\retval B_NO_MEMORY Ran out of memory trying to allocate \a name.
561*/
562
563
564/*!
565	\fn status_t BEntry::_Rename(BEntry& target, bool clobber)
566	\brief Renames the entry referred to by this object to the location
567		specified by \a target.
568
569	If an entry exists at the target location, the method fails, unless
570	\a clobber is \c true, in which case that entry is overwritten (doesn't
571	work for non-empty directories, though).
572
573	If the operation was successful, this entry is made a clone of the
574	supplied one and the supplied one is uninitialized.
575
576	\param target The entry specifying the target location.
577	\param clobber If \c true, the an entry existing at the target location
578		   will be overwritten.
579
580	\return \c B_OK, if everything went fine, another error code otherwise.
581*/
582
583
584/*!
585	\fn void BEntry::_Dump(const char* name)
586	\brief Debugging function, dumps the given entry to stdout.
587
588	\param name A pointer to a string to be printed along with the dump for
589		identification purposes.
590*/
591
592
593/*!
594	\fn status_t get_ref_for_path(const char* path, entry_ref* ref)
595	\brief Returns an entry_ref for a given path.
596
597	\param path The path name referring to the entry.
598	\param ref The entry_ref structure to be filled in.
599
600	\returns A status code.
601	\retval B_OK Everything went fine.
602	\retval B_BAD_VALUE \c NULL \a path or \a ref.
603	\retval B_ENTRY_NOT_FOUND A (non-leaf) path component does not exist.
604	\retval B_NO_MEMORY Insufficient memory for successful completion.
605*/
606
607
608/*!
609	\fn bool operator<(const entry_ref& a, const entry_ref& b)
610	\brief Returns whether an entry is less than another.
611
612	The components are compared in order \c device, \c directory, \c name.
613	A \c NULL \c name is less than any non-<tt>NULL</tt> name.
614
615	\retval true a < b
616	\retval false a >= b
617*/
618