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