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 void BEntry::Unset() 374 \brief Reinitializes the BEntry to an uninitialized BEntry object 375 376 \since BeOS R3 377*/ 378 379 380/*! 381 \fn status_t BEntry::GetRef(entry_ref* ref) const 382 \brief Gets an entry_ref structure for the BEntry. 383 384 \param ref A pointer to a preallocated entry_ref object into which the 385 result is copied. 386 387 \returns \c B_OK on success, or an error code on failure. 388 389 \since BeOS R3 390*/ 391 392 393/*! 394 \fn status_t BEntry::GetPath(BPath* path) const 395 \brief Gets the path for the BEntry. 396 397 \param path A pointer to a pre-allocated BPath object into which the 398 result is copied. 399 400 \returns \c B_OK on success, or an error code on failure. 401 402 \since BeOS R3 403*/ 404 405 406/*! 407 \fn status_t BEntry::GetParent(BEntry* entry) const 408 \brief Gets the parent of the BEntry as a BEntry. 409 410 If the function fails, the argument is Unset(). Destructive calls to 411 GetParent() are allowed, i.e.: 412 413\code 414BEntry entry("/boot/home/fido"); 415status_t err; 416char name[B_FILE_NAME_LENGTH]; 417 418// Spit out the path components backwards, one at a time. 419do { 420 entry.GetName(name); 421 printf("> %s\n", name); 422} while ((err=entry.GetParent(&entry)) == B_OK); 423 424// Complain for reasons other than reaching the top. 425if (err != B_ENTRY_NOT_FOUND) 426 printf(">> Error: %s\n", strerror(err)); 427\endcode 428 429 will output: 430 431\code 432> fido 433> home 434> boot 435> . 436\endcode 437 438 \param entry A pointer to a pre-allocated BEntry object into which the 439 result is stored. 440 441 \returns A status code. 442 \retval B_OK Success 443 \retval B_ENTRY_NOT_FOUND Attempted to get the parent of the root 444 directory. 445 446 \since BeOS R3 447*/ 448 449 450/*! 451 \fn status_t BEntry::GetParent(BDirectory* dir) const 452 \brief Gets the parent of the BEntry as a BDirectory. 453 454 If the function fails, the argument is Unset(). 455 456 \param dir A pointer to a pre-allocated BDirectory object into which the 457 result is copied. 458 459 \returns A status code. 460 \retval B_OK Success 461 \retval B_ENTRY_NOT_FOUND Attempted to get the parent of the root 462 directory. 463 464 \since BeOS R3 465*/ 466 467 468/*! 469 \fn status_t BEntry::GetName(char* buffer) const 470 \brief Gets the name of the leaf of the BEntry object. 471 472 \c buffer must be pre-allocated and of sufficient length to hold the 473 entire string. A length of \c B_FILE_NAME_LENGTH is recommended. 474 475 \param buffer A pointer to a pre-allocated string into which the result 476 is copied. 477 478 \returns \c B_OK on success, or an error code on failure. 479 480 \since BeOS R3 481*/ 482 483 484/*! 485 \fn status_t BEntry::Rename(const char* path, bool clobber) 486 \brief Renames the BEntry to \a path replacing an existing entry 487 if \a clobber is \c true. 488 489 \note The BEntry object must refer to an existing file, if it is abstract, 490 this method will fail. 491 492 \param path A pointer to a string containing the new name for the entry. 493 It may be an absolute or relative path. If it is a relative path 494 the entry is renamed within its current directory. 495 \param clobber If \c false and a file with the name given by \c path 496 already exists, the method will fail. If \c true and such a file 497 exists, it will be overwritten. 498 499 \returns A status code. 500 \retval B_OK Success 501 \retval B_ENTRY_EXISTS The new location already exists and \c clobber 502 is \c false. 503 \retval B_ENTRY_NOT_FOUND Attempted to rename an abstract entry. 504 505 \since BeOS R3 506*/ 507 508 509/*! 510 \fn status_t BEntry::MoveTo(BDirectory* dir, const char* path, 511 bool clobber) 512 \brief Moves the BEntry to directory or directory and path combination, 513 replacing an existing entry if clobber is true. 514 515 \note The BEntry object must refer to an existing file, if it is abstract, 516 this method will fail. 517 518 \param dir A pointer to a pre-allocated BDirectory into which the entry 519 should be moved. 520 \param path (optional) new leaf name for the entry. May be a simple leaf 521 or a relative path; either way, \c path is reckoned off of \c dir. 522 If \c NULL, the entry retains its previous leaf name. 523 \param clobber If \c false and an entry already exists at the specified 524 Spdestination, the method will fail. If \c true and such an entry 525 exists, it will be overwritten. 526 527 \returns A status code. 528 \retval B_OK Success 529 \retval B_ENTRY_EXISTS The new location already exists and \c clobber 530 is \c false. 531 \retval B_ENTRY_NOT_FOUND Attempted to rename an abstract entry. 532 533 \since BeOS R3 534*/ 535 536 537/*! 538 \fn status_t BEntry::Remove() 539 \brief Removes the entry from the file system. 540 541 \note If any file descriptors are open on the file when Remove() is called 542 the chunk of data they refer to will continue to exist until all such file 543 descriptors are closed. The BEntry object, however, becomes abstract and 544 no longer refers to any actual data in the filesystem. 545 546 \returns \c B_OK on success, or an error code on failure. 547 548 \since BeOS R3 549*/ 550 551 552/*! 553 \fn bool BEntry::operator==(const BEntry& item) const 554 \brief Returns \c true if the BEntry and \a item refer to the same entry 555 or if they are both uninitialized. 556 557 \returns Whether or not the items refer to the same entry. 558 \retval true Both BEntry objects refer to the same entry or they are 559 both uninitialized. 560 \retval false The BEntry objects refer to different entries. 561 562 \since BeOS R3 563*/ 564 565 566/*! 567 \fn bool BEntry::operator!=(const BEntry& item) const 568 \brief Returns false if the BEntry and \c item refer to the same entry or 569 if they are both uninitialized. 570 571 \returns Whether or not the items do NOT refer to the same entry. 572 \retval true The BEntry objects refer to different entries. 573 \retval false Both BEntry objects refer to the same entry or they are 574 both uninitialized. 575 576 \since BeOS R3 577*/ 578 579 580/*! 581 \fn BEntry& BEntry::operator=(const BEntry& item) 582 \brief Reinitializes the BEntry to be a copy of \a item. 583 584 \returns A pointer to the copy. 585 586 \since BeOS R3 587*/ 588 589 590/*! 591 \fn status_t get_ref_for_path(const char* path, entry_ref* ref) 592 \brief Returns an entry_ref for a given path. 593 594 \param path The path name referring to the entry. 595 \param ref The entry_ref structure to be filled in. 596 597 \returns A status code. 598 \retval B_OK Everything went fine. 599 \retval B_BAD_VALUE \c NULL \a path or \a ref. 600 \retval B_ENTRY_NOT_FOUND A (non-leaf) path component does not exist. 601 \retval B_NO_MEMORY Insufficient memory for successful completion. 602 603 \since BeOS R4 604*/ 605 606 607/*! 608 \fn bool operator<(const entry_ref& a, const entry_ref& b) 609 \brief Returns whether an entry is less than another. 610 611 The components are compared in order \c device, \c directory, \c name. 612 A \c NULL \c name is less than any non-<tt>NULL</tt> name. 613 614 \retval true a < b 615 \retval false a >= b 616 617 \since Haiku R1 618*/ 619