When no device is given, find ourselves which removable device (if any) can
be considered the default one. When several are, the command failed after listing them. This should avoid to always fallback to floppy as the default removable device. Closes #7247. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40601 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
+78
-18
@@ -1,25 +1,62 @@
|
|||||||
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
|
/*
|
||||||
//
|
* Copyright (c) 2003-2011, Haiku Inc.
|
||||||
// Copyright (c) 2003, OpenBeOS
|
* Distributed under the terms of the MIT license.
|
||||||
//
|
*
|
||||||
// This software is part of the OpenBeOS distribution and is covered
|
* Authors:
|
||||||
// by the OpenBeOS license.
|
* François Revol <[email protected]>
|
||||||
//
|
* Philippe Houdoin
|
||||||
//
|
*
|
||||||
// File: eject.c
|
* Description: ejects physical media from a drive.
|
||||||
// Author: François Revol ([email protected])
|
* This version also loads a media and can query for the status.
|
||||||
// Description: ejects physical media from a drive.
|
*/
|
||||||
// This version also loads a media and can query for the status.
|
|
||||||
//
|
|
||||||
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <Drivers.h>
|
|
||||||
#include <device/scsi.h>
|
#include <device/scsi.h>
|
||||||
|
#include <DiskDevice.h>
|
||||||
|
#include <DiskDeviceRoster.h>
|
||||||
|
#include <DiskDeviceVisitor.h>
|
||||||
|
#include <Drivers.h>
|
||||||
#include <fs_info.h>
|
#include <fs_info.h>
|
||||||
|
#include <ObjectList.h>
|
||||||
|
#include <Path.h>
|
||||||
|
#include <String.h>
|
||||||
|
|
||||||
|
|
||||||
|
class RemovableDevice {
|
||||||
|
public:
|
||||||
|
RemovableDevice(BDiskDevice* device) {
|
||||||
|
fName = device->Name();
|
||||||
|
device->GetPath(&fPath);
|
||||||
|
};
|
||||||
|
|
||||||
|
inline const char* Name() { return fName.String(); }
|
||||||
|
inline const char* Path() { return fPath.Path(); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
BString fName;
|
||||||
|
BPath fPath;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class RemovableDeviceVisitor : public BDiskDeviceVisitor {
|
||||||
|
public:
|
||||||
|
virtual bool Visit(BDiskDevice* device) {
|
||||||
|
if (device->IsRemovableMedia())
|
||||||
|
fRemovableDevices.AddItem(new RemovableDevice(device));
|
||||||
|
return false; // Don't stop yet!
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual bool Visit(BPartition* partition, int32 level) { return false; }
|
||||||
|
|
||||||
|
inline BObjectList<RemovableDevice>& RemovableDevices() { return fRemovableDevices; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
BObjectList<RemovableDevice> fRemovableDevices;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
static int usage(char *prog)
|
static int usage(char *prog)
|
||||||
@@ -64,8 +101,31 @@ int main(int argc, char **argv)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (device == NULL)
|
if (device == NULL) {
|
||||||
return do_eject(operation, "/dev/disk/floppy/raw");
|
BDiskDeviceRoster diskDeviceRoster;
|
||||||
|
RemovableDeviceVisitor visitor;
|
||||||
|
diskDeviceRoster.VisitEachDevice(&visitor);
|
||||||
|
|
||||||
|
int32 count = visitor.RemovableDevices().CountItems();
|
||||||
|
if (count < 1) {
|
||||||
|
printf("No removable device found!\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count > 1) {
|
||||||
|
printf("Multiple removable devices available:\n");
|
||||||
|
for (i = 0; i < count; i++) {
|
||||||
|
RemovableDevice* item = visitor.RemovableDevices().ItemAt(i);
|
||||||
|
printf(" %s\t\"%s\"\n", item->Path(), item->Name());
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default to single removable device found
|
||||||
|
device = (char*)visitor.RemovableDevices().FirstItem()->Path();
|
||||||
|
return do_eject(operation, device);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user