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