1263508Sdim// ---------------------------------------------------------------------- 2226584Sdim// This software is part of the Haiku distribution and is covered 3226584Sdim// by the MIT License. 4226584Sdim// 5226584Sdim// File Name: VolumeRoster.cpp 6226584Sdim// 7226584Sdim// Description: BVolumeRoster class 8226584Sdim// ---------------------------------------------------------------------- 9226584Sdim 10226584Sdim 11226584Sdim#include <errno.h> 12263508Sdim#include <new> 13226584Sdim 14226584Sdim#include <Bitmap.h> 15226584Sdim#include <Directory.h> 16226584Sdim#include <fs_info.h> 17226584Sdim#include <Node.h> 18226584Sdim#include <NodeMonitor.h> 19263508Sdim#include <VolumeRoster.h> 20226584Sdim 21226584Sdim 22226584Sdimstatic const char kBootVolumePath[] = "/boot"; 23226584Sdim 24226584Sdimusing namespace std; 25226584Sdim 26226584Sdim 27226584Sdim#ifdef USE_OPENBEOS_NAMESPACE 28263508Sdimnamespace OpenBeOS { 29263508Sdim#endif 30263508Sdim 31226584Sdim 32263508SdimBVolumeRoster::BVolumeRoster() 33226584Sdim : fCookie(0), 34226584Sdim fTarget(NULL) 35263508Sdim{ 36263508Sdim} 37263508Sdim 38226584Sdim 39263508Sdim// Deletes the volume roster and frees all associated resources. 40263508SdimBVolumeRoster::~BVolumeRoster() 41263508Sdim{ 42263508Sdim StopWatching(); 43263508Sdim} 44263508Sdim 45263508Sdim 46263508Sdim// Fills out the passed in BVolume object with the next available volume. 47263508Sdimstatus_t 48263508SdimBVolumeRoster::GetNextVolume(BVolume *volume) 49263508Sdim{ 50263508Sdim // check parameter 51263508Sdim status_t error = (volume ? B_OK : B_BAD_VALUE); 52263508Sdim // get next device 53263508Sdim dev_t device; 54263508Sdim if (error == B_OK) { 55263508Sdim device = next_dev(&fCookie); 56263508Sdim if (device < 0) 57263508Sdim error = device; 58263508Sdim } 59263508Sdim // init volume 60263508Sdim if (error == B_OK) 61263508Sdim error = volume->SetTo(device); 62263508Sdim return error; 63263508Sdim} 64263508Sdim 65263508Sdim 66263508Sdim// Rewinds the list of available volumes back to the first item. 67263508Sdimvoid 68263508SdimBVolumeRoster::Rewind() 69263508Sdim{ 70263508Sdim fCookie = 0; 71263508Sdim} 72263508Sdim 73263508Sdim 74226584Sdim// Fills out the passed in BVolume object with the boot volume. 75226584Sdimstatus_t 76226584SdimBVolumeRoster::GetBootVolume(BVolume *volume) 77263508Sdim{ 78263508Sdim // check parameter 79263508Sdim status_t error = (volume ? B_OK : B_BAD_VALUE); 80226584Sdim // get device 81263508Sdim dev_t device; 82263508Sdim if (error == B_OK) { 83263508Sdim device = dev_for_path(kBootVolumePath); 84263508Sdim if (device < 0) 85263508Sdim error = device; 86263508Sdim } 87263508Sdim // init volume 88263508Sdim if (error == B_OK) 89263508Sdim error = volume->SetTo(device); 90263508Sdim return error; 91263508Sdim} 92263508Sdim 93263508Sdim 94263508Sdim// Starts watching the available volumes for changes. 95263508Sdimstatus_t 96263508SdimBVolumeRoster::StartWatching(BMessenger messenger) 97263508Sdim{ 98263508Sdim StopWatching(); 99263508Sdim status_t error = (messenger.IsValid() ? B_OK : B_ERROR); 100263508Sdim // clone messenger 101263508Sdim if (error == B_OK) { 102263508Sdim fTarget = new(nothrow) BMessenger(messenger); 103263508Sdim if (!fTarget) 104263508Sdim error = B_NO_MEMORY; 105263508Sdim } 106263508Sdim // start watching 107263508Sdim if (error == B_OK) 108263508Sdim error = watch_node(NULL, B_WATCH_MOUNT, messenger); 109263508Sdim // cleanup on failure 110263508Sdim if (error != B_OK && fTarget) { 111263508Sdim delete fTarget; 112263508Sdim fTarget = NULL; 113263508Sdim } 114263508Sdim return error; 115263508Sdim} 116263508Sdim 117263508Sdim 118263508Sdim// Stops watching volumes initiated by StartWatching(). 119263508Sdimvoid 120263508SdimBVolumeRoster::StopWatching() 121263508Sdim{ 122263508Sdim if (fTarget) { 123263508Sdim stop_watching(*fTarget); 124263508Sdim delete fTarget; 125263508Sdim fTarget = NULL; 126263508Sdim } 127263508Sdim} 128263508Sdim 129263508Sdim 130263508Sdim// Returns the messenger currently watching the volume list. 131263508SdimBMessenger 132263508SdimBVolumeRoster::Messenger() const 133263508Sdim{ 134263508Sdim return (fTarget ? *fTarget : BMessenger()); 135263508Sdim} 136263508Sdim 137263508Sdim 138263508Sdim// FBC 139263508Sdimvoid BVolumeRoster::_SeveredVRoster1() {} 140263508Sdimvoid BVolumeRoster::_SeveredVRoster2() {} 141263508Sdim 142263508Sdim 143263508Sdim#ifdef USE_OPENBEOS_NAMESPACE 144263508Sdim} 145263508Sdim#endif 146263508Sdim