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