1 /* 2 * Copyright 2007-2013, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <dirent.h> 8 #include <errno.h> 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <string.h> 12 #include <sys/stat.h> 13 14 #include <FindDirectory.h> 15 #include <fs_info.h> 16 #include <fs_interface.h> 17 #include <KernelExport.h> 18 #include <Mime.h> 19 #include <NodeMonitor.h> 20 #include <TypeConstants.h> 21 22 #include <AutoDeleter.h> 23 #include <util/DoublyLinkedList.h> 24 25 #include "cdda.h" 26 #include "cddb.h" 27 #include "Lock.h" 28 29 30 //#define TRACE_CDDA 31 #ifdef TRACE_CDDA 32 # define TRACE(x) dprintf x 33 #else 34 # define TRACE(x) 35 #endif 36 37 38 class Attribute; 39 class Inode; 40 struct attr_cookie; 41 struct dir_cookie; 42 43 typedef DoublyLinkedList<Attribute> AttributeList; 44 typedef DoublyLinkedList<attr_cookie> AttrCookieList; 45 46 struct riff_header { 47 uint32 magic; 48 uint32 length; 49 uint32 id; 50 } _PACKED; 51 52 struct riff_chunk { 53 uint32 fourcc; 54 uint32 length; 55 } _PACEKD; 56 57 struct wav_format_chunk : riff_chunk { 58 uint16 format_tag; 59 uint16 channels; 60 uint32 samples_per_second; 61 uint32 average_bytes_per_second; 62 uint16 block_align; 63 uint16 bits_per_sample; 64 } _PACKED; 65 66 struct wav_header { 67 riff_header header; 68 wav_format_chunk format; 69 riff_chunk data; 70 } _PACKED; 71 72 enum attr_mode { 73 kDiscIDAttributes, 74 kSharedAttributes, 75 kDeviceAttributes 76 }; 77 78 class Volume { 79 public: 80 Volume(fs_volume* fsVolume); 81 ~Volume(); 82 83 status_t InitCheck(); 84 85 fs_volume* FSVolume() const { return fFSVolume; } 86 dev_t ID() const { return fFSVolume->id; } 87 uint32 DiscID() const { return fDiscID; } 88 Inode& RootNode() const { return *fRootNode; } 89 90 status_t Mount(const char* device); 91 int Device() const { return fDevice; } 92 ino_t GetNextNodeID() { return fNextID++; } 93 94 const char* Name() const { return fName; } 95 status_t SetName(const char* name); 96 97 Semaphore& Lock(); 98 99 Inode* Find(ino_t id); 100 Inode* Find(const char* name); 101 102 Inode* FirstEntry() const { return fFirstEntry; } 103 104 off_t NumBlocks() const { return fNumBlocks; } 105 size_t BufferSize() const { return 32 * kFrameSize; } 106 // TODO: for now 107 108 void SetCDDBLookupsEnabled(bool doLookup); 109 110 static void DetermineName(uint32 cddbId, int device, char* name, 111 size_t length); 112 113 private: 114 Inode* _CreateNode(Inode* parent, const char* name, 115 uint64 start, uint64 frames, int32 type); 116 int _OpenAttributes(int mode, 117 enum attr_mode attrMode = kDiscIDAttributes); 118 void _RestoreAttributes(); 119 void _RestoreAttributes(int fd); 120 void _StoreAttributes(); 121 void _RestoreSharedAttributes(); 122 void _StoreSharedAttributes(); 123 124 Semaphore fLock; 125 fs_volume* fFSVolume; 126 int fDevice; 127 uint32 fDiscID; 128 Inode* fRootNode; 129 ino_t fNextID; 130 char* fName; 131 off_t fNumBlocks; 132 bool fIgnoreCDDBLookupChanges; 133 134 // root directory contents - we don't support other directories 135 Inode* fFirstEntry; 136 }; 137 138 class Attribute : public DoublyLinkedListLinkImpl<Attribute> { 139 public: 140 Attribute(const char* name, type_code type); 141 ~Attribute(); 142 143 status_t InitCheck() const 144 { return fName != NULL ? B_OK : B_NO_MEMORY; } 145 status_t SetTo(const char* name, type_code type); 146 void SetType(type_code type) 147 { fType = type; } 148 149 status_t ReadAt(off_t offset, uint8* buffer, 150 size_t* _length); 151 status_t WriteAt(off_t offset, const uint8* buffer, 152 size_t* _length); 153 void Truncate(); 154 status_t SetSize(off_t size); 155 156 const char* Name() const { return fName; } 157 off_t Size() const { return fSize; } 158 type_code Type() const { return fType; } 159 uint8* Data() const { return fData; } 160 161 bool IsProtectedNamespace(); 162 static bool IsProtectedNamespace(const char* name); 163 164 private: 165 char* fName; 166 type_code fType; 167 uint8* fData; 168 off_t fSize; 169 }; 170 171 class Inode { 172 public: 173 Inode(Volume* volume, Inode* parent, 174 const char* name, uint64 start, uint64 frames, 175 int32 type); 176 ~Inode(); 177 178 status_t InitCheck(); 179 ino_t ID() const { return fID; } 180 181 const char* Name() const { return fName; } 182 status_t SetName(const char* name); 183 184 int32 Type() const 185 { return fType; } 186 gid_t GroupID() const 187 { return fGroupID; } 188 uid_t UserID() const 189 { return fUserID; } 190 time_t CreationTime() const 191 { return fCreationTime; } 192 time_t ModificationTime() const 193 { return fModificationTime; } 194 uint64 StartFrame() const 195 { return fStartFrame; } 196 uint64 FrameCount() const 197 { return fFrameCount; } 198 uint64 Size() const 199 { return fFrameCount * kFrameSize; } 200 // does not include the WAV header 201 202 Attribute* FindAttribute(const char* name) const; 203 status_t AddAttribute(Attribute* attribute, bool overwrite); 204 status_t AddAttribute(const char* name, type_code type, 205 bool overwrite, const uint8* data, 206 size_t length); 207 status_t AddAttribute(const char* name, type_code type, 208 const char* string); 209 status_t AddAttribute(const char* name, type_code type, 210 uint32 value); 211 status_t AddAttribute(const char* name, type_code type, 212 uint64 value); 213 status_t RemoveAttribute(const char* name, 214 bool checkNamespace = false); 215 216 void AddAttrCookie(attr_cookie* cookie); 217 void RemoveAttrCookie(attr_cookie* cookie); 218 void RewindAttrCookie(attr_cookie* cookie); 219 220 AttributeList::ConstIterator Attributes() const 221 { return fAttributes.GetIterator(); } 222 223 const wav_header* WAVHeader() const 224 { return &fWAVHeader; } 225 226 Inode* Next() const { return fNext; } 227 void SetNext(Inode *inode) { fNext = inode; } 228 229 private: 230 Inode* fNext; 231 ino_t fID; 232 int32 fType; 233 char* fName; 234 gid_t fGroupID; 235 uid_t fUserID; 236 time_t fCreationTime; 237 time_t fModificationTime; 238 uint64 fStartFrame; 239 uint64 fFrameCount; 240 AttributeList fAttributes; 241 AttrCookieList fAttrCookies; 242 wav_header fWAVHeader; 243 }; 244 245 struct dir_cookie { 246 Inode* current; 247 int state; // iteration state 248 }; 249 250 // directory iteration states 251 enum { 252 ITERATION_STATE_DOT = 0, 253 ITERATION_STATE_DOT_DOT = 1, 254 ITERATION_STATE_OTHERS = 2, 255 ITERATION_STATE_BEGIN = ITERATION_STATE_DOT, 256 }; 257 258 struct attr_cookie : DoublyLinkedListLinkImpl<attr_cookie> { 259 Attribute* current; 260 }; 261 262 struct file_cookie { 263 int open_mode; 264 off_t buffer_offset; 265 void* buffer; 266 }; 267 268 static const uint32 kMaxAttributeSize = 65536; 269 static const uint32 kMaxAttributes = 64; 270 271 static const char* kProtectedAttrNamespace = "CD:"; 272 273 static const char* kCddbIdAttribute = "CD:cddbid"; 274 static const char* kDoLookupAttribute = "CD:do_lookup"; 275 static const char* kTocAttribute = "CD:toc"; 276 277 extern fs_volume_ops gCDDAVolumeOps; 278 extern fs_vnode_ops gCDDAVnodeOps; 279 280 281 // #pragma mark helper functions 282 283 284 /*! Determines if the attribute is shared among all devices or among 285 all CDs in a specific device. 286 We use this to share certain Tracker attributes. 287 */ 288 static bool 289 is_special_attribute(const char* name, attr_mode attrMode) 290 { 291 if (attrMode == kDeviceAttributes) { 292 static const char* kAttributes[] = { 293 "_trk/windframe", 294 "_trk/pinfo", 295 "_trk/pinfo_le", 296 NULL, 297 }; 298 299 for (int32 i = 0; kAttributes[i]; i++) { 300 if (!strcmp(name, kAttributes[i])) 301 return true; 302 } 303 } else if (attrMode == kSharedAttributes) { 304 static const char* kAttributes[] = { 305 "_trk/columns", 306 "_trk/columns_le", 307 "_trk/viewstate", 308 "_trk/viewstate_le", 309 NULL, 310 }; 311 312 for (int32 i = 0; kAttributes[i]; i++) { 313 if (!strcmp(name, kAttributes[i])) 314 return true; 315 } 316 } 317 318 return false; 319 } 320 321 322 static void 323 write_line(int fd, const char* line) 324 { 325 if (line == NULL) 326 line = ""; 327 328 size_t length = strlen(line); 329 write(fd, line, length); 330 write(fd, "\n", 1); 331 } 332 333 334 static void 335 write_attributes(int fd, Inode* inode, attr_mode attrMode = kDiscIDAttributes) 336 { 337 // count attributes 338 339 AttributeList::ConstIterator iterator = inode->Attributes(); 340 uint32 count = 0; 341 while (iterator.HasNext()) { 342 Attribute* attribute = iterator.Next(); 343 if ((attrMode == kDiscIDAttributes 344 || is_special_attribute(attribute->Name(), attrMode)) 345 && !attribute->IsProtectedNamespace()) 346 count++; 347 } 348 349 // we're artificially limiting the attribute count per inode 350 if (count > kMaxAttributes) 351 count = kMaxAttributes; 352 353 count = B_HOST_TO_BENDIAN_INT32(count); 354 write(fd, &count, sizeof(uint32)); 355 356 // write attributes 357 358 iterator.Rewind(); 359 360 while (iterator.HasNext()) { 361 Attribute* attribute = iterator.Next(); 362 if ((attrMode != kDiscIDAttributes 363 && !is_special_attribute(attribute->Name(), attrMode)) 364 || attribute->IsProtectedNamespace()) 365 continue; 366 367 uint32 type = B_HOST_TO_BENDIAN_INT32(attribute->Type()); 368 write(fd, &type, sizeof(uint32)); 369 370 uint8 length = strlen(attribute->Name()); 371 write(fd, &length, 1); 372 write(fd, attribute->Name(), length); 373 374 uint32 size = B_HOST_TO_BENDIAN_INT32(attribute->Size()); 375 write(fd, &size, sizeof(uint32)); 376 if (size != 0) 377 write(fd, attribute->Data(), attribute->Size()); 378 379 if (--count == 0) 380 break; 381 } 382 } 383 384 385 static bool 386 read_line(int fd, char* line, size_t length) 387 { 388 bool first = true; 389 size_t pos = 0; 390 char c; 391 392 while (read(fd, &c, 1) == 1) { 393 first = false; 394 395 if (c == '\n') 396 break; 397 if (pos < length) 398 line[pos] = c; 399 400 pos++; 401 } 402 403 if (pos >= length - 1) 404 pos = length - 1; 405 line[pos] = '\0'; 406 407 return !first; 408 } 409 410 411 static bool 412 read_attributes(int fd, Inode* inode) 413 { 414 uint32 count; 415 if (read(fd, &count, sizeof(uint32)) != (ssize_t)sizeof(uint32)) 416 return false; 417 418 count = B_BENDIAN_TO_HOST_INT32(count); 419 if (count > kMaxAttributes) 420 return false; 421 422 for (uint32 i = 0; i < count; i++) { 423 char name[B_ATTR_NAME_LENGTH + 1]; 424 uint32 type, size; 425 uint8 length; 426 if (read(fd, &type, sizeof(uint32)) != (ssize_t)sizeof(uint32) 427 || read(fd, &length, 1) != 1 428 || read(fd, name, length) != length 429 || read(fd, &size, sizeof(uint32)) != (ssize_t)sizeof(uint32)) 430 return false; 431 432 type = B_BENDIAN_TO_HOST_INT32(type); 433 size = B_BENDIAN_TO_HOST_INT32(size); 434 name[length] = '\0'; 435 436 Attribute* attribute = new(std::nothrow) Attribute(name, type); 437 if (attribute == NULL) 438 return false; 439 440 if (attribute->IsProtectedNamespace()) { 441 // Attributes in the protected namespace are handled internally 442 // so we do not load them even if they are present in the 443 // attributes file. 444 delete attribute; 445 continue; 446 } 447 if (attribute->SetSize(size) != B_OK 448 || inode->AddAttribute(attribute, true) != B_OK) { 449 delete attribute; 450 } else 451 read(fd, attribute->Data(), size); 452 } 453 454 return true; 455 } 456 457 458 static int 459 open_attributes(uint32 cddbID, int deviceFD, int mode, 460 enum attr_mode attrMode) 461 { 462 char* path = (char*)malloc(B_PATH_NAME_LENGTH); 463 if (path == NULL) 464 return -1; 465 466 MemoryDeleter deleter(path); 467 bool create = (mode & O_WRONLY) != 0; 468 469 if (find_directory(B_USER_SETTINGS_DIRECTORY, -1, create, path, 470 B_PATH_NAME_LENGTH) != B_OK) { 471 return -1; 472 } 473 474 strlcat(path, "/cdda", B_PATH_NAME_LENGTH); 475 if (create) 476 mkdir(path, 0755); 477 478 if (attrMode == kDiscIDAttributes) { 479 char id[64]; 480 snprintf(id, sizeof(id), "/%08" B_PRIx32, cddbID); 481 strlcat(path, id, B_PATH_NAME_LENGTH); 482 } else if (attrMode == kDeviceAttributes) { 483 uint32 length = strlen(path); 484 char* deviceName = path + length; 485 if (ioctl(deviceFD, B_GET_PATH_FOR_DEVICE, deviceName, 486 B_PATH_NAME_LENGTH - length) < B_OK) { 487 return B_ERROR; 488 } 489 490 deviceName++; 491 492 // replace slashes in the device path 493 while (deviceName[0]) { 494 if (deviceName[0] == '/') 495 deviceName[0] = '_'; 496 497 deviceName++; 498 } 499 } else 500 strlcat(path, "/shared", B_PATH_NAME_LENGTH); 501 502 return open(path, mode | (create ? O_CREAT | O_TRUNC : 0), 0644); 503 } 504 505 506 static void 507 fill_stat_buffer(Volume* volume, Inode* inode, Attribute* attribute, 508 struct stat& stat) 509 { 510 stat.st_dev = volume->FSVolume()->id; 511 stat.st_ino = inode->ID(); 512 513 if (attribute != NULL) { 514 stat.st_size = attribute->Size(); 515 stat.st_blocks = 0; 516 stat.st_mode = S_ATTR | 0666; 517 stat.st_type = attribute->Type(); 518 } else { 519 stat.st_size = inode->Size() + sizeof(wav_header); 520 stat.st_blocks = inode->Size() / 512; 521 stat.st_mode = inode->Type(); 522 stat.st_type = 0; 523 } 524 525 stat.st_nlink = 1; 526 stat.st_blksize = 2048; 527 528 stat.st_uid = inode->UserID(); 529 stat.st_gid = inode->GroupID(); 530 531 stat.st_atim.tv_sec = time(NULL); 532 stat.st_atim.tv_nsec = 0; 533 stat.st_mtim.tv_sec = stat.st_ctim.tv_sec = inode->ModificationTime(); 534 stat.st_ctim.tv_nsec = stat.st_mtim.tv_nsec = 0; 535 stat.st_crtim.tv_sec = inode->CreationTime(); 536 stat.st_crtim.tv_nsec = 0; 537 } 538 539 540 bool 541 is_data_track(const scsi_toc_track& track) 542 { 543 return (track.control & 4) != 0; 544 } 545 546 547 uint32 548 count_audio_tracks(scsi_toc_toc* toc) 549 { 550 uint32 trackCount = toc->last_track + 1 - toc->first_track; 551 uint32 count = 0; 552 for (uint32 i = 0; i < trackCount; i++) { 553 if (!is_data_track(toc->tracks[i])) 554 count++; 555 } 556 557 return count; 558 } 559 560 561 // #pragma mark - Volume class 562 563 564 Volume::Volume(fs_volume* fsVolume) 565 : 566 fLock("cdda"), 567 fFSVolume(fsVolume), 568 fDevice(-1), 569 fRootNode(NULL), 570 fNextID(1), 571 fName(NULL), 572 fNumBlocks(0), 573 fIgnoreCDDBLookupChanges(false), 574 fFirstEntry(NULL) 575 { 576 } 577 578 579 Volume::~Volume() 580 { 581 if (fRootNode) { 582 _StoreAttributes(); 583 _StoreSharedAttributes(); 584 } 585 586 if (fDevice >= 0) 587 close(fDevice); 588 589 // put_vnode on the root to release the ref to it 590 if (fRootNode) 591 put_vnode(FSVolume(), fRootNode->ID()); 592 593 delete fRootNode; 594 595 Inode* inode; 596 Inode* next; 597 598 for (inode = fFirstEntry; inode != NULL; inode = next) { 599 next = inode->Next(); 600 delete inode; 601 } 602 603 free(fName); 604 } 605 606 607 status_t 608 Volume::InitCheck() 609 { 610 if (fLock.InitCheck() < B_OK) 611 return B_ERROR; 612 613 return B_OK; 614 } 615 616 617 status_t 618 Volume::Mount(const char* device) 619 { 620 fDevice = open(device, O_RDONLY); 621 if (fDevice < 0) 622 return errno; 623 624 scsi_toc_toc* toc = (scsi_toc_toc*)malloc(1024); 625 if (toc == NULL) 626 return B_NO_MEMORY; 627 628 MemoryDeleter deleter(toc); 629 630 status_t status = read_table_of_contents(fDevice, toc, 1024); 631 // there has to be at least one audio track 632 if (status == B_OK && count_audio_tracks(toc) == 0) 633 status = B_BAD_TYPE; 634 635 if (status != B_OK) 636 return status; 637 638 fDiscID = compute_cddb_disc_id(*toc); 639 640 // create the root vnode 641 fRootNode = _CreateNode(NULL, "", 0, 0, S_IFDIR | 0777); 642 if (fRootNode == NULL) 643 status = B_NO_MEMORY; 644 if (status == B_OK) { 645 status = publish_vnode(FSVolume(), fRootNode->ID(), fRootNode, 646 &gCDDAVnodeOps, fRootNode->Type(), 0); 647 } 648 if (status != B_OK) 649 return status; 650 651 bool doLookup = true; 652 cdtext text; 653 int fd = _OpenAttributes(O_RDONLY); 654 if (fd < 0) { 655 // We do not seem to have an attribute file so this is probably the 656 // first time this CD is inserted. In this case, try to read CD-Text 657 // data. 658 if (read_cdtext(fDevice, text) == B_OK) 659 doLookup = false; 660 else 661 TRACE(("CDDA: no CD-Text found.\n")); 662 } else { 663 doLookup = false; 664 } 665 666 int32 trackCount = toc->last_track + 1 - toc->first_track; 667 off_t totalFrames = 0; 668 char title[256]; 669 670 for (int32 i = 0; i < trackCount; i++) { 671 scsi_cd_msf& next = toc->tracks[i + 1].start.time; 672 // the last track is always lead-out 673 scsi_cd_msf& start = toc->tracks[i].start.time; 674 int32 track = i + 1; 675 676 uint64 startFrame = start.minute * kFramesPerMinute 677 + start.second * kFramesPerSecond + start.frame; 678 uint64 frames = next.minute * kFramesPerMinute 679 + next.second * kFramesPerSecond + next.frame 680 - startFrame; 681 682 // Adjust length of the last audio track according to the Blue Book 683 // specification in case of an Enhanced CD 684 if (i + 1 < trackCount && is_data_track(toc->tracks[i + 1]) 685 && !is_data_track(toc->tracks[i])) 686 frames -= kDataTrackLeadGap; 687 688 totalFrames += frames; 689 690 if (is_data_track(toc->tracks[i])) 691 continue; 692 693 if (text.titles[i] != NULL) { 694 if (text.artists[i] != NULL) { 695 snprintf(title, sizeof(title), "%02" B_PRId32 ". %s - %s.wav", 696 track, text.artists[i], text.titles[i]); 697 } else { 698 snprintf(title, sizeof(title), "%02" B_PRId32 ". %s.wav", 699 track, text.titles[i]); 700 } 701 } else 702 snprintf(title, sizeof(title), "Track %02" B_PRId32 ".wav", track); 703 704 // remove '/' and '\n' from title 705 for (int32 j = 0; title[j]; j++) { 706 if (title[j] == '/') 707 title[j] = '-'; 708 else if (title[j] == '\n') 709 title[j] = ' '; 710 } 711 712 Inode* inode = _CreateNode(fRootNode, title, startFrame, frames, 713 S_IFREG | 0444); 714 if (inode == NULL) 715 continue; 716 717 // add attributes 718 719 inode->AddAttribute("Audio:Artist", B_STRING_TYPE, 720 text.artists[i] != NULL ? text.artists[i] : text.artist); 721 inode->AddAttribute("Audio:Album", B_STRING_TYPE, text.album); 722 inode->AddAttribute("Audio:Title", B_STRING_TYPE, text.titles[i]); 723 inode->AddAttribute("Audio:Genre", B_STRING_TYPE, text.genre); 724 inode->AddAttribute("Audio:Track", B_INT32_TYPE, (uint32)track); 725 inode->AddAttribute("Audio:Bitrate", B_STRING_TYPE, "1411 kbps"); 726 inode->AddAttribute("Media:Length", B_INT64_TYPE, 727 inode->FrameCount() * 1000000L / kFramesPerSecond); 728 inode->AddAttribute("BEOS:TYPE", B_MIME_STRING_TYPE, "audio/x-wav"); 729 } 730 731 // Add CD:cddbid attribute. 732 fRootNode->AddAttribute(kCddbIdAttribute, B_UINT32_TYPE, fDiscID); 733 734 // Add CD:do_lookup attribute. 735 SetCDDBLookupsEnabled(true); 736 737 // Add CD:toc attribute. 738 fRootNode->AddAttribute(kTocAttribute, B_RAW_TYPE, true, 739 (const uint8*)toc, B_BENDIAN_TO_HOST_INT16(toc->data_length) + 2); 740 741 _RestoreSharedAttributes(); 742 if (fd >= 0) { 743 _RestoreAttributes(fd); 744 close(fd); 745 } 746 747 // determine volume title 748 DetermineName(fDiscID, fDevice, title, sizeof(title)); 749 750 fName = strdup(title); 751 if (fName == NULL) 752 return B_NO_MEMORY; 753 754 fNumBlocks = totalFrames; 755 return B_OK; 756 } 757 758 759 status_t 760 Volume::SetName(const char* name) 761 { 762 if (name == NULL || !name[0]) 763 return B_BAD_VALUE; 764 765 name = strdup(name); 766 if (name == NULL) 767 return B_NO_MEMORY; 768 769 free(fName); 770 fName = (char*)name; 771 return B_OK; 772 } 773 774 775 Semaphore& 776 Volume::Lock() 777 { 778 return fLock; 779 } 780 781 782 Inode* 783 Volume::Find(ino_t id) 784 { 785 for (Inode* inode = fFirstEntry; inode != NULL; inode = inode->Next()) { 786 if (inode->ID() == id) 787 return inode; 788 } 789 790 return NULL; 791 } 792 793 794 Inode* 795 Volume::Find(const char* name) 796 { 797 if (!strcmp(name, ".") 798 || !strcmp(name, "..")) 799 return fRootNode; 800 801 for (Inode* inode = fFirstEntry; inode != NULL; inode = inode->Next()) { 802 if (!strcmp(inode->Name(), name)) 803 return inode; 804 } 805 806 return NULL; 807 } 808 809 810 void 811 Volume::SetCDDBLookupsEnabled(bool doLookup) 812 { 813 if (!fIgnoreCDDBLookupChanges) { 814 fRootNode->AddAttribute(kDoLookupAttribute, B_BOOL_TYPE, true, 815 (const uint8*)&doLookup, sizeof(bool)); 816 } 817 } 818 819 820 /*static*/ void 821 Volume::DetermineName(uint32 cddbID, int device, char* name, size_t length) 822 { 823 name[0] = '\0'; 824 825 int attrFD = open_attributes(cddbID, device, O_RDONLY, 826 kDiscIDAttributes); 827 if (attrFD < 0) { 828 // We do not have attributes set. Read CD text. 829 cdtext text; 830 if (read_cdtext(device, text) == B_OK) { 831 if (text.artist != NULL && text.album != NULL) 832 snprintf(name, length, "%s - %s", text.artist, text.album); 833 else if (text.artist != NULL || text.album != NULL) { 834 snprintf(name, length, "%s", text.artist != NULL 835 ? text.artist : text.album); 836 } 837 } 838 } else { 839 // We have an attribute file. Read name from it. 840 if (!read_line(attrFD, name, length)) 841 name[0] = '\0'; 842 843 close(attrFD); 844 } 845 846 if (!name[0]) 847 strlcpy(name, "Audio CD", length); 848 } 849 850 851 Inode* 852 Volume::_CreateNode(Inode* parent, const char* name, uint64 start, 853 uint64 frames, int32 type) 854 { 855 Inode* inode = new(std::nothrow) Inode(this, parent, name, start, frames, 856 type); 857 if (inode == NULL) 858 return NULL; 859 860 if (inode->InitCheck() != B_OK) { 861 delete inode; 862 return NULL; 863 } 864 865 if (S_ISREG(type)) { 866 // we need to order it by track for compatibility with BeOS' cdda 867 Inode* current = fFirstEntry; 868 Inode* last = NULL; 869 while (current != NULL) { 870 last = current; 871 current = current->Next(); 872 } 873 874 if (last) 875 last->SetNext(inode); 876 else 877 fFirstEntry = inode; 878 } 879 880 return inode; 881 } 882 883 884 /*! Opens the file that contains the volume and inode titles as well as all 885 of their attributes. 886 The attributes are stored in files below B_USER_SETTINGS_DIRECTORY/cdda. 887 */ 888 int 889 Volume::_OpenAttributes(int mode, enum attr_mode attrMode) 890 { 891 return open_attributes(fDiscID, fDevice, mode, attrMode); 892 } 893 894 895 /*! Reads the attributes, if any, that belong to the CD currently being 896 mounted. 897 */ 898 void 899 Volume::_RestoreAttributes() 900 { 901 int fd = _OpenAttributes(O_RDONLY); 902 if (fd < 0) 903 return; 904 905 _RestoreAttributes(fd); 906 907 close(fd); 908 } 909 910 911 void 912 Volume::_RestoreAttributes(int fd) 913 { 914 char line[B_FILE_NAME_LENGTH]; 915 if (!read_line(fd, line, B_FILE_NAME_LENGTH)) 916 return; 917 918 SetName(line); 919 920 for (Inode* inode = fFirstEntry; inode != NULL; inode = inode->Next()) { 921 if (!read_line(fd, line, B_FILE_NAME_LENGTH)) 922 break; 923 924 inode->SetName(line); 925 } 926 927 if (read_attributes(fd, fRootNode)) { 928 for (Inode* inode = fFirstEntry; inode != NULL; inode = inode->Next()) { 929 if (!read_attributes(fd, inode)) 930 break; 931 } 932 } 933 } 934 935 936 void 937 Volume::_StoreAttributes() 938 { 939 int fd = _OpenAttributes(O_WRONLY); 940 if (fd < 0) 941 return; 942 943 write_line(fd, Name()); 944 945 for (Inode* inode = fFirstEntry; inode != NULL; inode = inode->Next()) { 946 write_line(fd, inode->Name()); 947 } 948 949 write_attributes(fd, fRootNode); 950 951 for (Inode* inode = fFirstEntry; inode != NULL; inode = inode->Next()) { 952 write_attributes(fd, inode); 953 } 954 955 close(fd); 956 } 957 958 959 /*! Restores the attributes, if any, that are shared between CDs; some are 960 stored per device, others are stored for all CDs no matter which device. 961 */ 962 void 963 Volume::_RestoreSharedAttributes() 964 { 965 // Don't affect CDDB lookup status while changing shared attributes 966 fIgnoreCDDBLookupChanges = true; 967 968 // device attributes overwrite shared attributes 969 int fd = _OpenAttributes(O_RDONLY, kSharedAttributes); 970 if (fd >= 0) { 971 read_attributes(fd, fRootNode); 972 close(fd); 973 } 974 975 fd = _OpenAttributes(O_RDONLY, kDeviceAttributes); 976 if (fd >= 0) { 977 read_attributes(fd, fRootNode); 978 close(fd); 979 } 980 981 fIgnoreCDDBLookupChanges = false; 982 } 983 984 985 void 986 Volume::_StoreSharedAttributes() 987 { 988 // write shared and device specific settings 989 990 int fd = _OpenAttributes(O_WRONLY, kSharedAttributes); 991 if (fd >= 0) { 992 write_attributes(fd, fRootNode, kSharedAttributes); 993 close(fd); 994 } 995 996 fd = _OpenAttributes(O_WRONLY, kDeviceAttributes); 997 if (fd >= 0) { 998 write_attributes(fd, fRootNode, kDeviceAttributes); 999 close(fd); 1000 } 1001 } 1002 1003 1004 // #pragma mark - Attribute class 1005 1006 1007 Attribute::Attribute(const char* name, type_code type) 1008 : 1009 fName(NULL), 1010 fType(0), 1011 fData(NULL), 1012 fSize(0) 1013 { 1014 SetTo(name, type); 1015 } 1016 1017 1018 Attribute::~Attribute() 1019 { 1020 free(fName); 1021 free(fData); 1022 } 1023 1024 1025 status_t 1026 Attribute::SetTo(const char* name, type_code type) 1027 { 1028 if (name == NULL || !name[0]) 1029 return B_BAD_VALUE; 1030 1031 name = strdup(name); 1032 if (name == NULL) 1033 return B_NO_MEMORY; 1034 1035 free(fName); 1036 1037 fName = (char*)name; 1038 fType = type; 1039 return B_OK; 1040 } 1041 1042 1043 status_t 1044 Attribute::ReadAt(off_t offset, uint8* buffer, size_t* _length) 1045 { 1046 size_t length = *_length; 1047 1048 if (offset < 0) 1049 return B_BAD_VALUE; 1050 if (offset >= fSize) { 1051 *_length = 0; 1052 return B_OK; 1053 } 1054 if (offset + (off_t)length > fSize) 1055 length = fSize - offset; 1056 1057 if (user_memcpy(buffer, fData + offset, length) < B_OK) 1058 return B_BAD_ADDRESS; 1059 1060 *_length = length; 1061 return B_OK; 1062 } 1063 1064 1065 /*! Writes to the attribute and enlarges it as needed. 1066 An attribute has a maximum size of 65536 bytes for now. 1067 */ 1068 status_t 1069 Attribute::WriteAt(off_t offset, const uint8* buffer, size_t* _length) 1070 { 1071 size_t length = *_length; 1072 1073 if (offset < 0) 1074 return B_BAD_VALUE; 1075 1076 // we limit the attribute size to something reasonable 1077 off_t end = offset + length; 1078 if (end > kMaxAttributeSize) { 1079 end = kMaxAttributeSize; 1080 length = end - offset; 1081 } 1082 if (offset > end) { 1083 *_length = 0; 1084 return E2BIG; 1085 } 1086 1087 if (end > fSize) { 1088 // make room in the data stream 1089 uint8* data = (uint8*)realloc(fData, end); 1090 if (data == NULL) 1091 return B_NO_MEMORY; 1092 1093 if (fSize < offset) 1094 memset(data + fSize, 0, offset - fSize); 1095 1096 fData = data; 1097 fSize = end; 1098 } 1099 1100 if (user_memcpy(fData + offset, buffer, length) < B_OK) 1101 return B_BAD_ADDRESS; 1102 1103 *_length = length; 1104 return B_OK; 1105 } 1106 1107 1108 //! Removes all data from the attribute. 1109 void 1110 Attribute::Truncate() 1111 { 1112 free(fData); 1113 fData = NULL; 1114 fSize = 0; 1115 } 1116 1117 1118 /*! Resizes the data part of an attribute to the requested amount \a size. 1119 An attribute has a maximum size of 65536 bytes for now. 1120 */ 1121 status_t 1122 Attribute::SetSize(off_t size) 1123 { 1124 if (size > kMaxAttributeSize) 1125 return E2BIG; 1126 1127 uint8* data = (uint8*)realloc(fData, size); 1128 if (data == NULL) 1129 return B_NO_MEMORY; 1130 1131 if (fSize < size) 1132 memset(data + fSize, 0, size - fSize); 1133 1134 fData = data; 1135 fSize = size; 1136 return B_OK; 1137 } 1138 1139 1140 bool 1141 Attribute::IsProtectedNamespace() 1142 { 1143 // Check if the attribute is in the restricted namespace. Attributes in 1144 // this namespace should not be edited by the user as they are handled 1145 // internally by the add-on. Calls the static version. 1146 return IsProtectedNamespace(fName); 1147 } 1148 1149 1150 bool 1151 Attribute::IsProtectedNamespace(const char* name) 1152 { 1153 // Convenience static version of the above method. Usually called when we 1154 // don't have a constructed Attribute object handy. 1155 return strncmp(kProtectedAttrNamespace, name, 1156 strlen(kProtectedAttrNamespace)) == 0; 1157 } 1158 1159 1160 // #pragma mark - Inode class 1161 1162 1163 Inode::Inode(Volume* volume, Inode* parent, const char* name, uint64 start, 1164 uint64 frames, int32 type) 1165 : 1166 fNext(NULL) 1167 { 1168 fName = strdup(name); 1169 if (fName == NULL) 1170 return; 1171 1172 fID = volume->GetNextNodeID(); 1173 fType = type; 1174 fStartFrame = start; 1175 fFrameCount = frames; 1176 1177 fUserID = geteuid(); 1178 fGroupID = parent ? parent->GroupID() : getegid(); 1179 1180 fCreationTime = fModificationTime = time(NULL); 1181 1182 if (frames) { 1183 // initialize WAV header 1184 1185 // RIFF header 1186 fWAVHeader.header.magic = B_HOST_TO_BENDIAN_INT32('RIFF'); 1187 fWAVHeader.header.length = B_HOST_TO_LENDIAN_INT32(Size() 1188 + sizeof(wav_header) - sizeof(riff_chunk)); 1189 fWAVHeader.header.id = B_HOST_TO_BENDIAN_INT32('WAVE'); 1190 1191 // 'fmt ' format chunk 1192 fWAVHeader.format.fourcc = B_HOST_TO_BENDIAN_INT32('fmt '); 1193 fWAVHeader.format.length = B_HOST_TO_LENDIAN_INT32( 1194 sizeof(wav_format_chunk) - sizeof(riff_chunk)); 1195 fWAVHeader.format.format_tag = B_HOST_TO_LENDIAN_INT16(1); 1196 fWAVHeader.format.channels = B_HOST_TO_LENDIAN_INT16(2); 1197 fWAVHeader.format.samples_per_second = B_HOST_TO_LENDIAN_INT32(44100); 1198 fWAVHeader.format.average_bytes_per_second = B_HOST_TO_LENDIAN_INT32( 1199 44100 * sizeof(uint16) * 2); 1200 fWAVHeader.format.block_align = B_HOST_TO_LENDIAN_INT16(4); 1201 fWAVHeader.format.bits_per_sample = B_HOST_TO_LENDIAN_INT16(16); 1202 1203 // 'data' chunk 1204 fWAVHeader.data.fourcc = B_HOST_TO_BENDIAN_INT32('data'); 1205 fWAVHeader.data.length = B_HOST_TO_LENDIAN_INT32(Size()); 1206 } 1207 } 1208 1209 1210 Inode::~Inode() 1211 { 1212 free(const_cast<char*>(fName)); 1213 } 1214 1215 1216 status_t 1217 Inode::InitCheck() 1218 { 1219 if (fName == NULL) 1220 return B_NO_MEMORY; 1221 1222 return B_OK; 1223 } 1224 1225 1226 status_t 1227 Inode::SetName(const char* name) 1228 { 1229 if (name == NULL || !name[0] 1230 || strchr(name, '/') != NULL 1231 || strchr(name, '\n') != NULL) 1232 return B_BAD_VALUE; 1233 1234 name = strdup(name); 1235 if (name == NULL) 1236 return B_NO_MEMORY; 1237 1238 free(fName); 1239 fName = (char*)name; 1240 return B_OK; 1241 } 1242 1243 1244 Attribute* 1245 Inode::FindAttribute(const char* name) const 1246 { 1247 if (name == NULL || !name[0]) 1248 return NULL; 1249 1250 AttributeList::ConstIterator iterator = fAttributes.GetIterator(); 1251 1252 while (iterator.HasNext()) { 1253 Attribute* attribute = iterator.Next(); 1254 if (!strcmp(attribute->Name(), name)) 1255 return attribute; 1256 } 1257 1258 return NULL; 1259 } 1260 1261 1262 status_t 1263 Inode::AddAttribute(Attribute* attribute, bool overwrite) 1264 { 1265 Attribute* oldAttribute = FindAttribute(attribute->Name()); 1266 if (oldAttribute != NULL) { 1267 if (!overwrite) 1268 return B_NAME_IN_USE; 1269 1270 fAttributes.Remove(oldAttribute); 1271 delete oldAttribute; 1272 } 1273 1274 fAttributes.Add(attribute); 1275 return B_OK; 1276 } 1277 1278 1279 status_t 1280 Inode::AddAttribute(const char* name, type_code type, bool overwrite, 1281 const uint8* data, size_t length) 1282 { 1283 Attribute* attribute = new(std::nothrow) Attribute(name, type); 1284 if (attribute == NULL) 1285 return B_NO_MEMORY; 1286 1287 status_t status = attribute->InitCheck(); 1288 if (status == B_OK && data != NULL && length != 0) 1289 status = attribute->WriteAt(0, data, &length); 1290 if (status == B_OK) 1291 status = AddAttribute(attribute, overwrite); 1292 if (status != B_OK) { 1293 delete attribute; 1294 return status; 1295 } 1296 1297 return B_OK; 1298 } 1299 1300 1301 status_t 1302 Inode::AddAttribute(const char* name, type_code type, const char* string) 1303 { 1304 if (string == NULL) 1305 return B_BAD_VALUE; 1306 1307 return AddAttribute(name, type, true, (const uint8*)string, 1308 strlen(string)); 1309 } 1310 1311 1312 status_t 1313 Inode::AddAttribute(const char* name, type_code type, uint32 value) 1314 { 1315 uint32 data = B_HOST_TO_LENDIAN_INT32(value); 1316 return AddAttribute(name, type, true, (const uint8*)&data, sizeof(uint32)); 1317 } 1318 1319 1320 status_t 1321 Inode::AddAttribute(const char* name, type_code type, uint64 value) 1322 { 1323 uint64 data = B_HOST_TO_LENDIAN_INT64(value); 1324 return AddAttribute(name, type, true, (const uint8*)&data, sizeof(uint64)); 1325 } 1326 1327 1328 status_t 1329 Inode::RemoveAttribute(const char* name, bool checkNamespace) 1330 { 1331 if (name == NULL || !name[0]) 1332 return B_ENTRY_NOT_FOUND; 1333 1334 AttributeList::Iterator iterator = fAttributes.GetIterator(); 1335 1336 while (iterator.HasNext()) { 1337 Attribute* attribute = iterator.Next(); 1338 if (!strcmp(attribute->Name(), name)) { 1339 // check for restricted namespace if required. 1340 if (checkNamespace && attribute->IsProtectedNamespace()) 1341 return B_NOT_ALLOWED; 1342 // look for attribute in cookies 1343 AttrCookieList::Iterator i = fAttrCookies.GetIterator(); 1344 while (i.HasNext()) { 1345 attr_cookie* cookie = i.Next(); 1346 if (cookie->current == attribute) { 1347 cookie->current 1348 = attribute->GetDoublyLinkedListLink()->next; 1349 } 1350 } 1351 1352 iterator.Remove(); 1353 delete attribute; 1354 return B_OK; 1355 } 1356 } 1357 1358 return B_ENTRY_NOT_FOUND; 1359 } 1360 1361 1362 void 1363 Inode::AddAttrCookie(attr_cookie* cookie) 1364 { 1365 fAttrCookies.Add(cookie); 1366 RewindAttrCookie(cookie); 1367 } 1368 1369 1370 void 1371 Inode::RemoveAttrCookie(attr_cookie* cookie) 1372 { 1373 fAttrCookies.Remove(cookie); 1374 } 1375 1376 1377 void 1378 Inode::RewindAttrCookie(attr_cookie* cookie) 1379 { 1380 cookie->current = fAttributes.First(); 1381 } 1382 1383 1384 // #pragma mark - Module API 1385 1386 1387 static float 1388 cdda_identify_partition(int fd, partition_data* partition, void** _cookie) 1389 { 1390 scsi_toc_toc* toc = (scsi_toc_toc*)malloc(2048); 1391 if (toc == NULL) 1392 return B_NO_MEMORY; 1393 1394 status_t status = read_table_of_contents(fd, toc, 2048); 1395 1396 // If we succeeded in reading the toc, check the tracks in the 1397 // partition, which may not be the whole CD, and if any are audio, 1398 // claim the partition. 1399 if (status == B_OK) { 1400 uint32 trackCount = toc->last_track + (uint32)1 - toc->first_track; 1401 uint64 sessionStartLBA = partition->offset / partition->block_size; 1402 uint64 sessionEndLBA = sessionStartLBA 1403 + (partition->size / partition->block_size); 1404 TRACE(("cdda_identify_partition: session at %lld-%lld\n", 1405 sessionStartLBA, sessionEndLBA)); 1406 status = B_ENTRY_NOT_FOUND; 1407 for (uint32 i = 0; i < trackCount; i++) { 1408 // We have to get trackLBA from track.start.time since 1409 // track.start.lba is useless for this. 1410 // This is how session gets it. 1411 uint64 trackLBA 1412 = ((toc->tracks[i].start.time.minute * kFramesPerMinute) 1413 + (toc->tracks[i].start.time.second * kFramesPerSecond) 1414 + toc->tracks[i].start.time.frame - 150); 1415 if (trackLBA >= sessionStartLBA && trackLBA < sessionEndLBA) { 1416 if (is_data_track(toc->tracks[i])) { 1417 TRACE(("cdda_identify_partition: track %ld at %lld is " 1418 "data\n", i + 1, trackLBA)); 1419 status = B_BAD_TYPE; 1420 } else { 1421 TRACE(("cdda_identify_partition: track %ld at %lld is " 1422 "audio\n", i + 1, trackLBA)); 1423 status = B_OK; 1424 break; 1425 } 1426 } 1427 } 1428 } 1429 1430 if (status != B_OK) { 1431 free(toc); 1432 return status; 1433 } 1434 1435 *_cookie = toc; 1436 return 0.8f; 1437 } 1438 1439 1440 static status_t 1441 cdda_scan_partition(int fd, partition_data* partition, void* _cookie) 1442 { 1443 scsi_toc_toc* toc = (scsi_toc_toc*)_cookie; 1444 1445 partition->status = B_PARTITION_VALID; 1446 partition->flags |= B_PARTITION_FILE_SYSTEM; 1447 1448 // compute length 1449 1450 uint32 lastTrack = toc->last_track + 1 - toc->first_track; 1451 scsi_cd_msf& end = toc->tracks[lastTrack].start.time; 1452 1453 partition->content_size = off_t(end.minute * kFramesPerMinute 1454 + end.second * kFramesPerSecond + end.frame) * kFrameSize; 1455 partition->block_size = kFrameSize; 1456 1457 // determine volume title 1458 1459 char name[256]; 1460 Volume::DetermineName(compute_cddb_disc_id(*toc), fd, name, sizeof(name)); 1461 1462 partition->content_name = strdup(name); 1463 if (partition->content_name == NULL) 1464 return B_NO_MEMORY; 1465 1466 return B_OK; 1467 } 1468 1469 1470 static void 1471 cdda_free_identify_partition_cookie(partition_data* partition, void* _cookie) 1472 { 1473 free(_cookie); 1474 } 1475 1476 1477 static status_t 1478 cdda_mount(fs_volume* fsVolume, const char* device, uint32 flags, 1479 const char* args, ino_t* _rootVnodeID) 1480 { 1481 TRACE(("cdda_mount: entry\n")); 1482 1483 Volume* volume = new(std::nothrow) Volume(fsVolume); 1484 if (volume == NULL) 1485 return B_NO_MEMORY; 1486 1487 status_t status = volume->InitCheck(); 1488 if (status == B_OK) 1489 status = volume->Mount(device); 1490 1491 if (status < B_OK) { 1492 delete volume; 1493 return status; 1494 } 1495 1496 *_rootVnodeID = volume->RootNode().ID(); 1497 fsVolume->private_volume = volume; 1498 fsVolume->ops = &gCDDAVolumeOps; 1499 1500 return B_OK; 1501 } 1502 1503 1504 static status_t 1505 cdda_unmount(fs_volume* _volume) 1506 { 1507 struct Volume* volume = (struct Volume*)_volume->private_volume; 1508 1509 TRACE(("cdda_unmount: entry fs = %p\n", _volume)); 1510 delete volume; 1511 1512 return 0; 1513 } 1514 1515 1516 static status_t 1517 cdda_read_fs_stat(fs_volume* _volume, struct fs_info* info) 1518 { 1519 Volume* volume = (Volume*)_volume->private_volume; 1520 Locker locker(volume->Lock()); 1521 1522 // File system flags. 1523 info->flags = B_FS_IS_PERSISTENT | B_FS_HAS_ATTR | B_FS_HAS_MIME 1524 | B_FS_IS_REMOVABLE; 1525 info->io_size = 65536; 1526 1527 info->block_size = 2048; 1528 info->total_blocks = volume->NumBlocks(); 1529 info->free_blocks = 0; 1530 1531 // Volume name 1532 strlcpy(info->volume_name, volume->Name(), sizeof(info->volume_name)); 1533 1534 // File system name 1535 strlcpy(info->fsh_name, "cdda", sizeof(info->fsh_name)); 1536 1537 return B_OK; 1538 } 1539 1540 1541 static status_t 1542 cdda_write_fs_stat(fs_volume* _volume, const struct fs_info* info, uint32 mask) 1543 { 1544 Volume* volume = (Volume*)_volume->private_volume; 1545 Locker locker(volume->Lock()); 1546 1547 status_t status = B_BAD_VALUE; 1548 1549 if ((mask & FS_WRITE_FSINFO_NAME) != 0) { 1550 status = volume->SetName(info->volume_name); 1551 } 1552 1553 return status; 1554 } 1555 1556 1557 static status_t 1558 cdda_sync(fs_volume* _volume) 1559 { 1560 TRACE(("cdda_sync: entry\n")); 1561 1562 return B_OK; 1563 } 1564 1565 1566 static status_t 1567 cdda_lookup(fs_volume* _volume, fs_vnode* _dir, const char* name, ino_t* _id) 1568 { 1569 Volume* volume = (Volume*)_volume->private_volume; 1570 status_t status; 1571 1572 TRACE(("cdda_lookup: entry dir %p, name '%s'\n", _dir, name)); 1573 1574 Inode* directory = (Inode*)_dir->private_node; 1575 if (!S_ISDIR(directory->Type())) 1576 return B_NOT_A_DIRECTORY; 1577 1578 Locker _(volume->Lock()); 1579 1580 Inode* inode = volume->Find(name); 1581 if (inode == NULL) 1582 return B_ENTRY_NOT_FOUND; 1583 1584 status = get_vnode(volume->FSVolume(), inode->ID(), NULL); 1585 if (status < B_OK) 1586 return status; 1587 1588 *_id = inode->ID(); 1589 return B_OK; 1590 } 1591 1592 1593 static status_t 1594 cdda_get_vnode_name(fs_volume* _volume, fs_vnode* _node, char* buffer, 1595 size_t bufferSize) 1596 { 1597 Volume* volume = (Volume*)_volume->private_volume; 1598 Inode* inode = (Inode*)_node->private_node; 1599 1600 TRACE(("cdda_get_vnode_name(): inode = %p\n", inode)); 1601 1602 Locker _(volume->Lock()); 1603 strlcpy(buffer, inode->Name(), bufferSize); 1604 return B_OK; 1605 } 1606 1607 1608 static status_t 1609 cdda_get_vnode(fs_volume* _volume, ino_t id, fs_vnode* _node, int* _type, 1610 uint32* _flags, bool reenter) 1611 { 1612 Volume* volume = (Volume*)_volume->private_volume; 1613 Inode* inode; 1614 1615 TRACE(("cdda_getvnode: asking for vnode 0x%Lx, r %d\n", id, reenter)); 1616 1617 inode = volume->Find(id); 1618 if (inode == NULL) 1619 return B_ENTRY_NOT_FOUND; 1620 1621 _node->private_node = inode; 1622 _node->ops = &gCDDAVnodeOps; 1623 *_type = inode->Type(); 1624 *_flags = 0; 1625 return B_OK; 1626 } 1627 1628 1629 static status_t 1630 cdda_put_vnode(fs_volume* _volume, fs_vnode* _node, bool reenter) 1631 { 1632 return B_OK; 1633 } 1634 1635 1636 static status_t 1637 cdda_open(fs_volume* _volume, fs_vnode* _node, int openMode, void** _cookie) 1638 { 1639 TRACE(("cdda_open(): node = %p, openMode = %d\n", _node, openMode)); 1640 1641 file_cookie* cookie = (file_cookie*)malloc(sizeof(file_cookie)); 1642 if (cookie == NULL) 1643 return B_NO_MEMORY; 1644 1645 TRACE((" open cookie = %p\n", cookie)); 1646 cookie->open_mode = openMode; 1647 cookie->buffer = NULL; 1648 1649 *_cookie = (void*)cookie; 1650 1651 return B_OK; 1652 } 1653 1654 1655 static status_t 1656 cdda_close(fs_volume* _volume, fs_vnode* _node, void* _cookie) 1657 { 1658 return B_OK; 1659 } 1660 1661 1662 static status_t 1663 cdda_free_cookie(fs_volume* _volume, fs_vnode* _node, void* _cookie) 1664 { 1665 file_cookie* cookie = (file_cookie*)_cookie; 1666 1667 TRACE(("cdda_freecookie: entry vnode %p, cookie %p\n", _node, _cookie)); 1668 1669 free(cookie); 1670 return B_OK; 1671 } 1672 1673 1674 static status_t 1675 cdda_fsync(fs_volume* _volume, fs_vnode* _node) 1676 { 1677 return B_OK; 1678 } 1679 1680 1681 static status_t 1682 cdda_read(fs_volume* _volume, fs_vnode* _node, void* _cookie, off_t offset, 1683 void* buffer, size_t* _length) 1684 { 1685 file_cookie* cookie = (file_cookie*)_cookie; 1686 Volume* volume = (Volume*)_volume->private_volume; 1687 Inode* inode = (Inode*)_node->private_node; 1688 1689 TRACE(("cdda_read(vnode = %p, offset %Ld, length = %lu, mode = %d)\n", 1690 _node, offset, *_length, cookie->open_mode)); 1691 1692 if (S_ISDIR(inode->Type())) 1693 return B_IS_A_DIRECTORY; 1694 if (offset < 0) 1695 return B_BAD_VALUE; 1696 1697 off_t maxSize = inode->Size() + sizeof(wav_header); 1698 if (offset >= maxSize) { 1699 *_length = 0; 1700 return B_OK; 1701 } 1702 1703 if (cookie->buffer == NULL) { 1704 // TODO: move that to open() to make sure reading can't fail for this reason? 1705 cookie->buffer = malloc(volume->BufferSize()); 1706 if (cookie->buffer == NULL) 1707 return B_NO_MEMORY; 1708 1709 cookie->buffer_offset = -1; 1710 } 1711 1712 size_t length = *_length; 1713 if (offset + (off_t)length > maxSize) 1714 length = maxSize - offset; 1715 1716 status_t status = B_OK; 1717 size_t bytesRead = 0; 1718 1719 if (offset < (off_t)sizeof(wav_header)) { 1720 // read fake WAV header 1721 size_t size = sizeof(wav_header) - offset; 1722 size = min_c(size, length); 1723 1724 if (user_memcpy(buffer, (uint8*)inode->WAVHeader() + offset, size) 1725 < B_OK) 1726 return B_BAD_ADDRESS; 1727 1728 buffer = (void*)((uint8*)buffer + size); 1729 length -= size; 1730 bytesRead += size; 1731 offset = 0; 1732 } else 1733 offset -= sizeof(wav_header); 1734 1735 if (length > 0) { 1736 // read actual CD data 1737 offset += inode->StartFrame() * kFrameSize; 1738 1739 status = read_cdda_data(volume->Device(), 1740 inode->StartFrame() + inode->FrameCount(), offset, buffer, length, 1741 cookie->buffer_offset, cookie->buffer, volume->BufferSize()); 1742 1743 bytesRead += length; 1744 } 1745 if (status == B_OK) 1746 *_length = bytesRead; 1747 1748 return status; 1749 } 1750 1751 1752 static bool 1753 cdda_can_page(fs_volume* _volume, fs_vnode* _node, void* cookie) 1754 { 1755 return false; 1756 } 1757 1758 1759 static status_t 1760 cdda_read_pages(fs_volume* _volume, fs_vnode* _node, void* cookie, off_t pos, 1761 const iovec* vecs, size_t count, size_t* _numBytes) 1762 { 1763 return B_NOT_ALLOWED; 1764 } 1765 1766 1767 static status_t 1768 cdda_write_pages(fs_volume* _volume, fs_vnode* _node, void* cookie, off_t pos, 1769 const iovec* vecs, size_t count, size_t* _numBytes) 1770 { 1771 return B_NOT_ALLOWED; 1772 } 1773 1774 1775 static status_t 1776 cdda_read_stat(fs_volume* _volume, fs_vnode* _node, struct stat* stat) 1777 { 1778 Volume* volume = (Volume*)_volume->private_volume; 1779 Inode* inode = (Inode*)_node->private_node; 1780 1781 TRACE(("cdda_read_stat: vnode %p (0x%Lx), stat %p\n", inode, inode->ID(), 1782 stat)); 1783 1784 fill_stat_buffer(volume, inode, NULL, *stat); 1785 1786 return B_OK; 1787 } 1788 1789 1790 status_t 1791 cdda_rename(fs_volume* _volume, fs_vnode* _oldDir, const char* oldName, 1792 fs_vnode* _newDir, const char* newName) 1793 { 1794 if (_oldDir->private_node != _newDir->private_node) 1795 return B_BAD_VALUE; 1796 1797 // we only have a single directory which simplifies things a bit :-) 1798 1799 Volume *volume = (Volume*)_volume->private_volume; 1800 Locker _(volume->Lock()); 1801 1802 Inode* inode = volume->Find(oldName); 1803 if (inode == NULL) 1804 return B_ENTRY_NOT_FOUND; 1805 1806 if (volume->Find(newName) != NULL) 1807 return B_NAME_IN_USE; 1808 1809 status_t status = inode->SetName(newName); 1810 if (status == B_OK) { 1811 // One of the tracks had its name edited from outside the filesystem 1812 // add-on. Disable CDDB lookups. Note this will usually mean that the 1813 // user manually renamed a track or that cddblinkd (or other program) 1814 // did this so we do not want to do it again. 1815 volume->SetCDDBLookupsEnabled(false); 1816 1817 notify_entry_moved(volume->ID(), volume->RootNode().ID(), oldName, 1818 volume->RootNode().ID(), newName, inode->ID()); 1819 } 1820 1821 return status; 1822 } 1823 1824 1825 // #pragma mark - directory functions 1826 1827 1828 static status_t 1829 cdda_open_dir(fs_volume* _volume, fs_vnode* _node, void** _cookie) 1830 { 1831 Volume* volume = (Volume*)_volume->private_volume; 1832 1833 TRACE(("cdda_open_dir(): vnode = %p\n", _node)); 1834 1835 Inode* inode = (Inode*)_node->private_node; 1836 if (!S_ISDIR(inode->Type())) 1837 return B_NOT_A_DIRECTORY; 1838 1839 if (inode != &volume->RootNode()) 1840 panic("pipefs: found directory that's not the root!"); 1841 1842 dir_cookie* cookie = (dir_cookie*)malloc(sizeof(dir_cookie)); 1843 if (cookie == NULL) 1844 return B_NO_MEMORY; 1845 1846 cookie->current = volume->FirstEntry(); 1847 cookie->state = ITERATION_STATE_BEGIN; 1848 1849 *_cookie = (void*)cookie; 1850 return B_OK; 1851 } 1852 1853 1854 static status_t 1855 cdda_read_dir(fs_volume* _volume, fs_vnode* _node, void* _cookie, 1856 struct dirent* buffer, size_t bufferSize, uint32* _num) 1857 { 1858 Volume* volume = (Volume*)_volume->private_volume; 1859 Inode* inode = (Inode*)_node->private_node; 1860 1861 TRACE(("cdda_read_dir: vnode %p, cookie %p, buffer = %p, bufferSize = %ld," 1862 " num = %p\n", _node, _cookie, buffer, bufferSize,_num)); 1863 1864 if ((Inode*)_node->private_node != &volume->RootNode()) 1865 return B_BAD_VALUE; 1866 1867 Locker _(volume->Lock()); 1868 1869 dir_cookie* cookie = (dir_cookie*)_cookie; 1870 Inode* childNode = NULL; 1871 const char* name = NULL; 1872 Inode* nextChildNode = NULL; 1873 int nextState = cookie->state; 1874 uint32 max = *_num; 1875 uint32 count = 0; 1876 1877 while (count < max && bufferSize > sizeof(dirent)) { 1878 switch (cookie->state) { 1879 case ITERATION_STATE_DOT: 1880 childNode = inode; 1881 name = "."; 1882 nextChildNode = volume->FirstEntry(); 1883 nextState = cookie->state + 1; 1884 break; 1885 case ITERATION_STATE_DOT_DOT: 1886 childNode = inode; // parent of the root node is the root node 1887 name = ".."; 1888 nextChildNode = volume->FirstEntry(); 1889 nextState = cookie->state + 1; 1890 break; 1891 default: 1892 childNode = cookie->current; 1893 if (childNode) { 1894 name = childNode->Name(); 1895 nextChildNode = childNode->Next(); 1896 } 1897 break; 1898 } 1899 1900 if (childNode == NULL) { 1901 // we're at the end of the directory 1902 break; 1903 } 1904 1905 buffer->d_dev = volume->FSVolume()->id; 1906 buffer->d_ino = childNode->ID(); 1907 buffer->d_reclen = strlen(name) + sizeof(struct dirent); 1908 1909 if (buffer->d_reclen > bufferSize) { 1910 if (count == 0) 1911 return ENOBUFS; 1912 1913 break; 1914 } 1915 1916 strcpy(buffer->d_name, name); 1917 1918 bufferSize -= buffer->d_reclen; 1919 buffer = (struct dirent*)((uint8*)buffer + buffer->d_reclen); 1920 count++; 1921 1922 cookie->current = nextChildNode; 1923 cookie->state = nextState; 1924 } 1925 1926 *_num = count; 1927 return B_OK; 1928 } 1929 1930 1931 static status_t 1932 cdda_rewind_dir(fs_volume* _volume, fs_vnode* _node, void* _cookie) 1933 { 1934 Volume* volume = (Volume*)_volume->private_volume; 1935 1936 dir_cookie* cookie = (dir_cookie*)_cookie; 1937 cookie->current = volume->FirstEntry(); 1938 cookie->state = ITERATION_STATE_BEGIN; 1939 1940 return B_OK; 1941 } 1942 1943 1944 static status_t 1945 cdda_close_dir(fs_volume* _volume, fs_vnode* _node, void* _cookie) 1946 { 1947 TRACE(("cdda_close: entry vnode %p, cookie %p\n", _node, _cookie)); 1948 1949 return 0; 1950 } 1951 1952 1953 static status_t 1954 cdda_free_dir_cookie(fs_volume* _volume, fs_vnode* _node, void* _cookie) 1955 { 1956 dir_cookie* cookie = (dir_cookie*)_cookie; 1957 1958 TRACE(("cdda_freecookie: entry vnode %p, cookie %p\n", _node, cookie)); 1959 1960 free(cookie); 1961 return 0; 1962 } 1963 1964 1965 // #pragma mark - attribute functions 1966 1967 1968 static status_t 1969 cdda_open_attr_dir(fs_volume* _volume, fs_vnode* _node, void** _cookie) 1970 { 1971 Volume* volume = (Volume*)_volume->private_volume; 1972 Inode* inode = (Inode*)_node->private_node; 1973 1974 attr_cookie* cookie = new(std::nothrow) attr_cookie; 1975 if (cookie == NULL) 1976 return B_NO_MEMORY; 1977 1978 Locker _(volume->Lock()); 1979 1980 inode->AddAttrCookie(cookie); 1981 *_cookie = cookie; 1982 return B_OK; 1983 } 1984 1985 1986 static status_t 1987 cdda_close_attr_dir(fs_volume* _volume, fs_vnode* _node, void* _cookie) 1988 { 1989 return B_OK; 1990 } 1991 1992 1993 static status_t 1994 cdda_free_attr_dir_cookie(fs_volume* _volume, fs_vnode* _node, void* _cookie) 1995 { 1996 Volume* volume = (Volume*)_volume->private_volume; 1997 Inode* inode = (Inode*)_node->private_node; 1998 attr_cookie* cookie = (attr_cookie*)_cookie; 1999 2000 Locker _(volume->Lock()); 2001 2002 inode->RemoveAttrCookie(cookie); 2003 delete cookie; 2004 return B_OK; 2005 } 2006 2007 2008 static status_t 2009 cdda_rewind_attr_dir(fs_volume* _volume, fs_vnode* _node, void* _cookie) 2010 { 2011 Volume* volume = (Volume*)_volume->private_volume; 2012 Inode* inode = (Inode*)_node->private_node; 2013 attr_cookie* cookie = (attr_cookie*)_cookie; 2014 2015 Locker _(volume->Lock()); 2016 2017 inode->RewindAttrCookie(cookie); 2018 return B_OK; 2019 } 2020 2021 2022 static status_t 2023 cdda_read_attr_dir(fs_volume* _volume, fs_vnode* _node, void* _cookie, 2024 struct dirent* dirent, size_t bufferSize, uint32* _num) 2025 { 2026 Volume* volume = (Volume*)_volume->private_volume; 2027 Inode* inode = (Inode*)_node->private_node; 2028 attr_cookie* cookie = (attr_cookie*)_cookie; 2029 2030 Locker _(volume->Lock()); 2031 Attribute* attribute = cookie->current; 2032 2033 if (attribute == NULL) { 2034 *_num = 0; 2035 return B_OK; 2036 } 2037 2038 size_t length = strlcpy(dirent->d_name, attribute->Name(), bufferSize); 2039 dirent->d_dev = volume->FSVolume()->id; 2040 dirent->d_ino = inode->ID(); 2041 dirent->d_reclen = sizeof(struct dirent) + length; 2042 2043 cookie->current = attribute->GetDoublyLinkedListLink()->next; 2044 *_num = 1; 2045 return B_OK; 2046 } 2047 2048 2049 static status_t 2050 cdda_create_attr(fs_volume* _volume, fs_vnode* _node, const char* name, 2051 uint32 type, int openMode, void** _cookie) 2052 { 2053 Volume *volume = (Volume*)_volume->private_volume; 2054 Inode *inode = (Inode*)_node->private_node; 2055 2056 Locker _(volume->Lock()); 2057 2058 Attribute* attribute = inode->FindAttribute(name); 2059 if (attribute == NULL) { 2060 if (Attribute::IsProtectedNamespace(name)) 2061 return B_NOT_ALLOWED; 2062 status_t status = inode->AddAttribute(name, type, true, NULL, 0); 2063 if (status != B_OK) 2064 return status; 2065 2066 notify_attribute_changed(volume->ID(), inode->ID(), name, 2067 B_ATTR_CREATED); 2068 } else if ((openMode & O_EXCL) == 0) { 2069 if (attribute->IsProtectedNamespace()) 2070 return B_NOT_ALLOWED; 2071 attribute->SetType(type); 2072 if ((openMode & O_TRUNC) != 0) 2073 attribute->Truncate(); 2074 } else 2075 return B_FILE_EXISTS; 2076 2077 *_cookie = strdup(name); 2078 if (*_cookie == NULL) 2079 return B_NO_MEMORY; 2080 2081 return B_OK; 2082 } 2083 2084 2085 static status_t 2086 cdda_open_attr(fs_volume* _volume, fs_vnode* _node, const char* name, 2087 int openMode, void** _cookie) 2088 { 2089 Volume* volume = (Volume*)_volume->private_volume; 2090 Inode* inode = (Inode*)_node->private_node; 2091 2092 Locker _(volume->Lock()); 2093 2094 Attribute* attribute = inode->FindAttribute(name); 2095 if (attribute == NULL) 2096 return B_ENTRY_NOT_FOUND; 2097 2098 *_cookie = strdup(name); 2099 if (*_cookie == NULL) 2100 return B_NO_MEMORY; 2101 2102 return B_OK; 2103 } 2104 2105 2106 static status_t 2107 cdda_close_attr(fs_volume* _volume, fs_vnode* _node, void* cookie) 2108 { 2109 return B_OK; 2110 } 2111 2112 2113 static status_t 2114 cdda_free_attr_cookie(fs_volume* _volume, fs_vnode* _node, void* cookie) 2115 { 2116 free(cookie); 2117 return B_OK; 2118 } 2119 2120 2121 static status_t 2122 cdda_read_attr(fs_volume* _volume, fs_vnode* _node, void* _cookie, 2123 off_t offset, void* buffer, size_t* _length) 2124 { 2125 Volume* volume = (Volume*)_volume->private_volume; 2126 Inode* inode = (Inode*)_node->private_node; 2127 2128 Locker _(volume->Lock()); 2129 2130 Attribute* attribute = inode->FindAttribute((const char*)_cookie); 2131 if (attribute == NULL) 2132 return B_ENTRY_NOT_FOUND; 2133 2134 return attribute->ReadAt(offset, (uint8*)buffer, _length); 2135 } 2136 2137 2138 static status_t 2139 cdda_write_attr(fs_volume* _volume, fs_vnode* _node, void* _cookie, 2140 off_t offset, const void* buffer, size_t* _length) 2141 { 2142 Volume* volume = (Volume*)_volume->private_volume; 2143 Inode* inode = (Inode*)_node->private_node; 2144 2145 Locker _(volume->Lock()); 2146 2147 Attribute* attribute = inode->FindAttribute((const char*)_cookie); 2148 if (attribute == NULL) 2149 return B_ENTRY_NOT_FOUND; 2150 2151 if (attribute->IsProtectedNamespace()) 2152 return B_NOT_ALLOWED; 2153 2154 status_t status = attribute->WriteAt(offset, (uint8*)buffer, _length); 2155 if (status == B_OK) { 2156 notify_attribute_changed(volume->ID(), inode->ID(), attribute->Name(), 2157 B_ATTR_CHANGED); 2158 } 2159 return status; 2160 } 2161 2162 2163 static status_t 2164 cdda_read_attr_stat(fs_volume* _volume, fs_vnode* _node, void* _cookie, 2165 struct stat* stat) 2166 { 2167 Volume* volume = (Volume*)_volume->private_volume; 2168 Inode* inode = (Inode*)_node->private_node; 2169 2170 Locker _(volume->Lock()); 2171 2172 Attribute* attribute = inode->FindAttribute((const char*)_cookie); 2173 if (attribute == NULL) 2174 return B_ENTRY_NOT_FOUND; 2175 2176 fill_stat_buffer(volume, inode, attribute, *stat); 2177 return B_OK; 2178 } 2179 2180 2181 static status_t 2182 cdda_write_attr_stat(fs_volume* _volume, fs_vnode* _node, void* cookie, 2183 const struct stat* stat, int statMask) 2184 { 2185 return EOPNOTSUPP; 2186 } 2187 2188 2189 static status_t 2190 cdda_remove_attr(fs_volume* _volume, fs_vnode* _node, const char* name) 2191 { 2192 if (name == NULL) 2193 return B_BAD_VALUE; 2194 2195 Volume* volume = (Volume*)_volume->private_volume; 2196 Inode* inode = (Inode*)_node->private_node; 2197 2198 Locker _(volume->Lock()); 2199 2200 status_t status = inode->RemoveAttribute(name, true); 2201 if (status == B_OK) { 2202 notify_attribute_changed(volume->ID(), inode->ID(), name, 2203 B_ATTR_REMOVED); 2204 } 2205 2206 return status; 2207 } 2208 2209 2210 fs_volume_ops gCDDAVolumeOps = { 2211 cdda_unmount, 2212 cdda_read_fs_stat, 2213 cdda_write_fs_stat, 2214 cdda_sync, 2215 cdda_get_vnode, 2216 2217 // the other operations are not yet supported (indices, queries) 2218 NULL, 2219 }; 2220 2221 fs_vnode_ops gCDDAVnodeOps = { 2222 cdda_lookup, 2223 cdda_get_vnode_name, 2224 cdda_put_vnode, 2225 NULL, // fs_remove_vnode() 2226 2227 cdda_can_page, 2228 cdda_read_pages, 2229 cdda_write_pages, 2230 2231 NULL, // io() 2232 NULL, // cancel_io() 2233 2234 NULL, // get_file_map() 2235 2236 // common 2237 NULL, // fs_ioctl() 2238 NULL, // fs_set_flags() 2239 NULL, // fs_select() 2240 NULL, // fs_deselect() 2241 cdda_fsync, 2242 2243 NULL, // fs_read_link() 2244 NULL, // fs_symlink() 2245 NULL, // fs_link() 2246 NULL, // fs_unlink() 2247 cdda_rename, 2248 2249 NULL, // fs_access() 2250 cdda_read_stat, 2251 NULL, // fs_write_stat() 2252 NULL, // fs_preallocate() 2253 2254 // file 2255 NULL, // fs_create() 2256 cdda_open, 2257 cdda_close, 2258 cdda_free_cookie, 2259 cdda_read, 2260 NULL, // fs_write() 2261 2262 // directory 2263 NULL, // fs_create_dir() 2264 NULL, // fs_remove_dir() 2265 cdda_open_dir, 2266 cdda_close_dir, 2267 cdda_free_dir_cookie, 2268 cdda_read_dir, 2269 cdda_rewind_dir, 2270 2271 // attribute directory operations 2272 cdda_open_attr_dir, 2273 cdda_close_attr_dir, 2274 cdda_free_attr_dir_cookie, 2275 cdda_read_attr_dir, 2276 cdda_rewind_attr_dir, 2277 2278 // attribute operations 2279 cdda_create_attr, 2280 cdda_open_attr, 2281 cdda_close_attr, 2282 cdda_free_attr_cookie, 2283 cdda_read_attr, 2284 cdda_write_attr, 2285 2286 cdda_read_attr_stat, 2287 cdda_write_attr_stat, 2288 NULL, // fs_rename_attr() 2289 cdda_remove_attr, 2290 2291 NULL, // fs_create_special_node() 2292 }; 2293 2294 static file_system_module_info sCDDAFileSystem = { 2295 { 2296 "file_systems/cdda" B_CURRENT_FS_API_VERSION, 2297 0, 2298 NULL, 2299 }, 2300 2301 "cdda", // short_name 2302 "CDDA File System", // pretty_name 2303 0, // DDM flags 2304 2305 cdda_identify_partition, 2306 cdda_scan_partition, 2307 cdda_free_identify_partition_cookie, 2308 NULL, // free_partition_content_cookie() 2309 2310 cdda_mount, 2311 2312 // all other functions are not supported 2313 NULL, 2314 }; 2315 2316 module_info* modules[] = { 2317 (module_info*)&sCDDAFileSystem, 2318 NULL, 2319 }; 2320